#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, int);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
int setsid();
int daemon(int nochdir, int noclose) {
  int nullfd;
  pid_t f;
  if (!nochdir) {
    if (chdir("/"))
      return -1;
  }
  if (!noclose) {
    if ((nullfd = open("/dev/null", O_RDWR)) < 0 || dup2(nullfd, 0) < 0 ||
        dup2(nullfd, 1) < 0 || dup2(nullfd, 2) < 0)
      return -1;
    close(nullfd);
  }
  f = fork();
  if (f < 0)
    return -1;
  else if (f > 0)
    _exit(0);
  return setsid();
}


x86 assembly (clang, -O0):
<daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11af <daemon+0x3f>
lea    0xe71(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     11aa <daemon+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <daemon+0x103>
jmp    11af <daemon+0x3f>
cmpl   $0x0,-0xc(%rbp)
jne    1230 <daemon+0xc0>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    0xe39(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jl     121c <daemon+0xac>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jl     121c <daemon+0xac>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jl     121c <daemon+0xac>
mov    -0x10(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    1228 <daemon+0xb8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <daemon+0x103>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1252 <daemon+0xe2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <daemon+0x103>
cmpq   $0x0,-0x18(%rbp)
jle    1264 <daemon+0xf4>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    1269 <daemon+0xf9>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon>:
push   %rbx
mov    %esi,%ebx
test   %edi,%edi
je     1191 <daemon+0x21>
test   %ebx,%ebx
je     11a4 <daemon+0x34>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11f8 <daemon+0x88>
jne    11ff <daemon+0x8f>
xor    %eax,%eax
pop    %rbx
jmp    1070 <setsid@plt>
lea    0xe68(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    11f8 <daemon+0x88>
jmp    1177 <daemon+0x7>
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11f8 <daemon+0x88>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     11f8 <daemon+0x88>
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     11f8 <daemon+0x88>
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     11f8 <daemon+0x88>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    117b <daemon+0xb>
mov    $0xffffffff,%eax
pop    %rbx
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
%7 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %2
%11 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %10
store i32 -1, i32* %3
br label %51
14: ; preds = %10
br label %15
15: ; preds = %14, %2
%16 = load i32, i32* %5
%17 = icmp ne i32 %16, 0
br i1 %17, label %38, label %18
18: ; preds = %15
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %18
%23 = load i32, i32* %6
%24 = call i64 @dup2(i32 %23, i32 0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %34, label %26
26: ; preds = %22
%27 = load i32, i32* %6
%28 = call i64 @dup2(i32 %27, i32 1)
%29 = icmp slt i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i32, i32* %6
%32 = call i64 @dup2(i32 %31, i32 2)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30, %26, %22, %18
store i32 -1, i32* %3
br label %51
35: ; preds = %30
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %35, %15
%39 = call i64 (...) @fork()
store i64 %39, i64* %7
%40 = load i64, i64* %7
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
store i32 -1, i32* %3
br label %51
43: ; preds = %38
%44 = load i64, i64* %7
%45 = icmp sgt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @_exit(i32 0)
unreachable
48: ; preds = %43
br label %49
49: ; preds = %48
%50 = call i32 (...) @setsid()
store i32 %50, i32* %3
br label %51
51: ; preds = %49, %42, %34, %13
%52 = load i32, i32* %3
ret i32 %52
}

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