#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);
  umask(0);
  (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>
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x1ff,%edi
call   1070 <umask@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jl     1277 <daemonize+0xf7>
lea    0xdf3(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jl     1277 <daemonize+0xf7>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jl     1277 <daemonize+0xf7>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jl     1277 <daemonize+0xf7>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    129a <daemonize+0x11a>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d35(%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     1233 <daemonize+0xb3>
jne    1245 <daemonize+0xc5>
test   %rbx,%rbx
je     11a7 <daemonize+0x27>
mov    %rbx,%rdi
call   1080 <write_pid_to_file@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x1ff,%edi
call   1070 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     1210 <daemonize+0x90>
lea    0xe48(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     1210 <daemonize+0x90>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
test   %rax,%rax
js     1210 <daemonize+0x90>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
test   %rax,%rax
js     1210 <daemonize+0x90>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
test   %rax,%rax
js     1210 <daemonize+0x90>
pop    %rbx
ret
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
jmp    1220 <daemonize+0xa0>
mov    0x2d74(%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 0)
%27 = call i32 @umask(i32 511)
%28 = call i64 (...) @setsid()
%29 = icmp slt i64 %28, 0
br i1 %29, label %45, label %30
30: ; preds = %25
%31 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp slt i64 %31, 0
br i1 %32, label %45, label %33
33: ; preds = %30
%34 = load i32, i32* @STDIN_FILENO
%35 = call i64 @close(i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %45, label %37
37: ; preds = %33
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i64 @close(i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %37
%42 = load i32, i32* @STDERR_FILENO
%43 = call i64 @close(i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %41, %37, %33, %30, %25
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %41
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 0)
%20 = tail call i32 @umask(i32 511)
%21 = tail call i64 (...) @setsid()
%22 = icmp slt i64 %21, 0
br i1 %22, label %38, label %23
23: ; preds = %18
%24 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp slt i64 %24, 0
br i1 %25, label %38, label %26
26: ; preds = %23
%27 = load i32, i32* @STDIN_FILENO
%28 = tail call i64 @close(i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %38, label %30
30: ; preds = %26
%31 = load i32, i32* @STDOUT_FILENO
%32 = tail call i64 @close(i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = load i32, i32* @STDERR_FILENO
%36 = tail call i64 @close(i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %34, %30, %26, %23, %18
%39 = load i32, i32* @stderr
%40 = tail call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = tail call i32 @exit(i32 %41)
unreachable
43: ; preds = %34
ret void
}