#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(void) {
  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);
  }
  if (freopen("/dev/null", "r", stdin) == NULL)
    exit(-1);
  if (freopen("/dev/null", "w", stdout) == NULL)
    exit(-1);
  if (freopen("/dev/null", "w", stderr) == NULL)
    exit(-1);
}


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    1296 <daemonize+0x126>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11ad <daemonize+0x3d>
mov    $0xffffffff,%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    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xe13(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    1206 <daemonize+0x96>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    0xdec(%rip),%rdi
lea    0xdef(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%rax
jne    1236 <daemonize+0xc6>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
lea    0xdbc(%rip),%rdi
lea    0xdc1(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%rax
jne    1266 <daemonize+0xf6>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edx
lea    0xd8c(%rip),%rdi
lea    0xd91(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%rax
jne    1296 <daemonize+0x126>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   1070 <getppid@plt>
cmp    $0x1,%eax
je     121e <daemonize+0xae>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1220 <daemonize+0xb0>
jne    122a <daemonize+0xba>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     1220 <daemonize+0xb0>
lea    0xe4f(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     1220 <daemonize+0xb0>
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
lea    0xe37(%rip),%rdi
lea    0xe3a(%rip),%rsi
call   1040 <freopen@plt>
test   %rax,%rax
je     1220 <daemonize+0xb0>
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
lea    0xe16(%rip),%rdi
lea    0xe1b(%rip),%rsi
call   1040 <freopen@plt>
test   %rax,%rax
je     1220 <daemonize+0xb0>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
lea    0xdf5(%rip),%rdi
lea    0xdfa(%rip),%rsi
call   1040 <freopen@plt>
test   %rax,%rax
je     1220 <daemonize+0xb0>
pop    %rax
ret
mov    $0xffffffff,%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 %47
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 = icmp eq i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %29
%36 = load i32, i32* @stdout
%37 = 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 %36)
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %35
%42 = load i32, i32* @stderr
%43 = 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 %42)
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @exit(i32 -1)
unreachable
47: ; preds = %5, %41
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 %41, 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 = icmp eq i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = tail call i32 @exit(i32 -1)
unreachable
29: ; preds = %23
%30 = load i32, i32* @stdout
%31 = 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 %30)
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = tail call i32 @exit(i32 -1)
unreachable
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = 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 %36)
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = tail call i32 @exit(i32 -1)
unreachable
41: ; preds = %35, %0
ret void
}