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


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     11af <daemonize+0x3f>
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a8 <daemonize+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <daemonize+0x12d>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffffffffffff,%rax
jne    11cc <daemonize+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <daemonize+0x12d>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     11fc <daemonize+0x8c>
cmpl   $0xffffffff,-0x8(%rbp)
jne    11f5 <daemonize+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <daemonize+0x12d>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xdfd(%rip),%rdi
call   1040 <chdir@plt>
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
xor    %edi,%edi
call   1060 <close@plt>
mov    %eax,-0x10(%rbp)
mov    $0x1,%edi
call   1060 <close@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    $0x2,%edi
call   1060 <close@plt>
mov    %eax,%ecx
mov    -0xc(%rbp),%eax
or     %ecx,%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
lea    0xdaf(%rip),%rdi
call   1080 <open@plt>
cmp    $0x0,%rax
jne    128c <daemonize+0x11c>
xor    %edi,%edi
call   1050 <dup@plt>
cmp    $0xffffffff,%eax
je     128c <daemonize+0x11c>
xor    %edi,%edi
call   1050 <dup@plt>
cmp    $0xffffffff,%eax
je     128c <daemonize+0x11c>
cmpl   $0x0,-0x8(%rbp)
je     1296 <daemonize+0x126>
mov    $0x1,%edi
call   1030 <_exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     123d <daemonize+0xcd>
test   %ebx,%ebx
jne    1248 <daemonize+0xd8>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffffffffffff,%rax
je     1238 <daemonize+0xc8>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     123d <daemonize+0xcd>
test   %ebx,%ebx
jne    1248 <daemonize+0xd8>
lea    0xe3c(%rip),%rdi
call   1040 <chdir@plt>
mov    %eax,%r14d
xor    %edi,%edi
call   1060 <close@plt>
mov    %eax,%r15d
mov    $0x1,%edi
call   1060 <close@plt>
mov    %eax,%ebp
mov    $0x2,%edi
call   1060 <close@plt>
mov    %eax,%r12d
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    0xe03(%rip),%rdi
call   1080 <open@plt>
test   %rax,%rax
jne    122e <daemonize+0xbe>
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <dup@plt>
cmp    $0xffffffff,%eax
je     122e <daemonize+0xbe>
xor    %edi,%edi
call   1050 <dup@plt>
cmp    $0xffffffff,%eax
je     122e <daemonize+0xbe>
or     %r14d,%r15d
or     %r12d,%ebp
or     %r15d,%ebp
je     123d <daemonize+0xcd>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1030 <_exit@plt>

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

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