#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;
scalar_t__ chdir(char *);
scalar_t__ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ umask(int);
int /*<<< orphan*/ write_pid_to_file(char *);
void daemonize(char *pidfile) {
  pid_t pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Failed to fork().\n");
    exit(EXIT_FAILURE);
  } else if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  if (pidfile)
    write_pid_to_file(pidfile);
  (void)umask(0777);
  if (setsid() < 0 || chdir("/") < 0 || close(STDIN_FILENO) < 0 ||
      close(STDOUT_FILENO) < 0 || close(STDERR_FILENO) < 0) {
    fprintf(stderr, "Failed to daemonize.\n");
    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    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11c5 <daemonize+0x45>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    11de <daemonize+0x5e>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    11e3 <daemonize+0x63>
cmpq   $0x0,-0x8(%rbp)
je     11f7 <daemonize+0x77>
mov    -0x8(%rbp),%rdi
call   1080 <write_pid_to_file@plt>
mov    $0x1ff,%edi
call   1070 <umask@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jl     1270 <daemonize+0xf0>
lea    0xdfa(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jl     1270 <daemonize+0xf0>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jl     1270 <daemonize+0xf0>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jl     1270 <daemonize+0xf0>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1293 <daemonize+0x113>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xd95(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     122c <daemonize+0xac>
jne    123e <daemonize+0xbe>
test   %rbx,%rbx
je     11a7 <daemonize+0x27>
mov    %rbx,%rdi
call   1080 <write_pid_to_file@plt>
mov    $0x1ff,%edi
call   1070 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     1209 <daemonize+0x89>
lea    0xe4f(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     1209 <daemonize+0x89>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
test   %rax,%rax
js     1209 <daemonize+0x89>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
test   %rax,%rax
js     1209 <daemonize+0x89>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
test   %rax,%rax
js     1209 <daemonize+0x89>
pop    %rbx
ret
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
jmp    1219 <daemonize+0x99>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"Failed to fork().\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [22 x i8] c"Failed to daemonize.\0A\00"
define void @daemonize(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
store i8* %0, i8** %2
%4 = call i64 (...) @fork()
store i64 %4, i64* %3
%5 = load i64, i64* %3
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 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 i64, i64* %3
%14 = icmp sgt i64 %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
br label %19
19: ; preds = %18
%20 = load i8*, i8** %2
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i8*, i8** %2
%24 = call i32 @write_pid_to_file(i8* %23)
br label %25
25: ; preds = %22, %19
%26 = call i32 @umask(i32 511)
%27 = call i64 (...) @setsid()
%28 = icmp slt i64 %27, 0
br i1 %28, label %44, label %29
29: ; preds = %25
%30 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp slt i64 %30, 0
br i1 %31, label %44, label %32
32: ; preds = %29
%33 = load i32, i32* @STDIN_FILENO
%34 = call i64 @close(i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %44, label %36
36: ; preds = %32
%37 = load i32, i32* @STDOUT_FILENO
%38 = call i64 @close(i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load i32, i32* @STDERR_FILENO
%42 = call i64 @close(i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %40, %36, %32, %29, %25
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %40
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"Failed to fork().\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [22 x i8] c"Failed to daemonize.\0A\00"
define void @daemonize(i8* %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %1
%10 = icmp eq i64 %2, 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 = icmp eq i8* %0, null
br i1 %15, label %18, label %16
16: ; preds = %14
%17 = tail call i32 @write_pid_to_file(i8* %0)
br label %18
18: ; preds = %16, %14
%19 = tail call i32 @umask(i32 511)
%20 = tail call i64 (...) @setsid()
%21 = icmp slt i64 %20, 0
br i1 %21, label %37, label %22
22: ; preds = %18
%23 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp slt i64 %23, 0
br i1 %24, label %37, label %25
25: ; preds = %22
%26 = load i32, i32* @STDIN_FILENO
%27 = tail call i64 @close(i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %37, label %29
29: ; preds = %25
%30 = load i32, i32* @STDOUT_FILENO
%31 = tail call i64 @close(i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i32, i32* @STDERR_FILENO
%35 = tail call i64 @close(i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %33, %29, %25, %22, %18
%38 = load i32, i32* @stderr
%39 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = tail call i32 @exit(i32 %40)
unreachable
42: ; preds = %33
ret void
}