#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 /*<<< orphan*/ dummy;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SIGHUP;
scalar_t__ SIG_ERR;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SetupStdio(char const *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ execv(char const *, char **);
int read(int, char *, int);
int setsid();
scalar_t__ signal(int /*<<< orphan*/, int /*<<< orphan*/);
void ExecAsDaemon(const char *log_path, bool log_append, int pid_done_fd,
                  const char *exe, char **argv) {
  char dummy;
  if (read(pid_done_fd, &dummy, sizeof(dummy)) == -1) {
    err(EXIT_FAILURE, "Failed to wait for pid file creation");
  }
  close(pid_done_fd);
  if (signal(SIGHUP, SIG_IGN) == SIG_ERR) {
    err(EXIT_FAILURE, "Failed to install SIGHUP handler");
  }
  if (setsid() == -1) {
    err(EXIT_FAILURE, "setsid failed");
  }
  SetupStdio(log_path, log_append);
  execv(exe, argv);
  err(EXIT_FAILURE, "Failed to execute %s", exe);
}


x86 assembly (clang, -O0):
<ExecAsDaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    11bb <ExecAsDaemon+0x4b>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
jne    1201 <ExecAsDaemon+0x91>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1228 <ExecAsDaemon+0xb8>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1070 <SetupStdio@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <execv@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ExecAsDaemon>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8,%r14
mov    %rcx,%rbx
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%r12
lea    0xf(%rsp),%rsi
mov    %edx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    11b7 <ExecAsDaemon+0x47>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
xor    %eax,%eax
call   1080 <err@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%rax
jne    11f8 <ExecAsDaemon+0x88>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
xor    %eax,%eax
call   1080 <err@plt>
xor    %eax,%eax
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
jne    121b <ExecAsDaemon+0xab>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
xor    %eax,%eax
call   1080 <err@plt>
mov    %r12,%rdi
mov    %r15d,%esi
call   1070 <SetupStdio@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <execv@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <err@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [37 x i8] c"Failed to wait for pid file creation\00"
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIG_ERR = global i64 0
@.str.1 = [33 x i8] c"Failed to install SIGHUP handler\00"
@.str.2 = [14 x i8] c"setsid failed\00"
@.str.3 = [21 x i8] c"Failed to execute %s\00"
define void @ExecAsDaemon(i8* %0, i32 %1, i32 %2, i8* %3, i8** %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8**
%11 = alloca i8
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i8** %4, i8*** %10
%12 = load i32, i32* %8
%13 = call i32 @read(i32 %12, i8* %11, i32 1)
%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 (i32, i8*, ...) @err(i32 %16, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %15, %5
%19 = load i32, i32* %8
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* @SIGHUP
%22 = load i32, i32* @SIG_IGN
%23 = call i64 @signal(i32 %21, i32 %22)
%24 = load i64, i64* @SIG_ERR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 (i32, i8*, ...) @err(i32 %27, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %26, %18
%30 = call i32 (...) @setsid()
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 (i32, i8*, ...) @err(i32 %33, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %32, %29
%36 = load i8*, i8** %6
%37 = load i32, i32* %7
%38 = call i32 @SetupStdio(i8* %36, i32 %37)
%39 = load i8*, i8** %9
%40 = load i8**, i8*** %10
%41 = call i32 @execv(i8* %39, i8** %40)
%42 = load i32, i32* @EXIT_FAILURE
%43 = load i8*, i8** %9
%44 = call i32 (i32, i8*, ...) @err(i32 %42, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %43)
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [37 x i8] c"Failed to wait for pid file creation\00"
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIG_ERR = global i64 0
@.str.1 = [33 x i8] c"Failed to install SIGHUP handler\00"
@.str.2 = [14 x i8] c"setsid failed\00"
@.str.3 = [21 x i8] c"Failed to execute %s\00"
define void @ExecAsDaemon(i8* %0, i32 %1, i32 %2, i8* %3, i8** %4) {
%6 = alloca i8
%7 = call i32 @read(i32 %2, i8* %6, i32 1)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 (i32, i8*, ...) @err(i32 %10, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %9, %5
%13 = tail call i32 @close(i32 %2)
%14 = load i32, i32* @SIGHUP
%15 = load i32, i32* @SIG_IGN
%16 = tail call i64 @signal(i32 %14, i32 %15)
%17 = load i64, i64* @SIG_ERR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 (i32, i8*, ...) @err(i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %19, %12
%23 = tail call i32 (...) @setsid()
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 (i32, i8*, ...) @err(i32 %26, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %25, %22
%29 = tail call i32 @SetupStdio(i8* %0, i32 %1)
%30 = tail call i32 @execv(i8* %3, i8** %4)
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 (i32, i8*, ...) @err(i32 %31, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %3)
ret void
}