#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void start_daemon() {
  int pid, ret;
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  } else if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  ret = setsid();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  } else if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  umask(0);
  ret = chdir("/");
  if (ret < 0) {
    exit(EXIT_FAILURE);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
}


x86 assembly (clang, -O0):
<start_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    118a <start_daemon+0x2a>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    11a2 <start_daemon+0x42>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11a7 <start_daemon+0x47>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11c9 <start_daemon+0x69>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11eb <start_daemon+0x8b>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    1203 <start_daemon+0xa3>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    1208 <start_daemon+0xa8>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xdea(%rip),%rdi
call   1030 <chdir@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1236 <start_daemon+0xd6>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_daemon>:
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
js     11c4 <start_daemon+0x64>
jne    11d2 <start_daemon+0x72>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
js     11c4 <start_daemon+0x64>
jne    11d2 <start_daemon+0x72>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xe70(%rip),%rdi
call   1030 <chdir@plt>
test   %eax,%eax
js     11c4 <start_daemon+0x64>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
pop    %rax
jmp    1040 <close@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

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