#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getdtablesize();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void init_daemon() {
  pid_t pid;
  int i;
  if ((pid = fork()) < 0) {
    perror("error fork");
    exit(1);
  } else if (pid > 0)
    exit(0);
  else {
  }
  setsid();
  if ((pid = fork()) < 0) {
    perror("error fork");
    exit(1);
  } else if (pid > 0)
    exit(0);
  else {
  }
  chdir("/");
  umask(0);
  for (i = 0; i < getdtablesize(); i++)
    close(i);
}


x86 assembly (clang, -O0):
<init_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jge    11b3 <init_daemon+0x33>
lea    0xe5c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11c5 <init_daemon+0x45>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    11ca <init_daemon+0x4a>
jmp    11cf <init_daemon+0x4f>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jge    1201 <init_daemon+0x81>
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    1213 <init_daemon+0x93>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1218 <init_daemon+0x98>
jmp    121d <init_daemon+0x9d>
lea    0xde7(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   10a0 <getdtablesize@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
cmp    %ecx,%eax
jge    1267 <init_daemon+0xe7>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1237 <init_daemon+0xb7>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_daemon>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11e7 <init_daemon+0x67>
jne    11fd <init_daemon+0x7d>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11e7 <init_daemon+0x67>
jne    11fd <init_daemon+0x7d>
lea    0xe60(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
call   10a0 <getdtablesize@plt>
test   %eax,%eax
jle    11e5 <init_daemon+0x65>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x1,%ebx
xor    %eax,%eax
call   10a0 <getdtablesize@plt>
cmp    %eax,%ebx
jl     11d0 <init_daemon+0x50>
pop    %rbx
ret
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"error fork\00"
@.str.1 = [2 x i8] c"/\00"
define void @init_daemon() {
%1 = alloca i64
%2 = alloca i32
%3 = call i64 (...) @fork()
store i64 %3, i64* %1
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i64, i64* %1
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %8
br label %14
14: ; preds = %13
br label %15
15: ; preds = %14
%16 = call i32 (...) @setsid()
%17 = call i64 (...) @fork()
store i64 %17, i64* %1
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = load i64, i64* %1
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %22
br label %28
28: ; preds = %27
br label %29
29: ; preds = %28
%30 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @umask(i32 0)
store i32 0, i32* %2
br label %32
32: ; preds = %39, %29
%33 = load i32, i32* %2
%34 = call i32 (...) @getdtablesize()
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i32, i32* %2
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %36
%40 = load i32, i32* %2
%41 = add nsw i32 %40, 1
store i32 %41, i32* %2
br label %32
42: ; preds = %32
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"error fork\00"
@.str.1 = [2 x i8] c"/\00"
define void @init_daemon() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %6
%11 = tail call i32 (...) @setsid()
%12 = tail call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = icmp eq i64 %12, 0
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = tail call i32 @exit(i32 0)
unreachable
21: ; preds = %17
%22 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @umask(i32 0)
%24 = tail call i32 (...) @getdtablesize()
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %21, %26
%27 = phi i32 [ %29, %26 ], [ 0, %21 ]
%28 = tail call i32 @close(i32 %27)
%29 = add nuw nsw i32 %27, 1
%30 = tail call i32 (...) @getdtablesize()
%31 = icmp slt i32 %29, %30
br i1 %31, label %26, label %32
32: ; preds = %26, %21
ret void
}