#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*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fatal(char *);
int fork();
int open(char *, int /*<<< orphan*/);
int setsid();
int /*<<< orphan*/ write_pidfile(char const *);
void daemonize(const char *pidfile) {
  pid_t pid;
  int null;
  pid = fork();
  if (pid == -1)
    fatal("Failed to fork()");
  else if (pid != 0)
    exit(0);
  if (setsid() == -1)
    fatal("Failed to setsid()");
  if (chdir("/") == -1)
    fatal("Failed to chdir()");
  null = open("/dev/null", O_RDWR);
  if (null == -1)
    fatal("Failed to open /dev/null");
  dup2(null, STDIN_FILENO);
  dup2(null, STDOUT_FILENO);
  dup2(null, STDERR_FILENO);
  close(null);
  if (pidfile)
    write_pidfile(pidfile);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c1 <daemonize+0x31>
lea    0xe49(%rip),%rdi
call   10a0 <fatal@plt>
jmp    11d7 <daemonize+0x47>
cmpl   $0x0,-0xc(%rbp)
je     11d2 <daemonize+0x42>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    11d7 <daemonize+0x47>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11f3 <daemonize+0x63>
lea    0xe23(%rip),%rdi
call   10a0 <fatal@plt>
lea    0xe2a(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1214 <daemonize+0x84>
lea    0xe17(%rip),%rdi
call   10a0 <fatal@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0xe23(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1242 <daemonize+0xb2>
lea    0xdfb(%rip),%rdi
call   10a0 <fatal@plt>
mov    -0x10(%rbp),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
cmpq   $0x0,-0x8(%rbp)
je     1291 <daemonize+0x101>
mov    -0x8(%rbp),%rdi
call   1070 <write_pidfile@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11b6 <daemonize+0x26>
cmp    $0xffffffff,%eax
jne    1264 <daemonize+0xd4>
lea    0xe4f(%rip),%rdi
call   10a0 <fatal@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11ce <daemonize+0x3e>
lea    0xe48(%rip),%rdi
call   10a0 <fatal@plt>
lea    0xe4f(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11eb <daemonize+0x5b>
lea    0xe40(%rip),%rdi
call   10a0 <fatal@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1213 <daemonize+0x83>
lea    0xe2a(%rip),%rdi
call   10a0 <fatal@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rbx,%rbx
je     125d <daemonize+0xcd>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <write_pidfile@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   1090 <exit@plt>

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

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