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


x86 assembly (clang, -O0):
<fh_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11bd <fh_daemonize+0x3d>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    11ce <fh_daemonize+0x4e>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    $0x0,%al
call   10a0 <getdtablesize@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11ff <fh_daemonize+0x7f>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    11df <fh_daemonize+0x5f>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0xdf1(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <dup@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <dup@plt>
mov    %eax,-0x8(%rbp)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fh_daemonize>:
push   %rbx
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     11f0 <fh_daemonize+0x70>
jne    11fa <fh_daemonize+0x7a>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %eax,%eax
call   10a0 <getdtablesize@plt>
test   %eax,%eax
js     11cc <fh_daemonize+0x4c>
mov    %eax,%ebx
nopl   0x0(%rax)
mov    %ebx,%edi
call   1040 <close@plt>
add    $0xffffffff,%ebx
jb     11c0 <fh_daemonize+0x40>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    0xe24(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%edi
call   1030 <dup@plt>
mov    %eax,%edi
pop    %rbx
jmp    1030 <dup@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @fh_daemonize() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @SIGCHLD
%4 = load i32, i32* @SIG_IGN
%5 = call i32 @signal(i32 %3, i32 %4)
%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 = %0
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%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* %2
br label %19
19: ; preds = %25, %16
%20 = load i32, i32* %2
%21 = icmp sge i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %22
%26 = load i32, i32* %2
%27 = add nsw i32 %26, -1
store i32 %27, i32* %2
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* %2
%31 = load i32, i32* %2
%32 = call i32 @dup(i32 %31)
store i32 %32, i32* %2
%33 = load i32, i32* %2
%34 = call i32 @dup(i32 %33)
store i32 %34, i32* %2
ret void
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @fh_daemonize() {
%1 = load i32, i32* @SIGCHLD
%2 = load i32, i32* @SIG_IGN
%3 = tail call i32 @signal(i32 %1, i32 %2)
%4 = tail call i32 (...) @fork()
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%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 %24)
ret void
}