#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*/ EXIT_SUCCESS;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WTERMSIG(int);
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ execv(char *const, char *const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *strsignal(int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
int daemonize(char *const argv[]) {
  int status;
  pid_t pid;
  pid = fork();
  if (pid == -1) {
    perror("fork");
    return -1;
  }
  if (pid) {
    if (waitpid(pid, &status, 0) == -1) {
      perror("waitpid");
      return -1;
    }
    if (WIFEXITED(status))
      return WEXITSTATUS(status);
    else if (WIFSIGNALED(status))
      printf("%s\n", strsignal(WTERMSIG(status)));
    return status;
  }
  pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  } else if (pid) {
    exit(EXIT_SUCCESS);
  }
  execv(argv[0], argv);
  perror("execv");
  _exit(127);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11f8 <daemonize+0x38>
lea    0xe19(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <daemonize+0x144>
cmpl   $0x0,-0x18(%rbp)
je     1293 <daemonize+0xd3>
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1231 <daemonize+0x71>
lea    0xde5(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <daemonize+0x144>
mov    -0x14(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1253 <daemonize+0x93>
mov    -0x14(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    1304 <daemonize+0x144>
mov    -0x14(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1283 <daemonize+0xc3>
mov    -0x14(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,%edi
call   1070 <strsignal@plt>
mov    %rax,%rsi
lea    0xd8f(%rip),%rdi
call   1040 <printf@plt>
jmp    1288 <daemonize+0xc8>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1304 <daemonize+0x144>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12c1 <daemonize+0x101>
lea    0xd52(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
je     12d9 <daemonize+0x119>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    12de <daemonize+0x11e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   10d0 <execv@plt>
lea    0xd1c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   10e0 <fork@plt>
cmp    $0xffffffff,%eax
je     1233 <daemonize+0x73>
test   %eax,%eax
je     1253 <daemonize+0x93>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    $0xffffffff,%eax
je     123c <daemonize+0x7c>
mov    0xc(%rsp),%edi
call   10b0 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     1204 <daemonize+0x44>
call   1080 <WEXITSTATUS@plt>
jmp    124d <daemonize+0x8d>
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
je     122d <daemonize+0x6d>
mov    0xc(%rsp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,%edi
call   1070 <strsignal@plt>
lea    0xde8(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    0xc(%rsp),%eax
jmp    124d <daemonize+0x8d>
lea    0xdc6(%rip),%rdi
jmp    1243 <daemonize+0x83>
lea    0xdc2(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     127d <daemonize+0xbd>
cmp    $0xffffffff,%eax
jne    129e <daemonize+0xde>
lea    0xd96(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   10d0 <execv@plt>
lea    0xd82(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.3 = [6 x i8] c"execv\00"
define i32 @daemonize(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
store i8** %0, i8*** %3
%6 = call i32 (...) @fork()
store i32 %6, i32* %5
%7 = load i32, i32* %5
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %62
11: ; preds = %1
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %39
14: ; preds = %11
%15 = load i32, i32* %5
%16 = call i32 @waitpid(i32 %15, i32* %4, i32 0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %62
20: ; preds = %14
%21 = load i32, i32* %4
%22 = call i64 @WIFEXITED(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* %4
%26 = call i32 @WEXITSTATUS(i32 %25)
store i32 %26, i32* %2
br label %62
27: ; preds = %20
%28 = load i32, i32* %4
%29 = call i64 @WIFSIGNALED(i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = load i32, i32* %4
%33 = call i32 @WTERMSIG(i32 %32)
%34 = call i8* @strsignal(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %31, %27
br label %37
37: ; preds = %36
%38 = load i32, i32* %4
store i32 %38, i32* %2
br label %62
39: ; preds = %11
%40 = call i32 (...) @fork()
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %39
%48 = load i32, i32* %5
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* @EXIT_SUCCESS
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %47
br label %54
54: ; preds = %53
%55 = load i8**, i8*** %3
%56 = getelementptr inbounds i8*, i8** %55, i64 0
%57 = load i8*, i8** %56
%58 = load i8**, i8*** %3
%59 = call i32 @execv(i8* %57, i8** %58)
%60 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 @_exit(i32 127)
unreachable
62: ; preds = %37, %24, %18, %9
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.3 = [6 x i8] c"execv\00"
define i32 @daemonize(i8** %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 (...) @fork()
switch i32 %4, label %7 [
i32 -1, label %5
i32 0, label %29
]
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %43
7: ; preds = %1
%8 = call i32 @waitpid(i32 %4, i32* %2, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %43
12: ; preds = %7
%13 = load i32, i32* %2
%14 = call i64 @WIFEXITED(i32 %13)
%15 = icmp eq i64 %14, 0
%16 = load i32, i32* %2
br i1 %15, label %19, label %17
17: ; preds = %12
%18 = call i32 @WEXITSTATUS(i32 %16)
br label %43
19: ; preds = %12
%20 = call i64 @WIFSIGNALED(i32 %16)
%21 = icmp eq i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = load i32, i32* %2
%24 = call i32 @WTERMSIG(i32 %23)
%25 = call i8* @strsignal(i32 %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %19, %22
%28 = load i32, i32* %2
br label %43
29: ; preds = %1
%30 = tail call i32 (...) @fork()
switch i32 %30, label %35 [
i32 -1, label %31
i32 0, label %38
]
31: ; preds = %29
%32 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %29
%36 = load i32, i32* @EXIT_SUCCESS
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %29
%39 = load i8*, i8** %0
%40 = tail call i32 @execv(i8* %39, i8** %0)
%41 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%42 = tail call i32 @_exit(i32 127)
unreachable
43: ; preds = %27, %17, %10, %5
%44 = phi i32 [ -1, %5 ], [ -1, %10 ], [ %18, %17 ], [ %28, %27 ]
ret i32 %44
}