#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*/ O_RDWR;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGTSTP;
int /*<<< orphan*/ SIGTTIN;
int /*<<< orphan*/ SIGTTOU;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ exit(int);
int fork();
int getdtablesize();
int getppid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int);
void daemonize_init() {
  int i;
  if (getppid() == 1)
    return;
  i = fork();
  if (i < 0)
    exit(1);
  if (i > 0)
    exit(0);
  setsid();
  for (i = getdtablesize(); i >= 0; --i)
    close(i);
  i = open("/dev/null", O_RDWR);
  dup(i);
  dup(i);
  umask(027);
  signal(SIGCHLD, SIG_IGN);
  signal(SIGTSTP, SIG_IGN);
  signal(SIGTTOU, SIG_IGN);
  signal(SIGTTIN, SIG_IGN);
}


x86 assembly (clang, -O0):
<daemonize_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <getppid@plt>
cmp    $0x1,%eax
jne    11bd <daemonize_init+0x1d>
jmp    12ab <daemonize_init+0x10b>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11db <daemonize_init+0x3b>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    11ec <daemonize_init+0x4c>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    $0x0,%al
call   10c0 <getdtablesize@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     121d <daemonize_init+0x7d>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
jmp    11fd <daemonize_init+0x5d>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
lea    0xdd3(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <dup@plt>
mov    -0x4(%rbp),%edi
call   1030 <dup@plt>
mov    $0x17,%edi
call   1070 <umask@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize_init>:
push   %rbx
xor    %eax,%eax
call   1090 <getppid@plt>
cmp    $0x1,%eax
jne    11af <daemonize_init+0xf>
pop    %rbx
ret
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1263 <daemonize_init+0xc3>
jne    126d <daemonize_init+0xcd>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %eax,%eax
call   10c0 <getdtablesize@plt>
test   %eax,%eax
js     11ec <daemonize_init+0x4c>
mov    %eax,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1040 <close@plt>
add    $0xffffffff,%ebx
jb     11e0 <daemonize_init+0x40>
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
lea    0xe04(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <dup@plt>
mov    %ebx,%edi
call   1030 <dup@plt>
mov    $0x17,%edi
call   1070 <umask@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    0x2d95(%rip),%rbx
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
pop    %rbx
jmp    1060 <signal@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

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

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