#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_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_PID;
int NOFILE;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ ignore_signals();
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void init_daemon(void) {
  pid_t pid;
  int i;
  ignore_signals();
  pid = fork();
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  } else if (pid < 0) {
    perror("can not fork(), error:");
    exit(EXIT_FAILURE);
  }
  if (setsid() < 0) {
    perror("can not setsid(), error:");
    exit(EXIT_FAILURE);
  }
  pid = fork();
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  } else if (pid < 0) {
    perror("can not fork(), error:");
    exit(EXIT_FAILURE);
  }
  for (i = 0; i < NOFILE; i++) {
    close(i);
  }
  chdir("/tmp");
  umask(0);
  openlog("KB_PM", LOG_PID, LOG_DAEMON);
}


x86 assembly (clang, -O0):
<init_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <ignore_signals@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jle    11c3 <init_daemon+0x33>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jge    11e8 <init_daemon+0x58>
lea    0xe2b(%rip),%rdi
call   1080 <perror@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    11ed <init_daemon+0x5d>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    1218 <init_daemon+0x88>
lea    0xe12(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jle    123c <init_daemon+0xac>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jge    1261 <init_daemon+0xd1>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1266 <init_daemon+0xd6>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2d41(%rip),%rcx
cmp    (%rcx),%eax
jge    1295 <init_daemon+0x105>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    126d <init_daemon+0xdd>
lea    0xd94(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
mov    0x2d30(%rip),%rax
mov    (%rax),%edx
lea    0xd74(%rip),%rdi
call   1090 <openlog@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_daemon>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <ignore_signals@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jg     1227 <init_daemon+0x97>
js     1235 <init_daemon+0xa5>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     123e <init_daemon+0xae>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jg     1227 <init_daemon+0x97>
js     1235 <init_daemon+0xa5>
mov    0x2de2(%rip),%r14
cmpl   $0x0,(%r14)
jle    11ef <init_daemon+0x5f>
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    (%r14),%ebx
jl     11e0 <init_daemon+0x50>
lea    0xe3a(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
lea    0xe1a(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1090 <openlog@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdc4(%rip),%rdi
jmp    1245 <init_daemon+0xb5>
lea    0xdd2(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@.str = [23 x i8] c"can not fork(), error:\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [25 x i8] c"can not setsid(), error:\00"
@NOFILE = global i32 0
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [6 x i8] c"KB_PM\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
define void @init_daemon() {
%1 = alloca i64
%2 = alloca i32
%3 = call i32 (...) @ignore_signals()
%4 = call i64 (...) @fork()
store i64 %4, i64* %1
%5 = load i64, i64* %1
%6 = icmp sgt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @EXIT_SUCCESS
%9 = call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = load i64, i64* %1
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
br label %18
18: ; preds = %17
%19 = call i64 (...) @setsid()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %18
%26 = call i64 (...) @fork()
store i64 %26, i64* %1
%27 = load i64, i64* %1
%28 = icmp sgt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* @EXIT_SUCCESS
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %25
%33 = load i64, i64* %1
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %32
br label %40
40: ; preds = %39
store i32 0, i32* %2
br label %41
41: ; preds = %48, %40
%42 = load i32, i32* %2
%43 = load i32, i32* @NOFILE
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = load i32, i32* %2
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %45
%49 = load i32, i32* %2
%50 = add nsw i32 %49, 1
store i32 %50, i32* %2
br label %41
51: ; preds = %41
%52 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%53 = call i32 @umask(i32 0)
%54 = load i32, i32* @LOG_PID
%55 = load i32, i32* @LOG_DAEMON
%56 = call i32 @openlog(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %54, i32 %55)
ret void
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@.str = [23 x i8] c"can not fork(), error:\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [25 x i8] c"can not setsid(), error:\00"
@NOFILE = global i32 0
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [6 x i8] c"KB_PM\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
define void @init_daemon() {
%1 = tail call i32 (...) @ignore_signals()
%2 = tail call i64 (...) @fork()
%3 = icmp sgt i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = load i32, i32* @EXIT_SUCCESS
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = icmp slt i64 %2, 0
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %7
%14 = tail call i64 (...) @setsid()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %13
%21 = tail call i64 (...) @fork()
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @EXIT_SUCCESS
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %20
%27 = icmp slt i64 %21, 0
br i1 %27, label %31, label %28
28: ; preds = %26
%29 = load i32, i32* @NOFILE
%30 = icmp sgt i32 %29, 0
br i1 %30, label %35, label %41
31: ; preds = %26
%32 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %28, %35
%36 = phi i32 [ %38, %35 ], [ 0, %28 ]
%37 = tail call i32 @close(i32 %36)
%38 = add nuw nsw i32 %36, 1
%39 = load i32, i32* @NOFILE
%40 = icmp slt i32 %38, %39
br i1 %40, label %35, label %41
41: ; preds = %35, %28
%42 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%43 = tail call i32 @umask(i32 0)
%44 = load i32, i32* @LOG_PID
%45 = load i32, i32* @LOG_DAEMON
%46 = tail call i32 @openlog(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %44, i32 %45)
ret void
}