#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*/ _SC_OPEN_MAX;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int setsid();
int sysconf(int /*<<< orphan*/);
int deamonize(void) {
  int fd, maxfd;
  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);
  }
  maxfd = sysconf(_SC_OPEN_MAX);
  if (maxfd == -1)
    maxfd = 1024;
  for (fd = 0; fd < maxfd; fd++)
    close(fd);
  open("/dev/null", O_RDWR);
  dup(0);
  dup(0);
  return 0;
}


x86 assembly (clang, -O0):
<deamonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11a2 <deamonize+0x32>
jmp    1192 <deamonize+0x22>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11ae <deamonize+0x3e>
jmp    11b3 <deamonize+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    129b <deamonize+0x12b>
jmp    11c1 <deamonize+0x51>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11dd <deamonize+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    129b <deamonize+0x12b>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     1207 <deamonize+0x97>
jmp    11f7 <deamonize+0x87>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     1213 <deamonize+0xa3>
jmp    1218 <deamonize+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    129b <deamonize+0x12b>
jmp    1226 <deamonize+0xb6>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1248 <deamonize+0xd8>
movl   $0x400,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1271 <deamonize+0x101>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    124f <deamonize+0xdf>
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
lea    0xd7f(%rip),%rdi
call   1070 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<deamonize>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     120a <deamonize+0x9a>
test   %ebx,%ebx
jne    1213 <deamonize+0xa3>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     1205 <deamonize+0x95>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     120a <deamonize+0x9a>
test   %ebx,%ebx
jne    1213 <deamonize+0xa3>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
cmp    $0xffffffff,%eax
mov    $0x400,%ebp
cmovne %eax,%ebp
test   %ebp,%ebp
jle    11de <deamonize+0x6e>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    11d0 <deamonize+0x60>
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
lea    0xe12(%rip),%rdi
call   1070 <open@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
jmp    120a <deamonize+0x9a>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@_SC_OPEN_MAX = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @deamonize() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
switch i32 %4, label %7 [
i32 -1, label %5
i32 0, label %6
]
5: ; preds = %0
store i32 -1, i32* %1
br label %43
6: ; preds = %0
br label %10
7: ; preds = %0
%8 = load i32, i32* @EXIT_SUCCESS
%9 = call i32 @_exit(i32 %8)
unreachable
10: ; preds = %6
%11 = call i32 (...) @setsid()
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %10
store i32 -1, i32* %1
br label %43
14: ; preds = %10
%15 = call i32 (...) @fork()
switch i32 %15, label %18 [
i32 -1, label %16
i32 0, label %17
]
16: ; preds = %14
store i32 -1, i32* %1
br label %43
17: ; preds = %14
br label %21
18: ; preds = %14
%19 = load i32, i32* @EXIT_SUCCESS
%20 = call i32 @_exit(i32 %19)
unreachable
21: ; preds = %17
%22 = load i32, i32* @_SC_OPEN_MAX
%23 = call i32 @sysconf(i32 %22)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 1024, i32* %3
br label %27
27: ; preds = %26, %21
store i32 0, i32* %2
br label %28
28: ; preds = %35, %27
%29 = load i32, i32* %2
%30 = load i32, i32* %3
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %38
32: ; preds = %28
%33 = load i32, i32* %2
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %32
%36 = load i32, i32* %2
%37 = add nsw i32 %36, 1
store i32 %37, i32* %2
br label %28
38: ; preds = %28
%39 = load i32, i32* @O_RDWR
%40 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = call i32 @dup(i32 0)
%42 = call i32 @dup(i32 0)
store i32 0, i32* %1
br label %43
43: ; preds = %38, %16, %13, %5
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@_SC_OPEN_MAX = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @deamonize() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %2 [
i32 -1, label %29
i32 0, label %5
]
2: ; preds = %0
%3 = load i32, i32* @EXIT_SUCCESS
%4 = tail call i32 @_exit(i32 %3)
unreachable
5: ; preds = %0
%6 = tail call i32 (...) @setsid()
%7 = icmp eq i32 %6, -1
br i1 %7, label %29, label %8
8: ; preds = %5
%9 = tail call i32 (...) @fork()
switch i32 %9, label %10 [
i32 -1, label %29
i32 0, label %13
]
10: ; preds = %8
%11 = load i32, i32* @EXIT_SUCCESS
%12 = tail call i32 @_exit(i32 %11)
unreachable
13: ; preds = %8
%14 = load i32, i32* @_SC_OPEN_MAX
%15 = tail call i32 @sysconf(i32 %14)
%16 = icmp eq i32 %15, -1
%17 = select i1 %16, i32 1024, i32 %15
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %13, %19
%20 = phi i32 [ %22, %19 ], [ 0, %13 ]
%21 = tail call i32 @close(i32 %20)
%22 = add nuw nsw i32 %20, 1
%23 = icmp eq i32 %22, %17
br i1 %23, label %24, label %19
24: ; preds = %19, %13
%25 = load i32, i32* @O_RDWR
%26 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = tail call i32 @dup(i32 0)
%28 = tail call i32 @dup(i32 0)
br label %29
29: ; preds = %8, %5, %0, %24
%30 = phi i32 [ 0, %24 ], [ %1, %0 ], [ -1, %5 ], [ %9, %8 ]
ret i32 %30
}