#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
void daemonize(void) {
  switch (fork()) {
  case -1:
    perror("fork");
    exit(1);
  case 0:
    close(0);
    close(1);
    close(2);
    setsid();
    open("/dev/null", O_RDWR);
    dup(0);
    dup(0);
    break;
  default:
    exit(0);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x4(%rbp)
sub    $0xffffffff,%eax
je     11a2 <daemonize+0x32>
jmp    1192 <daemonize+0x22>
mov    -0x4(%rbp),%eax
test   %eax,%eax
je     11b8 <daemonize+0x48>
jmp    1202 <daemonize+0x92>
lea    0xe57(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    0xe1b(%rip),%rdi
call   1060 <open@plt>
xor    %edi,%edi
call   1030 <dup@plt>
xor    %edi,%edi
call   1030 <dup@plt>
jmp    1209 <daemonize+0x99>
xor    %edi,%edi
call   1080 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    11c2 <daemonize+0x52>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0xe57(%rip),%rdi
call   1060 <open@plt>
xor    %edi,%edi
call   1030 <dup@plt>
xor    %edi,%edi
pop    %rax
jmp    1030 <dup@plt>
cmp    $0xffffffff,%eax
jne    11dd <daemonize+0x6d>
lea    0xe32(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @daemonize() {
%1 = call i32 (...) @fork()
switch i32 %1, label %14 [
i32 -1, label %2
i32 0, label %5
]
2: ; preds = %0
%3 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @exit(i32 1)
unreachable
5: ; preds = %0
%6 = call i32 @close(i32 0)
%7 = call i32 @close(i32 1)
%8 = call i32 @close(i32 2)
%9 = call i32 (...) @setsid()
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = call i32 @dup(i32 0)
%13 = call i32 @dup(i32 0)
br label %16
14: ; preds = %0
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %5
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @daemonize() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %14 [
i32 -1, label %2
i32 0, label %5
]
2: ; preds = %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @exit(i32 1)
unreachable
5: ; preds = %0
%6 = tail call i32 @close(i32 0)
%7 = tail call i32 @close(i32 1)
%8 = tail call i32 @close(i32 2)
%9 = tail call i32 (...) @setsid()
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = tail call i32 @dup(i32 0)
%13 = tail call i32 @dup(i32 0)
ret void
14: ; preds = %0
%15 = tail call i32 @exit(i32 0)
unreachable
}