#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*/ LogError(char *, int /*<<< orphan*/);
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ _SysErrorMsg(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int setsid();
void BecomeDaemon(void) {
  switch (fork()) {
  case -1:
    LogError("daemon fork failed, %s\n", _SysErrorMsg(errno));
    exit(1);
    break;
  case 0:
    break;
  default:
    exit(0);
  }
  if (setsid() == -1) {
    LogError("setting session id for daemon failed: %s\n", _SysErrorMsg(errno));
    exit(1);
  }
  chdir("/");
  close(0);
  close(1);
  close(2);
  (void)open("/dev/null", O_RDWR);
  (void)dup2(0, 1);
  (void)dup2(0, 2);
}


x86 assembly (clang, -O0):
<BecomeDaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x4(%rbp)
sub    $0xffffffff,%eax
je     11c2 <BecomeDaemon+0x32>
jmp    11b2 <BecomeDaemon+0x22>
mov    -0x4(%rbp),%eax
test   %eax,%eax
je     11e8 <BecomeDaemon+0x58>
jmp    11ed <BecomeDaemon+0x5d>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1080 <_SysErrorMsg@plt>
mov    %eax,%esi
lea    0xe27(%rip),%rdi
call   1070 <LogError@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11f4 <BecomeDaemon+0x64>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    122a <BecomeDaemon+0x9a>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1080 <_SysErrorMsg@plt>
mov    %eax,%esi
lea    0xdfd(%rip),%rdi
call   1070 <LogError@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe11(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
lea    0xde3(%rip),%rdi
call   1090 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BecomeDaemon>:
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    11fd <BecomeDaemon+0x6d>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     1219 <BecomeDaemon+0x89>
lea    0xe93(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe65(%rip),%rdi
call   1090 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
pop    %rax
jmp    1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    123f <BecomeDaemon+0xaf>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1080 <_SysErrorMsg@plt>
lea    0xde9(%rip),%rdi
jmp    122e <BecomeDaemon+0x9e>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1080 <_SysErrorMsg@plt>
lea    0xdea(%rip),%rdi
mov    %eax,%esi
call   1070 <LogError@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <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"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @BecomeDaemon() {
%1 = call i32 (...) @fork()
switch i32 %1, label %8 [
i32 -1, label %2
i32 0, label %7
]
2: ; preds = %0
%3 = load i32, i32* @errno
%4 = call i32 @_SysErrorMsg(i32 %3)
%5 = call i32 @LogError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32 @exit(i32 1)
unreachable
7: ; preds = %0
br label %10
8: ; preds = %0
%9 = call i32 @exit(i32 0)
unreachable
10: ; preds = %7
%11 = call i32 (...) @setsid()
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = load i32, i32* @errno
%15 = call i32 @_SysErrorMsg(i32 %14)
%16 = call i32 @LogError(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @close(i32 0)
%21 = call i32 @close(i32 1)
%22 = call i32 @close(i32 2)
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = call i32 @dup2(i32 0, i32 1)
%26 = call i32 @dup2(i32 0, i32 2)
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"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @BecomeDaemon() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %7 [
i32 -1, label %2
i32 0, label %9
]
2: ; preds = %0
%3 = load i32, i32* @errno
%4 = tail call i32 @_SysErrorMsg(i32 %3)
%5 = tail call i32 @LogError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %0
%10 = tail call i32 (...) @setsid()
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = tail call i32 @_SysErrorMsg(i32 %13)
%15 = tail call i32 @LogError(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @close(i32 0)
%20 = tail call i32 @close(i32 1)
%21 = tail call i32 @close(i32 2)
%22 = load i32, i32* @O_RDWR
%23 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = tail call i32 @dup2(i32 0, i32 1)
%25 = tail call i32 @dup2(i32 0, i32 2)
ret void
}