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


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jle    118c <daemonize+0x2c>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jge    11a5 <daemonize+0x45>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11aa <daemonize+0x4a>
xor    %edi,%edi
call   1060 <umask@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11d4 <daemonize+0x74>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
lea    0xdf2(%rip),%rdi
lea    0xdf5(%rip),%rsi
call   1030 <freopen@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jg     11d7 <daemonize+0x77>
js     11c9 <daemonize+0x69>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     11c9 <daemonize+0x69>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
lea    0xe44(%rip),%rdi
lea    0xe47(%rip),%rsi
pop    %rax
jmp    1030 <freopen@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

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