#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ SIGCHLD;
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*/);
scalar_t__ fork();
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(void) {
  pid_t pid;
  pid = fork();
  if (pid < 0)
    exit(EXIT_FAILURE);
  if (pid > 0)
    exit(EXIT_SUCCESS);
  if (setsid() < 0)
    exit(EXIT_FAILURE);
  signal(SIGCHLD, SIG_IGN);
  signal(SIGHUP, SIG_IGN);
  pid = fork();
  if (pid < 0)
    exit(EXIT_FAILURE);
  if (pid > 0)
    exit(EXIT_SUCCESS);
  umask(0);
  chdir("/");
  int x;
  for (x = sysconf(_SC_OPEN_MAX); x >= 0; x--) {
    close(x);
  }
  openlog("my_daemon", LOG_PID, LOG_DAEMON);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11bc <daemonize+0x2c>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11d5 <daemonize+0x45>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jge    11f4 <daemonize+0x64>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1246 <daemonize+0xb6>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    125f <daemonize+0xcf>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xd93(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     12a3 <daemonize+0x113>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1283 <daemonize+0xf3>
mov    0x2d0e(%rip),%rax
mov    (%rax),%esi
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
lea    0xd46(%rip),%rdi
call   1090 <openlog@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     124b <daemonize+0xbb>
jne    1259 <daemonize+0xc9>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     124b <daemonize+0xbb>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rbx
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1060 <signal@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     124b <daemonize+0xbb>
jne    1259 <daemonize+0xc9>
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xe06(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
test   %eax,%eax
js     122c <daemonize+0x9c>
mov    %eax,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
call   1040 <close@plt>
add    $0xffffffff,%ebx
jb     1220 <daemonize+0x90>
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
lea    0xdbd(%rip),%rdi
pop    %rbx
jmp    1090 <openlog@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@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"my_daemon\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
define void @daemonize() {
%1 = alloca i64
%2 = alloca i32
%3 = call i64 (...) @fork()
store i64 %3, i64* %1
%4 = load i64, i64* %1
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @EXIT_FAILURE
%8 = call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = load i64, i64* %1
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @EXIT_SUCCESS
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %9
%16 = call i64 (...) @setsid()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %15
%22 = load i32, i32* @SIGCHLD
%23 = load i32, i32* @SIG_IGN
%24 = call i32 @signal(i32 %22, i32 %23)
%25 = load i32, i32* @SIGHUP
%26 = load i32, i32* @SIG_IGN
%27 = call i32 @signal(i32 %25, i32 %26)
%28 = call i64 (...) @fork()
store i64 %28, i64* %1
%29 = load i64, i64* %1
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %21
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %21
%35 = load i64, i64* %1
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* @EXIT_SUCCESS
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %34
%41 = call i32 @umask(i32 0)
%42 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%43 = load i32, i32* @_SC_OPEN_MAX
%44 = call i32 @sysconf(i32 %43)
store i32 %44, i32* %2
br label %45
45: ; preds = %51, %40
%46 = load i32, i32* %2
%47 = icmp sge i32 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = load i32, i32* %2
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48
%52 = load i32, i32* %2
%53 = add nsw i32 %52, -1
store i32 %53, i32* %2
br label %45
54: ; preds = %45
%55 = load i32, i32* @LOG_PID
%56 = load i32, i32* @LOG_DAEMON
%57 = call i32 @openlog(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %55, i32 %56)
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@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"my_daemon\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
define void @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = load i32, i32* @EXIT_FAILURE
%5 = tail call i32 @exit(i32 %4)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %11, label %8
8: ; preds = %6
%9 = load i32, i32* @EXIT_SUCCESS
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %6
%12 = tail call i64 (...) @setsid()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %11
%18 = load i32, i32* @SIGCHLD
%19 = load i32, i32* @SIG_IGN
%20 = tail call i32 @signal(i32 %18, i32 %19)
%21 = load i32, i32* @SIGHUP
%22 = load i32, i32* @SIG_IGN
%23 = tail call i32 @signal(i32 %21, i32 %22)
%24 = tail call i64 (...) @fork()
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %17
%30 = icmp eq i64 %24, 0
br i1 %30, label %34, label %31
31: ; preds = %29
%32 = load i32, i32* @EXIT_SUCCESS
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %29
%35 = tail call i32 @umask(i32 0)
%36 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* @_SC_OPEN_MAX
%38 = tail call i32 @sysconf(i32 %37)
%39 = icmp sgt i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %34, %40
%41 = phi i32 [ %43, %40 ], [ %38, %34 ]
%42 = tail call i32 @close(i32 %41)
%43 = add nsw i32 %41, -1
%44 = icmp eq i32 %41, 0
br i1 %44, label %45, label %40
45: ; preds = %40, %34
%46 = load i32, i32* @LOG_PID
%47 = load i32, i32* @LOG_DAEMON
%48 = tail call i32 @openlog(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %46, i32 %47)
ret void
}