#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
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(EXIT_SUCCESS);
  }
  if (setsid() == -1)
    return (-1);
  if (nochdir == 0)
    (void)chdir("/");
  if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
    (void)dup2(fd, STDIN_FILENO);
    (void)dup2(fd, STDOUT_FILENO);
    (void)dup2(fd, STDERR_FILENO);
    if (fd > STDERR_FILENO)
      (void)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    127f <daemon+0x10f>
jmp    11c7 <daemon+0x57>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11e3 <daemon+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <daemon+0x10f>
cmpl   $0x0,-0x8(%rbp)
jne    11f9 <daemon+0x89>
lea    0xe0c(%rip),%rdi
call   1040 <chdir@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1278 <daemon+0x108>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
lea    0xdef(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
je     1278 <daemon+0x108>
mov    -0x10(%rbp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%eax
mov    0x2d6d(%rip),%rcx
cmp    (%rcx),%eax
jle    1273 <daemon+0x103>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
jmp    1278 <daemon+0x108>
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     121a <daemon+0xaa>
test   %ebx,%ebx
jne    1221 <daemon+0xb1>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     1215 <daemon+0xa5>
test   %ebp,%ebp
jne    11af <daemon+0x3f>
lea    0xe56(%rip),%rdi
call   1040 <chdir@plt>
xor    %ebx,%ebx
test   %r14d,%r14d
jne    121a <daemon+0xaa>
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     121a <daemon+0xaa>
mov    %eax,%ebp
mov    0x2de3(%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),%r14
mov    (%r14),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
cmp    (%r14),%ebp
jle    121a <daemon+0xaa>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    121a <daemon+0xaa>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@.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 %47
9: ; preds = %2
br label %13
10: ; preds = %2
%11 = load i32, i32* @EXIT_SUCCESS
%12 = call i32 @_exit(i32 %11)
unreachable
13: ; preds = %9
%14 = call i32 (...) @setsid()
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 -1, i32* %3
br label %47
17: ; preds = %13
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %46
25: ; preds = %22
%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 = icmp ne i32 %27, -1
br i1 %28, label %29, label %46
29: ; preds = %25
%30 = load i32, i32* %6
%31 = load i32, i32* @STDIN_FILENO
%32 = call i32 @dup2(i32 %30, i32 %31)
%33 = load i32, i32* %6
%34 = load i32, i32* @STDOUT_FILENO
%35 = call i32 @dup2(i32 %33, i32 %34)
%36 = load i32, i32* %6
%37 = load i32, i32* @STDERR_FILENO
%38 = call i32 @dup2(i32 %36, i32 %37)
%39 = load i32, i32* %6
%40 = load i32, i32* @STDERR_FILENO
%41 = icmp sgt i32 %39, %40
br i1 %41, label %42, label %45
42: ; preds = %29
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %42, %29
br label %46
46: ; preds = %45, %25, %22
store i32 0, i32* %3
br label %47
47: ; preds = %46, %16, %8
%48 = load i32, i32* %3
ret i32 %48
}

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