#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*/ ExecAsDaemon(char const *, int, int, char const *, char **);
int /*<<< orphan*/ WritePidFile(int, char const *, int);
int /*<<< orphan*/ abort();
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int fork();
int pipe(int *);
void Daemonize(const char *log_path, bool log_append, const char *pid_path,
               const char *exe, char **argv) {
  assert(argv[0] != NULL);
  int pid_done_fds[2];
  if (pipe(pid_done_fds) == -1) {
    err(EXIT_FAILURE, "pipe failed");
  }
  pid_t pid = fork();
  if (pid == -1) {
    err(EXIT_FAILURE, "fork failed");
  } else if (pid == 0) {
    close(pid_done_fds[1]);
    ExecAsDaemon(log_path, log_append, pid_done_fds[0], exe, argv);
    abort();
  }
  close(pid_done_fds[0]);
  WritePidFile(pid, pid_path, pid_done_fds[1]);
}


x86 assembly (clang, -O0):
<Daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
lea    -0x30(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d7 <Daemonize+0x57>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1070 <err@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1205 <Daemonize+0x85>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
call   1070 <err@plt>
jmp    123a <Daemonize+0xba>
cmpl   $0x0,-0x34(%rbp)
jne    1235 <Daemonize+0xb5>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x30(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1080 <ExecAsDaemon@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    123a <Daemonize+0xba>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1090 <WritePidFile@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Daemonize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%rbx
mov    %rcx,%r15
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%r13
xor    %edi,%edi
cmpq   $0x0,(%r8)
setne  %dil
call   1060 <assert@plt>
mov    %rsp,%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11cb <Daemonize+0x4b>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1070 <err@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1201 <Daemonize+0x81>
test   %ebp,%ebp
jne    1216 <Daemonize+0x96>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edx
mov    %r13,%rdi
mov    %r12d,%esi
mov    %r15,%rcx
mov    %rbx,%r8
call   1080 <ExecAsDaemon@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
call   1070 <err@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1090 <WritePidFile@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [12 x i8] c"pipe failed\00"
@.str.1 = [12 x i8] c"fork failed\00"
define void @Daemonize(i8* %0, i32 %1, i8* %2, i8* %3, i8** %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8**
%11 = alloca [2 x i32]
%12 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8** %4, i8*** %10
%13 = load i8**, i8*** %10
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = icmp ne i8* %15, null
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%20 = call i32 @pipe(i32* %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %5
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @err(i32 %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %22, %5
%26 = call i32 (...) @fork()
store i32 %26, i32* %12
%27 = load i32, i32* %12
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @err(i32 %30, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %48
32: ; preds = %25
%33 = load i32, i32* %12
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %47
35: ; preds = %32
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = load i8*, i8** %6
%40 = load i32, i32* %7
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%42 = load i32, i32* %41
%43 = load i8*, i8** %9
%44 = load i8**, i8*** %10
%45 = call i32 @ExecAsDaemon(i8* %39, i32 %40, i32 %42, i8* %43, i8** %44)
%46 = call i32 (...) @abort()
unreachable
47: ; preds = %32
br label %48
48: ; preds = %47, %29
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %12
%53 = load i8*, i8** %8
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%55 = load i32, i32* %54
%56 = call i32 @WritePidFile(i32 %52, i8* %53, i32 %55)
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [12 x i8] c"pipe failed\00"
@.str.1 = [12 x i8] c"fork failed\00"
define void @Daemonize(i8* %0, i32 %1, i8* %2, i8* %3, i8** %4) {
%6 = alloca [2 x i32]
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = bitcast [2 x i32]* %6 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %5
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @err(i32 %16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %15, %5
%19 = call i32 (...) @fork()
switch i32 %19, label %30 [
i32 -1, label %20
i32 0, label %23
]
20: ; preds = %18
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @err(i32 %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %30
23: ; preds = %18
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %12
%28 = call i32 @ExecAsDaemon(i8* %0, i32 %1, i32 %27, i8* %3, i8** %4)
%29 = call i32 (...) @abort()
unreachable
30: ; preds = %18, %20
%31 = load i32, i32* %12
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @WritePidFile(i32 %19, i8* %2, i32 %34)
ret void
}