#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*/ SIGCHLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ SIGTSTP;
int /*<<< orphan*/ SIGTTIN;
int /*<<< orphan*/ SIGTTOU;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ SIG_IGN;
scalar_t__ chdir(char *);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
char *strerror(int);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(int close_stderr) {
  pid_t pid;
  pid_t sid;
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "unable to fork daemon, code=%d (%s)\n", errno,
            strerror(errno));
    goto __exit_fail;
  }
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  signal(SIGCHLD, SIG_DFL);
  signal(SIGTSTP, SIG_IGN);
  signal(SIGTTOU, SIG_IGN);
  signal(SIGTTIN, SIG_IGN);
  signal(SIGHUP, SIG_IGN);
  signal(SIGTERM, SIG_DFL);
  umask(0);
  sid = setsid();
  if (sid < 0) {
    fprintf(stderr, "unable to create a new session, code %d (%s)\n", errno,
            strerror(errno));
    goto __exit_fail;
  }
  if ((chdir("/")) < 0) {
    fprintf(stderr,
            "unable to change directory to %s, "
            "code %d (%s)\n",
            "/", errno, strerror(errno));
    goto __exit_fail;
  }
  freopen("/dev/null", "r", stdin);
  freopen("/dev/null", "w", stdout);
  if (close_stderr) {
    freopen("/dev/null", "w", stderr);
  }
  return 0;
__exit_fail:
  return -1;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11f3 <daemonize+0x63>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    %rax,%rcx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13be <daemonize+0x22e>
cmpq   $0x0,-0x10(%rbp)
jle    120c <daemonize+0x7c>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
mov    0x2d01(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    12f5 <daemonize+0x165>
mov    0x2ce6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    %rax,%rcx
lea    0xd3c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13be <daemonize+0x22e>
lea    0xd57(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    1354 <daemonize+0x1c4>
mov    0x2c8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2c7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2c6e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%ecx
mov    %rax,%r8
lea    0xd14(%rip),%rsi
lea    0xd0b(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13be <daemonize+0x22e>
mov    0x2c75(%rip),%rax
mov    (%rax),%edx
lea    0xd21(%rip),%rdi
lea    0xd24(%rip),%rsi
call   1040 <freopen@plt>
mov    0x2c11(%rip),%rax
mov    (%rax),%edx
lea    0xd05(%rip),%rdi
lea    0xd0a(%rip),%rsi
call   1040 <freopen@plt>
cmpl   $0x0,-0x8(%rbp)
je     13b2 <daemonize+0x222>
mov    0x2c03(%rip),%rax
mov    (%rax),%edx
lea    0xcdf(%rip),%rdi
lea    0xce4(%rip),%rsi
call   1040 <freopen@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c5 <daemonize+0x235>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     12a5 <daemonize+0x115>
jne    1338 <daemonize+0x1a8>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    0x2dd3(%rip),%rbp
mov    0x0(%rbp),%esi
call   1060 <signal@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    0x2ddb(%rip),%rbx
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%esi
call   1060 <signal@plt>
xor    %edi,%edi
call   1080 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     12c7 <daemonize+0x137>
lea    0xe18(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     1301 <daemonize+0x171>
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
lea    0xe2c(%rip),%rbp
lea    0xe2f(%rip),%rsi
mov    %rbp,%rdi
call   1040 <freopen@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%edx
lea    0xe19(%rip),%rsi
mov    %rbp,%rdi
call   1040 <freopen@plt>
xor    %ebp,%ebp
test   %r14d,%r14d
je     12fa <daemonize+0x16a>
mov    0x2d12(%rip),%rax
mov    (%rax),%edx
lea    0xdee(%rip),%rdi
lea    0xdf3(%rip),%rsi
call   1040 <freopen@plt>
jmp    12fa <daemonize+0x16a>
mov    0x2cf4(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce3(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xd3b(%rip),%rsi
jmp    12e7 <daemonize+0x157>
mov    0x2cd2(%rip),%rax
mov    (%rax),%ebx
mov    0x2cc1(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xd3e(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2c98(%rip),%rax
mov    (%rax),%ebx
mov    0x2c87(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xd34(%rip),%rsi
lea    0xd2b(%rip),%rdx
mov    %ebx,%edi
mov    %ebp,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12f5 <daemonize+0x165>
mov    0x2c41(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"unable to fork daemon, code=%d (%s)\0A\00"
@errno = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@SIG_DFL = global i32 0
@SIGTSTP = global i32 0
@SIG_IGN = global i32 0
@SIGTTOU = global i32 0
@SIGTTIN = global i32 0
@SIGHUP = global i32 0
@SIGTERM = global i32 0
@.str.1 = [46 x i8] c"unable to create a new session, code %d (%s)\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [48 x i8] c"unable to change directory to %s, code %d (%s)\0A\00"
@.str.4 = [10 x i8] c"/dev/null\00"
@.str.5 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.6 = [2 x i8] c"w\00"
@stdout = global i32 0
define i32 @daemonize(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
store i32 %0, i32* %3
%6 = call i64 (...) @fork()
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %11, i8* %13)
br label %70
15: ; preds = %1
%16 = load i64, i64* %4
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @EXIT_SUCCESS
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %15
%22 = load i32, i32* @SIGCHLD
%23 = load i32, i32* @SIG_DFL
%24 = call i32 @signal(i32 %22, i32 %23)
%25 = load i32, i32* @SIGTSTP
%26 = load i32, i32* @SIG_IGN
%27 = call i32 @signal(i32 %25, i32 %26)
%28 = load i32, i32* @SIGTTOU
%29 = load i32, i32* @SIG_IGN
%30 = call i32 @signal(i32 %28, i32 %29)
%31 = load i32, i32* @SIGTTIN
%32 = load i32, i32* @SIG_IGN
%33 = call i32 @signal(i32 %31, i32 %32)
%34 = load i32, i32* @SIGHUP
%35 = load i32, i32* @SIG_IGN
%36 = call i32 @signal(i32 %34, i32 %35)
%37 = load i32, i32* @SIGTERM
%38 = load i32, i32* @SIG_DFL
%39 = call i32 @signal(i32 %37, i32 %38)
%40 = call i32 @umask(i32 0)
%41 = call i64 (...) @setsid()
store i64 %41, i64* %5
%42 = load i64, i64* %5
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %21
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %46, i8* %48)
br label %70
50: ; preds = %21
%51 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %50
%54 = load i32, i32* @stderr
%55 = load i32, i32* @errno
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %55, i8* %57)
br label %70
59: ; preds = %50
%60 = load i32, i32* @stdin
%61 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %60)
%62 = load i32, i32* @stdout
%63 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %62)
%64 = load i32, i32* %3
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %59
%67 = load i32, i32* @stderr
%68 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %67)
br label %69
69: ; preds = %66, %59
store i32 0, i32* %2
br label %71
70: ; preds = %53, %44, %9
store i32 -1, i32* %2
br label %71
71: ; preds = %70, %69
%72 = load i32, i32* %2
ret i32 %72
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"unable to fork daemon, code=%d (%s)\0A\00"
@errno = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@SIG_DFL = global i32 0
@SIGTSTP = global i32 0
@SIG_IGN = global i32 0
@SIGTTOU = global i32 0
@SIGTTIN = global i32 0
@SIGHUP = global i32 0
@SIGTERM = global i32 0
@.str.1 = [46 x i8] c"unable to create a new session, code %d (%s)\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [48 x i8] c"unable to change directory to %s, code %d (%s)\0A\00"
@.str.4 = [10 x i8] c"/dev/null\00"
@.str.5 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.6 = [2 x i8] c"w\00"
@stdout = global i32 0
define i32 @daemonize(i32 %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %6, i8* %7)
br label %58
9: ; preds = %1
%10 = icmp eq i64 %2, 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 = load i32, i32* @SIGCHLD
%16 = load i32, i32* @SIG_DFL
%17 = tail call i32 @signal(i32 %15, i32 %16)
%18 = load i32, i32* @SIGTSTP
%19 = load i32, i32* @SIG_IGN
%20 = tail call i32 @signal(i32 %18, i32 %19)
%21 = load i32, i32* @SIGTTOU
%22 = load i32, i32* @SIG_IGN
%23 = tail call i32 @signal(i32 %21, i32 %22)
%24 = load i32, i32* @SIGTTIN
%25 = load i32, i32* @SIG_IGN
%26 = tail call i32 @signal(i32 %24, i32 %25)
%27 = load i32, i32* @SIGHUP
%28 = load i32, i32* @SIG_IGN
%29 = tail call i32 @signal(i32 %27, i32 %28)
%30 = load i32, i32* @SIGTERM
%31 = load i32, i32* @SIG_DFL
%32 = tail call i32 @signal(i32 %30, i32 %31)
%33 = tail call i32 @umask(i32 0)
%34 = tail call i64 (...) @setsid()
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %14
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = tail call i8* @strerror(i32 %38)
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %38, i8* %39)
br label %58
41: ; preds = %14
%42 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = tail call i8* @strerror(i32 %46)
%48 = tail call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %46, i8* %47)
br label %58
49: ; preds = %41
%50 = load i32, i32* @stdin
%51 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %50)
%52 = load i32, i32* @stdout
%53 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %52)
%54 = icmp eq i32 %0, 0
br i1 %54, label %58, label %55
55: ; preds = %49
%56 = load i32, i32* @stderr
%57 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %56)
br label %58
58: ; preds = %4, %36, %44, %49, %55
%59 = phi i32 [ 0, %55 ], [ 0, %49 ], [ -1, %44 ], [ -1, %36 ], [ -1, %4 ]
ret i32 %59
}