#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_SUCCESS;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ save_pid_to_file(char *, scalar_t__);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(char *pid_file, char *run_dir, int change_root) {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Error: could not create child process\n");
    return 0;
  }
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  umask(0);
  sid = setsid();
  if (sid < 0) {
    fprintf(stderr, "Error: could not create new SID\n");
    return 0;
  }
  if (save_pid_to_file(pid_file, sid) < 0) {
    fprintf(stderr, "Error: could open pid file for writing: %s\n", pid_file);
    return 0;
  }
  if (chdir(run_dir) < 0) {
    fprintf(stderr, "Error: could not change working directory\n");
    return 0;
  }
  if (change_root && (chroot(run_dir) < 0)) {
    fprintf(stderr, "Error: could not change root directory\n");
    return 0;
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  return 1;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11dc <daemonize+0x4c>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    131a <daemonize+0x18a>
cmpq   $0x0,-0x28(%rbp)
jle    11f5 <daemonize+0x65>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1090 <umask@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1235 <daemonize+0xa5>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    131a <daemonize+0x18a>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <save_pid_to_file@plt>
cmp    $0x0,%rax
jge    1273 <daemonize+0xe3>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    131a <daemonize+0x18a>
mov    -0x18(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    12a9 <daemonize+0x119>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    131a <daemonize+0x18a>
cmpl   $0x0,-0x1c(%rbp)
je     12e9 <daemonize+0x159>
mov    -0x18(%rbp),%rdi
call   1040 <chroot@plt>
cmp    $0x0,%rax
jge    12e9 <daemonize+0x159>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    131a <daemonize+0x18a>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbp
xor    %ebx,%ebx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1235 <daemonize+0xa5>
jne    12af <daemonize+0x11f>
xor    %ebx,%ebx
xor    %edi,%edi
call   1090 <umask@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     1247 <daemonize+0xb7>
mov    %rbp,%rdi
mov    %rax,%rsi
call   1030 <save_pid_to_file@plt>
test   %rax,%rax
js     1259 <daemonize+0xc9>
mov    %r14,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
js     1277 <daemonize+0xe7>
test   %r15d,%r15d
je     1204 <daemonize+0x74>
mov    %r14,%rdi
call   1040 <chroot@plt>
test   %rax,%rax
js     1289 <daemonize+0xf9>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    $0x1,%ebx
jmp    12a2 <daemonize+0x112>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
jmp    129b <daemonize+0x10b>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
jmp    129b <daemonize+0x10b>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
xor    %ebx,%ebx
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12a2 <daemonize+0x112>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
jmp    1299 <daemonize+0x109>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"Error: could not create child process\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [33 x i8] c"Error: could not create new SID\0A\00"
@.str.2 = [44 x i8] c"Error: could open pid file for writing: %s\0A\00"
@.str.3 = [43 x i8] c"Error: could not change working directory\0A\00"
@.str.4 = [40 x i8] c"Error: could not change root directory\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonize(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = call i64 (...) @fork()
store i64 %10, i64* %8
%11 = load i64, i64* %8
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %63
16: ; preds = %3
%17 = load i64, i64* %8
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @EXIT_SUCCESS
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %16
%23 = call i32 @umask(i32 0)
%24 = call i64 (...) @setsid()
store i64 %24, i64* %9
%25 = load i64, i64* %9
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %63
30: ; preds = %22
%31 = load i8*, i8** %5
%32 = load i64, i64* %9
%33 = call i64 @save_pid_to_file(i8* %31, i64 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %5
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %37)
store i32 0, i32* %4
br label %63
39: ; preds = %30
%40 = load i8*, i8** %6
%41 = call i64 @chdir(i8* %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %4
br label %63
46: ; preds = %39
%47 = load i32, i32* %7
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %46
%50 = load i8*, i8** %6
%51 = call i64 @chroot(i8* %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %4
br label %63
56: ; preds = %49, %46
%57 = load i32, i32* @STDIN_FILENO
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* @STDOUT_FILENO
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* @STDERR_FILENO
%62 = call i32 @close(i32 %61)
store i32 1, i32* %4
br label %63
63: ; preds = %56, %53, %43, %35, %27, %13
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"Error: could not create child process\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [33 x i8] c"Error: could not create new SID\0A\00"
@.str.2 = [44 x i8] c"Error: could open pid file for writing: %s\0A\00"
@.str.3 = [43 x i8] c"Error: could not change working directory\0A\00"
@.str.4 = [40 x i8] c"Error: could not change root directory\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonize(i8* %0, i8* %1, i32 %2) {
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %48
9: ; preds = %3
%10 = icmp eq i64 %4, 0
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = load i32, i32* @EXIT_SUCCESS
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %9
%15 = tail call i32 @umask(i32 0)
%16 = tail call i64 (...) @setsid()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %48
21: ; preds = %14
%22 = tail call i64 @save_pid_to_file(i8* %0, i64 %16)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %48
27: ; preds = %21
%28 = tail call i64 @chdir(i8* %1)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
br label %48
33: ; preds = %27
%34 = icmp eq i32 %2, 0
br i1 %34, label %41, label %35
35: ; preds = %33
%36 = tail call i64 @chroot(i8* %1)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
br label %48
41: ; preds = %35, %33
%42 = load i32, i32* @STDIN_FILENO
%43 = tail call i32 @close(i32 %42)
%44 = load i32, i32* @STDOUT_FILENO
%45 = tail call i32 @close(i32 %44)
%46 = load i32, i32* @STDERR_FILENO
%47 = tail call i32 @close(i32 %46)
br label %48
48: ; preds = %41, %38, %30, %24, %18, %6
%49 = phi i32 [ 0, %6 ], [ 0, %18 ], [ 0, %24 ], [ 0, %30 ], [ 0, %38 ], [ 1, %41 ]
ret i32 %49
}