#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*/ LOG_PID;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ openlog(char const *, int /*<<< orphan*/, int);
int setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int daemon_init(const char *pname, int facility) {
  int i;
  pid_t pid;
  if ((pid = fork()) < 0)
    return -1;
  else if (pid != 0)
    _exit(EXIT_SUCCESS);
  if (setsid() == -1)
    return -1;
  signal(SIGHUP, SIG_IGN);
  if ((pid = fork()) < 0)
    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(pname, 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   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    11b0 <daemon_init+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <daemon_init+0x14e>
cmpq   $0x0,-0x20(%rbp)
je     11c9 <daemon_init+0x49>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    11ce <daemon_init+0x4e>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11ea <daemon_init+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <daemon_init+0x14e>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    1222 <daemon_init+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <daemon_init+0x14e>
cmpq   $0x0,-0x20(%rbp)
je     123b <daemon_init+0xbb>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    1240 <daemon_init+0xc0>
lea    0xdb9(%rip),%rdi
call   1040 <chdir@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x40,-0x18(%rbp)
jge    1273 <daemon_init+0xf3>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1253 <daemon_init+0xd3>
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
lea    0xd7f(%rip),%rdi
call   1080 <open@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
lea    0xd6a(%rip),%rdi
call   1080 <open@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
lea    0xd55(%rip),%rdi
call   1080 <open@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d03(%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   10a0 <fork@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     1248 <daemon_init+0xc8>
jne    1255 <daemon_init+0xd5>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     1248 <daemon_init+0xc8>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1248 <daemon_init+0xc8>
jne    1255 <daemon_init+0xd5>
lea    0xe1c(%rip),%rdi
call   1040 <chdir@plt>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x1,%ebp
cmp    $0x40,%ebp
jne    11f0 <daemon_init+0x70>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0xdf3(%rip),%rbp
mov    %rbp,%rdi
call   1080 <open@plt>
mov    0x2dc2(%rip),%rbx
mov    (%rbx),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
mov    (%rbx),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
mov    %r14d,%edx
call   1090 <openlog@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [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 = icmp slt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %58
11: ; preds = %2
%12 = load i64, i64* %7
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EXIT_SUCCESS
%16 = call i32 @_exit(i32 %15)
unreachable
17: ; preds = %11
br label %18
18: ; preds = %17
%19 = call i32 (...) @setsid()
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 -1, i32* %3
br label %58
22: ; preds = %18
%23 = load i32, i32* @SIGHUP
%24 = load i32, i32* @SIG_IGN
%25 = call i32 @signal(i32 %23, i32 %24)
%26 = call i64 (...) @fork()
store i64 %26, i64* %7
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
store i32 -1, i32* %3
br label %58
29: ; preds = %22
%30 = load i64, i64* %7
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @_exit(i32 %33)
unreachable
35: ; preds = %29
br label %36
36: ; preds = %35
%37 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %38
38: ; preds = %44, %36
%39 = load i32, i32* %6
%40 = icmp slt i32 %39, 64
br i1 %40, label %41, label %47
41: ; preds = %38
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
br label %44
44: ; preds = %41
%45 = load i32, i32* %6
%46 = add nsw i32 %45, 1
store i32 %46, i32* %6
br label %38
47: ; preds = %38
%48 = load i32, i32* @O_RDONLY
%49 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %48)
%50 = load i32, i32* @O_RDWR
%51 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %50)
%52 = load i32, i32* @O_RDWR
%53 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %52)
%54 = load i8*, i8** %4
%55 = load i32, i32* @LOG_PID
%56 = load i32, i32* %5
%57 = call i32 @openlog(i8* %54, i32 %55, i32 %56)
store i32 0, i32* %3
br label %58
58: ; preds = %47, %28, %21, %10
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [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 %40, label %5
5: ; preds = %2
%6 = icmp eq i64 %3, 0
br i1 %6, label %10, label %7
7: ; preds = %5
%8 = load i32, i32* @EXIT_SUCCESS
%9 = tail call i32 @_exit(i32 %8)
unreachable
10: ; preds = %5
%11 = tail call i32 (...) @setsid()
%12 = icmp eq i32 %11, -1
br i1 %12, label %40, label %13
13: ; preds = %10
%14 = load i32, i32* @SIGHUP
%15 = load i32, i32* @SIG_IGN
%16 = tail call i32 @signal(i32 %14, i32 %15)
%17 = tail call i64 (...) @fork()
%18 = icmp slt i64 %17, 0
br i1 %18, label %40, label %19
19: ; preds = %13
%20 = icmp eq i64 %17, 0
br i1 %20, label %24, label %21
21: ; preds = %19
%22 = load i32, i32* @EXIT_SUCCESS
%23 = tail call i32 @_exit(i32 %22)
unreachable
24: ; preds = %19
%25 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %26
%27 = phi i32 [ 0, %24 ], [ %29, %26 ]
%28 = tail call i32 @close(i32 %27)
%29 = add nuw nsw i32 %27, 1
%30 = icmp eq i32 %29, 64
br i1 %30, label %31, label %26
31: ; preds = %26
%32 = load i32, i32* @O_RDONLY
%33 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i32, i32* @O_RDWR
%35 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = load i32, i32* @O_RDWR
%37 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @LOG_PID
%39 = tail call i32 @openlog(i8* %0, i32 %38, i32 %1)
br label %40
40: ; preds = %13, %10, %2, %31
%41 = phi i32 [ 0, %31 ], [ -1, %2 ], [ -1, %10 ], [ -1, %13 ]
ret i32 %41
}