#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 */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize() {
  int max_fd;
  pid_t pid;
  int i;
  if ((pid = fork()) != 0) {
    exit(0);
  }
  setsid();
  signal(SIGHUP, SIG_IGN);
  if ((pid = fork()) != 0) {
    exit(0);
  }
  chdir("/");
  umask(0);
  max_fd = (int)sysconf(_SC_OPEN_MAX);
  for (i = 0; i < max_fd; i++) {
    close(i);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     11a2 <daemonize+0x22>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     11da <daemonize+0x5a>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xe1f(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1227 <daemonize+0xa7>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1205 <daemonize+0x85>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    1207 <daemonize+0x87>
xor    %eax,%eax
call   1050 <setsid@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    1207 <daemonize+0x87>
lea    0xe41(%rip),%rdi
call   1030 <chdir@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %rax,%r14
test   %r14d,%r14d
jle    11ff <daemonize+0x7f>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%r14d
jne    11f0 <daemonize+0x70>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str = [2 x i8] c"/\00"
@_SC_OPEN_MAX = global i32 0
define void @daemonize() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @exit(i32 0)
unreachable
8: ; preds = %0
%9 = call i32 (...) @setsid()
%10 = load i32, i32* @SIGHUP
%11 = load i32, i32* @SIG_IGN
%12 = call i32 @signal(i32 %10, i32 %11)
%13 = call i32 (...) @fork()
store i32 %13, i32* %2
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %8
%18 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @umask(i32 0)
%20 = load i32, i32* @_SC_OPEN_MAX
%21 = call i64 @sysconf(i32 %20)
%22 = trunc i64 %21 to i32
store i32 %22, i32* %1
store i32 0, i32* %3
br label %23
23: ; preds = %30, %17
%24 = load i32, i32* %3
%25 = load i32, i32* %1
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load i32, i32* %3
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %27
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 1
store i32 %32, i32* %3
br label %23
33: ; preds = %23
ret void
}

LLVM IR (-O2):
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str = [2 x i8] c"/\00"
@_SC_OPEN_MAX = global i32 0
define void @daemonize() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %1, 0
br i1 %2, label %5, label %3
3: ; preds = %0
%4 = tail call i32 @exit(i32 0)
unreachable
5: ; preds = %0
%6 = tail call i32 (...) @setsid()
%7 = load i32, i32* @SIGHUP
%8 = load i32, i32* @SIG_IGN
%9 = tail call i32 @signal(i32 %7, i32 %8)
%10 = tail call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %5
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %5
%15 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @umask(i32 0)
%17 = load i32, i32* @_SC_OPEN_MAX
%18 = tail call i64 @sysconf(i32 %17)
%19 = trunc i64 %18 to i32
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %14, %21
%22 = phi i32 [ %24, %21 ], [ 0, %14 ]
%23 = tail call i32 @close(i32 %22)
%24 = add nuw nsw i32 %22, 1
%25 = icmp eq i32 %24, %19
br i1 %25, label %26, label %21
26: ; preds = %21, %14
ret void
}