#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_EMERG;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ set_signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int);
void daemon_init(void) {
  pid_t pid;
  int fh;
  pid = fork();
  if (pid == (pid_t)-1) {
    syslog(LOG_EMERG, "daemon_init(): fork() = %s", strerror(errno));
    exit(EXIT_FAILURE);
  } else if (pid != 0)
    exit(EXIT_SUCCESS);
  setsid();
  set_signal(SIGHUP, SIG_IGN);
  pid = fork();
  if (pid == (pid_t)-1) {
    syslog(LOG_EMERG, "daemon_init(): fork(2) = %s", strerror(errno));
    exit(EXIT_FAILURE);
  } else if (pid != 0)
    _exit(EXIT_SUCCESS);
  chdir("/");
  umask(022);
  fh = open("/dev/null", O_RDWR);
  if (fh == -1) {
    syslog(LOG_EMERG, "daemon_init(): open(/dev/null) = %s", strerror(errno));
    exit(EXIT_FAILURE);
  }
  assert(fh > 2);
  dup2(fh, STDIN_FILENO);
  dup2(fh, STDOUT_FILENO);
  dup2(fh, STDERR_FILENO);
  close(fh);
}


x86 assembly (clang, -O0):
<daemon_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0xffffffffffffffff,-0x8(%rbp)
jne    1227 <daemon_init+0x57>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%edx
lea    0xdec(%rip),%rsi
call   10b0 <syslog@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     1240 <daemon_init+0x70>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    1245 <daemon_init+0x75>
mov    $0x0,%al
call   1080 <setsid@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
call   1040 <set_signal@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0xffffffffffffffff,-0x8(%rbp)
jne    12b2 <daemon_init+0xe2>
mov    0x2d50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
lea    0xd7c(%rip),%rsi
call   10b0 <syslog@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     12cb <daemon_init+0xfb>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    12d0 <daemon_init+0x100>
lea    0xd60(%rip),%rdi
call   1050 <chdir@plt>
mov    $0x12,%edi
call   10a0 <umask@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
lea    0xd43(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1341 <daemon_init+0x171>
mov    0x2cc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xd15(%rip),%rsi
call   10b0 <syslog@plt>
mov    0x2c6e(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpl   $0x2,-0xc(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0xc(%rbp),%edi
mov    0x2c3c(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2c7b(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2c32(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_init>:
push   %rbx
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
jne    128b <daemon_init+0xbb>
xor    %eax,%eax
call   1080 <setsid@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
call   1040 <set_signal@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
jne    12b1 <daemon_init+0xe1>
lea    0xe21(%rip),%rdi
call   1050 <chdir@plt>
mov    $0x12,%edi
call   10a0 <umask@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
lea    0xe04(%rip),%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     12d7 <daemon_init+0x107>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0x3,%eax
setge  %dil
call   1090 <assert@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <dup2@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <dup2@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <dup2@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1070 <close@plt>
cmp    $0xffffffffffffffff,%rax
jne    130c <daemon_init+0x13c>
mov    0x2d38(%rip),%rax
mov    (%rax),%ebx
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd51(%rip),%rsi
jmp    12f5 <daemon_init+0x125>
cmp    $0xffffffffffffffff,%rax
jne    131a <daemon_init+0x14a>
mov    0x2d12(%rip),%rax
mov    (%rax),%ebx
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd46(%rip),%rsi
jmp    12f5 <daemon_init+0x125>
mov    0x2cf2(%rip),%rax
mov    (%rax),%ebx
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd4e(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   10b0 <syslog@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@LOG_EMERG = global i32 0
@.str = [27 x i8] c"daemon_init(): fork() = %s\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.1 = [28 x i8] c"daemon_init(): fork(2) = %s\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.4 = [36 x i8] c"daemon_init(): open(/dev/null) = %s\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @daemon_init() {
%1 = alloca i64
%2 = alloca i32
%3 = call i64 (...) @fork()
store i64 %3, i64* %1
%4 = load i64, i64* %1
%5 = icmp eq i64 %4, -1
br i1 %5, label %6, label %13
6: ; preds = %0
%7 = load i32, i32* @LOG_EMERG
%8 = load i32, i32* @errno
%9 = call i32 @strerror(i32 %8)
%10 = call i32 @syslog(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = load i64, i64* %1
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @EXIT_SUCCESS
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %13
br label %20
20: ; preds = %19
%21 = call i32 (...) @setsid()
%22 = load i32, i32* @SIGHUP
%23 = load i32, i32* @SIG_IGN
%24 = call i32 @set_signal(i32 %22, i32 %23)
%25 = call i64 (...) @fork()
store i64 %25, i64* %1
%26 = load i64, i64* %1
%27 = icmp eq i64 %26, -1
br i1 %27, label %28, label %35
28: ; preds = %20
%29 = load i32, i32* @LOG_EMERG
%30 = load i32, i32* @errno
%31 = call i32 @strerror(i32 %30)
%32 = call i32 @syslog(i32 %29, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %20
%36 = load i64, i64* %1
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* @EXIT_SUCCESS
%40 = call i32 @_exit(i32 %39)
unreachable
41: ; preds = %35
br label %42
42: ; preds = %41
%43 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @umask(i32 18)
%45 = load i32, i32* @O_RDWR
%46 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %45)
store i32 %46, i32* %2
%47 = load i32, i32* %2
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %56
49: ; preds = %42
%50 = load i32, i32* @LOG_EMERG
%51 = load i32, i32* @errno
%52 = call i32 @strerror(i32 %51)
%53 = call i32 @syslog(i32 %50, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %52)
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %42
%57 = load i32, i32* %2
%58 = icmp sgt i32 %57, 2
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
%61 = load i32, i32* %2
%62 = load i32, i32* @STDIN_FILENO
%63 = call i32 @dup2(i32 %61, i32 %62)
%64 = load i32, i32* %2
%65 = load i32, i32* @STDOUT_FILENO
%66 = call i32 @dup2(i32 %64, i32 %65)
%67 = load i32, i32* %2
%68 = load i32, i32* @STDERR_FILENO
%69 = call i32 @dup2(i32 %67, i32 %68)
%70 = load i32, i32* %2
%71 = call i32 @close(i32 %70)
ret void
}

LLVM IR (-O2):
@LOG_EMERG = global i32 0
@.str = [27 x i8] c"daemon_init(): fork() = %s\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.1 = [28 x i8] c"daemon_init(): fork(2) = %s\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.4 = [36 x i8] c"daemon_init(): open(/dev/null) = %s\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @daemon_init() {
%1 = tail call i64 (...) @fork()
switch i64 %1, label %9 [
i64 -1, label %2
i64 0, label %12
]
2: ; preds = %0
%3 = load i32, i32* @LOG_EMERG
%4 = load i32, i32* @errno
%5 = tail call i32 @strerror(i32 %4)
%6 = tail call i32 @syslog(i32 %3, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = load i32, i32* @EXIT_SUCCESS
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = tail call i32 (...) @setsid()
%14 = load i32, i32* @SIGHUP
%15 = load i32, i32* @SIG_IGN
%16 = tail call i32 @set_signal(i32 %14, i32 %15)
%17 = tail call i64 (...) @fork()
switch i64 %17, label %25 [
i64 -1, label %18
i64 0, label %28
]
18: ; preds = %12
%19 = load i32, i32* @LOG_EMERG
%20 = load i32, i32* @errno
%21 = tail call i32 @strerror(i32 %20)
%22 = tail call i32 @syslog(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %12
%26 = load i32, i32* @EXIT_SUCCESS
%27 = tail call i32 @_exit(i32 %26)
unreachable
28: ; preds = %12
%29 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @umask(i32 18)
%31 = load i32, i32* @O_RDWR
%32 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %41
34: ; preds = %28
%35 = load i32, i32* @LOG_EMERG
%36 = load i32, i32* @errno
%37 = tail call i32 @strerror(i32 %36)
%38 = tail call i32 @syslog(i32 %35, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = tail call i32 @exit(i32 %39)
unreachable
41: ; preds = %28
%42 = icmp sgt i32 %32, 2
%43 = zext i1 %42 to i32
%44 = tail call i32 @assert(i32 %43)
%45 = load i32, i32* @STDIN_FILENO
%46 = tail call i32 @dup2(i32 %32, i32 %45)
%47 = load i32, i32* @STDOUT_FILENO
%48 = tail call i32 @dup2(i32 %32, i32 %47)
%49 = load i32, i32* @STDERR_FILENO
%50 = tail call i32 @dup2(i32 %32, i32 %49)
%51 = tail call i32 @close(i32 %32)
ret void
}