#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(int /*<<< orphan*/);
scalar_t__ chdir(char *);
int /*<<< orphan*/ errno;
int fork();
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ log_exit(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void become_daemon(void) {
  int n;
  if (chdir("/") < 0) {
    log_exit("chdir(2) failed: %s", strerror(errno));
  }
  freopen("/dev/null", "r", stdin);
  freopen("/dev/null", "r", stdout);
  freopen("/dev/null", "r", stderr);
  n = fork();
  if (n < 0) {
    log_exit("fork(2) failed: %s", strerror(errno));
  }
  if (n != 0) {
    _exit(0);
  }
  if (setsid() < 0) {
    log_exit("setsid(2) failed: %s", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<become_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe81(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    11aa <become_daemon+0x3a>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe5d(%rip),%rdi
call   1070 <log_exit@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
lea    0xe5c(%rip),%rdi
lea    0xe5f(%rip),%rsi
call   1050 <freopen@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
lea    0xe40(%rip),%rdi
lea    0xe43(%rip),%rsi
call   1050 <freopen@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edx
lea    0xe24(%rip),%rdi
lea    0xe27(%rip),%rsi
call   1050 <freopen@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    122e <become_daemon+0xbe>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xdf9(%rip),%rdi
call   1070 <log_exit@plt>
cmpl   $0x0,-0x4(%rbp)
je     123f <become_daemon+0xcf>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    126c <become_daemon+0xfc>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xdce(%rip),%rdi
call   1070 <log_exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<become_daemon>:
push   %r14
push   %rbx
push   %rax
lea    0xe85(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jns    11a1 <become_daemon+0x31>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe68(%rip),%rdi
mov    %eax,%esi
call   1070 <log_exit@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
lea    0xe65(%rip),%rbx
lea    0xe68(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <freopen@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <freopen@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <freopen@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     122f <become_daemon+0xbf>
jne    124b <become_daemon+0xdb>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     120c <become_daemon+0x9c>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe14(%rip),%rdi
mov    %eax,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <log_exit@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdde(%rip),%rdi
mov    %eax,%esi
call   1070 <log_exit@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [20 x i8] c"chdir(2) failed: %s\00"
@errno = global i32 0
@.str.2 = [10 x i8] c"/dev/null\00"
@.str.3 = [2 x i8] c"r\00"
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@.str.4 = [19 x i8] c"fork(2) failed: %s\00"
@.str.5 = [21 x i8] c"setsid(2) failed: %s\00"
define void @become_daemon() {
%1 = alloca i32
%2 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = load i32, i32* @errno
%6 = call i32 @strerror(i32 %5)
%7 = call i32 @log_exit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %8
8: ; preds = %4, %0
%9 = load i32, i32* @stdin
%10 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @stdout
%12 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @stderr
%14 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = call i32 (...) @fork()
store i32 %15, i32* %1
%16 = load i32, i32* %1
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %8
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @log_exit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %18, %8
%23 = load i32, i32* %1
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @_exit(i32 0)
unreachable
27: ; preds = %22
%28 = call i64 (...) @setsid()
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 @log_exit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %30, %27
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [20 x i8] c"chdir(2) failed: %s\00"
@errno = global i32 0
@.str.2 = [10 x i8] c"/dev/null\00"
@.str.3 = [2 x i8] c"r\00"
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@.str.4 = [19 x i8] c"fork(2) failed: %s\00"
@.str.5 = [21 x i8] c"setsid(2) failed: %s\00"
define void @become_daemon() {
%1 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = load i32, i32* @errno
%5 = tail call i32 @strerror(i32 %4)
%6 = tail call i32 @log_exit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %7
7: ; preds = %3, %0
%8 = load i32, i32* @stdin
%9 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @stdout
%11 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @stderr
%13 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = tail call i32 (...) @fork()
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %7
%17 = load i32, i32* @errno
%18 = tail call i32 @strerror(i32 %17)
%19 = tail call i32 @log_exit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %18)
br label %22
20: ; preds = %7
%21 = icmp eq i32 %14, 0
br i1 %21, label %24, label %22
22: ; preds = %16, %20
%23 = tail call i32 @_exit(i32 0)
unreachable
24: ; preds = %20
%25 = tail call i64 (...) @setsid()
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = tail call i32 @strerror(i32 %28)
%30 = tail call i32 @log_exit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %24
ret void
}