#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 /*<<< orphan*/ O_RDWR;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
void daemonize(void) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    perror("fork");
    exit(1);
  } else if (pid != 0)
    exit(0);
  setsid();
  if (chdir("/") < 0) {
    perror("chdir");
    exit(1);
  }
  close(0);
  open("/dev/null", O_RDWR);
  dup2(0, 1);
  dup2(0, 2);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jge    11b3 <daemonize+0x33>
lea    0xe5c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     11c5 <daemonize+0x45>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    11ca <daemonize+0x4a>
mov    $0x0,%al
call   1060 <setsid@plt>
lea    0xe2d(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    11fd <daemonize+0x7d>
lea    0xe19(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
lea    0xdf9(%rip),%rdi
call   1070 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11dc <daemonize+0x5c>
jne    11e5 <daemonize+0x65>
xor    %eax,%eax
call   1060 <setsid@plt>
lea    0xe68(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     11ec <daemonize+0x6c>
xor    %edi,%edi
call   1050 <close@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1070 <open@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
xor    %edi,%edi
mov    $0x2,%esi
pop    %rax
jmp    1040 <dup2@plt>
lea    0xe1d(%rip),%rdi
jmp    11f3 <daemonize+0x73>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xe14(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [6 x i8] c"chdir\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @daemonize() {
%1 = alloca i64
%2 = call i64 (...) @fork()
store i64 %2, i64* %1
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = load i64, i64* %1
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = call i32 @exit(i32 0)
unreachable
12: ; preds = %7
br label %13
13: ; preds = %12
%14 = call i32 (...) @setsid()
%15 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = call i32 @close(i32 0)
%22 = load i32, i32* @O_RDWR
%23 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = call i32 @dup2(i32 0, i32 1)
%25 = call i32 @dup2(i32 0, i32 2)
ret void
}

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