#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*/ 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(0);
  }
  if (setsid() == -1) {
    return -1;
  }
  switch ((fork())) {
  case -1:
    return -1;
  case 0:
    break;
  default:
    _exit(0);
  }
  if (!(flags & 0x4)) {
    umask(0);
  }
  if (!(flags & 0x2)) {
    chdir("/");
  }
  if (!(flags & 0x1)) {
    maxfd = sysconf(_SC_OPEN_MAX);
    if (maxfd == -1) {
      maxfd = 85896;
    }
    for (int i = 0; i < maxfd; i++) {
      close(i);
    }
    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,-0x18(%rbp)
sub    $0xffffffff,%eax
je     11c5 <daemonize+0x35>
jmp    11b5 <daemonize+0x25>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     11d1 <daemonize+0x41>
jmp    11d6 <daemonize+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    137e <daemonize+0x1ee>
jmp    11dd <daemonize+0x4d>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11f9 <daemonize+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    137e <daemonize+0x1ee>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     1223 <daemonize+0x93>
jmp    1213 <daemonize+0x83>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     122f <daemonize+0x9f>
jmp    1234 <daemonize+0xa4>
movl   $0xffffffff,-0x4(%rbp)
jmp    137e <daemonize+0x1ee>
jmp    123b <daemonize+0xab>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    -0x8(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
jne    1251 <daemonize+0xc1>
xor    %edi,%edi
call   1080 <umask@plt>
mov    -0x8(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
jne    126c <daemonize+0xdc>
lea    0xd99(%rip),%rdi
call   1040 <chdir@plt>
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    1377 <daemonize+0x1e7>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    129d <daemonize+0x10d>
movl   $0x14f88,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12c6 <daemonize+0x136>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12a4 <daemonize+0x114>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
lea    0xd1e(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2cca(%rip),%rcx
cmp    (%rcx),%eax
je     130a <daemonize+0x17a>
movl   $0xffffffff,-0x4(%rbp)
jmp    137e <daemonize+0x1ee>
mov    0x2caf(%rip),%rax
mov    (%rax),%edi
mov    0x2cd6(%rip),%rax
mov    (%rax),%rsi
call   1050 <dup2@plt>
mov    0x2cc7(%rip),%rcx
cmp    (%rcx),%rax
je     133e <daemonize+0x1ae>
movl   $0xffffffff,-0x4(%rbp)
jmp    137e <daemonize+0x1ee>
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
mov    0x2c7a(%rip),%rax
mov    (%rax),%rsi
call   1050 <dup2@plt>
mov    0x2c6b(%rip),%rcx
cmp    (%rcx),%rax
je     1372 <daemonize+0x1e2>
movl   $0xffffffff,-0x4(%rbp)
jmp    137e <daemonize+0x1ee>
jmp    1377 <daemonize+0x1e7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     12a4 <daemonize+0x114>
test   %eax,%eax
jne    12ab <daemonize+0x11b>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     129f <daemonize+0x10f>
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     12a4 <daemonize+0x114>
test   %eax,%eax
jne    12ab <daemonize+0x11b>
test   $0x4,%bl
jne    11f0 <daemonize+0x60>
xor    %edi,%edi
call   1080 <umask@plt>
test   $0x2,%bl
je     11f5 <daemonize+0x65>
test   $0x1,%bl
je     120a <daemonize+0x7a>
jmp    129b <daemonize+0x10b>
test   $0x2,%bl
jne    11e6 <daemonize+0x56>
lea    0xe04(%rip),%rdi
call   1040 <chdir@plt>
test   $0x1,%bl
jne    129b <daemonize+0x10b>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
cmp    $0xffffffff,%eax
mov    $0x14f88,%ebp
cmovne %eax,%ebp
test   %ebp,%ebp
jle    123e <daemonize+0xae>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1230 <daemonize+0xa0>
mov    0x2d7b(%rip),%rbx
mov    (%rbx),%edi
call   1060 <close@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
lea    0xda6(%rip),%rdi
call   1090 <open@plt>
cmp    (%rbx),%eax
jne    129f <daemonize+0x10f>
mov    0x2d84(%rip),%rbp
mov    0x0(%rbp),%rsi
mov    %eax,%edi
call   1050 <dup2@plt>
cmp    0x0(%rbp),%rax
jne    129f <daemonize+0x10f>
mov    (%rbx),%edi
mov    0x2d42(%rip),%rbx
mov    (%rbx),%rsi
call   1050 <dup2@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmp    (%rbx),%rcx
jne    12a4 <daemonize+0x114>
xor    %eax,%eax
jmp    12a4 <daemonize+0x114>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   1030 <_exit@plt>

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

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