#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ chdir(char *);
int /*<<< orphan*/ ecritical(char *);
int /*<<< orphan*/ eerror(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fclose(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(void) {
  pid_t pid, sid;
  switch ((pid = fork())) {
  case -1:
    perror("fork");
    return -1;
  case 0:
    break;
  default:
    exit(EXIT_SUCCESS);
  }
  umask(0);
  if ((sid = setsid()) < 0) {
    ecritical("setsid");
    return -1;
  }
  if (chdir("/") < 0) {
    ecritical("chdir");
    return -1;
  }
  if (fclose(stdin) < 0)
    eerror("fclose stdin");
  if (fclose(stdout) < 0)
    eerror("fclose stdout");
  if (fclose(stderr) < 0)
    eerror("fclose stderr");
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11c5 <daemonize+0x35>
jmp    11b5 <daemonize+0x25>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11dd <daemonize+0x4d>
jmp    11e2 <daemonize+0x52>
lea    0xe34(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <daemonize+0x133>
jmp    11f0 <daemonize+0x60>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1222 <daemonize+0x92>
lea    0xdf4(%rip),%rdi
call   1060 <ecritical@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <daemonize+0x133>
lea    0xde3(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1250 <daemonize+0xc0>
lea    0xdcf(%rip),%rdi
call   1060 <ecritical@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <daemonize+0x133>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
cmp    $0x0,%rax
jge    1274 <daemonize+0xe4>
lea    0xda5(%rip),%rdi
call   1080 <eerror@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
cmp    $0x0,%rax
jge    1298 <daemonize+0x108>
lea    0xd8e(%rip),%rdi
call   1080 <eerror@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
cmp    $0x0,%rax
jge    12bc <daemonize+0x12c>
lea    0xd78(%rip),%rdi
call   1080 <eerror@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    1242 <daemonize+0xb2>
xor    %edi,%edi
call   1070 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %eax,%eax
js     1224 <daemonize+0x94>
lea    0xe53(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     122d <daemonize+0x9d>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
test   %rax,%rax
jns    11e2 <daemonize+0x52>
lea    0xe37(%rip),%rdi
call   1080 <eerror@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
test   %rax,%rax
jns    1201 <daemonize+0x71>
lea    0xe25(%rip),%rdi
call   1080 <eerror@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
xor    %ebx,%ebx
test   %rax,%rax
jns    123e <daemonize+0xae>
lea    0xe12(%rip),%rdi
call   1080 <eerror@plt>
jmp    123e <daemonize+0xae>
lea    0xdda(%rip),%rdi
jmp    1234 <daemonize+0xa4>
lea    0xdda(%rip),%rdi
call   1060 <ecritical@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret
cmp    $0xffffffff,%eax
jne    1255 <daemonize+0xc5>
lea    0xdb2(%rip),%rdi
call   1090 <perror@plt>
jmp    1239 <daemonize+0xa9>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [7 x i8] c"setsid\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [6 x i8] c"chdir\00"
@stdin = global i32 0
@.str.4 = [13 x i8] c"fclose stdin\00"
@stdout = global i32 0
@.str.5 = [14 x i8] c"fclose stdout\00"
@stderr = global i32 0
@.str.6 = [14 x i8] c"fclose stderr\00"
define i32 @daemonize() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
switch i32 %4, label %8 [
i32 -1, label %5
i32 0, label %7
]
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %41
7: ; preds = %0
br label %11
8: ; preds = %0
%9 = load i32, i32* @EXIT_SUCCESS
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %7
%12 = call i32 @umask(i32 0)
%13 = call i32 (...) @setsid()
store i32 %13, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @ecritical(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %41
17: ; preds = %11
%18 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @ecritical(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %41
22: ; preds = %17
%23 = load i32, i32* @stdin
%24 = call i64 @fclose(i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @eerror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
%29 = load i32, i32* @stdout
%30 = call i64 @fclose(i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @eerror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = load i32, i32* @stderr
%36 = call i64 @fclose(i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @eerror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
store i32 0, i32* %1
br label %41
41: ; preds = %40, %20, %15, %5
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [7 x i8] c"setsid\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [6 x i8] c"chdir\00"
@stdin = global i32 0
@.str.4 = [13 x i8] c"fclose stdin\00"
@stdout = global i32 0
@.str.5 = [14 x i8] c"fclose stdout\00"
@stderr = global i32 0
@.str.6 = [14 x i8] c"fclose stderr\00"
define i32 @daemonize() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %4 [
i32 -1, label %2
i32 0, label %7
]
2: ; preds = %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %36
4: ; preds = %0
%5 = load i32, i32* @EXIT_SUCCESS
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = tail call i32 @umask(i32 0)
%9 = tail call i32 (...) @setsid()
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @ecritical(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %36
13: ; preds = %7
%14 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @ecritical(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %36
18: ; preds = %13
%19 = load i32, i32* @stdin
%20 = tail call i64 @fclose(i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @eerror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i32, i32* @stdout
%26 = tail call i64 @fclose(i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = tail call i32 @eerror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = load i32, i32* @stderr
%32 = tail call i64 @fclose(i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = tail call i32 @eerror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %36
36: ; preds = %30, %34, %16, %11, %2
%37 = phi i32 [ -1, %11 ], [ -1, %16 ], [ -1, %2 ], [ 0, %34 ], [ 0, %30 ]
ret i32 %37
}