#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*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(void) {
  pid_t pid;
  if ((pid = fork()) < 0)
    err(1, "Failed to fork daemon");
  if (pid != 0)
    exit(0);
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  setsid();
  chdir("/");
  umask(0);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jge    119e <daemonize+0x2e>
mov    $0x1,%edi
lea    0xe67(%rip),%rsi
call   1070 <err@plt>
cmpq   $0x0,-0x8(%rbp)
je     11b0 <daemonize+0x40>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
lea    0xe2e(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11c4 <daemonize+0x54>
jne    11d5 <daemonize+0x65>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
lea    0xe5f(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
pop    %rax
jmp    1060 <umask@plt>
lea    0xe35(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"Failed to fork daemon\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [2 x i8] c"/\00"
define void @daemonize() {
%1 = alloca i64
%2 = call i64 (...) @fork()
store i64 %2, i64* %1
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = call i32 @err(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = load i64, i64* %1
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %6
%12 = load i32, i32* @STDIN_FILENO
%13 = call i32 @close(i32 %12)
%14 = load i32, i32* @STDOUT_FILENO
%15 = call i32 @close(i32 %14)
%16 = load i32, i32* @STDERR_FILENO
%17 = call i32 @close(i32 %16)
%18 = call i32 (...) @setsid()
%19 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @umask(i32 0)
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"Failed to fork daemon\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [2 x i8] c"/\00"
define void @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %7
5: ; preds = %0
%6 = icmp eq i64 %1, 0
br i1 %6, label %9, label %7
7: ; preds = %3, %5
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %5
%10 = load i32, i32* @STDIN_FILENO
%11 = tail call i32 @close(i32 %10)
%12 = load i32, i32* @STDOUT_FILENO
%13 = tail call i32 @close(i32 %12)
%14 = load i32, i32* @STDERR_FILENO
%15 = tail call i32 @close(i32 %14)
%16 = tail call i32 (...) @setsid()
%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)
ret void
}