#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*/ O_RDWR;
int /*<<< orphan*/ RLIMIT_NOFILE;
int RLIM_INFINITY;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemon_init(void) {
  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 (int i = 0; i < rl.rlim_max; i++)
    close(i);
  int fd0 = open("/dev/null", O_RDWR);
  int fd1 = dup(0);
  int fd2 = dup(0);
}


x86 assembly (clang, -O0):
<daemon_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11bf <daemon_init+0x2f>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11d1 <daemon_init+0x41>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    11d6 <daemon_init+0x46>
mov    $0x0,%al
call   1060 <setsid@plt>
lea    0xe1c(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   10a0 <getrlimit@plt>
mov    -0x10(%rbp),%eax
mov    0x2dcb(%rip),%rcx
cmp    (%rcx),%eax
jne    1214 <daemon_init+0x84>
movl   $0x400,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    123d <daemon_init+0xad>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    121b <daemon_init+0x8b>
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    0xdb5(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x1c(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x20(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_init>:
push   %rbx
sub    $0x10,%rsp
xor    %edi,%edi
call   1070 <umask@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1239 <daemon_init+0xa9>
jne    1243 <daemon_init+0xb3>
xor    %eax,%eax
call   1060 <setsid@plt>
lea    0xe40(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   10a0 <getrlimit@plt>
mov    0x8(%rsp),%eax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%eax
jne    11f1 <daemon_init+0x61>
movl   $0x400,0x8(%rsp)
jmp    11f5 <daemon_init+0x65>
test   %eax,%eax
jle    1210 <daemon_init+0x80>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    0x8(%rsp),%ebx
jl     1200 <daemon_init+0x70>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    0xde2(%rip),%rdi
call   1080 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <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
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @daemon_init() {
%1 = alloca i64
%2 = alloca %struct.rlimit
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = call i32 @umask(i32 0)
%8 = call i64 (...) @fork()
store i64 %8, i64* %1
%9 = load i64, i64* %1
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i64, i64* %1
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %13
br label %19
19: ; preds = %18
%20 = call i32 (...) @setsid()
%21 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @RLIMIT_NOFILE
%23 = call i32 @getrlimit(i32 %22, %struct.rlimit* %2)
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @RLIM_INFINITY
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %19
%29 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
store i32 1024, i32* %29
br label %30
30: ; preds = %28, %19
store i32 0, i32* %3
br label %31
31: ; preds = %39, %30
%32 = load i32, i32* %3
%33 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%34 = load i32, i32* %33
%35 = icmp slt i32 %32, %34
br i1 %35, label %36, label %42
36: ; preds = %31
%37 = load i32, i32* %3
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %36
%40 = load i32, i32* %3
%41 = add nsw i32 %40, 1
store i32 %41, i32* %3
br label %31
42: ; preds = %31
%43 = load i32, i32* @O_RDWR
%44 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %43)
store i32 %44, i32* %4
%45 = call i32 @dup(i32 0)
store i32 %45, i32* %5
%46 = call i32 @dup(i32 0)
store i32 %46, i32* %6
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
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = 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 %25
24: ; preds = %21, %22
br label %30
25: ; preds = %30, %22
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = call i32 @dup(i32 0)
%29 = call i32 @dup(i32 0)
ret void
30: ; preds = %24, %30
%31 = phi i32 [ %33, %30 ], [ 0, %24 ]
%32 = call i32 @close(i32 %31)
%33 = add nuw nsw i32 %31, 1
%34 = load i32, i32* %17
%35 = icmp slt i32 %33, %34
br i1 %35, label %30, label %25
}