#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;
scalar_t__ STDERR_FILENO;
int STDIN_FILENO;
scalar_t__ STDOUT_FILENO;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, scalar_t__);
int fork();
int open(char *, int /*<<< orphan*/);
int setsid();
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(int flags) {
  int maxfd, fd;
  switch (fork()) {
  case -1:
    return -1;
  case 0:
    break;
  default:
    _exit(EXIT_SUCCESS);
  }
  if (setsid() == -1)
    return -1;
  switch (fork()) {
  case -1:
    return -1;
  case 0:
    break;
  default:
    _exit(EXIT_SUCCESS);
  }
  if (!(flags & 010))
    umask(0);
  if (!(flags & 01))
    chdir("/");
  if (!(flags & 02)) {
    maxfd = sysconf(_SC_OPEN_MAX);
    if (maxfd == -1)
      maxfd = 8192;
    for (fd = 0; fd < maxfd; fd++)
      close(fd);
  }
  if (!(flags & 04)) {
    close(STDIN_FILENO);
    fd = open("/dev/null", O_RDWR);
    if (fd != STDIN_FILENO)
      return -1;
    if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO)
      return -1;
    if (dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11c5 <daemonize+0x35>
jmp    11b5 <daemonize+0x25>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     11d1 <daemonize+0x41>
jmp    11d6 <daemonize+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a0 <daemonize+0x210>
jmp    11e4 <daemonize+0x54>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1200 <daemonize+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a0 <daemonize+0x210>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
sub    $0xffffffff,%eax
je     122a <daemonize+0x9a>
jmp    121a <daemonize+0x8a>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     1236 <daemonize+0xa6>
jmp    123b <daemonize+0xab>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a0 <daemonize+0x210>
jmp    1249 <daemonize+0xb9>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x8(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
jne    125f <daemonize+0xcf>
xor    %edi,%edi
call   1080 <umask@plt>
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    127a <daemonize+0xea>
lea    0xd8b(%rip),%rdi
call   1040 <chdir@plt>
mov    -0x8(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
jne    12d9 <daemonize+0x149>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12ab <daemonize+0x11b>
movl   $0x2000,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12d4 <daemonize+0x144>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    12b2 <daemonize+0x122>
jmp    12d9 <daemonize+0x149>
mov    -0x8(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
jne    1399 <daemonize+0x209>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2ce3(%rip),%rax
mov    (%rax),%esi
lea    0xcfc(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2ca0(%rip),%rcx
cmp    (%rcx),%eax
je     132c <daemonize+0x19c>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a0 <daemonize+0x210>
mov    0x2c85(%rip),%rax
mov    (%rax),%edi
mov    0x2cb4(%rip),%rax
mov    (%rax),%rsi
call   1050 <dup2@plt>
mov    0x2ca5(%rip),%rcx
cmp    (%rcx),%rax
je     1360 <daemonize+0x1d0>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a0 <daemonize+0x210>
mov    0x2c51(%rip),%rax
mov    (%rax),%edi
mov    0x2c58(%rip),%rax
mov    (%rax),%rsi
call   1050 <dup2@plt>
mov    0x2c49(%rip),%rcx
cmp    (%rcx),%rax
je     1394 <daemonize+0x204>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a0 <daemonize+0x210>
jmp    1399 <daemonize+0x209>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     12aa <daemonize+0x11a>
test   %eax,%eax
jne    12af <daemonize+0x11f>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     12a5 <daemonize+0x115>
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     12aa <daemonize+0x11a>
test   %eax,%eax
jne    12af <daemonize+0x11f>
test   $0x8,%r14b
jne    11f2 <daemonize+0x62>
xor    %edi,%edi
call   1080 <umask@plt>
test   $0x1,%r14b
je     11f8 <daemonize+0x68>
test   $0x2,%r14b
je     120a <daemonize+0x7a>
jmp    123e <daemonize+0xae>
test   $0x1,%r14b
jne    11ea <daemonize+0x5a>
lea    0xe01(%rip),%rdi
call   1040 <chdir@plt>
test   $0x2,%r14b
jne    123e <daemonize+0xae>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
cmp    $0xffffffff,%eax
mov    $0x2000,%ebx
cmovne %eax,%ebx
test   %ebx,%ebx
jle    123e <daemonize+0xae>
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1230 <daemonize+0xa0>
test   $0x4,%r14b
jne    12a1 <daemonize+0x111>
mov    0x2d6d(%rip),%rbx
mov    (%rbx),%edi
call   1060 <close@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    0xda0(%rip),%rdi
call   1090 <open@plt>
cmp    (%rbx),%eax
jne    12a5 <daemonize+0x115>
mov    0x2d7e(%rip),%rbp
mov    0x0(%rbp),%rsi
mov    %eax,%edi
call   1050 <dup2@plt>
cmp    0x0(%rbp),%rax
jne    12a5 <daemonize+0x115>
mov    (%rbx),%edi
mov    0x2d3c(%rip),%rbx
mov    (%rbx),%rsi
call   1050 <dup2@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmp    (%rbx),%rcx
jne    12aa <daemonize+0x11a>
xor    %eax,%eax
jmp    12aa <daemonize+0x11a>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

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

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