#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);
  freopen(log_file, "w", stdout);
  freopen(log_file, "w", 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>
mov    -0x8(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
lea    0xe18(%rip),%rsi
call   1050 <freopen@plt>
mov    -0x8(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    0xdff(%rip),%rsi
call   1050 <freopen@plt>
mov    0x2dd6(%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    1247 <daemonize+0xc7>
lea    0xdd3(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xdc0(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1276 <daemonize+0xf6>
lea    0xdad(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d61(%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     1200 <daemonize+0x80>
test   %eax,%eax
jg     1209 <daemonize+0x89>
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
lea    0xe57(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <freopen@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <freopen@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     1217 <daemonize+0x97>
lea    0xe20(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
je     1220 <daemonize+0xa0>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf9(%rip),%rdi
jmp    1227 <daemonize+0xa7>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xde9(%rip),%rdi
jmp    1227 <daemonize+0xa7>
lea    0xde9(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d9d(%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 = load i32, i32* @stdout
%22 = call i32 @freopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i8*, i8** %2
%24 = load i32, i32* @stderr
%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 i32, i32* @stdin
%27 = call i32 @fclose(i32 %26)
%28 = call i32 (...) @setsid()
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %18
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %18
%36 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %35
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 = load i32, i32* @stdout
%16 = tail call i32 @freopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @stderr
%18 = tail call i32 @freopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = load i32, i32* @stdin
%20 = tail call i32 @fclose(i32 %19)
%21 = tail call i32 (...) @setsid()
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %13
%24 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %13
%28 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %27
ret void
}