#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ die(char *, ...);
scalar_t__ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fd_cloexec(int);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int setsid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void become_daemon(void) {
  pid_t pid;
  int nullfd = open("/dev/null", O_RDWR, 0);
  if (nullfd < 0)
    die("cannot daemonize, unable to open '/dev/null': $E\n");
  fd_cloexec(nullfd);
  if (dup2(nullfd, STDIN_FILENO) < 0)
    die("cannot daemonize, unable to redirect stdin: %s\n", (strerror(errno)));
  if (dup2(nullfd, STDOUT_FILENO) < 0)
    die("cannot daemonize, unable to redirect stdout: %s\n", (strerror(errno)));
  if (dup2(nullfd, STDERR_FILENO) < 0)
    die("cannot daemonize, unable to redirect stderr: %s\n", (strerror(errno)));
  pid = fork();
  if (pid < 0)
    die("cannot daemonize: %s\n", (strerror(errno)));
  if (pid > 0)
    _exit(EXIT_SUCCESS);
  if (setsid() == -1)
    _exit(111);
}


x86 assembly (clang, -O0):
<become_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ba <become_daemon+0x3a>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
mov    -0xc(%rbp),%edi
call   1060 <fd_cloexec@plt>
mov    -0xc(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    11fb <become_daemon+0x7b>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
mov    -0xc(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    1234 <become_daemon+0xb4>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%esi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
mov    -0xc(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    126d <become_daemon+0xed>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%esi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    12a1 <become_daemon+0x121>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%esi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
cmpq   $0x0,-0x8(%rbp)
jle    12ba <become_daemon+0x13a>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0xffffffff,%eax
jne    12d4 <become_daemon+0x154>
mov    $0x6f,%edi
call   1030 <_exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<become_daemon>:
push   %rbx
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ac <become_daemon+0x2c>
lea    0xe65(%rip),%rdi
xor    %eax,%eax
call   1080 <die@plt>
mov    %ebx,%edi
call   1060 <fd_cloexec@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
test   %rax,%rax
jns    11e6 <become_daemon+0x66>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe5f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <die@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
test   %rax,%rax
jns    1219 <become_daemon+0x99>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe5c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <die@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
test   %rax,%rax
jns    124c <become_daemon+0xcc>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe5a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <die@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1268 <become_daemon+0xe8>
je     1286 <become_daemon+0x106>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe51(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <die@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
cmp    $0xffffffff,%eax
je     1294 <become_daemon+0x114>
pop    %rbx
ret
mov    $0x6f,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.1 = [50 x i8] c"cannot daemonize, unable to open '/dev/null': $E\0A\00"
@STDIN_FILENO = global i32 0
@.str.2 = [48 x i8] c"cannot daemonize, unable to redirect stdin: %s\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@.str.3 = [49 x i8] c"cannot daemonize, unable to redirect stdout: %s\0A\00"
@STDERR_FILENO = global i32 0
@.str.4 = [49 x i8] c"cannot daemonize, unable to redirect stderr: %s\0A\00"
@.str.5 = [22 x i8] c"cannot daemonize: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define void @become_daemon() {
%1 = alloca i64
%2 = alloca i32
%3 = load i32, i32* @O_RDWR
%4 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3, i32 0)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = load i32, i32* %2
%11 = call i32 @fd_cloexec(i32 %10)
%12 = load i32, i32* %2
%13 = load i32, i32* @STDIN_FILENO
%14 = call i64 @dup2(i32 %12, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %9
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %16, %9
%21 = load i32, i32* %2
%22 = load i32, i32* @STDOUT_FILENO
%23 = call i64 @dup2(i32 %21, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %25, %20
%30 = load i32, i32* %2
%31 = load i32, i32* @STDERR_FILENO
%32 = call i64 @dup2(i32 %30, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i32, i32* @errno
%36 = call i32 @strerror(i32 %35)
%37 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %34, %29
%39 = call i64 (...) @fork()
store i64 %39, i64* %1
%40 = load i64, i64* %1
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i32, i32* @errno
%44 = call i32 @strerror(i32 %43)
%45 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %42, %38
%47 = load i64, i64* %1
%48 = icmp sgt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* @EXIT_SUCCESS
%51 = call i32 @_exit(i32 %50)
unreachable
52: ; preds = %46
%53 = call i32 (...) @setsid()
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 @_exit(i32 111)
unreachable
57: ; preds = %52
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.1 = [50 x i8] c"cannot daemonize, unable to open '/dev/null': $E\0A\00"
@STDIN_FILENO = global i32 0
@.str.2 = [48 x i8] c"cannot daemonize, unable to redirect stdin: %s\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@.str.3 = [49 x i8] c"cannot daemonize, unable to redirect stdout: %s\0A\00"
@STDERR_FILENO = global i32 0
@.str.4 = [49 x i8] c"cannot daemonize, unable to redirect stderr: %s\0A\00"
@.str.5 = [22 x i8] c"cannot daemonize: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define void @become_daemon() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1, i32 0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = tail call i32 @fd_cloexec(i32 %2)
%8 = load i32, i32* @STDIN_FILENO
%9 = tail call i64 @dup2(i32 %2, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %6
%12 = load i32, i32* @errno
%13 = tail call i32 @strerror(i32 %12)
%14 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %11, %6
%16 = load i32, i32* @STDOUT_FILENO
%17 = tail call i64 @dup2(i32 %2, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @errno
%21 = tail call i32 @strerror(i32 %20)
%22 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %19, %15
%24 = load i32, i32* @STDERR_FILENO
%25 = tail call i64 @dup2(i32 %2, i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @errno
%29 = tail call i32 @strerror(i32 %28)
%30 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %23
%32 = tail call i64 (...) @fork()
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i32, i32* @errno
%36 = tail call i32 @strerror(i32 %35)
%37 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %36)
br label %43
38: ; preds = %31
%39 = icmp eq i64 %32, 0
br i1 %39, label %43, label %40
40: ; preds = %38
%41 = load i32, i32* @EXIT_SUCCESS
%42 = tail call i32 @_exit(i32 %41)
unreachable
43: ; preds = %34, %38
%44 = tail call i32 (...) @setsid()
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = tail call i32 @_exit(i32 111)
unreachable
48: ; preds = %43
ret void
}