#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;
int chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(void) {
  pid_t pid;
  if (0 > ((pid = fork()))) {
    perror("deamoinze[fork]");
    exit(EXIT_FAILURE);
  } else if (0 != pid) {
    exit(EXIT_SUCCESS);
  }
  setsid();
  if (0 > ((pid = fork()))) {
    perror("deamoinze[fork]");
    exit(EXIT_FAILURE);
  } else if (0 != pid) {
    exit(EXIT_SUCCESS);
  }
  umask(0);
  if (-1 == chdir("/")) {
    perror("daemonize");
    exit(EXIT_FAILURE);
  }
  stderr = freopen("/dev/null", "w", stderr);
  stdin = freopen("/dev/null", "r", stdin);
  stdout = freopen("/dev/null", "w", stdout);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,%rcx
mov    %rcx,-0x8(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jle    11ab <daemonize+0x3b>
lea    0xe68(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
je     11c5 <daemonize+0x55>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11ca <daemonize+0x5a>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,%rcx
mov    %rcx,-0x8(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jle    1204 <daemonize+0x94>
lea    0xe0f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
je     121e <daemonize+0xae>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1223 <daemonize+0xb3>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xddf(%rip),%rdi
call   1030 <chdir@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    125f <daemonize+0xef>
lea    0xdc6(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
lea    0xdad(%rip),%rdi
lea    0xdb0(%rip),%rsi
call   1040 <freopen@plt>
mov    %eax,%ecx
mov    0x2d54(%rip),%rax
mov    %ecx,(%rax)
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
lea    0xd86(%rip),%rdi
lea    0xd8b(%rip),%rsi
call   1040 <freopen@plt>
mov    %eax,%ecx
mov    0x2d3d(%rip),%rax
mov    %ecx,(%rax)
mov    0x2d14(%rip),%rax
mov    (%rax),%edx
lea    0xd5f(%rip),%rdi
lea    0xd62(%rip),%rsi
call   1040 <freopen@plt>
mov    %eax,%ecx
mov    0x2cf6(%rip),%rax
mov    %ecx,(%rax)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1225 <daemonize+0xb5>
jne    122e <daemonize+0xbe>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1225 <daemonize+0xb5>
jne    122e <daemonize+0xbe>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xe57(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
je     123c <daemonize+0xcc>
mov    0x2e0e(%rip),%r15
mov    (%r15),%edx
lea    0xe48(%rip),%r12
lea    0xe4b(%rip),%r14
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <freopen@plt>
mov    %eax,(%r15)
mov    0x2df8(%rip),%rbx
mov    (%rbx),%edx
lea    0xe2f(%rip),%rsi
mov    %r12,%rdi
call   1040 <freopen@plt>
mov    %eax,(%rbx)
mov    0x2dbe(%rip),%rbx
mov    (%rbx),%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <freopen@plt>
mov    %eax,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdd4(%rip),%rdi
jmp    1243 <daemonize+0xd3>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xdcf(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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

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