#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*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int setsid();
int daemonize(int nochdir, int noclose) {
  int fd;
  switch (fork()) {
  case -1:
    return -1;
  case 0:
    break;
  default:
    exit(0);
  }
  if (setsid() == -1)
    return -1;
  if (!nochdir)
    chdir("/");
  if (noclose)
    return 0;
  fd = open("/dev/null", O_RDWR, 0);
  if (fd != -1) {
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
    dup2(fd, STDERR_FILENO);
    if (fd > 2)
      close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11a8 <daemonize+0x38>
jmp    1198 <daemonize+0x28>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     11b4 <daemonize+0x44>
jmp    11b9 <daemonize+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <daemonize+0x10d>
jmp    11c0 <daemonize+0x50>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11dc <daemonize+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <daemonize+0x10d>
cmpl   $0x0,-0x8(%rbp)
jne    11f2 <daemonize+0x82>
lea    0xe13(%rip),%rdi
call   1030 <chdir@plt>
cmpl   $0x0,-0xc(%rbp)
je     1208 <daemonize+0x98>
movl   $0x0,-0x4(%rbp)
jmp    127d <daemonize+0x10d>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    0xdea(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     1276 <daemonize+0x106>
mov    -0x10(%rbp),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmpl   $0x2,-0x10(%rbp)
jle    1271 <daemonize+0x101>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
jmp    1276 <daemonize+0x106>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1219 <daemonize+0xa9>
test   %ebx,%ebx
jne    1220 <daemonize+0xb0>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     1214 <daemonize+0xa4>
test   %ebp,%ebp
jne    11af <daemonize+0x3f>
lea    0xe56(%rip),%rdi
call   1030 <chdir@plt>
xor    %ebx,%ebx
test   %r14d,%r14d
jne    1219 <daemonize+0xa9>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xe3c(%rip),%rdi
xor    %ebx,%ebx
xor    %edx,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1219 <daemonize+0xa9>
mov    %eax,%ebp
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
cmp    $0x3,%ebp
jl     1219 <daemonize+0xa9>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1219 <daemonize+0xa9>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1080 <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
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonize(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = call i32 (...) @fork()
switch i32 %7, label %10 [
i32 -1, label %8
i32 0, label %9
]
8: ; preds = %2
store i32 -1, i32* %3
br label %47
9: ; preds = %2
br label %12
10: ; preds = %2
%11 = call i32 @exit(i32 0)
unreachable
12: ; preds = %9
%13 = call i32 (...) @setsid()
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 -1, i32* %3
br label %47
16: ; preds = %12
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 0, i32* %3
br label %47
25: ; preds = %21
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 0)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp ne i32 %28, -1
br i1 %29, label %30, label %46
30: ; preds = %25
%31 = load i32, i32* %6
%32 = load i32, i32* @STDIN_FILENO
%33 = call i32 @dup2(i32 %31, i32 %32)
%34 = load i32, i32* %6
%35 = load i32, i32* @STDOUT_FILENO
%36 = call i32 @dup2(i32 %34, i32 %35)
%37 = load i32, i32* %6
%38 = load i32, i32* @STDERR_FILENO
%39 = call i32 @dup2(i32 %37, i32 %38)
%40 = load i32, i32* %6
%41 = icmp sgt i32 %40, 2
br i1 %41, label %42, label %45
42: ; preds = %30
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %42, %30
br label %46
46: ; preds = %45, %25
store i32 0, i32* %3
br label %47
47: ; preds = %46, %24, %15, %8
%48 = load i32, i32* %3
ret i32 %48
}

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