#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*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemon_init(void) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    return -1;
  } else if (pid > 0) {
    exit(0);
  }
  printf("Daemon PID[%d].\n", getpid());
  setsid();
  chdir("/");
  umask(0);
  close(0);
  close(1);
  close(2);
  return 0;
}


x86 assembly (clang, -O0):
<daemon_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11a9 <daemon_init+0x29>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <daemon_init+0x91>
cmpq   $0x0,-0x10(%rbp)
jle    11bb <daemon_init+0x3b>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    11c0 <daemon_init+0x40>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe30(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
lea    0xe2e(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_init>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11dd <daemon_init+0x5d>
jne    11e6 <daemon_init+0x66>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe61(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
lea    0xe5d(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
jmp    11e2 <daemon_init+0x62>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"Daemon PID[%d].\0A\00"
@.str.1 = [2 x i8] c"/\00"
define i32 @daemon_init() {
%1 = alloca i32
%2 = alloca i64
%3 = call i64 (...) @fork()
store i64 %3, i64* %2
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %6
5: ; preds = %0
store i32 -1, i32* %1
br label %21
6: ; preds = %0
%7 = load i64, i64* %2
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %6
br label %12
12: ; preds = %11
%13 = call i32 (...) @getpid()
%14 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 (...) @setsid()
%16 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @umask(i32 0)
%18 = call i32 @close(i32 0)
%19 = call i32 @close(i32 1)
%20 = call i32 @close(i32 2)
store i32 0, i32* %1
br label %21
21: ; preds = %12, %5
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@.str = [17 x i8] c"Daemon PID[%d].\0A\00"
@.str.1 = [2 x i8] c"/\00"
define i32 @daemon_init() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %16, label %3
3: ; preds = %0
%4 = icmp eq i64 %1, 0
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %3
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = tail call i32 (...) @setsid()
%11 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @umask(i32 0)
%13 = tail call i32 @close(i32 0)
%14 = tail call i32 @close(i32 1)
%15 = tail call i32 @close(i32 2)
br label %16
16: ; preds = %0, %7
%17 = phi i32 [ 0, %7 ], [ -1, %0 ]
ret i32 %17
}