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


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
je     11a0 <init_daemon+0x30>
lea    0xe6c(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jge    11b5 <init_daemon+0x45>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11ba <init_daemon+0x4a>
mov    $0x0,%al
call   1060 <setsid@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x103c8,-0xc(%rbp)
jge    11eb <init_daemon+0x7b>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c8 <init_daemon+0x58>
lea    0xe16(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1070 <umask@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_daemon>:
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    11b6 <init_daemon+0x46>
xor    %eax,%eax
call   1060 <setsid@plt>
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    $0x103c8,%ebx
jne    1190 <init_daemon+0x20>
lea    0xe57(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
pop    %rbx
jmp    1070 <umask@plt>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"exit 1\0A\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 ne i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 0)
unreachable
8: ; preds = %0
%9 = load i64, i64* %1
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %8
br label %14
14: ; preds = %13
%15 = call i32 (...) @setsid()
store i32 0, i32* %2
br label %16
16: ; preds = %22, %14
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 66504
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
br label %22
22: ; preds = %19
%23 = load i32, i32* %2
%24 = add nsw i32 %23, 1
store i32 %24, i32* %2
br label %16
25: ; preds = %16
%26 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @umask(i32 0)
ret void
}

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