#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 /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ assert(int);
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
scalar_t__ fork_a_child();
int max_fd();
int maybe_dup2(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(fd_set *leave_open, int flags) {
  assert(flags == 0);
  if (fork_a_child() != 0) {
    return -1;
  }
  if (setsid() == -1) {
    return -1;
  }
  if (fork_a_child() != 0) {
    return -1;
  }
  umask(0);
  if (chdir("/") != 0) {
    return -1;
  }
  for (int fd = 0; fd < max_fd(); fd++) {
    if (leave_open && FD_ISSET(fd, leave_open)) {
      continue;
    }
    close(fd);
  }
  int null_fd = open("/dev/null", O_RDWR);
  if ((null_fd == -1) ||
      (maybe_dup2(null_fd, STDIN_FILENO, leave_open) == -1) ||
      (maybe_dup2(null_fd, STDOUT_FILENO, leave_open) == -1) ||
      (maybe_dup2(null_fd, STDERR_FILENO, leave_open) == -1)) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    $0x0,%al
call   10c0 <fork_a_child@plt>
cmp    $0x0,%rax
je     11dd <daemonize+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <daemonize+0x18b>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11f9 <daemonize+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <daemonize+0x18b>
mov    $0x0,%al
call   10c0 <fork_a_child@plt>
cmp    $0x0,%rax
je     1216 <daemonize+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <daemonize+0x18b>
xor    %edi,%edi
call   1090 <umask@plt>
lea    0xddc(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     123f <daemonize+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <daemonize+0x18b>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <max_fd@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jge    129c <daemonize+0xfc>
cmpq   $0x0,-0x10(%rbp)
je     1286 <daemonize+0xe6>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1286 <daemonize+0xe6>
jmp    128e <daemonize+0xee>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1246 <daemonize+0xa6>
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
lea    0xd56(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     1318 <daemonize+0x178>
mov    -0x1c(%rbp),%edi
mov    0x2d00(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   10a0 <maybe_dup2@plt>
cmp    $0xffffffff,%eax
je     1318 <daemonize+0x178>
mov    -0x1c(%rbp),%edi
mov    0x2d0a(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   10a0 <maybe_dup2@plt>
cmp    $0xffffffff,%eax
je     1318 <daemonize+0x178>
mov    -0x1c(%rbp),%edi
mov    0x2ccc(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   10a0 <maybe_dup2@plt>
cmp    $0xffffffff,%eax
jne    1324 <daemonize+0x184>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <daemonize+0x18b>
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    %rdi,%rbx
xor    %edi,%edi
test   %esi,%esi
sete   %dil
call   1080 <assert@plt>
xor    %eax,%eax
call   10c0 <fork_a_child@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11ce <daemonize+0x2e>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     11c6 <daemonize+0x26>
xor    %eax,%eax
call   10c0 <fork_a_child@plt>
test   %rax,%rax
jne    11c6 <daemonize+0x26>
xor    %edi,%edi
call   1090 <umask@plt>
lea    0xe0c(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jne    11c6 <daemonize+0x26>
xor    %eax,%eax
call   1030 <max_fd@plt>
test   %eax,%eax
jle    123b <daemonize+0x9b>
xor    %ebp,%ebp
jmp    1225 <daemonize+0x85>
nopl   (%rax)
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x1,%ebp
xor    %eax,%eax
call   1030 <max_fd@plt>
cmp    %eax,%ebp
jge    123b <daemonize+0x9b>
test   %rbx,%rbx
je     1210 <daemonize+0x70>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    1217 <daemonize+0x77>
jmp    1210 <daemonize+0x70>
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
lea    0xdb7(%rip),%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     11c6 <daemonize+0x26>
mov    %eax,%ebp
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   10a0 <maybe_dup2@plt>
cmp    $0xffffffff,%eax
je     11c6 <daemonize+0x26>
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   10a0 <maybe_dup2@plt>
cmp    $0xffffffff,%eax
je     11c6 <daemonize+0x26>
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   10a0 <maybe_dup2@plt>
xor    %r14d,%r14d
cmp    $0xffffffff,%eax
sete   %r14b
neg    %r14d
jmp    11c6 <daemonize+0x26>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @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 = load i32, i32* %5
%9 = icmp eq i32 %8, 0
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = call i64 (...) @fork_a_child()
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %73
15: ; preds = %2
%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 %73
19: ; preds = %15
%20 = call i64 (...) @fork_a_child()
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 -1, i32* %3
br label %73
23: ; preds = %19
%24 = call i32 @umask(i32 0)
%25 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 -1, i32* %3
br label %73
28: ; preds = %23
store i32 0, i32* %6
br label %29
29: ; preds = %45, %28
%30 = load i32, i32* %6
%31 = call i32 (...) @max_fd()
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %48
33: ; preds = %29
%34 = load i32*, i32** %4
%35 = icmp ne i32* %34, null
br i1 %35, label %36, label %42
36: ; preds = %33
%37 = load i32, i32* %6
%38 = load i32*, i32** %4
%39 = call i64 @FD_ISSET(i32 %37, i32* %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %36
br label %45
42: ; preds = %36, %33
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %42, %41
%46 = load i32, i32* %6
%47 = add nsw i32 %46, 1
store i32 %47, i32* %6
br label %29
48: ; preds = %29
%49 = load i32, i32* @O_RDWR
%50 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %49)
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, -1
br i1 %52, label %71, label %53
53: ; preds = %48
%54 = load i32, i32* %7
%55 = load i32, i32* @STDIN_FILENO
%56 = load i32*, i32** %4
%57 = call i32 @maybe_dup2(i32 %54, i32 %55, i32* %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %71, label %59
59: ; preds = %53
%60 = load i32, i32* %7
%61 = load i32, i32* @STDOUT_FILENO
%62 = load i32*, i32** %4
%63 = call i32 @maybe_dup2(i32 %60, i32 %61, i32* %62)
%64 = icmp eq i32 %63, -1
br i1 %64, label %71, label %65
65: ; preds = %59
%66 = load i32, i32* %7
%67 = load i32, i32* @STDERR_FILENO
%68 = load i32*, i32** %4
%69 = call i32 @maybe_dup2(i32 %66, i32 %67, i32* %68)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %72
71: ; preds = %65, %59, %53, %48
store i32 -1, i32* %3
br label %73
72: ; preds = %65
store i32 0, i32* %3
br label %73
73: ; preds = %72, %71, %27, %22, %18, %14
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonize(i32* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = tail call i64 (...) @fork_a_child()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %51
8: ; preds = %2
%9 = tail call i32 (...) @setsid()
%10 = icmp eq i32 %9, -1
br i1 %10, label %51, label %11
11: ; preds = %8
%12 = tail call i64 (...) @fork_a_child()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %51
14: ; preds = %11
%15 = tail call i32 @umask(i32 0)
%16 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %51
18: ; preds = %14
%19 = tail call i32 (...) @max_fd()
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = icmp eq i32* %0, null
br label %27
23: ; preds = %34, %18
%24 = load i32, i32* @O_RDWR
%25 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %51, label %38
27: ; preds = %21, %34
%28 = phi i32 [ 0, %21 ], [ %35, %34 ]
br i1 %22, label %32, label %29
29: ; preds = %27
%30 = tail call i64 @FD_ISSET(i32 %28, i32* %0)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29, %27
%33 = tail call i32 @close(i32 %28)
br label %34
34: ; preds = %29, %32
%35 = add nuw nsw i32 %28, 1
%36 = tail call i32 (...) @max_fd()
%37 = icmp slt i32 %35, %36
br i1 %37, label %27, label %23
38: ; preds = %23
%39 = load i32, i32* @STDIN_FILENO
%40 = tail call i32 @maybe_dup2(i32 %25, i32 %39, i32* %0)
%41 = icmp eq i32 %40, -1
br i1 %41, label %51, label %42
42: ; preds = %38
%43 = load i32, i32* @STDOUT_FILENO
%44 = tail call i32 @maybe_dup2(i32 %25, i32 %43, i32* %0)
%45 = icmp eq i32 %44, -1
br i1 %45, label %51, label %46
46: ; preds = %42
%47 = load i32, i32* @STDERR_FILENO
%48 = tail call i32 @maybe_dup2(i32 %25, i32 %47, i32* %0)
%49 = icmp eq i32 %48, -1
%50 = sext i1 %49 to i32
br label %51
51: ; preds = %46, %42, %38, %23, %14, %11, %8, %2
%52 = phi i32 [ -1, %2 ], [ -1, %8 ], [ -1, %11 ], [ -1, %14 ], [ -1, %42 ], [ -1, %38 ], [ -1, %23 ], [ %50, %46 ]
ret i32 %52
}