#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 LOG_DAEMON;
int LOG_INFO;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ openlog(char const *, int /*<<< orphan*/, int);
scalar_t__ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int, char *);
int daemon_init(const char *ident, int facility) {
  int i;
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    syslog(LOG_INFO | LOG_DAEMON, "daemon_init first fork");
    return -1;
  } else if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  if (setsid() < 0) {
    syslog(LOG_INFO | LOG_DAEMON, "daemon_init setsid");
    return -1;
  }
  signal(SIGHUP, SIG_IGN);
  pid = fork();
  if (pid < 0) {
    syslog(LOG_INFO | LOG_DAEMON, "daemon_init second fork");
    return -1;
  } else if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  chdir("/");
  for (i = 0; i < 64; i++) {
    close(i);
  }
  open("/dev/null", O_RDONLY);
  open("/dev/null", O_RDWR);
  open("/dev/null", O_RDWR);
  openlog(ident, LOG_PID, facility);
  return 0;
}


x86 assembly (clang, -O0):
<daemon_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11df <daemon_init+0x4f>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    0x2e13(%rip),%rax
or     (%rax),%edi
lea    0xe32(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <daemon_init+0x1ab>
cmpq   $0x0,-0x20(%rbp)
jle    11f8 <daemon_init+0x68>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    11fd <daemon_init+0x6d>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jge    1238 <daemon_init+0xa8>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    0x2dba(%rip),%rax
or     (%rax),%edi
lea    0xdf0(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <daemon_init+0x1ab>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    128f <daemon_init+0xff>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
mov    0x2d63(%rip),%rax
or     (%rax),%edi
lea    0xdac(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <daemon_init+0x1ab>
cmpq   $0x0,-0x20(%rbp)
jle    12a8 <daemon_init+0x118>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    12ad <daemon_init+0x11d>
lea    0xd8e(%rip),%rdi
call   1030 <chdir@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x40,-0x18(%rbp)
jge    12e0 <daemon_init+0x150>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12c0 <daemon_init+0x130>
mov    0x2ce9(%rip),%rax
mov    (%rax),%esi
lea    0xd54(%rip),%rdi
call   1080 <open@plt>
mov    0x2ce4(%rip),%rax
mov    (%rax),%esi
lea    0xd3f(%rip),%rdi
call   1080 <open@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%esi
lea    0xd2a(%rip),%rdi
call   1080 <open@plt>
mov    -0x10(%rbp),%rdi
mov    0x2c8e(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1090 <openlog@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     125a <daemon_init+0xca>
jne    12be <daemon_init+0x12e>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     1275 <daemon_init+0xe5>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1290 <daemon_init+0x100>
jne    12be <daemon_init+0x12e>
lea    0xe4b(%rip),%rdi
call   1030 <chdir@plt>
xor    %ebp,%ebp
xchg   %ax,%ax
mov    %ebp,%edi
call   1040 <close@plt>
add    $0x1,%ebp
cmp    $0x40,%ebp
jne    1200 <daemon_init+0x70>
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
lea    0xe25(%rip),%rbp
mov    %rbp,%rdi
call   1080 <open@plt>
mov    0x2db2(%rip),%rbx
mov    (%rbx),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
mov    (%rbx),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
mov    %r14d,%edx
call   1090 <openlog@plt>
xor    %eax,%eax
jmp    12b3 <daemon_init+0x123>
mov    0x2d47(%rip),%rax
mov    0x2d70(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
lea    0xd8d(%rip),%rsi
jmp    12a9 <daemon_init+0x119>
mov    0x2d2c(%rip),%rax
mov    0x2d55(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
lea    0xd89(%rip),%rsi
jmp    12a9 <daemon_init+0x119>
mov    0x2d11(%rip),%rax
mov    0x2d3a(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
lea    0xd81(%rip),%rsi
call   1070 <syslog@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@LOG_INFO = global i32 0
@LOG_DAEMON = global i32 0
@.str = [23 x i8] c"daemon_init first fork\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [19 x i8] c"daemon_init setsid\00"
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.2 = [24 x i8] c"daemon_init second fork\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@LOG_PID = global i32 0
define i32 @daemon_init(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %7
%9 = load i64, i64* %7
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @LOG_INFO
%13 = load i32, i32* @LOG_DAEMON
%14 = or i32 %12, %13
%15 = call i32 @syslog(i32 %14, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
16: ; preds = %2
%17 = load i64, i64* %7
%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
br label %23
23: ; preds = %22
%24 = call i64 (...) @setsid()
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i32, i32* @LOG_INFO
%28 = load i32, i32* @LOG_DAEMON
%29 = or i32 %27, %28
%30 = call i32 @syslog(i32 %29, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
31: ; preds = %23
%32 = load i32, i32* @SIGHUP
%33 = load i32, i32* @SIG_IGN
%34 = call i32 @signal(i32 %32, i32 %33)
%35 = call i64 (...) @fork()
store i64 %35, i64* %7
%36 = load i64, i64* %7
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %31
%39 = load i32, i32* @LOG_INFO
%40 = load i32, i32* @LOG_DAEMON
%41 = or i32 %39, %40
%42 = call i32 @syslog(i32 %41, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
43: ; preds = %31
%44 = load i64, i64* %7
%45 = icmp sgt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32, i32* @EXIT_SUCCESS
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %43
br label %50
50: ; preds = %49
%51 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %6
br label %52
52: ; preds = %58, %50
%53 = load i32, i32* %6
%54 = icmp slt i32 %53, 64
br i1 %54, label %55, label %61
55: ; preds = %52
%56 = load i32, i32* %6
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %55
%59 = load i32, i32* %6
%60 = add nsw i32 %59, 1
store i32 %60, i32* %6
br label %52
61: ; preds = %52
%62 = load i32, i32* @O_RDONLY
%63 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %62)
%64 = load i32, i32* @O_RDWR
%65 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %64)
%66 = load i32, i32* @O_RDWR
%67 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %66)
%68 = load i8*, i8** %4
%69 = load i32, i32* @LOG_PID
%70 = load i32, i32* %5
%71 = call i32 @openlog(i8* %68, i32 %69, i32 %70)
store i32 0, i32* %3
br label %72
72: ; preds = %61, %38, %26, %11
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@LOG_INFO = global i32 0
@LOG_DAEMON = global i32 0
@.str = [23 x i8] c"daemon_init first fork\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [19 x i8] c"daemon_init setsid\00"
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.2 = [24 x i8] c"daemon_init second fork\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@LOG_PID = global i32 0
define i32 @daemon_init(i8* %0, i32 %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @LOG_INFO
%7 = load i32, i32* @LOG_DAEMON
%8 = or i32 %7, %6
%9 = tail call i32 @syslog(i32 %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %55
10: ; preds = %2
%11 = icmp eq i64 %3, 0
br i1 %11, label %15, label %12
12: ; preds = %10
%13 = load i32, i32* @EXIT_SUCCESS
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %10
%16 = tail call i64 (...) @setsid()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i32, i32* @LOG_INFO
%20 = load i32, i32* @LOG_DAEMON
%21 = or i32 %20, %19
%22 = tail call i32 @syslog(i32 %21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %55
23: ; preds = %15
%24 = load i32, i32* @SIGHUP
%25 = load i32, i32* @SIG_IGN
%26 = tail call i32 @signal(i32 %24, i32 %25)
%27 = tail call i64 (...) @fork()
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %23
%30 = load i32, i32* @LOG_INFO
%31 = load i32, i32* @LOG_DAEMON
%32 = or i32 %31, %30
%33 = tail call i32 @syslog(i32 %32, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %55
34: ; preds = %23
%35 = icmp eq i64 %27, 0
br i1 %35, label %39, label %36
36: ; preds = %34
%37 = load i32, i32* @EXIT_SUCCESS
%38 = tail call i32 @exit(i32 %37)
unreachable
39: ; preds = %34
%40 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %41
%42 = phi i32 [ 0, %39 ], [ %44, %41 ]
%43 = tail call i32 @close(i32 %42)
%44 = add nuw nsw i32 %42, 1
%45 = icmp eq i32 %44, 64
br i1 %45, label %46, label %41
46: ; preds = %41
%47 = load i32, i32* @O_RDONLY
%48 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %47)
%49 = load i32, i32* @O_RDWR
%50 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %49)
%51 = load i32, i32* @O_RDWR
%52 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %51)
%53 = load i32, i32* @LOG_PID
%54 = tail call i32 @openlog(i8* %0, i32 %53, i32 %1)
br label %55
55: ; preds = %46, %29, %18, %5
%56 = phi i32 [ -1, %5 ], [ -1, %18 ], [ -1, %29 ], [ 0, %46 ]
ret i32 %56
}