#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 */
struct rlimit {
  int rlim_max;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_NOFILE;
int RLIM_INFINITY;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemon_init(void) {
  int i = 0;
  umask(0);
  pid_t pid = fork();
  if (pid < 0)
    exit(1);
  else if (pid > 0)
    exit(0);
  setsid();
  chdir("/");
  struct rlimit rl;
  getrlimit(RLIMIT_NOFILE, &rl);
  if (rl.rlim_max == RLIM_INFINITY)
    rl.rlim_max = 1024;
  for (i = 0; i < rl.rlim_max; i++)
    close(i);
  close(0);
  close(1);
  close(2);
}


x86 assembly (clang, -O0):
<daemon_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
call   1060 <umask@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11a6 <daemon_init+0x36>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    11b8 <daemon_init+0x48>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    11bd <daemon_init+0x4d>
mov    $0x0,%al
call   1050 <setsid@plt>
lea    0xe35(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1080 <getrlimit@plt>
mov    -0x18(%rbp),%eax
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%eax
jne    11fb <daemon_init+0x8b>
movl   $0x400,-0x18(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1224 <daemon_init+0xb4>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1202 <daemon_init+0x92>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_init>:
push   %rbx
sub    $0x10,%rsp
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1211 <daemon_init+0xa1>
jne    121b <daemon_init+0xab>
xor    %eax,%eax
call   1050 <setsid@plt>
lea    0xe60(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1080 <getrlimit@plt>
mov    0x8(%rsp),%eax
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%eax
jne    11d1 <daemon_init+0x61>
movl   $0x400,0x8(%rsp)
jmp    11d5 <daemon_init+0x65>
test   %eax,%eax
jle    11f0 <daemon_init+0x80>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x1,%ebx
cmp    0x8(%rsp),%ebx
jl     11e0 <daemon_init+0x70>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>

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

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