#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 /*<<< 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);
  }
  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   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1189 <daemon_init+0x29>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <daemon_init+0x7c>
cmpq   $0x0,-0x10(%rbp)
je     119b <daemon_init+0x3b>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    11a0 <daemon_init+0x40>
mov    $0x0,%al
call   1050 <setsid@plt>
lea    0xe52(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <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   1080 <fork@plt>
test   %rax,%rax
js     11a8 <daemon_init+0x48>
jne    11b1 <daemon_init+0x51>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <setsid@plt>
lea    0xe81(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
jmp    11ad <daemon_init+0x4d>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [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 %19
6: ; preds = %0
%7 = load i64, i64* %2
%8 = icmp ne 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 (...) @setsid()
%14 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @umask(i32 0)
%16 = call i32 @close(i32 0)
%17 = call i32 @close(i32 1)
%18 = call i32 @close(i32 2)
store i32 0, i32* %1
br label %19
19: ; preds = %12, %5
%20 = load i32, i32* %1
ret i32 %20
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i32 @daemon_init() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %14, 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 (...) @setsid()
%9 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @umask(i32 0)
%11 = tail call i32 @close(i32 0)
%12 = tail call i32 @close(i32 1)
%13 = tail call i32 @close(i32 2)
br label %14
14: ; preds = %0, %7
%15 = phi i32 [ 0, %7 ], [ -1, %0 ]
ret i32 %15
}