#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*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void init_daemon() {
  int pid;
  int i;
  int max_fd = 64;
  if ((pid = fork()) != 0)
    exit(0);
  setsid();
  signal(SIGHUP, SIG_IGN);
  if ((pid = fork()) != 0)
    exit(0);
  for (i = 3; i < max_fd; i++)
    close(i);
  printf("%d\n", i);
  chdir("/");
  umask(0);
}


x86 assembly (clang, -O0):
<init_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x40,-0xc(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
je     11a9 <init_daemon+0x29>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
je     11e1 <init_daemon+0x61>
xor    %edi,%edi
call   1090 <exit@plt>
movl   $0x3,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    120a <init_daemon+0x8a>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11e8 <init_daemon+0x68>
mov    -0x8(%rbp),%esi
lea    0xdec(%rip),%rdi
call   1040 <printf@plt>
lea    0xde4(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_daemon>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    11f4 <init_daemon+0x74>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    11f4 <init_daemon+0x74>
mov    $0x3,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    $0x40,%ebx
jne    11c0 <init_daemon+0x40>
lea    0xe2a(%rip),%rdi
mov    $0x40,%esi
call   1040 <printf@plt>
lea    0xe1d(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
pop    %rbx
jmp    1080 <umask@plt>
xor    %edi,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [2 x i8] c"/\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 = load i32, i32* @SIGHUP
%8 = load i32, i32* @SIG_IGN
%9 = tail call i32 @signal(i32 %7, i32 %8)
%10 = tail call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %5
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %5, %14
%15 = phi i32 [ %17, %14 ], [ 3, %5 ]
%16 = tail call i32 @close(i32 %15)
%17 = add nuw nsw i32 %15, 1
%18 = icmp eq i32 %17, 64
br i1 %18, label %19, label %14
19: ; preds = %14
%20 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 64)
%21 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @umask(i32 0)
ret void
}