#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int FD_SETSIZE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char const *);
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int daemonize(const char *root_path) {
  pid_t pid;
  int i;
  if ((pid = fork()) == -1)
    return -1;
  else if (pid)
    _exit(EXIT_SUCCESS);
  if (setsid() == -1)
    return -1;
  signal(SIGHUP, SIG_IGN);
  if ((pid = fork()) == -1)
    return -1;
  else if (pid)
    _exit(EXIT_SUCCESS);
  chdir(root_path);
  for (i = 0; i < FD_SETSIZE; i++)
    close(i);
  open("/dev/null", O_RDONLY);
  open("/dev/null", O_RDWR);
  open("/dev/null", O_RDWR);
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    119b <daemonize+0x2b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <daemonize+0x135>
cmpl   $0x0,-0x14(%rbp)
je     11b3 <daemonize+0x43>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    11b8 <daemonize+0x48>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11d4 <daemonize+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <daemonize+0x135>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    120a <daemonize+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <daemonize+0x135>
cmpl   $0x0,-0x14(%rbp)
je     1222 <daemonize+0xb2>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    1227 <daemonize+0xb7>
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2d7f(%rip),%rcx
cmp    (%rcx),%eax
jge    125f <daemonize+0xef>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1237 <daemonize+0xc7>
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
lea    0xd91(%rip),%rdi
call   1080 <open@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
lea    0xd7c(%rip),%rdi
call   1080 <open@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
lea    0xd67(%rip),%rdi
call   1080 <open@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     122d <daemonize+0xbd>
test   %eax,%eax
jne    1235 <daemonize+0xc5>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     1228 <daemonize+0xb8>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     122d <daemonize+0xbd>
test   %eax,%eax
jne    1235 <daemonize+0xc5>
mov    %rbx,%rdi
call   1040 <chdir@plt>
mov    0x2deb(%rip),%r14
cmpl   $0x0,(%r14)
jle    11ef <daemonize+0x7f>
xor    %ebx,%ebx
nopl   (%rax)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    (%r14),%ebx
jl     11e0 <daemonize+0x70>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0xe01(%rip),%rbx
mov    %rbx,%rdi
call   1080 <open@plt>
mov    0x2dd2(%rip),%r14
mov    (%r14),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
xor    %eax,%eax
jmp    122d <daemonize+0xbd>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

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

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