#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_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void daemonize(void) {
  int fd;
  switch (fork()) {
  case 0:
    break;
  case -1:
    fprintf(stderr, "fork() error: %s\n", strerror(errno));
    exit(0);
    break;
  default:
    _exit(0);
  }
  if (setsid() < 0) {
    fprintf(stderr, "setsid() error: %s\n", strerror(errno));
    exit(0);
  }
  switch (fork()) {
  case 0:
    break;
  case -1:
    fprintf(stderr, "fork() error: %s\n", strerror(errno));
    exit(0);
    break;
  default:
    _exit(0);
  }
  fd = open("/dev/null", O_RDONLY);
  if (fd != 0) {
    dup2(fd, 0);
    close(fd);
  }
  fd = open("/dev/null", O_WRONLY);
  if (fd != 1) {
    dup2(fd, 1);
    close(fd);
  }
  fd = open("/dev/null", O_WRONLY);
  if (fd != 2) {
    dup2(fd, 2);
    close(fd);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
sub    $0xffffffff,%eax
je     11c7 <daemonize+0x37>
jmp    11b2 <daemonize+0x22>
mov    -0x8(%rbp),%eax
test   %eax,%eax
jne    11fa <daemonize+0x6a>
jmp    11c2 <daemonize+0x32>
jmp    1201 <daemonize+0x71>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0xc(%rbp),%edi
mov    %rax,%rdx
lea    0xe12(%rip),%rsi
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    1245 <daemonize+0xb5>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%rdx
lea    0xdd9(%rip),%rsi
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     1274 <daemonize+0xe4>
jmp    125f <daemonize+0xcf>
mov    -0x14(%rbp),%eax
test   %eax,%eax
jne    12a7 <daemonize+0x117>
jmp    126f <daemonize+0xdf>
jmp    12ae <daemonize+0x11e>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xd65(%rip),%rsi
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
lea    0xd68(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     12e2 <daemonize+0x152>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%esi
lea    0xd34(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x1,-0x4(%rbp)
je     1319 <daemonize+0x189>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    0x2ca0(%rip),%rax
mov    (%rax),%esi
lea    0xcfd(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     1350 <daemonize+0x1c0>
mov    -0x4(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    1255 <daemonize+0xc5>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     1281 <daemonize+0xf1>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    1255 <daemonize+0xc5>
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    0xe54(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
je     11ed <daemonize+0x5d>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2dcc(%rip),%r14
mov    (%r14),%esi
lea    0xe28(%rip),%rdi
call   1080 <open@plt>
cmp    $0x1,%eax
je     121d <daemonize+0x8d>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    (%r14),%esi
lea    0xdff(%rip),%rdi
call   1080 <open@plt>
cmp    $0x2,%eax
jne    1239 <daemonize+0xa9>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <close@plt>
cmp    $0xffffffff,%eax
jne    127a <daemonize+0xea>
mov    0x2d77(%rip),%rax
mov    (%rax),%ebx
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd88(%rip),%rsi
jmp    129f <daemonize+0x10f>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd73(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"fork() error: %s\0A\00"
@errno = global i32 0
@.str.1 = [20 x i8] c"setsid() error: %s\0A\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
define void @daemonize() {
%1 = alloca i32
%2 = call i32 (...) @fork()
switch i32 %2, label %10 [
i32 0, label %3
i32 -1, label %4
]
3: ; preds = %0
br label %12
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = call i8* @strerror(i32 %6)
%8 = call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = call i32 @_exit(i32 0)
unreachable
12: ; preds = %3
%13 = call i64 (...) @setsid()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %12
%22 = call i32 (...) @fork()
switch i32 %22, label %30 [
i32 0, label %23
i32 -1, label %24
]
23: ; preds = %21
br label %32
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %21
%31 = call i32 @_exit(i32 0)
unreachable
32: ; preds = %23
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %33)
store i32 %34, i32* %1
%35 = load i32, i32* %1
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %32
%38 = load i32, i32* %1
%39 = call i32 @dup2(i32 %38, i32 0)
%40 = load i32, i32* %1
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %37, %32
%43 = load i32, i32* @O_WRONLY
%44 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %43)
store i32 %44, i32* %1
%45 = load i32, i32* %1
%46 = icmp ne i32 %45, 1
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = load i32, i32* %1
%49 = call i32 @dup2(i32 %48, i32 1)
%50 = load i32, i32* %1
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %47, %42
%53 = load i32, i32* @O_WRONLY
%54 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %53)
store i32 %54, i32* %1
%55 = load i32, i32* %1
%56 = icmp ne i32 %55, 2
br i1 %56, label %57, label %62
57: ; preds = %52
%58 = load i32, i32* %1
%59 = call i32 @dup2(i32 %58, i32 2)
%60 = load i32, i32* %1
%61 = call i32 @close(i32 %60)
br label %62
62: ; preds = %57, %52
ret void
}

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