#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*/ _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(0);
  }
  if (setsid() == -1)
    return (-1);
  if (!nochdir)
    (void)chdir("/");
  if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
    (void)dup2(fd, 0);
    (void)dup2(fd, 1);
    (void)dup2(fd, 2);
    if (fd > 2)
      (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    1261 <daemon+0xf1>
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    1261 <daemon+0xf1>
cmpl   $0x0,-0x8(%rbp)
jne    11f2 <daemon+0x82>
lea    0xe13(%rip),%rdi
call   1040 <chdir@plt>
cmpl   $0x0,-0xc(%rbp)
jne    125a <daemon+0xea>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
lea    0xdf6(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
je     125a <daemon+0xea>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
cmpl   $0x2,-0x10(%rbp)
jle    1255 <daemon+0xe5>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
jmp    125a <daemon+0xea>
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     120c <daemon+0x9c>
test   %ebx,%ebx
jne    1213 <daemon+0xa3>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     1207 <daemon+0x97>
test   %ebp,%ebp
jne    11af <daemon+0x3f>
lea    0xe56(%rip),%rdi
call   1040 <chdir@plt>
xor    %ebx,%ebx
test   %r14d,%r14d
jne    120c <daemon+0x9c>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe3c(%rip),%rdi
xor    %ebx,%ebx
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     120c <daemon+0x9c>
mov    %eax,%ebp
xor    %ebx,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
cmp    $0x3,%ebp
jl     120c <daemon+0x9c>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    120c <daemon+0x9c>
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
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 %42
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 %42
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 %41, 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 %41
28: ; preds = %24
%29 = load i32, i32* %6
%30 = call i32 @dup2(i32 %29, i32 0)
%31 = load i32, i32* %6
%32 = call i32 @dup2(i32 %31, i32 1)
%33 = load i32, i32* %6
%34 = call i32 @dup2(i32 %33, i32 2)
%35 = load i32, i32* %6
%36 = icmp sgt i32 %35, 2
br i1 %36, label %37, label %40
37: ; preds = %28
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %37, %28
br label %41
41: ; preds = %40, %24, %21
store i32 0, i32* %3
br label %42
42: ; preds = %41, %15, %8
%43 = load i32, i32* %3
ret i32 %43
}

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 @daemon(i32 %0, i32 %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %4 [
i32 -1, label %26
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 %26, 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 %26
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 %26, label %19
19: ; preds = %15
%20 = tail call i32 @dup2(i32 %17, i32 0)
%21 = tail call i32 @dup2(i32 %17, i32 1)
%22 = tail call i32 @dup2(i32 %17, i32 2)
%23 = icmp sgt i32 %17, 2
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = tail call i32 @close(i32 %17)
br label %26
26: ; preds = %13, %15, %24, %19, %6, %2
%27 = phi i32 [ %3, %2 ], [ -1, %6 ], [ 0, %19 ], [ 0, %24 ], [ 0, %15 ], [ 0, %13 ]
ret i32 %27
}