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


x86 assembly (clang, -O0):
<daemon>:
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 <daemon+0x38>
jmp    1198 <daemon+0x28>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     11b4 <daemon+0x44>
jmp    11b9 <daemon+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1289 <daemon+0x119>
jmp    11c0 <daemon+0x50>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11dc <daemon+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1289 <daemon+0x119>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%eax
je     11f3 <daemon+0x83>
xor    %edi,%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1209 <daemon+0x99>
lea    0xdfc(%rip),%rdi
call   1040 <chdir@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1282 <daemon+0x112>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
je     1282 <daemon+0x112>
mov    -0x10(%rbp),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmpl   $0x2,-0x10(%rbp)
jle    127d <daemon+0x10d>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
jmp    1282 <daemon+0x112>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon>:
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     122e <daemon+0xbe>
test   %ebx,%ebx
jne    1235 <daemon+0xc5>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     1229 <daemon+0xb9>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    1235 <daemon+0xc5>
test   %ebp,%ebp
jne    11c2 <daemon+0x52>
lea    0xe43(%rip),%rdi
call   1040 <chdir@plt>
xor    %ebx,%ebx
test   %r14d,%r14d
jne    122e <daemon+0xbe>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     122e <daemon+0xbe>
mov    %eax,%ebp
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
cmp    $0x3,%ebp
jl     122e <daemon+0xbe>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    122e <daemon+0xbe>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@_PATH_DEVNULL = global i32 0
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemon(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 %51
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 %51
16: ; preds = %12
%17 = call i32 (...) @fork()
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @_exit(i32 0)
unreachable
21: ; preds = %16
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = load i32, i32* %5
%28 = icmp ne i32 %27, 0
br i1 %28, label %50, label %29
29: ; preds = %26
%30 = load i32, i32* @_PATH_DEVNULL
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open(i32 %30, i32 %31, i32 0)
store i32 %32, i32* %6
%33 = icmp ne i32 %32, -1
br i1 %33, label %34, label %50
34: ; preds = %29
%35 = load i32, i32* %6
%36 = load i32, i32* @STDIN_FILENO
%37 = call i32 @dup2(i32 %35, i32 %36)
%38 = load i32, i32* %6
%39 = load i32, i32* @STDOUT_FILENO
%40 = call i32 @dup2(i32 %38, i32 %39)
%41 = load i32, i32* %6
%42 = load i32, i32* @STDERR_FILENO
%43 = call i32 @dup2(i32 %41, i32 %42)
%44 = load i32, i32* %6
%45 = icmp sgt i32 %44, 2
br i1 %45, label %46, label %49
46: ; preds = %34
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %46, %34
br label %50
50: ; preds = %49, %29, %26
store i32 0, i32* %3
br label %51
51: ; preds = %50, %15, %8
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@_PATH_DEVNULL = global i32 0
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemon(i32 %0, i32 %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %4 [
i32 -1, label %35
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 %35, label %9
9: ; preds = %6
%10 = tail call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @_exit(i32 0)
unreachable
14: ; preds = %9
%15 = icmp eq i32 %0, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
%19 = icmp eq i32 %1, 0
br i1 %19, label %20, label %35
20: ; preds = %18
%21 = load i32, i32* @_PATH_DEVNULL
%22 = load i32, i32* @O_RDWR
%23 = tail call i32 @open(i32 %21, i32 %22, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %35, label %25
25: ; preds = %20
%26 = load i32, i32* @STDIN_FILENO
%27 = tail call i32 @dup2(i32 %23, i32 %26)
%28 = load i32, i32* @STDOUT_FILENO
%29 = tail call i32 @dup2(i32 %23, i32 %28)
%30 = load i32, i32* @STDERR_FILENO
%31 = tail call i32 @dup2(i32 %23, i32 %30)
%32 = icmp sgt i32 %23, 2
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = tail call i32 @close(i32 %23)
br label %35
35: ; preds = %18, %20, %33, %25, %6, %2
%36 = phi i32 [ %3, %2 ], [ -1, %6 ], [ 0, %25 ], [ 0, %33 ], [ 0, %20 ], [ 0, %18 ]
ret i32 %36
}