#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*/ DetachStdio();
int /*<<< orphan*/ FatalError(char *, int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int setsid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void BecomeDaemon(void) {
  switch (fork()) {
  case -1:
    FatalError("daemon fork failed, %s\n", strerror(errno));
    break;
  case 0:
    break;
  default:
    exit(0);
  }
  if (setsid() == -1)
    FatalError("setting session id for daemon failed: %s\n", strerror(errno));
  chdir("/");
  DetachStdio();
}


x86 assembly (clang, -O0):
<BecomeDaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x4(%rbp)
sub    $0xffffffff,%eax
je     11a2 <BecomeDaemon+0x32>
jmp    1192 <BecomeDaemon+0x22>
mov    -0x4(%rbp),%eax
test   %eax,%eax
je     11c3 <BecomeDaemon+0x53>
jmp    11c8 <BecomeDaemon+0x58>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe47(%rip),%rdi
call   1060 <FatalError@plt>
jmp    11cf <BecomeDaemon+0x5f>
jmp    11cf <BecomeDaemon+0x5f>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11fb <BecomeDaemon+0x8b>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe22(%rip),%rdi
call   1060 <FatalError@plt>
lea    0xe40(%rip),%rdi
call   1040 <chdir@plt>
mov    $0x0,%al
call   1030 <DetachStdio@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BecomeDaemon>:
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     119d <BecomeDaemon+0x2d>
cmp    $0xffffffff,%eax
jne    11d9 <BecomeDaemon+0x69>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe6a(%rip),%rdi
mov    %eax,%esi
call   1060 <FatalError@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11c5 <BecomeDaemon+0x55>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe5a(%rip),%rdi
mov    %eax,%esi
call   1060 <FatalError@plt>
lea    0xe76(%rip),%rdi
call   1040 <chdir@plt>
xor    %eax,%eax
pop    %rcx
jmp    1030 <DetachStdio@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"daemon fork failed, %s\0A\00"
@errno = global i32 0
@.str.1 = [42 x i8] c"setting session id for daemon failed: %s\0A\00"
@.str.2 = [2 x i8] c"/\00"
define void @BecomeDaemon() {
%1 = call i32 (...) @fork()
switch i32 %1, label %7 [
i32 -1, label %2
i32 0, label %6
]
2: ; preds = %0
%3 = load i32, i32* @errno
%4 = call i32 @strerror(i32 %3)
%5 = call i32 @FatalError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %4)
br label %9
6: ; preds = %0
br label %9
7: ; preds = %0
%8 = call i32 @exit(i32 0)
unreachable
9: ; preds = %6, %2
%10 = call i32 (...) @setsid()
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = call i32 @FatalError(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %12, %9
%17 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 (...) @DetachStdio()
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"daemon fork failed, %s\0A\00"
@errno = global i32 0
@.str.1 = [42 x i8] c"setting session id for daemon failed: %s\0A\00"
@.str.2 = [2 x i8] c"/\00"
define void @BecomeDaemon() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %6 [
i32 -1, label %2
i32 0, label %8
]
2: ; preds = %0
%3 = load i32, i32* @errno
%4 = tail call i32 @strerror(i32 %3)
%5 = tail call i32 @FatalError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %4)
br label %8
6: ; preds = %0
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %0, %2
%9 = tail call i32 (...) @setsid()
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* @errno
%13 = tail call i32 @strerror(i32 %12)
%14 = tail call i32 @FatalError(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %11, %8
%16 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 (...) @DetachStdio()
ret void
}