#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*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ openlog(char const *, int /*<<< orphan*/, int);
scalar_t__ setsid();
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemon_init(const char *pname, int facility) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    return -1;
  } else if (pid != 0) {
    exit(0);
  }
  umask(0);
  if (setsid() < 0) {
    return -1;
  }
  if (chdir("/") < 0) {
    return -1;
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  openlog(pname, LOG_PID, facility);
  syslog(LOG_DEBUG, "started");
  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)
cmpq   $0x0,-0x20(%rbp)
jge    11b1 <daemon_init+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <daemon_init+0xe9>
cmpq   $0x0,-0x20(%rbp)
je     11c3 <daemon_init+0x43>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    11c8 <daemon_init+0x48>
xor    %edi,%edi
call   1060 <umask@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jge    11ec <daemon_init+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <daemon_init+0xe9>
lea    0xe0d(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    120e <daemon_init+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <daemon_init+0xe9>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1080 <openlog@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
call   1070 <syslog@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_init>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %eax,%eax
call   10a0 <fork@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     121a <daemon_init+0x9a>
jne    1221 <daemon_init+0xa1>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     121a <daemon_init+0x9a>
lea    0xe45(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     121a <daemon_init+0x9a>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1080 <openlog@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   1070 <syslog@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@LOG_PID = global i32 0
@LOG_DEBUG = global i32 0
@.str.1 = [8 x i8] c"started\00"
define i32 @daemon_init(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = load i64, i64* %6
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %39
11: ; preds = %2
%12 = load i64, i64* %6
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %11
br label %17
17: ; preds = %16
%18 = call i32 @umask(i32 0)
%19 = call i64 (...) @setsid()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -1, i32* %3
br label %39
22: ; preds = %17
%23 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 -1, i32* %3
br label %39
26: ; preds = %22
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @STDOUT_FILENO
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* @STDERR_FILENO
%32 = call i32 @close(i32 %31)
%33 = load i8*, i8** %4
%34 = load i32, i32* @LOG_PID
%35 = load i32, i32* %5
%36 = call i32 @openlog(i8* %33, i32 %34, i32 %35)
%37 = load i32, i32* @LOG_DEBUG
%38 = call i32 @syslog(i32 %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %39
39: ; preds = %26, %25, %21, %10
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@LOG_PID = global i32 0
@LOG_DEBUG = global i32 0
@.str.1 = [8 x i8] c"started\00"
define i32 @daemon_init(i8* %0, i32 %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %27, label %5
5: ; preds = %2
%6 = icmp eq i64 %3, 0
br i1 %6, label %9, label %7
7: ; preds = %5
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %5
%10 = tail call i32 @umask(i32 0)
%11 = tail call i64 (...) @setsid()
%12 = icmp slt i64 %11, 0
br i1 %12, label %27, label %13
13: ; preds = %9
%14 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp slt i64 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %13
%17 = load i32, i32* @STDIN_FILENO
%18 = tail call i32 @close(i32 %17)
%19 = load i32, i32* @STDOUT_FILENO
%20 = tail call i32 @close(i32 %19)
%21 = load i32, i32* @STDERR_FILENO
%22 = tail call i32 @close(i32 %21)
%23 = load i32, i32* @LOG_PID
%24 = tail call i32 @openlog(i8* %0, i32 %23, i32 %1)
%25 = load i32, i32* @LOG_DEBUG
%26 = tail call i32 @syslog(i32 %25, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %13, %9, %2, %16
%28 = phi i32 [ 0, %16 ], [ -1, %2 ], [ -1, %9 ], [ -1, %13 ]
ret i32 %28
}