#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
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*/);
extern int daemonize() {
  pid_t pid, sid;
  if (getppid() == 1)
    return 0;
  pid = fork();
  if (pid < 0)
    exit(EXIT_FAILURE);
  if (pid > 0)
    exit(EXIT_SUCCESS);
  umask(0);
  sid = setsid();
  if (sid < 0)
    exit(EXIT_FAILURE);
  if ((chdir("/")) < 0)
    exit(EXIT_FAILURE);
  if (freopen("/dev/null", "r", stdin) == NULL)
    return -1;
  if (freopen("/dev/null", "w", stdout) == NULL)
    return -1;
  if (freopen("/dev/null", "w", stderr) == NULL)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1070 <getppid@plt>
cmp    $0x1,%eax
jne    1194 <daemonize+0x24>
movl   $0x0,-0x4(%rbp)
jmp    12bd <daemonize+0x14d>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11b8 <daemonize+0x48>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    11d1 <daemonize+0x61>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1060 <umask@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11fc <daemonize+0x8c>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xdfd(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    1220 <daemonize+0xb0>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
lea    0xdd2(%rip),%rdi
lea    0xdd5(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%rax
jne    1252 <daemonize+0xe2>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <daemonize+0x14d>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edx
lea    0xda0(%rip),%rdi
lea    0xda5(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%rax
jne    1284 <daemonize+0x114>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <daemonize+0x14d>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edx
lea    0xd6e(%rip),%rdi
lea    0xd73(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%rax
jne    12b6 <daemonize+0x146>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <daemonize+0x14d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
cmp    $0x1,%eax
je     1226 <daemonize+0xb6>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     122a <daemonize+0xba>
jne    1238 <daemonize+0xc8>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     122a <daemonize+0xba>
lea    0xe4d(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     122a <daemonize+0xba>
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
lea    0xe35(%rip),%rdi
lea    0xe38(%rip),%rsi
call   1040 <freopen@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1226 <daemonize+0xb6>
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
lea    0xe0f(%rip),%rdi
lea    0xe14(%rip),%rsi
call   1040 <freopen@plt>
test   %rax,%rax
je     1226 <daemonize+0xb6>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    0xdee(%rip),%rdi
lea    0xdf3(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x1,%rax
sbb    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
ret
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.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 i32 @daemonize() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = call i32 (...) @getppid()
%5 = icmp eq i32 %4, 1
br i1 %5, label %6, label %7
6: ; preds = %0
store i32 0, i32* %1
br label %50
7: ; preds = %0
%8 = call i64 (...) @fork()
store i64 %8, i64* %2
%9 = load i64, i64* %2
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %7
%15 = load i64, i64* %2
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @EXIT_SUCCESS
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %14
%21 = call i32 @umask(i32 0)
%22 = call i64 (...) @setsid()
store i64 %22, i64* %3
%23 = load i64, i64* %3
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %20
%29 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %28
%35 = load i32, i32* @stdin
%36 = 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 %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %39
38: ; preds = %34
store i32 -1, i32* %1
br label %50
39: ; preds = %34
%40 = load i32, i32* @stdout
%41 = 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 %40)
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %44
43: ; preds = %39
store i32 -1, i32* %1
br label %50
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = 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 %45)
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %49
48: ; preds = %44
store i32 -1, i32* %1
br label %50
49: ; preds = %44
store i32 0, i32* %1
br label %50
50: ; preds = %49, %48, %43, %38, %6
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.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 i32 @daemonize() {
%1 = tail call i32 (...) @getppid()
%2 = icmp eq i32 %1, 1
br i1 %2, label %40, label %3
3: ; preds = %0
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %3
%10 = icmp eq i64 %4, 0
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = load i32, i32* @EXIT_SUCCESS
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %9
%15 = tail call i32 @umask(i32 0)
%16 = tail call i64 (...) @setsid()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %14
%22 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %21
%28 = load i32, i32* @stdin
%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.2, i64 0, i64 0), i32 %28)
%30 = icmp eq i32* %29, null
br i1 %30, label %40, label %31
31: ; preds = %27
%32 = load i32, i32* @stdout
%33 = 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 %32)
%34 = icmp eq i32* %33, null
br i1 %34, label %40, label %35
35: ; preds = %31
%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
%39 = sext i1 %38 to i32
br label %40
40: ; preds = %35, %31, %27, %0
%41 = phi i32 [ 0, %0 ], [ -1, %27 ], [ -1, %31 ], [ %39, %35 ]
ret i32 %41
}