#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*/ SIGHUP;
int /*<<< orphan*/ SIGTSTP;
int /*<<< orphan*/ SIGTTIN;
int /*<<< orphan*/ SIGTTOU;
int /*<<< orphan*/ SIG_IGN;
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
void daemonize(void) {
  signal(SIGTTOU, SIG_IGN);
  signal(SIGTTIN, SIG_IGN);
  signal(SIGTSTP, SIG_IGN);
  if (0 != fork())
    exit(0);
  if (-1 == setsid())
    exit(0);
  signal(SIGHUP, SIG_IGN);
  if (0 != fork())
    exit(0);
  if (0 != chdir("/"))
    exit(0);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11b5 <daemonize+0x65>
xor    %edi,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11d2 <daemonize+0x82>
xor    %edi,%edi
call   1060 <exit@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1205 <daemonize+0xb5>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xdf4(%rip),%rdi
call   1030 <chdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1226 <daemonize+0xd6>
xor    %edi,%edi
call   1060 <exit@plt>
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
mov    0x2e67(%rip),%rbx
mov    (%rbx),%esi
call   1050 <signal@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1050 <signal@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1050 <signal@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11cf <daemonize+0x7f>
xor    %eax,%eax
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
je     11cf <daemonize+0x7f>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1050 <signal@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11cf <daemonize+0x7f>
lea    0xe3d(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
jne    11cf <daemonize+0x7f>
pop    %rbx
ret
xor    %edi,%edi
call   1060 <exit@plt>

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

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