#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
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) {
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     1195 <daemonize+0x35>
jmp    1185 <daemonize+0x25>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11b6 <daemonize+0x56>
jmp    11bb <daemonize+0x5b>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <daemonize+0xb5>
jmp    11c2 <daemonize+0x62>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0xe37(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
je     120e <daemonize+0xae>
mov    -0x8(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1191 <daemonize+0x31>
cmp    $0xffffffff,%eax
jne    11df <daemonize+0x7f>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    11d6 <daemonize+0x76>
xor    %eax,%eax
call   1040 <setsid@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d6 <daemonize+0x76>
mov    %eax,%ebp
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   1070 <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
@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 %24
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 ne i32 %14, -1
br i1 %15, label %16, label %23
16: ; preds = %11
%17 = load i32, i32* %2
%18 = load i32, i32* @STDIN_FILENO
%19 = call i32 @dup2(i32 %17, i32 %18)
%20 = load i32, i32* %2
%21 = load i32, i32* @STDOUT_FILENO
%22 = call i32 @dup2(i32 %20, i32 %21)
br label %23
23: ; preds = %16, %11
store i32 0, i32* %1
br label %24
24: ; preds = %23, %5
%25 = load i32, i32* %1
ret i32 %25
}

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
@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 %17
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 %17, label %12
12: ; preds = %7
%13 = load i32, i32* @STDIN_FILENO
%14 = tail call i32 @dup2(i32 %10, i32 %13)
%15 = load i32, i32* @STDOUT_FILENO
%16 = tail call i32 @dup2(i32 %10, i32 %15)
br label %17
17: ; preds = %7, %12, %2
%18 = phi i32 [ -1, %2 ], [ 0, %12 ], [ 0, %7 ]
ret i32 %18
}