#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*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ exit(int);
int fork();
int getdtablesize();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsid();
int /*<<< orphan*/ umask(int);
int daemonize(void) {
  int pid, i;
  switch (fork()) {
  case -1:
    perror("fork()");
    exit(1);
  case 0:
    if ((pid = setsid()) < 0) {
      perror("setsid()");
      exit(1);
    }
    for (i = getdtablesize(); i >= 0; --i)
      close(i);
    i = open("/dev/null", O_RDWR);
    dup(i);
    dup(i);
    umask(027);
    chdir("/");
    return pid;
  default:
    exit(0);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
sub    $0xffffffff,%eax
je     11d2 <daemonize+0x32>
jmp    11c2 <daemonize+0x22>
mov    -0xc(%rbp),%eax
test   %eax,%eax
je     11e8 <daemonize+0x48>
jmp    1282 <daemonize+0xe2>
lea    0xe27(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jge    1211 <daemonize+0x71>
lea    0xe05(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <getdtablesize@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     123b <daemonize+0x9b>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    121b <daemonize+0x7b>
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
lea    0xdc5(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <dup@plt>
mov    -0x8(%rbp),%edi
call   1040 <dup@plt>
mov    $0x17,%edi
call   1070 <umask@plt>
lea    0xda6(%rip),%rdi
call   1030 <chdir@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    1220 <daemonize+0x80>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %eax,%eax
js     122e <daemonize+0x8e>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <getdtablesize@plt>
test   %eax,%eax
js     11dc <daemonize+0x3c>
mov    %eax,%ebp
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1050 <close@plt>
add    $0xffffffff,%ebp
jb     11d0 <daemonize+0x30>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    0xe24(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <dup@plt>
mov    %ebp,%edi
call   1040 <dup@plt>
mov    $0x17,%edi
call   1070 <umask@plt>
lea    0xe08(%rip),%rdi
call   1030 <chdir@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
cmp    $0xffffffff,%eax
jne    1244 <daemonize+0xa4>
lea    0xdd4(%rip),%rdi
jmp    1235 <daemonize+0x95>
lea    0xdd2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

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

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