#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 /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ setsid();
int /*<<< orphan*/ umask(int);
int /*<<< orphan*/ write_log(char *);
void daemonize(int nofork) {
  pid_t process_id = 0;
  if (!nofork)
    process_id = fork();
  if (process_id > 0) {
    _exit(EXIT_SUCCESS);
  } else if (process_id < 0) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (!nofork) {
    pid_t session_id = setsid();
    if (session_id < 0) {
      write_log("Could not create session. Exiting.");
      exit(EXIT_FAILURE);
    }
  }
  umask(077);
  if (chdir("/") < 0) {
    write_log("Could not change directory to /. Exiting.");
    exit(EXIT_FAILURE);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  if (!nofork)
    close(STDERR_FILENO);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    11b8 <daemonize+0x28>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    11d1 <daemonize+0x41>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
cmpq   $0x0,-0x10(%rbp)
jge    11f6 <daemonize+0x66>
lea    0xe1d(%rip),%rdi
call   1090 <perror@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    11fb <daemonize+0x6b>
cmpl   $0x0,-0x4(%rbp)
jne    123a <daemonize+0xaa>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1235 <daemonize+0xa5>
lea    0xde3(%rip),%rdi
call   1080 <write_log@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    123a <daemonize+0xaa>
mov    $0x3f,%edi
call   1070 <umask@plt>
lea    0xddd(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1274 <daemonize+0xe4>
lea    0xdc9(%rip),%rdi
call   1080 <write_log@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x4(%rbp)
jne    12a8 <daemonize+0x118>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
mov    %edi,%ebx
test   %edi,%edi
jne    11b1 <daemonize+0x21>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jg     1206 <daemonize+0x76>
js     1214 <daemonize+0x84>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     1222 <daemonize+0x92>
mov    $0x3f,%edi
call   1070 <umask@plt>
lea    0xe66(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     11fd <daemonize+0x6d>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
test   %ebx,%ebx
je     11ee <daemonize+0x5e>
pop    %rbx
ret
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
pop    %rbx
jmp    1050 <close@plt>
lea    0xe26(%rip),%rdi
jmp    1229 <daemonize+0x99>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
lea    0xde5(%rip),%rdi
call   1090 <perror@plt>
jmp    122e <daemonize+0x9e>
lea    0xddc(%rip),%rdi
call   1080 <write_log@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [35 x i8] c"Could not create session. Exiting.\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [42 x i8] c"Could not change directory to /. Exiting.\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @daemonize(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %3, label %22
3: ; preds = %1
%4 = tail call i64 (...) @fork()
%5 = icmp sgt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @EXIT_SUCCESS
%8 = tail call i32 @_exit(i32 %7)
unreachable
9: ; preds = %3
%10 = icmp slt i64 %4, 0
br i1 %10, label %11, label %15
11: ; preds = %9
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %9
%16 = tail call i64 (...) @setsid()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = tail call i32 @write_log(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %1, %15
%23 = tail call i32 @umask(i32 63)
%24 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = tail call i32 @write_log(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %22
%31 = load i32, i32* @STDIN_FILENO
%32 = tail call i32 @close(i32 %31)
%33 = load i32, i32* @STDOUT_FILENO
%34 = tail call i32 @close(i32 %33)
br i1 %2, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* @STDERR_FILENO
%37 = tail call i32 @close(i32 %36)
br label %38
38: ; preds = %35, %30
ret void
}