#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*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int getdtablesize();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void init_daemon() {
  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 < getdtablesize(); 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     1192 <init_daemon+0x22>
xor    %edi,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jge    11a6 <init_daemon+0x36>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    11ab <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     11cc <init_daemon+0x5c>
xor    %edi,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jge    11e0 <init_daemon+0x70>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    11e5 <init_daemon+0x75>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1090 <getdtablesize@plt>
mov    %eax,%ecx
mov    -0xc(%rbp),%eax
cmp    %ecx,%eax
jge    121c <init_daemon+0xac>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11ec <init_daemon+0x7c>
lea    0xddd(%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   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11c9 <init_daemon+0x59>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11c9 <init_daemon+0x59>
xor    %eax,%eax
call   1090 <getdtablesize@plt>
test   %eax,%eax
jle    11b5 <init_daemon+0x45>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x1,%ebx
xor    %eax,%eax
call   1090 <getdtablesize@plt>
cmp    %eax,%ebx
jl     11a0 <init_daemon+0x30>
lea    0xe44(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
pop    %rbx
jmp    1060 <umask@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.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 = call i32 (...) @getdtablesize()
%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):
@.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 = tail call i32 (...) @getdtablesize()
%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 = tail call i32 (...) @getdtablesize()
%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
}