#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*/ O_RDONLY;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemon_start(void) {
  if (fork())
    exit(0);
  chdir("/");
  umask(0);
  close(0);
  close(1);
  close(2);
  open("/", O_RDONLY);
  dup2(0, 1);
  dup2(0, 2);
  setsid();
}


x86 assembly (clang, -O0):
<daemon_start>:
push   %rbp
mov    %rsp,%rbp
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
je     119c <daemon_start+0x1c>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xe5d(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1070 <umask@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
lea    0xe26(%rip),%rdi
call   1080 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_start>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jne    11ef <daemon_start+0x6f>
lea    0xe6c(%rip),%rbx
mov    %rbx,%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1070 <umask@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
xor    %eax,%eax
pop    %rbx
jmp    1060 <setsid@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
define void @daemon_start() {
%1 = call i64 (...) @fork()
%2 = icmp ne i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = call i32 @exit(i32 0)
unreachable
5: ; preds = %0
%6 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @umask(i32 0)
%8 = call i32 @close(i32 0)
%9 = call i32 @close(i32 1)
%10 = call i32 @close(i32 2)
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i32 @dup2(i32 0, i32 1)
%14 = call i32 @dup2(i32 0, i32 2)
%15 = call i32 (...) @setsid()
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
define void @daemon_start() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %5, label %3
3: ; preds = %0
%4 = tail call i32 @exit(i32 0)
unreachable
5: ; preds = %0
%6 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @umask(i32 0)
%8 = tail call i32 @close(i32 0)
%9 = tail call i32 @close(i32 1)
%10 = tail call i32 @close(i32 2)
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = tail call i32 @dup2(i32 0, i32 1)
%14 = tail call i32 @dup2(i32 0, i32 2)
%15 = tail call i32 (...) @setsid()
ret void
}