#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_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int getdtablesize();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
void daemonize(void) {
  int maxfd;
  int i;
  if ((i = fork()) < 0) {
    perror("couldn't fork");
    exit(2);
  } else if (i > 0) {
    _exit(0);
  }
  setsid();
  if ((i = fork()) < 0) {
    perror("couldn't fork #2");
    exit(2);
  } else if (i > 0) {
    _exit(0);
  }
  chdir("/");
  maxfd = getdtablesize();
  for (i = 0; i < maxfd; i++) {
    close(i);
  }
  open("/dev/null", O_RDONLY);
  open("/dev/null", O_WRONLY);
  open("/dev/null", O_WRONLY);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11c1 <daemonize+0x31>
lea    0xe4e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jle    11d2 <daemonize+0x42>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    11d7 <daemonize+0x47>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1207 <daemonize+0x77>
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jle    1218 <daemonize+0x88>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    121d <daemonize+0x8d>
lea    0xdfb(%rip),%rdi
call   1040 <chdir@plt>
mov    $0x0,%al
call   10b0 <getdtablesize@plt>
mov    %eax,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    125c <daemonize+0xcc>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    123a <daemonize+0xaa>
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
lea    0xdb5(%rip),%rdi
call   1070 <open@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
lea    0xda0(%rip),%rdi
call   1070 <open@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
lea    0xd8b(%rip),%rdi
call   1070 <open@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     1230 <daemonize+0xa0>
jne    1229 <daemonize+0x99>
xor    %eax,%eax
call   1060 <setsid@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     1239 <daemonize+0xa9>
jne    1229 <daemonize+0x99>
lea    0xe58(%rip),%rdi
call   1040 <chdir@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10b0 <getdtablesize@plt>
test   %eax,%eax
jle    11ee <daemonize+0x5e>
mov    %eax,%ebp
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    11e0 <daemonize+0x50>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xe23(%rip),%rbx
mov    %rbx,%rdi
call   1070 <open@plt>
mov    0x2dc3(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <open@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xdc9(%rip),%rdi
jmp    1240 <daemonize+0xb0>
lea    0xdce(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"couldn't fork\00"
@.str.1 = [17 x i8] c"couldn't fork\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
define void @daemonize() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @fork()
store i32 %3, i32* %2
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 2)
unreachable
8: ; preds = %0
%9 = load i32, i32* %2
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @_exit(i32 0)
unreachable
13: ; preds = %8
br label %14
14: ; preds = %13
%15 = call i32 (...) @setsid()
%16 = call i32 (...) @fork()
store i32 %16, i32* %2
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 2)
unreachable
21: ; preds = %14
%22 = load i32, i32* %2
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @_exit(i32 0)
unreachable
26: ; preds = %21
br label %27
27: ; preds = %26
%28 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 (...) @getdtablesize()
store i32 %29, i32* %1
store i32 0, i32* %2
br label %30
30: ; preds = %37, %27
%31 = load i32, i32* %2
%32 = load i32, i32* %1
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i32, i32* %2
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %34
%38 = load i32, i32* %2
%39 = add nsw i32 %38, 1
store i32 %39, i32* %2
br label %30
40: ; preds = %30
%41 = load i32, i32* @O_RDONLY
%42 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = load i32, i32* @O_WRONLY
%44 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %43)
%45 = load i32, i32* @O_WRONLY
%46 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %45)
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"couldn't fork\00"
@.str.1 = [17 x i8] c"couldn't fork\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
define void @daemonize() {
%1 = tail call i32 (...) @fork()
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 2)
unreachable
6: ; preds = %0
%7 = icmp eq i32 %1, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i32 @_exit(i32 0)
unreachable
10: ; preds = %6
%11 = tail call i32 (...) @setsid()
%12 = tail call i32 (...) @fork()
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 2)
unreachable
17: ; preds = %10
%18 = icmp eq i32 %12, 0
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = tail call i32 @_exit(i32 0)
unreachable
21: ; preds = %17
%22 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 (...) @getdtablesize()
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %21, %25
%26 = phi i32 [ %28, %25 ], [ 0, %21 ]
%27 = tail call i32 @close(i32 %26)
%28 = add nuw nsw i32 %26, 1
%29 = icmp eq i32 %28, %23
br i1 %29, label %30, label %25
30: ; preds = %25, %21
%31 = load i32, i32* @O_RDONLY
%32 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @O_WRONLY
%34 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = load i32, i32* @O_WRONLY
%36 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %35)
ret void
}