#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 chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsid();
void daemonize() {
  int pid;
  pid = fork();
  if (pid == -1) {
    perror("fork: ");
    exit(2);
  } else if (pid != 0)
    exit(0);
  if (setsid() == -1)
    exit(2);
  if (chdir("/") == -1)
    exit(2);
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  open("/dev/null", O_RDWR);
  dup(0);
  dup(0);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11b2 <daemonize+0x32>
lea    0xe5d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
je     11c3 <daemonize+0x43>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    11c8 <daemonize+0x48>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11e2 <daemonize+0x62>
mov    $0x2,%edi
call   1090 <exit@plt>
lea    0xe1e(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1201 <daemonize+0x81>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
lea    0xdce(%rip),%rdi
call   1070 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    11f7 <daemonize+0x77>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     1208 <daemonize+0x88>
lea    0xe68(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
je     1208 <daemonize+0x88>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    0xe26(%rip),%rdi
call   1070 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
pop    %rax
jmp    1040 <dup@plt>
cmp    $0xffffffff,%eax
jne    1212 <daemonize+0x92>
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"fork: \00"
@.str.1 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @daemonize() {
%1 = alloca i32
%2 = call i32 (...) @fork()
store i32 %2, i32* %1
%3 = load i32, i32* %1
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 2)
unreachable
8: ; preds = %0
%9 = load i32, i32* %1
%10 = icmp ne 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 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @exit(i32 2)
unreachable
19: ; preds = %14
%20 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @exit(i32 2)
unreachable
24: ; preds = %19
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* @STDOUT_FILENO
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @STDERR_FILENO
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = call i32 @dup(i32 0)
%34 = call i32 @dup(i32 0)
ret void
}

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