#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ freopen(char const *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(const char *log_file) {
  pid_t pid, sid;
  pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (pid > 0)
    exit(EXIT_SUCCESS);
  umask(0);
  if (log_file != NULL) {
    freopen(log_file, "w", stdout);
    freopen(log_file, "w", stderr);
  } else {
    fclose(stdout);
    fclose(stderr);
  }
  fclose(stdin);
  sid = setsid();
  if (sid == -1) {
    perror("setsid");
    exit(EXIT_FAILURE);
  }
  if (chdir("/") == -1) {
    perror("chdir");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ba <daemonize+0x3a>
lea    0xe59(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jle    11d2 <daemonize+0x52>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
cmpq   $0x0,-0x8(%rbp)
je     121b <daemonize+0x9b>
mov    -0x8(%rbp),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
lea    0xe0d(%rip),%rsi
call   1050 <freopen@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
lea    0xdf4(%rip),%rsi
call   1050 <freopen@plt>
jmp    1237 <daemonize+0xb7>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1273 <daemonize+0xf3>
lea    0xda7(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xd94(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
jne    12a2 <daemonize+0x122>
lea    0xd81(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     1224 <daemonize+0xa4>
test   %eax,%eax
jg     122d <daemonize+0xad>
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
test   %rbx,%rbx
je     11dc <daemonize+0x5c>
lea    0xe4a(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <freopen@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <freopen@plt>
jmp    11f1 <daemonize+0x71>
mov    %edx,%edi
call   1030 <fclose@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     123b <daemonize+0xbb>
lea    0xdfc(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
je     1244 <daemonize+0xc4>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd5(%rip),%rdi
jmp    124b <daemonize+0xcb>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xdc5(%rip),%rdi
jmp    124b <daemonize+0xcb>
lea    0xdc5(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

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

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