#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 chdir(char *);
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(void) {
  int fr = 0;
  fr = fork();
  if (fr < 0) {
    fprintf(stderr, "fork() failed\n");
    exit(1);
  }
  if (fr > 0) {
    exit(0);
  }
  if (setsid() < 0) {
    fprintf(stderr, "setsid() failed\n");
    exit(1);
  }
  fr = fork();
  if (fr < 0) {
    fprintf(stderr, "fork() failed\n");
    exit(1);
  }
  if (fr > 0) {
    fprintf(stderr, "forked to background (%d)\n", fr);
    exit(0);
  }
  umask(0);
  fr = chdir("/");
  if (fr != 0) {
    fprintf(stderr, "chdir(/) failed\n");
    exit(0);
  }
  close(0);
  close(1);
  close(2);
  open("/dev/null", O_RDWR);
  fr = dup(0);
  fr = dup(0);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11d4 <daemonize+0x44>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    11e5 <daemonize+0x55>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    1217 <daemonize+0x87>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    124c <daemonize+0xbc>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    1277 <daemonize+0xe7>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xdb6(%rip),%rdi
call   1030 <chdir@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     12b5 <daemonize+0x125>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
lea    0xd6e(%rip),%rdi
call   1090 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x4(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x4(%rbp)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1221 <daemonize+0x91>
jne    1233 <daemonize+0xa3>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     123a <daemonize+0xaa>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1221 <daemonize+0x91>
jne    125b <daemonize+0xcb>
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xe66(%rip),%rdi
call   1030 <chdir@plt>
test   %eax,%eax
jne    127b <daemonize+0xeb>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
lea    0xe41(%rip),%rdi
call   1090 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
pop    %rax
jmp    1040 <dup@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
jmp    124a <daemonize+0xba>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d76(%rip),%rcx
mov    (%rcx),%edi
lea    0xdb5(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"fork() failed\0A\00"
@.str.1 = [17 x i8] c"setsid() failed\0A\00"
@.str.2 = [27 x i8] c"forked to background (%d)\0A\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [17 x i8] c"chdir(/) failed\0A\00"
@.str.5 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @daemonize() {
%1 = alloca i32
store i32 0, i32* %1
%2 = call i32 (...) @fork()
store i32 %2, i32* %1
%3 = load i32, i32* %1
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* %1
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %9
%15 = call i64 (...) @setsid()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = call i32 (...) @fork()
store i32 %22, i32* %1
%23 = load i32, i32* %1
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = load i32, i32* %1
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = load i32, i32* %1
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %29
%38 = call i32 @umask(i32 0)
%39 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 %39, i32* %1
%40 = load i32, i32* %1
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %37
%47 = call i32 @close(i32 0)
%48 = call i32 @close(i32 1)
%49 = call i32 @close(i32 2)
%50 = load i32, i32* @O_RDWR
%51 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %50)
%52 = call i32 @dup(i32 0)
store i32 %52, i32* %1
%53 = call i32 @dup(i32 0)
store i32 %53, i32* %1
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"fork() failed\0A\00"
@.str.1 = [17 x i8] c"setsid() failed\0A\00"
@.str.2 = [27 x i8] c"forked to background (%d)\0A\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [17 x i8] c"chdir(/) failed\0A\00"
@.str.5 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @daemonize() {
%1 = tail call i32 (...) @fork()
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = icmp eq i32 %1, 0
br i1 %8, label %11, label %9
9: ; preds = %7
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %7
%12 = tail call i64 (...) @setsid()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = tail call i32 (...) @fork()
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %18
%26 = icmp eq i32 %19, 0
br i1 %26, label %31, label %27
27: ; preds = %25
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%30 = tail call i32 @exit(i32 0)
unreachable
31: ; preds = %25
%32 = tail call i32 @umask(i32 0)
%33 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%34 = icmp eq i32 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%38 = tail call i32 @exit(i32 0)
unreachable
39: ; preds = %31
%40 = tail call i32 @close(i32 0)
%41 = tail call i32 @close(i32 1)
%42 = tail call i32 @close(i32 2)
%43 = load i32, i32* @O_RDWR
%44 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %43)
%45 = tail call i32 @dup(i32 0)
%46 = tail call i32 @dup(i32 0)
ret void
}