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

/* Variables and functions */
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fatal(char *);
scalar_t__ fork();
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int getppid();
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(void) {
  pid_t pid, sid;
  if (getppid() == 1)
    return;
  pid = fork();
  if (pid < 0)
    fatal("cannot fork()");
  if (pid > 0)
    exit(0);
  umask(0);
  sid = setsid();
  if (sid < 0)
    fatal("cannot setsid()");
  if ((chdir("/")) < 0)
    fatal("cannot chdir()");
  freopen("/dev/null", "r", stdin);
  freopen("/dev/null", "w", stdout);
  freopen("/dev/null", "w", stderr);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1070 <getppid@plt>
cmp    $0x1,%eax
jne    119d <daemonize+0x1d>
jmp    1270 <daemonize+0xf0>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11bf <daemonize+0x3f>
lea    0xe46(%rip),%rdi
call   1090 <fatal@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11d1 <daemonize+0x51>
xor    %edi,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1060 <umask@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11fa <daemonize+0x7a>
lea    0xe19(%rip),%rdi
call   1090 <fatal@plt>
lea    0xe1d(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    121c <daemonize+0x9c>
lea    0xe09(%rip),%rdi
call   1090 <fatal@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
lea    0xe03(%rip),%rdi
lea    0xe06(%rip),%rsi
call   1040 <freopen@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
lea    0xde7(%rip),%rdi
lea    0xdec(%rip),%rsi
call   1040 <freopen@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
lea    0xdcb(%rip),%rdi
lea    0xdd0(%rip),%rsi
call   1040 <freopen@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <getppid@plt>
cmp    $0x1,%eax
jne    1198 <daemonize+0x18>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11ad <daemonize+0x2d>
je     11b9 <daemonize+0x39>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xe4c(%rip),%rdi
call   1090 <fatal@plt>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
jns    11d8 <daemonize+0x58>
lea    0xe3b(%rip),%rdi
call   1090 <fatal@plt>
lea    0xe3f(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
jns    11f5 <daemonize+0x75>
lea    0xe30(%rip),%rdi
call   1090 <fatal@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
lea    0xe2a(%rip),%rbx
lea    0xe2d(%rip),%rsi
mov    %rbx,%rdi
call   1040 <freopen@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
lea    0xe17(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <freopen@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <freopen@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"cannot fork()\00"
@.str.1 = [16 x i8] c"cannot setsid()\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [15 x i8] c"cannot chdir()\00"
@.str.4 = [10 x i8] c"/dev/null\00"
@.str.5 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.6 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
define void @daemonize() {
%1 = alloca i64
%2 = alloca i64
%3 = call i32 (...) @getppid()
%4 = icmp eq i32 %3, 1
br i1 %4, label %5, label %6
5: ; preds = %0
br label %36
6: ; preds = %0
%7 = call i64 (...) @fork()
store i64 %7, i64* %1
%8 = load i64, i64* %1
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = call i32 @fatal(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = load i64, i64* %1
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %12
%18 = call i32 @umask(i32 0)
%19 = call i64 (...) @setsid()
store i64 %19, i64* %2
%20 = load i64, i64* %2
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %17
%25 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @fatal(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = load i32, i32* @stdin
%31 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %30)
%32 = load i32, i32* @stdout
%33 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %32)
%34 = load i32, i32* @stderr
%35 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %29, %5
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"cannot fork()\00"
@.str.1 = [16 x i8] c"cannot setsid()\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [15 x i8] c"cannot chdir()\00"
@.str.4 = [10 x i8] c"/dev/null\00"
@.str.5 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.6 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
define void @daemonize() {
%1 = tail call i32 (...) @getppid()
%2 = icmp eq i32 %1, 1
br i1 %2, label %30, label %3
3: ; preds = %0
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @fatal(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %12
8: ; preds = %3
%9 = icmp eq i64 %4, 0
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %6, %8
%13 = tail call i32 @umask(i32 0)
%14 = tail call i64 (...) @setsid()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @fatal(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
%24 = load i32, i32* @stdin
%25 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %24)
%26 = load i32, i32* @stdout
%27 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %26)
%28 = load i32, i32* @stderr
%29 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %0, %23
ret void
}