#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 NOFILE;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void init_daemon(void) {
  int pid;
  int i;
  if (pid = fork())
    exit(0);
  else if (pid < 0)
    exit(1);
  setsid();
  if (pid = fork())
    exit(0);
  else if (pid < 0)
    exit(1);
  for (i = 0; i < NOFILE; ++i)
    close(i);
  chdir("/tmp");
  umask(0);
  return;
}


x86 assembly (clang, -O0):
<init_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
je     1182 <init_daemon+0x22>
xor    %edi,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jge    1196 <init_daemon+0x36>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    119b <init_daemon+0x3b>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
je     11bc <init_daemon+0x5c>
xor    %edi,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jge    11d0 <init_daemon+0x70>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11d5 <init_daemon+0x75>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%eax
jge    1204 <init_daemon+0xa4>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11dc <init_daemon+0x7c>
lea    0xdf5(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_daemon>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11b9 <init_daemon+0x59>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11b9 <init_daemon+0x59>
mov    0x2e50(%rip),%r14
cmpl   $0x0,(%r14)
jle    119f <init_daemon+0x3f>
xor    %ebx,%ebx
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x1,%ebx
cmp    (%r14),%ebx
jl     1190 <init_daemon+0x30>
lea    0xe5a(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <umask@plt>
xor    %edi,%edi
call   1070 <exit@plt>

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

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