#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*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ logger_daemonize();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize() {
  int pid;
  pid = fork();
  if (pid == -1) {
    die("Unable to fork", errno);
  }
  if (pid) {
    exit(0);
  }
  logger_daemonize();
  setsid();
  pid = fork();
  if (pid == -1) {
    die("Unable to fork", errno);
  }
  if (pid) {
    exit(0);
  }
  chdir("/");
  umask(0);
  int devnull_fd = open("/dev/null", O_RDWR);
  if (devnull_fd >= 0) {
    dup2(devnull_fd, 0);
    dup2(devnull_fd, 1);
    dup2(devnull_fd, 2);
    if (devnull_fd > 2)
      close(devnull_fd);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11d1 <daemonize+0x31>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
call   10a0 <die@plt>
cmpl   $0x0,-0x4(%rbp)
je     11e2 <daemonize+0x42>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1030 <logger_daemonize@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1219 <daemonize+0x79>
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
lea    0xdec(%rip),%rdi
call   10a0 <die@plt>
cmpl   $0x0,-0x4(%rbp)
je     122a <daemonize+0x8a>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xdde(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    0xdc4(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     129a <daemonize+0xfa>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
cmpl   $0x2,-0x8(%rbp)
jle    1295 <daemonize+0xf5>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
jmp    129a <daemonize+0xfa>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jne    1223 <daemonize+0x83>
xor    %eax,%eax
call   1030 <logger_daemonize@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jne    1223 <daemonize+0x83>
lea    0xe43(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0xe29(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1221 <daemonize+0x81>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
cmp    $0x3,%ebx
jb     1221 <daemonize+0x81>
mov    %ebx,%edi
pop    %rbx
jmp    1060 <close@plt>
pop    %rbx
ret
cmp    $0xffffffff,%eax
jne    123d <daemonize+0x9d>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
lea    0xdc8(%rip),%rdi
call   10a0 <die@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
@.str = [15 x i8] c"Unable to fork\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @daemonize() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %5 [
i32 -1, label %2
i32 0, label %7
]
2: ; preds = %0
%3 = load i32, i32* @errno
%4 = tail call i32 @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %5
5: ; preds = %2, %0
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %0
%8 = tail call i32 (...) @logger_daemonize()
%9 = tail call i32 (...) @setsid()
%10 = tail call i32 (...) @fork()
switch i32 %10, label %14 [
i32 -1, label %11
i32 0, label %16
]
11: ; preds = %7
%12 = load i32, i32* @errno
%13 = tail call i32 @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %11, %7
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %7
%17 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @umask(i32 0)
%19 = load i32, i32* @O_RDWR
%20 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = icmp sgt i32 %20, -1
br i1 %21, label %22, label %29
22: ; preds = %16
%23 = tail call i32 @dup2(i32 %20, i32 0)
%24 = tail call i32 @dup2(i32 %20, i32 1)
%25 = tail call i32 @dup2(i32 %20, i32 2)
%26 = icmp ugt i32 %20, 2
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = tail call i32 @close(i32 %20)
br label %29
29: ; preds = %22, %27, %16
ret void
}