#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);
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() {
  pid_t pid, sid;
  if (getppid() == 1)
    return;
  pid = fork();
  if (pid < 0)
    exit(1);
  if (pid > 0)
    exit(0);
  umask(0);
  sid = setsid();
  if (sid < 0)
    exit(1);
  if ((chdir("/") < 0))
    exit(1);
  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    118d <daemonize+0x1d>
jmp    125a <daemonize+0xea>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11ad <daemonize+0x3d>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11bf <daemonize+0x4f>
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    11e6 <daemonize+0x76>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe13(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    1206 <daemonize+0x96>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    0xdec(%rip),%rdi
lea    0xdef(%rip),%rsi
call   1040 <freopen@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
lea    0xdd0(%rip),%rdi
lea    0xdd5(%rip),%rsi
call   1040 <freopen@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
lea    0xdb4(%rip),%rdi
lea    0xdb9(%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    1188 <daemonize+0x18>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1213 <daemonize+0xa3>
jne    121d <daemonize+0xad>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     1213 <daemonize+0xa3>
lea    0xe4c(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     1213 <daemonize+0xa3>
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
lea    0xe34(%rip),%rbx
lea    0xe37(%rip),%rsi
mov    %rbx,%rdi
call   1040 <freopen@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
lea    0xe21(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <freopen@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <freopen@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.3 = [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 @exit(i32 1)
unreachable
12: ; preds = %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 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %24
%30 = load i32, i32* @stdin
%31 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, 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.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, 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.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %29, %5
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.3 = [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 @exit(i32 1)
unreachable
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 = %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 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %18
%24 = load i32, i32* @stdin
%25 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, 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.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, 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.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %0, %23
ret void
}