#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 */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int daemon(int nochdir, int noclose) {
  pid_t parent_pid = getpid();
  switch (fork()) {
  case 0:
    break;
  case -1:
    return -1;
  default:
    _exit(0);
  }
  if (setsid() < 0)
    return -1;
  switch (fork()) {
  case 0:
    break;
  case -1:
    return -1;
  default:
    _exit(0);
  }
  if (!nochdir)
    chdir("/");
  if (!noclose) {
    int i, j = sysconf(_SC_OPEN_MAX);
    for (i = 0; i < j; ++i)
      close(i);
    open("/dev/null", O_RDWR);
    dup(0);
    dup(0);
  }
  while (!kill(parent_pid, 0)) {
    usleep(10);
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xor    %eax,%eax
mov    %al,-0x1d(%rbp)
call   1040 <getpid@plt>
mov    %eax,%ecx
mov    -0x1d(%rbp),%al
mov    %ecx,-0x10(%rbp)
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     11fd <daemon+0x4d>
jmp    11e8 <daemon+0x38>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
jne    1209 <daemon+0x59>
jmp    11f8 <daemon+0x48>
jmp    1210 <daemon+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <daemon+0x176>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
cmp    $0x0,%rax
jge    122d <daemon+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <daemon+0x176>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
sub    $0xffffffff,%eax
je     125c <daemon+0xac>
jmp    1247 <daemon+0x97>
mov    -0x24(%rbp),%eax
test   %eax,%eax
jne    1268 <daemon+0xb8>
jmp    1257 <daemon+0xa7>
jmp    126f <daemon+0xbf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <daemon+0x176>
xor    %edi,%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1285 <daemon+0xd5>
lea    0xd80(%rip),%rdi
call   1050 <chdir@plt>
cmpl   $0x0,-0xc(%rbp)
jne    12ec <daemon+0x13c>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12c9 <daemon+0x119>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12a7 <daemon+0xf7>
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
lea    0xd29(%rip),%rdi
call   10a0 <open@plt>
xor    %edi,%edi
call   1060 <dup@plt>
xor    %edi,%edi
call   1060 <dup@plt>
jmp    12f1 <daemon+0x141>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1090 <kill@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1310 <daemon+0x160>
jmp    131f <daemon+0x16f>
mov    $0xa,%edi
call   10d0 <usleep@plt>
jmp    12f1 <daemon+0x141>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %edi,%r15d
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     12af <daemon+0xff>
test   %ebp,%ebp
jne    12ba <daemon+0x10a>
xor    %eax,%eax
call   1080 <setsid@plt>
test   %rax,%rax
js     12aa <daemon+0xfa>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     12af <daemon+0xff>
test   %ebp,%ebp
jne    12ba <daemon+0x10a>
test   %r15d,%r15d
jne    121d <daemon+0x6d>
lea    0xde8(%rip),%rdi
call   1050 <chdir@plt>
test   %r14d,%r14d
jne    1271 <daemon+0xc1>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
test   %eax,%eax
jle    124e <daemon+0x9e>
mov    %eax,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1240 <daemon+0x90>
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
lea    0xda4(%rip),%rdi
call   10a0 <open@plt>
xor    %edi,%edi
call   1060 <dup@plt>
xor    %edi,%edi
call   1060 <dup@plt>
xor    %ebp,%ebp
mov    %r12d,%edi
xor    %esi,%esi
call   1090 <kill@plt>
test   %eax,%eax
jne    12af <daemon+0xff>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0xa,%edi
call   10d0 <usleep@plt>
mov    %r12d,%edi
xor    %esi,%esi
call   1090 <kill@plt>
test   %eax,%eax
je     1290 <daemon+0xe0>
jmp    12af <daemon+0xff>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@_SC_OPEN_MAX = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @daemon(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = call i32 (...) @getpid()
store i32 %9, i32* %6
%10 = call i32 (...) @fork()
switch i32 %10, label %13 [
i32 0, label %11
i32 -1, label %12
]
11: ; preds = %2
br label %15
12: ; preds = %2
store i32 -1, i32* %3
br label %60
13: ; preds = %2
%14 = call i32 @_exit(i32 0)
unreachable
15: ; preds = %11
%16 = call i64 (...) @setsid()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 -1, i32* %3
br label %60
19: ; preds = %15
%20 = call i32 (...) @fork()
switch i32 %20, label %23 [
i32 0, label %21
i32 -1, label %22
]
21: ; preds = %19
br label %25
22: ; preds = %19
store i32 -1, i32* %3
br label %60
23: ; preds = %19
%24 = call i32 @_exit(i32 0)
unreachable
25: ; preds = %21
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = load i32, i32* %5
%32 = icmp ne i32 %31, 0
br i1 %32, label %51, label %33
33: ; preds = %30
%34 = load i32, i32* @_SC_OPEN_MAX
%35 = call i32 @sysconf(i32 %34)
store i32 %35, i32* %8
store i32 0, i32* %7
br label %36
36: ; preds = %43, %33
%37 = load i32, i32* %7
%38 = load i32, i32* %8
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %46
40: ; preds = %36
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %40
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
br label %36
46: ; preds = %36
%47 = load i32, i32* @O_RDWR
%48 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %47)
%49 = call i32 @dup(i32 0)
%50 = call i32 @dup(i32 0)
br label %51
51: ; preds = %46, %30
br label %52
52: ; preds = %57, %51
%53 = load i32, i32* %6
%54 = call i32 @kill(i32 %53, i32 0)
%55 = icmp ne i32 %54, 0
%56 = xor i1 %55, true
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @usleep(i32 10)
br label %52
59: ; preds = %52
store i32 0, i32* %3
br label %60
60: ; preds = %59, %22, %18, %12
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@_SC_OPEN_MAX = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @daemon(i32 %0, i32 %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (...) @fork()
switch i32 %4, label %5 [
i32 0, label %7
i32 -1, label %41
]
5: ; preds = %2
%6 = tail call i32 @_exit(i32 0)
unreachable
7: ; preds = %2
%8 = tail call i64 (...) @setsid()
%9 = icmp slt i64 %8, 0
br i1 %9, label %41, label %10
10: ; preds = %7
%11 = tail call i32 (...) @fork()
switch i32 %11, label %12 [
i32 0, label %14
i32 -1, label %41
]
12: ; preds = %10
%13 = tail call i32 @_exit(i32 0)
unreachable
14: ; preds = %10
%15 = icmp eq i32 %0, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
%19 = icmp eq i32 %1, 0
br i1 %19, label %20, label %34
20: ; preds = %18
%21 = load i32, i32* @_SC_OPEN_MAX
%22 = tail call i32 @sysconf(i32 %21)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %20, %24
%25 = phi i32 [ %27, %24 ], [ 0, %20 ]
%26 = tail call i32 @close(i32 %25)
%27 = add nuw nsw i32 %25, 1
%28 = icmp eq i32 %27, %22
br i1 %28, label %29, label %24
29: ; preds = %24, %20
%30 = load i32, i32* @O_RDWR
%31 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = tail call i32 @dup(i32 0)
%33 = tail call i32 @dup(i32 0)
br label %34
34: ; preds = %29, %18
%35 = tail call i32 @kill(i32 %3, i32 0)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34, %37
%38 = tail call i32 @usleep(i32 10)
%39 = tail call i32 @kill(i32 %3, i32 0)
%40 = icmp eq i32 %39, 0
br i1 %40, label %37, label %41
41: ; preds = %37, %34, %10, %7, %2
%42 = phi i32 [ %4, %2 ], [ -1, %7 ], [ %11, %10 ], [ 0, %34 ], [ 0, %37 ]
ret i32 %42
}