#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*/ exit(int);
int fork();
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int getppid();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
void daemonize(void) {
  int i;
  if (getppid() == 1)
    return;
  i = fork();
  if (i < 0)
    exit(1);
  if (i > 0)
    exit(0);
  setsid();
  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   1050 <getppid@plt>
cmp    $0x1,%eax
jne    116d <daemonize+0x1d>
jmp    11f7 <daemonize+0xa7>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    118b <daemonize+0x3b>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    119c <daemonize+0x4c>
xor    %edi,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
lea    0xe4d(%rip),%rdi
lea    0xe50(%rip),%rsi
call   1030 <freopen@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
lea    0xe31(%rip),%rdi
lea    0xe36(%rip),%rsi
call   1030 <freopen@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
lea    0xe15(%rip),%rdi
lea    0xe1a(%rip),%rsi
call   1030 <freopen@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <getppid@plt>
cmp    $0x1,%eax
jne    1168 <daemonize+0x18>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
js     11d1 <daemonize+0x81>
jne    11db <daemonize+0x8b>
xor    %eax,%eax
call   1040 <setsid@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
lea    0xe74(%rip),%rbx
lea    0xe77(%rip),%rsi
mov    %rbx,%rdi
call   1030 <freopen@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
lea    0xe61(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <freopen@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <freopen@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [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 %20, label %3
3: ; preds = %0
%4 = tail call i32 (...) @fork()
%5 = icmp slt i32 %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 i32 %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 (...) @setsid()
%14 = load i32, i32* @stdin
%15 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i32, i32* @stdout
%17 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @stderr
%19 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %0, %12
ret void
}