#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _PATH_DEVNULL;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int fork();
int open(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setsid();
int daemon(int nochdir, int noclose) {
  int fd;
  switch (fork()) {
  case -1:
    return (-1);
  case 0:
    break;
  default:
    _exit(0);
  }
  if (setsid() == -1)
    return (-1);
  if (!nochdir)
    chdir("/");
  if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
    dup2(fd, STDERR_FILENO);
    if (fd > 2)
      close(fd);
  }
  return (0);
}


x86 assembly (clang, -O0):
<daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11a8 <daemon+0x38>
jmp    1198 <daemon+0x28>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     11b4 <daemon+0x44>
jmp    11b9 <daemon+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <daemon+0x102>
jmp    11c0 <daemon+0x50>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11dc <daemon+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <daemon+0x102>
cmpl   $0x0,-0x8(%rbp)
jne    11f2 <daemon+0x82>
lea    0xe13(%rip),%rdi
call   1040 <chdir@plt>
cmpl   $0x0,-0xc(%rbp)
jne    126b <daemon+0xfb>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
je     126b <daemon+0xfb>
mov    -0x10(%rbp),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmpl   $0x2,-0x10(%rbp)
jle    1266 <daemon+0xf6>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
jmp    126b <daemon+0xfb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     121b <daemon+0xab>
test   %ebx,%ebx
jne    1222 <daemon+0xb2>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     1216 <daemon+0xa6>
test   %ebp,%ebp
jne    11af <daemon+0x3f>
lea    0xe56(%rip),%rdi
call   1040 <chdir@plt>
xor    %ebx,%ebx
test   %r14d,%r14d
jne    121b <daemon+0xab>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     121b <daemon+0xab>
mov    %eax,%ebp
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
cmp    $0x3,%ebp
jl     121b <daemon+0xab>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    121b <daemon+0xab>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@_PATH_DEVNULL = global i32 0
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemon(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = call i32 (...) @fork()
switch i32 %7, label %10 [
i32 -1, label %8
i32 0, label %9
]
8: ; preds = %2
store i32 -1, i32* %3
br label %46
9: ; preds = %2
br label %12
10: ; preds = %2
%11 = call i32 @_exit(i32 0)
unreachable
12: ; preds = %9
%13 = call i32 (...) @setsid()
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 -1, i32* %3
br label %46
16: ; preds = %12
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, 0
br i1 %23, label %45, label %24
24: ; preds = %21
%25 = load i32, i32* @_PATH_DEVNULL
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i32 %25, i32 %26, i32 0)
store i32 %27, i32* %6
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %45
29: ; preds = %24
%30 = load i32, i32* %6
%31 = load i32, i32* @STDIN_FILENO
%32 = call i32 @dup2(i32 %30, i32 %31)
%33 = load i32, i32* %6
%34 = load i32, i32* @STDOUT_FILENO
%35 = call i32 @dup2(i32 %33, i32 %34)
%36 = load i32, i32* %6
%37 = load i32, i32* @STDERR_FILENO
%38 = call i32 @dup2(i32 %36, i32 %37)
%39 = load i32, i32* %6
%40 = icmp sgt i32 %39, 2
br i1 %40, label %41, label %44
41: ; preds = %29
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
br label %44
44: ; preds = %41, %29
br label %45
45: ; preds = %44, %24, %21
store i32 0, i32* %3
br label %46
46: ; preds = %45, %15, %8
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@_PATH_DEVNULL = global i32 0
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemon(i32 %0, i32 %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %4 [
i32 -1, label %30
i32 0, label %6
]
4: ; preds = %2
%5 = tail call i32 @_exit(i32 0)
unreachable
6: ; preds = %2
%7 = tail call i32 (...) @setsid()
%8 = icmp eq i32 %7, -1
br i1 %8, label %30, label %9
9: ; preds = %6
%10 = icmp eq i32 %0, 0
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %9
%14 = icmp eq i32 %1, 0
br i1 %14, label %15, label %30
15: ; preds = %13
%16 = load i32, i32* @_PATH_DEVNULL
%17 = load i32, i32* @O_RDWR
%18 = tail call i32 @open(i32 %16, i32 %17, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %30, label %20
20: ; preds = %15
%21 = load i32, i32* @STDIN_FILENO
%22 = tail call i32 @dup2(i32 %18, i32 %21)
%23 = load i32, i32* @STDOUT_FILENO
%24 = tail call i32 @dup2(i32 %18, i32 %23)
%25 = load i32, i32* @STDERR_FILENO
%26 = tail call i32 @dup2(i32 %18, i32 %25)
%27 = icmp sgt i32 %18, 2
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = tail call i32 @close(i32 %18)
br label %30
30: ; preds = %13, %15, %28, %20, %6, %2
%31 = phi i32 [ %3, %2 ], [ -1, %6 ], [ 0, %20 ], [ 0, %28 ], [ 0, %15 ], [ 0, %13 ]
ret i32 %31
}