#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*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize() {
  pid_t pid, sid;
  if (chdir("/") < 0)
    exit(EXIT_FAILURE);
  pid = fork();
  if (pid < 0)
    exit(EXIT_FAILURE);
  if (pid > 0)
    exit(EXIT_SUCCESS);
  sid = setsid();
  if (sid < 0)
    exit(EXIT_FAILURE);
  umask(0);
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  return;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    118c <daemonize+0x2c>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11b0 <daemonize+0x50>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11c9 <daemonize+0x69>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11ed <daemonize+0x8d>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
lea    0xe98(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     11be <daemonize+0x5e>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11be <daemonize+0x5e>
jne    11cc <daemonize+0x6c>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     11be <daemonize+0x5e>
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
pop    %rax
jmp    1040 <close@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @daemonize() {
%1 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%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 = tail call i64 (...) @fork()
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %6
%13 = icmp eq i64 %7, 0
br i1 %13, label %17, label %14
14: ; preds = %12
%15 = load i32, i32* @EXIT_SUCCESS
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %12
%18 = tail call i64 (...) @setsid()
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %17
%24 = tail call i32 @umask(i32 0)
%25 = load i32, i32* @STDIN_FILENO
%26 = tail call i32 @close(i32 %25)
%27 = load i32, i32* @STDOUT_FILENO
%28 = tail call i32 @close(i32 %27)
%29 = load i32, i32* @STDERR_FILENO
%30 = tail call i32 @close(i32 %29)
ret void
}