#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 int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ logger_stderr(char *, int /*<<< orphan*/,
                                 int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int daemonize(void) {
  int fd;
  pid_t pid;
  switch (pid = fork()) {
  case -1:
    fprintf(stderr, "fork() failed.\n");
    return -1;
  case 0:
    break;
  default:
    exit(0);
  }
  setsid();
  if ((fd = open("/dev/null", O_RDWR, 0)) == -1) {
    logger_stderr("open [/dev/null] failed (%d: %s)", errno, strerror(errno));
    return -1;
  } else {
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11b5 <daemonize+0x35>
jmp    11a5 <daemonize+0x25>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11d6 <daemonize+0x56>
jmp    11db <daemonize+0x5b>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <daemonize+0xec>
jmp    11e2 <daemonize+0x62>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xe17(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1243 <daemonize+0xc3>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xde8(%rip),%rdi
call   1040 <logger_stderr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <daemonize+0xec>
mov    -0x8(%rbp),%edi
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11ae <daemonize+0x2e>
cmp    $0xffffffff,%eax
jne    1225 <daemonize+0xa5>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1217 <daemonize+0x97>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <setsid@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11f7 <daemonize+0x77>
mov    %eax,%ebp
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
jmp    121c <daemonize+0x9c>
mov    0x2dca(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xe0c(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <logger_stderr@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"fork() failed.\0A\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.2 = [33 x i8] c"open [/dev/null] failed (%d: %s)\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @daemonize() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
store i32 %4, i32* %3
switch i32 %4, label %9 [
i32 -1, label %5
i32 0, label %8
]
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %29
8: ; preds = %0
br label %11
9: ; preds = %0
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %8
%12 = call i32 (...) @setsid()
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 0)
store i32 %14, i32* %2
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = load i32, i32* @errno
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @logger_stderr(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %19)
store i32 -1, i32* %1
br label %29
21: ; preds = %11
%22 = load i32, i32* %2
%23 = load i32, i32* @STDIN_FILENO
%24 = call i32 @dup2(i32 %22, i32 %23)
%25 = load i32, i32* %2
%26 = load i32, i32* @STDOUT_FILENO
%27 = call i32 @dup2(i32 %25, i32 %26)
br label %28
28: ; preds = %21
store i32 0, i32* %1
br label %29
29: ; preds = %28, %16, %5
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"fork() failed.\0A\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.2 = [33 x i8] c"open [/dev/null] failed (%d: %s)\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @daemonize() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %5 [
i32 -1, label %2
i32 0, label %7
]
2: ; preds = %0
%3 = load i32, i32* @stderr
%4 = tail call i32 @fprintf(i32 %3, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %21
5: ; preds = %0
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %0
%8 = tail call i32 (...) @setsid()
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 @logger_stderr(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %14)
br label %21
16: ; preds = %7
%17 = load i32, i32* @STDIN_FILENO
%18 = tail call i32 @dup2(i32 %10, i32 %17)
%19 = load i32, i32* @STDOUT_FILENO
%20 = tail call i32 @dup2(i32 %10, i32 %19)
br label %21
21: ; preds = %16, %12, %2
%22 = phi i32 [ -1, %12 ], [ 0, %16 ], [ -1, %2 ]
ret i32 %22
}