#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;
typedef int /*<<< orphan*/ pid;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EXIT_FAILURE;
scalar_t__ EXIT_SUCCESS;
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ _exit(scalar_t__);
int /*<<< orphan*/ assert(char const *);
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
scalar_t__ closeall(int);
scalar_t__ errno;
int /*<<< orphan*/ execv(char const *, char *const *);
int fork();
int getpid();
scalar_t__ pipe(int *);
int read(int, int *, int);
int setsid();
int waitpid(int, int *, int /*<<< orphan*/);
int write(int, int *, int);
pid_t daemonize(const char *path, char *const argv[], int nochdir,
                int noclose) {
  assert(path);
  pid_t pid;
  int fds[2];
  if (pipe(fds)) {
    return (-1);
  }
  if ((pid = fork())) {
    close(fds[1]);
    if (-1 == pid) {
      goto done;
    }
    int status;
    pid_t rc;
    while (-1 == (rc = waitpid(pid, &status, 0))) {
      if (EINTR != errno) {
        pid = -1;
        goto done;
      }
    }
    if (EXIT_FAILURE == WEXITSTATUS(status)) {
      pid = -1;
      goto done;
    }
    if (sizeof(pid) != read(fds[0], &pid, sizeof(pid))) {
      pid = -1;
    }
  done:
    close(fds[0]);
    return (pid);
  }
  if ((pid = fork())) {
    if (-1 == pid) {
      _exit(EXIT_FAILURE);
    }
    _exit(EXIT_SUCCESS);
  }
  if (-1 == setsid()) {
    _exit(EXIT_FAILURE);
  }
  pid = getpid();
  if (sizeof(pid) != write(fds[1], &pid, sizeof(pid))) {
    _exit(EXIT_FAILURE);
  }
  if (closeall(noclose)) {
    _exit(EXIT_FAILURE);
  }
  if (!nochdir && chdir("/")) {
    _exit(EXIT_FAILURE);
  }
  execv(path, argv);
  _exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <assert@plt>
lea    -0x2c(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
je     121e <daemonize+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    141a <daemonize+0x23a>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     130f <daemonize+0x12f>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
mov    $0xffffffff,%eax
cmp    -0x24(%rbp),%eax
jne    124c <daemonize+0x6c>
jmp    12fc <daemonize+0x11c>
jmp    1251 <daemonize+0x71>
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   10e0 <waitpid@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    129c <daemonize+0xbc>
mov    0x2d58(%rip),%rax
mov    (%rax),%rax
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%rax
je     1297 <daemonize+0xb7>
movl   $0xffffffff,-0x24(%rbp)
jmp    12fc <daemonize+0x11c>
jmp    1251 <daemonize+0x71>
mov    0x2d35(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%edi
call   10d0 <WEXITSTATUS@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jne    12ce <daemonize+0xee>
movl   $0xffffffff,-0x24(%rbp)
jmp    12fc <daemonize+0x11c>
mov    -0x2c(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <read@plt>
movslq %eax,%rcx
mov    $0x4,%eax
cmp    %rcx,%rax
je     12f7 <daemonize+0x117>
movl   $0xffffffff,-0x24(%rbp)
jmp    12fc <daemonize+0x11c>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    141a <daemonize+0x23a>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     134e <daemonize+0x16e>
mov    $0xffffffff,%eax
cmp    -0x24(%rbp),%eax
jne    133f <daemonize+0x15f>
mov    0x2ca1(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>
mov    0x2c82(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1090 <setsid@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1373 <daemonize+0x193>
mov    0x2c6d(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
movslq %eax,%rcx
mov    $0x4,%eax
cmp    %rcx,%rax
je     13ae <daemonize+0x1ce>
mov    0x2c32(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>
mov    -0x20(%rbp),%edi
call   10c0 <closeall@plt>
cmp    $0x0,%rax
je     13cf <daemonize+0x1ef>
mov    0x2c11(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    13fe <daemonize+0x21e>
lea    0xc20(%rip),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
je     13fe <daemonize+0x21e>
mov    0x2be2(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10f0 <execv@plt>
mov    0x2bc6(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r13
call   10b0 <assert@plt>
lea    0xc(%rsp),%rdi
call   1080 <pipe@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jne    12c1 <daemonize+0xe1>
xor    %eax,%eax
call   1100 <fork@plt>
mov    %eax,0x8(%rsp)
test   %eax,%eax
je     12d3 <daemonize+0xf3>
mov    %eax,%ebx
mov    0x10(%rsp),%edi
call   1070 <close@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%ebx
je     12b8 <daemonize+0xd8>
lea    0x14(%rsp),%r15
mov    0x2d83(%rip),%r12
mov    0x2d8c(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   10e0 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    127d <daemonize+0x9d>
mov    (%r12),%rax
cmp    0x0(%rbp),%rax
je     1260 <daemonize+0x80>
jmp    12b8 <daemonize+0xd8>
mov    0x2d54(%rip),%rax
mov    (%rax),%rbx
mov    0x14(%rsp),%edi
call   10d0 <WEXITSTATUS@plt>
cmp    %rax,%rbx
je     12b8 <daemonize+0xd8>
mov    0xc(%rsp),%edi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   10a0 <read@plt>
mov    $0xffffffff,%r14d
cmp    $0x4,%eax
jne    12b8 <daemonize+0xd8>
mov    0x8(%rsp),%r14d
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     12f2 <daemonize+0x112>
cmp    $0xffffffff,%eax
je     134f <daemonize+0x16f>
mov    0x2cde(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>
xor    %eax,%eax
call   1090 <setsid@plt>
cmp    $0xffffffff,%eax
je     134f <daemonize+0x16f>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0x8(%rsp)
mov    0x10(%rsp),%edi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmp    $0x4,%eax
jne    134f <daemonize+0x16f>
mov    %r12d,%edi
call   10c0 <closeall@plt>
test   %rax,%rax
jne    134f <daemonize+0x16f>
test   %r15d,%r15d
jne    1344 <daemonize+0x164>
lea    0xcc6(%rip),%rdi
call   1060 <chdir@plt>
test   %rax,%rax
jne    134f <daemonize+0x16f>
mov    %r13,%rdi
mov    %rbp,%rsi
call   10f0 <execv@plt>
mov    0x2c82(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>

LLVM IR (-O0):
@EINTR = global i64 0
@errno = global i64 0
@EXIT_FAILURE = global i64 0
@EXIT_SUCCESS = global i64 0
@.str = [2 x i8] c"/\00"
define i32 @daemonize(i8* %0, i8** %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [2 x i32]
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = load i8*, i8** %6
%15 = call i32 @assert(i8* %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%17 = call i64 @pipe(i32* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %4
store i32 -1, i32* %5
br label %110
20: ; preds = %4
%21 = call i32 (...) @fork()
store i32 %21, i32* %10
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %60
23: ; preds = %20
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %10
%28 = icmp eq i32 -1, %27
br i1 %28, label %29, label %30
29: ; preds = %23
br label %55
30: ; preds = %23
br label %31
31: ; preds = %40, %30
%32 = load i32, i32* %10
%33 = call i32 @waitpid(i32 %32, i32* %12, i32 0)
store i32 %33, i32* %13
%34 = icmp eq i32 -1, %33
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = load i64, i64* @EINTR
%37 = load i64, i64* @errno
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 -1, i32* %10
br label %55
40: ; preds = %35
br label %31
41: ; preds = %31
%42 = load i64, i64* @EXIT_FAILURE
%43 = load i32, i32* %12
%44 = call i64 @WEXITSTATUS(i32 %43)
%45 = icmp eq i64 %42, %44
br i1 %45, label %46, label %47
46: ; preds = %41
store i32 -1, i32* %10
br label %55
47: ; preds = %41
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32 @read(i32 %49, i32* %10, i32 4)
%51 = sext i32 %50 to i64
%52 = icmp ne i64 4, %51
br i1 %52, label %53, label %54
53: ; preds = %47
store i32 -1, i32* %10
br label %54
54: ; preds = %53, %47
br label %55
55: ; preds = %54, %46, %39, %29
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%57 = load i32, i32* %56
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* %10
store i32 %59, i32* %5
br label %110
60: ; preds = %20
%61 = call i32 (...) @fork()
store i32 %61, i32* %10
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %72
63: ; preds = %60
%64 = load i32, i32* %10
%65 = icmp eq i32 -1, %64
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i64, i64* @EXIT_FAILURE
%68 = call i32 @_exit(i64 %67)
unreachable
69: ; preds = %63
%70 = load i64, i64* @EXIT_SUCCESS
%71 = call i32 @_exit(i64 %70)
unreachable
72: ; preds = %60
%73 = call i32 (...) @setsid()
%74 = icmp eq i32 -1, %73
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load i64, i64* @EXIT_FAILURE
%77 = call i32 @_exit(i64 %76)
unreachable
78: ; preds = %72
%79 = call i32 (...) @getpid()
store i32 %79, i32* %10
%80 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%81 = load i32, i32* %80
%82 = call i32 @write(i32 %81, i32* %10, i32 4)
%83 = sext i32 %82 to i64
%84 = icmp ne i64 4, %83
br i1 %84, label %85, label %88
85: ; preds = %78
%86 = load i64, i64* @EXIT_FAILURE
%87 = call i32 @_exit(i64 %86)
unreachable
88: ; preds = %78
%89 = load i32, i32* %9
%90 = call i64 @closeall(i32 %89)
%91 = icmp ne i64 %90, 0
br i1 %91, label %92, label %95
92: ; preds = %88
%93 = load i64, i64* @EXIT_FAILURE
%94 = call i32 @_exit(i64 %93)
unreachable
95: ; preds = %88
%96 = load i32, i32* %8
%97 = icmp ne i32 %96, 0
br i1 %97, label %104, label %98
98: ; preds = %95
%99 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%100 = icmp ne i64 %99, 0
br i1 %100, label %101, label %104
101: ; preds = %98
%102 = load i64, i64* @EXIT_FAILURE
%103 = call i32 @_exit(i64 %102)
unreachable
104: ; preds = %98, %95
%105 = load i8*, i8** %6
%106 = load i8**, i8*** %7
%107 = call i32 @execv(i8* %105, i8** %106)
%108 = load i64, i64* @EXIT_FAILURE
%109 = call i32 @_exit(i64 %108)
unreachable
110: ; preds = %55, %19
%111 = load i32, i32* %5
ret i32 %111
}

LLVM IR (-O2):
@EINTR = global i64 0
@errno = global i64 0
@EXIT_FAILURE = global i64 0
@EXIT_SUCCESS = global i64 0
@.str = [2 x i8] c"/\00"
define i32 @daemonize(i8* %0, i8** %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = tail call i32 @assert(i8* %0)
%9 = bitcast i32* %5 to i8*
%10 = bitcast [2 x i32]* %6 to i8*
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%12 = call i64 @pipe(i32* %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %85
14: ; preds = %4
%15 = call i32 (...) @fork()
store i32 %15, i32* %5
%16 = icmp eq i32 %15, 0
br i1 %16, label %44, label %17
17: ; preds = %14
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
%21 = icmp eq i32 %15, -1
br i1 %21, label %40, label %22
22: ; preds = %17, %25
%23 = call i32 @waitpid(i32 %15, i32* %7, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i64, i64* @EINTR
%27 = load i64, i64* @errno
%28 = icmp eq i64 %26, %27
br i1 %28, label %22, label %40
29: ; preds = %22
%30 = load i64, i64* @EXIT_FAILURE
%31 = load i32, i32* %7
%32 = call i64 @WEXITSTATUS(i32 %31)
%33 = icmp eq i64 %30, %32
br i1 %33, label %40, label %34
34: ; preds = %29
%35 = load i32, i32* %11
%36 = call i32 @read(i32 %35, i32* %5, i32 4)
%37 = icmp eq i32 %36, 4
%38 = load i32, i32* %5
%39 = select i1 %37, i32 %38, i32 -1
br label %40
40: ; preds = %25, %34, %29, %17
%41 = phi i32 [ -1, %17 ], [ -1, %29 ], [ %39, %34 ], [ -1, %25 ]
%42 = load i32, i32* %11
%43 = call i32 @close(i32 %42)
br label %85
44: ; preds = %14
%45 = call i32 (...) @fork()
switch i32 %45, label %49 [
i32 0, label %52
i32 -1, label %46
]
46: ; preds = %44
%47 = load i64, i64* @EXIT_FAILURE
%48 = call i32 @_exit(i64 %47)
unreachable
49: ; preds = %44
%50 = load i64, i64* @EXIT_SUCCESS
%51 = call i32 @_exit(i64 %50)
unreachable
52: ; preds = %44
%53 = call i32 (...) @setsid()
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i64, i64* @EXIT_FAILURE
%57 = call i32 @_exit(i64 %56)
unreachable
58: ; preds = %52
%59 = call i32 (...) @getpid()
store i32 %59, i32* %5
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%61 = load i32, i32* %60
%62 = call i32 @write(i32 %61, i32* %5, i32 4)
%63 = icmp eq i32 %62, 4
br i1 %63, label %67, label %64
64: ; preds = %58
%65 = load i64, i64* @EXIT_FAILURE
%66 = call i32 @_exit(i64 %65)
unreachable
67: ; preds = %58
%68 = call i64 @closeall(i32 %3)
%69 = icmp eq i64 %68, 0
br i1 %69, label %73, label %70
70: ; preds = %67
%71 = load i64, i64* @EXIT_FAILURE
%72 = call i32 @_exit(i64 %71)
unreachable
73: ; preds = %67
%74 = icmp eq i32 %2, 0
br i1 %74, label %75, label %81
75: ; preds = %73
%76 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%77 = icmp eq i64 %76, 0
br i1 %77, label %81, label %78
78: ; preds = %75
%79 = load i64, i64* @EXIT_FAILURE
%80 = call i32 @_exit(i64 %79)
unreachable
81: ; preds = %75, %73
%82 = call i32 @execv(i8* %0, i8** %1)
%83 = load i64, i64* @EXIT_FAILURE
%84 = call i32 @_exit(i64 %83)
unreachable
85: ; preds = %4, %40
%86 = phi i32 [ %41, %40 ], [ -1, %4 ]
ret i32 %86
}