#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ msg(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(void) {
  pid_t pid;
  pid = fork();
  switch (pid) {
  case -1:
    msg("Error! fork ioniced failed, exiting.");
    exit(EXIT_FAILURE);
  case 0:
    setsid();
    chdir("/");
    umask(0);
    break;
  default:
    exit(EXIT_SUCCESS);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
sub    $0xffffffff,%eax
je     1196 <daemonize+0x36>
jmp    1186 <daemonize+0x26>
mov    -0x8(%rbp),%eax
test   %eax,%eax
je     11b0 <daemonize+0x50>
jmp    11cf <daemonize+0x6f>
lea    0xe63(%rip),%rdi
call   1080 <msg@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
lea    0xe67(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1050 <umask@plt>
jmp    11dd <daemonize+0x7d>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
jne    1187 <daemonize+0x27>
xor    %eax,%eax
call   1040 <setsid@plt>
lea    0xeab(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
pop    %rax
jmp    1050 <umask@plt>
cmp    $0xffffffff,%eax
jne    11a6 <daemonize+0x46>
lea    0xe6d(%rip),%rdi
call   1080 <msg@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"Error! fork ioniced failed, exiting.\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"/\00"
@EXIT_SUCCESS = global i32 0
define void @daemonize() {
%1 = alloca i32
%2 = call i32 (...) @fork()
store i32 %2, i32* %1
%3 = load i32, i32* %1
switch i32 %3, label %12 [
i32 -1, label %4
i32 0, label %8
]
4: ; preds = %0
%5 = call i32 @msg(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = call i32 (...) @setsid()
%10 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @umask(i32 0)
br label %15
12: ; preds = %0
%13 = load i32, i32* @EXIT_SUCCESS
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %8
ret void
}

LLVM IR (-O2):
@.str = [37 x i8] c"Error! fork ioniced failed, exiting.\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"/\00"
@EXIT_SUCCESS = global i32 0
define void @daemonize() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %10 [
i32 -1, label %2
i32 0, label %6
]
2: ; preds = %0
%3 = tail call i32 @msg(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @EXIT_FAILURE
%5 = tail call i32 @exit(i32 %4)
unreachable
6: ; preds = %0
%7 = tail call i32 (...) @setsid()
%8 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @umask(i32 0)
ret void
10: ; preds = %0
%11 = load i32, i32* @EXIT_SUCCESS
%12 = tail call i32 @exit(i32 %11)
unreachable
}