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


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11a8 <daemonize+0x38>
lea    0xe6b(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11c1 <daemonize+0x51>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11c6 <daemonize+0x56>
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    11f8 <daemonize+0x88>
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe19(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    1228 <daemonize+0xb8>
lea    0xe05(%rip),%rdi
call   1040 <printf@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1050 <close@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     11c0 <daemonize+0x50>
jne    11c9 <daemonize+0x59>
xor    %edi,%edi
call   1070 <umask@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     11d7 <daemonize+0x67>
lea    0xe7f(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     11e0 <daemonize+0x70>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
pop    %rax
jmp    1050 <close@plt>
lea    0xe39(%rip),%rdi
jmp    11e7 <daemonize+0x77>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe2d(%rip),%rdi
jmp    11e7 <daemonize+0x77>
lea    0xe33(%rip),%rdi
call   1040 <printf@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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