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

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
void daemonize(void) {
  pid_t pid;
  fclose(stdin);
  fclose(stdout);
  fclose(stderr);
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  pid = fork();
  if (pid == -1)
    err(1, "fork");
  else if (pid == 0)
    return;
  else
    exit(0);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11d6 <daemonize+0x86>
mov    $0x1,%edi
lea    0xe34(%rip),%rsi
call   1050 <err@plt>
jmp    11ec <daemonize+0x9c>
cmpl   $0x0,-0x4(%rbp)
jne    11e5 <daemonize+0x95>
jmp    11ec <daemonize+0x9c>
xor    %edi,%edi
call   1060 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     11c7 <daemonize+0x77>
cmp    $0xffffffff,%eax
jne    11c9 <daemonize+0x79>
lea    0xe44(%rip),%rsi
mov    $0x1,%edi
pop    %rax
jmp    1050 <err@plt>
pop    %rax
ret
xor    %edi,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [5 x i8] c"fork\00"
define void @daemonize() {
%1 = load i32, i32* @stdin
%2 = tail call i32 @fclose(i32 %1)
%3 = load i32, i32* @stdout
%4 = tail call i32 @fclose(i32 %3)
%5 = load i32, i32* @stderr
%6 = tail call i32 @fclose(i32 %5)
%7 = load i32, i32* @STDIN_FILENO
%8 = tail call i32 @close(i32 %7)
%9 = load i32, i32* @STDOUT_FILENO
%10 = tail call i32 @close(i32 %9)
%11 = load i32, i32* @STDERR_FILENO
%12 = tail call i32 @close(i32 %11)
%13 = tail call i32 (...) @fork()
switch i32 %13, label %16 [
i32 -1, label %14
i32 0, label %18
]
14: ; preds = %0
%15 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %18
16: ; preds = %0
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %0, %14
ret void
}