#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*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int fork();
int open(char *, 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 (!nochdir)
    chdir("/");
  if (!noclose && (fd = open("/dev/null", 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    1270 <daemon+0x100>
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    1270 <daemon+0x100>
cmpl   $0x0,-0x8(%rbp)
jne    11f2 <daemon+0x82>
lea    0xe13(%rip),%rdi
call   1040 <chdir@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1269 <daemon+0xf9>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
lea    0xdf6(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
je     1269 <daemon+0xf9>
mov    -0x10(%rbp),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmpl   $0x2,-0x10(%rbp)
jle    1264 <daemon+0xf4>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
jmp    1269 <daemon+0xf9>
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     1219 <daemon+0xa9>
test   %ebx,%ebx
jne    1220 <daemon+0xb0>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     1214 <daemon+0xa4>
test   %ebp,%ebp
jne    11af <daemon+0x3f>
lea    0xe56(%rip),%rdi
call   1040 <chdir@plt>
xor    %ebx,%ebx
test   %r14d,%r14d
jne    1219 <daemon+0xa9>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xe3c(%rip),%rdi
xor    %ebx,%ebx
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1219 <daemon+0xa9>
mov    %eax,%ebp
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
cmp    $0x3,%ebp
jl     1219 <daemon+0xa9>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1219 <daemon+0xa9>
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"
@.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 @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 %45
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 %45
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 %44, label %24
24: ; preds = %21
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %25, i32 0)
store i32 %26, i32* %6
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %44
28: ; preds = %24
%29 = load i32, i32* %6
%30 = load i32, i32* @STDIN_FILENO
%31 = call i32 @dup2(i32 %29, i32 %30)
%32 = load i32, i32* %6
%33 = load i32, i32* @STDOUT_FILENO
%34 = call i32 @dup2(i32 %32, i32 %33)
%35 = load i32, i32* %6
%36 = load i32, i32* @STDERR_FILENO
%37 = call i32 @dup2(i32 %35, i32 %36)
%38 = load i32, i32* %6
%39 = icmp sgt i32 %38, 2
br i1 %39, label %40, label %43
40: ; preds = %28
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %40, %28
br label %44
44: ; preds = %43, %24, %21
store i32 0, i32* %3
br label %45
45: ; preds = %44, %15, %8
%46 = load i32, i32* %3
ret i32 %46
}

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 @daemon(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 = %13, %15, %27, %19, %6, %2
%30 = phi i32 [ %3, %2 ], [ -1, %6 ], [ 0, %19 ], [ 0, %27 ], [ 0, %15 ], [ 0, %13 ]
ret i32 %30
}