#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_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ exit_with_failure(char *);
scalar_t__ fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int /*<<< orphan*/ wait(int *);
void daemonize() {
  pid_t pid;
  pid = fork();
  if (pid < 0)
    exit_with_failure("fork");
  if (pid > 0) {
    int ignore;
    wait(&ignore);
    exit(EXIT_SUCCESS);
  }
  if (setsid() == -1)
    exit_with_failure("setsid");
  chdir("/");
  umask(0);
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  open("/dev/null", O_RDONLY);
  open("/dev/null", O_WRONLY);
  open("/dev/null", O_RDWR);
  pid = fork();
  if (pid < 0)
    exit_with_failure("fork");
  if (pid > 0)
    exit(EXIT_SUCCESS);
}


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    11ba <daemonize+0x2a>
lea    0xe4b(%rip),%rdi
call   1030 <exit_with_failure@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11dc <daemonize+0x4c>
lea    -0xc(%rbp),%rdi
call   10a0 <wait@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11f8 <daemonize+0x68>
lea    0xe12(%rip),%rdi
call   1030 <exit_with_failure@plt>
lea    0xe0d(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
lea    0xdc9(%rip),%rdi
call   1080 <open@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
lea    0xdb4(%rip),%rdi
call   1080 <open@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
lea    0xd9f(%rip),%rdi
call   1080 <open@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1296 <daemonize+0x106>
lea    0xd6f(%rip),%rdi
call   1030 <exit_with_failure@plt>
cmpq   $0x0,-0x8(%rbp)
jle    12af <daemonize+0x11f>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     11b2 <daemonize+0x22>
je     11be <daemonize+0x2e>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
jmp    1272 <daemonize+0xe2>
lea    0xe47(%rip),%rdi
call   1030 <exit_with_failure@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11d6 <daemonize+0x46>
lea    0xe34(%rip),%rdi
call   1030 <exit_with_failure@plt>
lea    0xe2f(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
lea    0xdeb(%rip),%rbx
mov    %rbx,%rdi
call   1080 <open@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1261 <daemonize+0xd1>
jne    1272 <daemonize+0xe2>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xd98(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <exit_with_failure@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [7 x i8] c"setsid\00"
@.str.2 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_RDWR = 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 %8
6: ; preds = %0
%7 = call i32 @exit_with_failure(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i64, i64* %1
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = call i32 @wait(i32* %2)
%13 = load i32, i32* @EXIT_SUCCESS
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %8
%16 = call i32 (...) @setsid()
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @exit_with_failure(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @umask(i32 0)
%23 = load i32, i32* @STDIN_FILENO
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* @STDERR_FILENO
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @O_RDONLY
%30 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @O_WRONLY
%32 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @O_RDWR
%34 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = call i64 (...) @fork()
store i64 %35, i64* %1
%36 = load i64, i64* %1
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %20
%39 = call i32 @exit_with_failure(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %40
40: ; preds = %38, %20
%41 = load i64, i64* %1
%42 = icmp sgt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* @EXIT_SUCCESS
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %40
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [7 x i8] c"setsid\00"
@.str.2 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
define void @daemonize() {
%1 = alloca i32
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @exit_with_failure(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %13
6: ; preds = %0
%7 = icmp eq i64 %2, 0
br i1 %7, label %13, label %8
8: ; preds = %6
%9 = bitcast i32* %1 to i8*
%10 = call i32 @wait(i32* %1)
%11 = load i32, i32* @EXIT_SUCCESS
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %4, %6
%14 = tail call i32 (...) @setsid()
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @exit_with_failure(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @umask(i32 0)
%21 = load i32, i32* @STDIN_FILENO
%22 = tail call i32 @close(i32 %21)
%23 = load i32, i32* @STDOUT_FILENO
%24 = tail call i32 @close(i32 %23)
%25 = load i32, i32* @STDERR_FILENO
%26 = tail call i32 @close(i32 %25)
%27 = load i32, i32* @O_RDONLY
%28 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = load i32, i32* @O_WRONLY
%30 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @O_RDWR
%32 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = tail call i64 (...) @fork()
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %18
%36 = tail call i32 @exit_with_failure(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %42
37: ; preds = %18
%38 = icmp eq i64 %33, 0
br i1 %38, label %42, label %39
39: ; preds = %37
%40 = load i32, i32* @EXIT_SUCCESS
%41 = tail call i32 @exit(i32 %40)
unreachable
42: ; preds = %35, %37
ret void
}