#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*/ O_RDWR;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ log_debug(int, char *);
int /*<<< orphan*/ log_error(char *);
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
scalar_t__ umask(int /*<<< orphan*/);
void daemon_init(void) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    log_error("error starting daemon : %m");
    exit(-1);
  } else if (pid)
    exit(0);
  close(0);
  open("/dev/null", O_RDWR);
  dup2(0, 1);
  dup2(0, 2);
  setsid();
  if (umask(0) < 0) {
    log_error("failed to set umask 0 : %m");
    exit(-1);
  }
  if (chdir("/") < 0) {
    log_error("failed to set working dir to / : %m");
    exit(-1);
  }
  log_debug(1, "daemon_init success");
}


x86 assembly (clang, -O0):
<daemon_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11d4 <daemon_init+0x34>
lea    0xe3b(%rip),%rdi
call   1030 <log_error@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     11e6 <daemon_init+0x46>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    11eb <daemon_init+0x4b>
xor    %edi,%edi
call   1070 <close@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    0xe19(%rip),%rdi
call   10a0 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
xor    %edi,%edi
call   1090 <umask@plt>
cmp    $0x0,%rax
jge    124d <daemon_init+0xad>
lea    0xde7(%rip),%rdi
call   1030 <log_error@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xdec(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1279 <daemon_init+0xd9>
lea    0xdd8(%rip),%rdi
call   1030 <log_error@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    $0x1,%edi
lea    0xde1(%rip),%rsi
call   1060 <log_debug@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_init>:
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1219 <daemon_init+0x79>
jne    1222 <daemon_init+0x82>
xor    %edi,%edi
call   1070 <close@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
call   10a0 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
xor    %edi,%edi
call   1090 <umask@plt>
test   %rax,%rax
js     1229 <daemon_init+0x89>
lea    0xe43(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     1232 <daemon_init+0x92>
lea    0xe58(%rip),%rsi
mov    $0x1,%edi
pop    %rax
jmp    1060 <log_debug@plt>
lea    0xde0(%rip),%rdi
jmp    1239 <daemon_init+0x99>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xdf5(%rip),%rdi
jmp    1239 <daemon_init+0x99>
lea    0xe09(%rip),%rdi
call   1030 <log_error@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"error starting daemon : %m\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.2 = [27 x i8] c"failed to set umask 0 : %m\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [36 x i8] c"failed to set working dir to / : %m\00"
@.str.5 = [20 x i8] c"daemon_init success\00"
define void @daemon_init() {
%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 %8
5: ; preds = %0
%6 = call i32 @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 -1)
unreachable
8: ; preds = %0
%9 = load i64, i64* %1
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %8
br label %14
14: ; preds = %13
%15 = call i32 @close(i32 0)
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = call i32 @dup2(i32 0, i32 1)
%19 = call i32 @dup2(i32 0, i32 2)
%20 = call i32 (...) @setsid()
%21 = call i64 @umask(i32 0)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = call i32 @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %14
%27 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = call i32 @log_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %26
%33 = call i32 @log_debug(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"error starting daemon : %m\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.2 = [27 x i8] c"failed to set umask 0 : %m\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [36 x i8] c"failed to set working dir to / : %m\00"
@.str.5 = [20 x i8] c"daemon_init success\00"
define void @daemon_init() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 -1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %6
%11 = tail call i32 @close(i32 0)
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = tail call i32 @dup2(i32 0, i32 1)
%15 = tail call i32 @dup2(i32 0, i32 2)
%16 = tail call i32 (...) @setsid()
%17 = tail call i64 @umask(i32 0)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = tail call i32 @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @exit(i32 -1)
unreachable
22: ; preds = %10
%23 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = tail call i32 @log_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%27 = tail call i32 @exit(i32 -1)
unreachable
28: ; preds = %22
%29 = tail call i32 @log_debug(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
ret void
}