#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIGHUP;
scalar_t__ SIG_ERR;
int /*<<< orphan*/ SIG_IGN;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ chdir(char *);
scalar_t__ close(int);
scalar_t__ dup2(int, int);
int fork();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsid();
scalar_t__ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int ez_daemonize(int nochdir, int noclose) {
  int fd;
  int pid;
  pid = fork();
  switch (pid) {
  case -1:
    return (-1);
  case 0:
    break;
  default:
    _exit(EXIT_SUCCESS);
  }
  if (setsid() == -1)
    return (-1);
  if (signal(SIGHUP, SIG_IGN) == SIG_ERR) {
    perror("child signal(SIGHUP, SIG_IGN) error!");
    return -1;
  }
  pid = fork();
  switch (pid) {
  case -1:
    return -1;
  case 0:
    break;
  default:
    _exit(EXIT_SUCCESS);
  }
  if (nochdir == 0) {
    if (chdir("/") != 0) {
      perror("chdir");
      return (-1);
    }
  }
  umask(0);
  if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
    if (dup2(fd, STDIN_FILENO) < 0) {
      perror("dup2 stdin");
      return (-1);
    }
    if (dup2(fd, STDOUT_FILENO) < 0) {
      perror("dup2 stdout");
      return (-1);
    }
    if (dup2(fd, STDERR_FILENO) < 0) {
      perror("dup2 stderr");
      return (-1);
    }
    if (fd > STDERR_FILENO) {
      if (close(fd) < 0) {
        perror("close");
        return (-1);
      }
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<ez_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     11dc <ez_daemonize+0x3c>
jmp    11cc <ez_daemonize+0x2c>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     11e8 <ez_daemonize+0x48>
jmp    11ed <ez_daemonize+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fa <ez_daemonize+0x25a>
jmp    11fb <ez_daemonize+0x5b>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1217 <ez_daemonize+0x77>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fa <ez_daemonize+0x25a>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    0x2d8b(%rip),%rcx
cmp    (%rcx),%rax
jne    1256 <ez_daemonize+0xb6>
lea    0xdbb(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fa <ez_daemonize+0x25a>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     1284 <ez_daemonize+0xe4>
jmp    1274 <ez_daemonize+0xd4>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     1290 <ez_daemonize+0xf0>
jmp    1295 <ez_daemonize+0xf5>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fa <ez_daemonize+0x25a>
jmp    12a3 <ez_daemonize+0x103>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    12e0 <ez_daemonize+0x140>
lea    0xd71(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     12db <ez_daemonize+0x13b>
lea    0xd5d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fa <ez_daemonize+0x25a>
jmp    12e0 <ez_daemonize+0x140>
xor    %edi,%edi
call   1090 <umask@plt>
cmpl   $0x0,-0xc(%rbp)
jne    13f3 <ez_daemonize+0x253>
mov    0x2ce0(%rip),%rax
mov    (%rax),%esi
lea    0xd2c(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
je     13f3 <ez_daemonize+0x253>
mov    -0x10(%rbp),%edi
mov    0x2c8a(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    1347 <ez_daemonize+0x1a7>
lea    0xd01(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fa <ez_daemonize+0x25a>
mov    -0x10(%rbp),%edi
mov    0x2c97(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    137a <ez_daemonize+0x1da>
lea    0xcd9(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fa <ez_daemonize+0x25a>
mov    -0x10(%rbp),%edi
mov    0x2c34(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    13ad <ez_daemonize+0x20d>
lea    0xcb2(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fa <ez_daemonize+0x25a>
mov    -0x10(%rbp),%eax
mov    0x2c01(%rip),%rcx
cmp    (%rcx),%eax
jle    13ee <ez_daemonize+0x24e>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    13e9 <ez_daemonize+0x249>
lea    0xc82(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fa <ez_daemonize+0x25a>
jmp    13ee <ez_daemonize+0x24e>
jmp    13f3 <ez_daemonize+0x253>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ez_daemonize>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12cf <ez_daemonize+0x12f>
test   %ebx,%ebx
jne    12fa <ez_daemonize+0x15a>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     12cf <ez_daemonize+0x12f>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
je     12be <ez_daemonize+0x11e>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12cf <ez_daemonize+0x12f>
test   %ebx,%ebx
jne    12fa <ez_daemonize+0x15a>
test   %ebp,%ebp
jne    1232 <ez_daemonize+0x92>
lea    0xe01(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    12d6 <ez_daemonize+0x136>
xor    %ebx,%ebx
xor    %edi,%edi
call   1090 <umask@plt>
test   %r14d,%r14d
jne    12cf <ez_daemonize+0x12f>
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
lea    0xdd9(%rip),%rdi
xor    %ebx,%ebx
xor    %edx,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12cf <ez_daemonize+0x12f>
mov    %eax,%ebp
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
test   %rax,%rax
js     12df <ez_daemonize+0x13f>
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
test   %rax,%rax
js     12e8 <ez_daemonize+0x148>
mov    0x2d23(%rip),%r14
mov    (%r14),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
test   %rax,%rax
js     12f1 <ez_daemonize+0x151>
cmp    (%r14),%ebp
jle    12cf <ez_daemonize+0x12f>
mov    %ebp,%edi
call   1060 <close@plt>
test   %rax,%rax
jns    12cf <ez_daemonize+0x12f>
lea    0xd9e(%rip),%rdi
jmp    12c5 <ez_daemonize+0x125>
lea    0xd3b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd4a(%rip),%rdi
jmp    12c5 <ez_daemonize+0x125>
lea    0xd51(%rip),%rdi
jmp    12c5 <ez_daemonize+0x125>
lea    0xd53(%rip),%rdi
jmp    12c5 <ez_daemonize+0x125>
lea    0xd56(%rip),%rdi
jmp    12c5 <ez_daemonize+0x125>
mov    0x2caf(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIG_ERR = global i64 0
@.str = [37 x i8] c"child signal(SIGHUP, SIG_IGN) error!\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [6 x i8] c"chdir\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@.str.4 = [11 x i8] c"dup2 stdin\00"
@STDOUT_FILENO = global i32 0
@.str.5 = [12 x i8] c"dup2 stdout\00"
@STDERR_FILENO = global i32 0
@.str.6 = [12 x i8] c"dup2 stderr\00"
@.str.7 = [6 x i8] c"close\00"
define i32 @ez_daemonize(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = call i32 (...) @fork()
store i32 %8, i32* %7
%9 = load i32, i32* %7
switch i32 %9, label %12 [
i32 -1, label %10
i32 0, label %11
]
10: ; preds = %2
store i32 -1, i32* %3
br label %86
11: ; preds = %2
br label %15
12: ; preds = %2
%13 = load i32, i32* @EXIT_SUCCESS
%14 = call i32 @_exit(i32 %13)
unreachable
15: ; preds = %11
%16 = call i32 (...) @setsid()
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 -1, i32* %3
br label %86
19: ; preds = %15
%20 = load i32, i32* @SIGHUP
%21 = load i32, i32* @SIG_IGN
%22 = call i64 @signal(i32 %20, i32 %21)
%23 = load i64, i64* @SIG_ERR
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %86
27: ; preds = %19
%28 = call i32 (...) @fork()
store i32 %28, i32* %7
%29 = load i32, i32* %7
switch i32 %29, label %32 [
i32 -1, label %30
i32 0, label %31
]
30: ; preds = %27
store i32 -1, i32* %3
br label %86
31: ; preds = %27
br label %35
32: ; preds = %27
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @_exit(i32 %33)
unreachable
35: ; preds = %31
%36 = load i32, i32* %4
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %35
%39 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %86
43: ; preds = %38
br label %44
44: ; preds = %43, %35
%45 = call i32 @umask(i32 0)
%46 = load i32, i32* %5
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %85
48: ; preds = %44
%49 = load i32, i32* @O_RDWR
%50 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %49, i32 0)
store i32 %50, i32* %6
%51 = icmp ne i32 %50, -1
br i1 %51, label %52, label %85
52: ; preds = %48
%53 = load i32, i32* %6
%54 = load i32, i32* @STDIN_FILENO
%55 = call i64 @dup2(i32 %53, i32 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %86
59: ; preds = %52
%60 = load i32, i32* %6
%61 = load i32, i32* @STDOUT_FILENO
%62 = call i64 @dup2(i32 %60, i32 %61)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %59
%65 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %86
66: ; preds = %59
%67 = load i32, i32* %6
%68 = load i32, i32* @STDERR_FILENO
%69 = call i64 @dup2(i32 %67, i32 %68)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %66
%72 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %86
73: ; preds = %66
%74 = load i32, i32* %6
%75 = load i32, i32* @STDERR_FILENO
%76 = icmp sgt i32 %74, %75
br i1 %76, label %77, label %84
77: ; preds = %73
%78 = load i32, i32* %6
%79 = call i64 @close(i32 %78)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %77
%82 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %86
83: ; preds = %77
br label %84
84: ; preds = %83, %73
br label %85
85: ; preds = %84, %48, %44
store i32 0, i32* %3
br label %86
86: ; preds = %85, %81, %71, %64, %57, %41, %30, %25, %18, %10
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIG_ERR = global i64 0
@.str = [37 x i8] c"child signal(SIGHUP, SIG_IGN) error!\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [6 x i8] c"chdir\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@.str.4 = [11 x i8] c"dup2 stdin\00"
@STDOUT_FILENO = global i32 0
@.str.5 = [12 x i8] c"dup2 stdout\00"
@STDERR_FILENO = global i32 0
@.str.6 = [12 x i8] c"dup2 stderr\00"
@.str.7 = [6 x i8] c"close\00"
define i32 @ez_daemonize(i32 %0, i32 %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %4 [
i32 -1, label %54
i32 0, label %7
]
4: ; preds = %2
%5 = load i32, i32* @EXIT_SUCCESS
%6 = tail call i32 @_exit(i32 %5)
unreachable
7: ; preds = %2
%8 = tail call i32 (...) @setsid()
%9 = icmp eq i32 %8, -1
br i1 %9, label %54, label %10
10: ; preds = %7
%11 = load i32, i32* @SIGHUP
%12 = load i32, i32* @SIG_IGN
%13 = tail call i64 @signal(i32 %11, i32 %12)
%14 = load i64, i64* @SIG_ERR
%15 = icmp eq i64 %13, %14
br i1 %15, label %51, label %16
16: ; preds = %10
%17 = tail call i32 (...) @fork()
switch i32 %17, label %18 [
i32 -1, label %54
i32 0, label %21
]
18: ; preds = %16
%19 = load i32, i32* @EXIT_SUCCESS
%20 = tail call i32 @_exit(i32 %19)
unreachable
21: ; preds = %16
%22 = icmp eq i32 %0, 0
br i1 %22, label %23, label %26
23: ; preds = %21
%24 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %51
26: ; preds = %23, %21
%27 = tail call i32 @umask(i32 0)
%28 = icmp eq i32 %1, 0
br i1 %28, label %29, label %54
29: ; preds = %26
%30 = load i32, i32* @O_RDWR
%31 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %30, i32 0)
%32 = icmp eq i32 %31, -1
br i1 %32, label %54, label %33
33: ; preds = %29
%34 = load i32, i32* @STDIN_FILENO
%35 = tail call i64 @dup2(i32 %31, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %51, label %37
37: ; preds = %33
%38 = load i32, i32* @STDOUT_FILENO
%39 = tail call i64 @dup2(i32 %31, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %51, label %41
41: ; preds = %37
%42 = load i32, i32* @STDERR_FILENO
%43 = tail call i64 @dup2(i32 %31, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %51, label %45
45: ; preds = %41
%46 = load i32, i32* @STDERR_FILENO
%47 = icmp sgt i32 %31, %46
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = tail call i64 @close(i32 %31)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48, %41, %37, %33, %23, %10
%52 = phi i8* [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), %10 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %23 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), %33 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), %37 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), %41 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), %48 ]
%53 = tail call i32 @perror(i8* %52)
br label %54
54: ; preds = %51, %26, %29, %48, %45, %16, %7, %2
%55 = phi i32 [ %3, %2 ], [ -1, %7 ], [ %17, %16 ], [ 0, %45 ], [ 0, %48 ], [ 0, %29 ], [ 0, %26 ], [ -1, %51 ]
ret i32 %55
}