#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 sigaction {
  scalar_t__ sa_flags;
  int /*<<< orphan*/ sa_handler;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ sigaction(int /*<<< orphan*/, struct sigaction *,
                             int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemon_init() {
  struct sigaction act;
  int i, maxfd;
  if (fork() != 0)
    exit(0);
  if (setsid() < 0)
    return (-1);
  act.sa_handler = SIG_IGN;
  act.sa_flags = 0;
  sigaction(SIGHUP, &act, 0);
  if (fork() != 0)
    exit(0);
  chdir("/");
  umask(0);
  maxfd = sysconf(_SC_OPEN_MAX);
  for (i = 0; i < maxfd; i++)
    close(i);
  open("/dev/null", O_RDWR);
  dup(0);
  dup(1);
  dup(2);
  return (0);
}


x86 assembly (clang, -O0):
<daemon_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%rax
je     11c0 <daemon_init+0x20>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    11dd <daemon_init+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <daemon_init+0x101>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%rax
je     121d <daemon_init+0x7d>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xddc(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    126a <daemon_init+0xca>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1248 <daemon_init+0xa8>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
lea    0xd88(%rip),%rdi
call   1090 <open@plt>
xor    %edi,%edi
call   1050 <dup@plt>
mov    $0x1,%edi
call   1050 <dup@plt>
mov    $0x2,%edi
call   1050 <dup@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_init>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
jne    1282 <daemon_init+0xe2>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     1270 <daemon_init+0xd0>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movq   $0x0,(%rsp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
jne    1282 <daemon_init+0xe2>
lea    0xdf9(%rip),%rdi
call   1040 <chdir@plt>
xor    %r14d,%r14d
xor    %edi,%edi
call   1080 <umask@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
test   %eax,%eax
jle    123e <daemon_init+0x9e>
mov    %eax,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1230 <daemon_init+0x90>
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
lea    0xdb4(%rip),%rdi
call   1090 <open@plt>
xor    %edi,%edi
call   1050 <dup@plt>
mov    $0x1,%edi
call   1050 <dup@plt>
mov    $0x2,%edi
call   1050 <dup@plt>
jmp    1276 <daemon_init+0xd6>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   10b0 <exit@plt>

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

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