#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_RDONLY;
int /*<<< orphan*/ O_WRONLY;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int dup(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int daemon(int nochdir, int noclose) {
  if (!nochdir == 0) {
    if (chdir("/"))
      return -1;
  }
  if (!noclose) {
    close(0);
    if (open("/dev/null", O_RDONLY, 0) != 0)
      return -1;
    close(1);
    if (open("/dev/null", O_WRONLY, 0) != 1)
      return -1;
    close(2);
    if (dup(1) != 2)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
cmp    $0x0,%eax
jne    118c <daemon+0x4c>
lea    0xe94(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     1187 <daemon+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <daemon+0xf4>
jmp    118c <daemon+0x4c>
cmpl   $0x0,-0xc(%rbp)
jne    122d <daemon+0xed>
xor    %edi,%edi
call   1050 <close@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
cmp    $0x0,%eax
je     11c9 <daemon+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <daemon+0xf4>
mov    $0x1,%edi
call   1050 <close@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
lea    0xe1f(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
cmp    $0x1,%eax
je     11ff <daemon+0xbf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <daemon+0xf4>
mov    $0x2,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1040 <dup@plt>
cmp    $0x2,%eax
je     1228 <daemon+0xe8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <daemon+0xf4>
jmp    122d <daemon+0xed>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
test   %edi,%edi
je     115f <daemon+0x1f>
lea    0xeb0(%rip),%rdi
call   1030 <chdir@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    11cd <daemon+0x8d>
test   %ebp,%ebp
je     1167 <daemon+0x27>
xor    %ebx,%ebx
jmp    11cd <daemon+0x8d>
xor    %edi,%edi
call   1050 <close@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
jne    11cd <daemon+0x8d>
mov    $0x1,%edi
call   1050 <close@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
cmp    $0x1,%eax
jne    11cd <daemon+0x8d>
mov    $0x2,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1040 <dup@plt>
cmp    $0x2,%eax
je     1163 <daemon+0x23>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
define i32 @daemon(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%6 = load i32, i32* %4
%7 = icmp ne i32 %6, 0
%8 = xor i1 %7, true
%9 = zext i1 %8 to i32
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11
store i32 -1, i32* %3
br label %38
15: ; preds = %11
br label %16
16: ; preds = %15, %2
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %37, label %19
19: ; preds = %16
%20 = call i32 @close(i32 0)
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 0)
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %3
br label %38
25: ; preds = %19
%26 = call i32 @close(i32 1)
%27 = load i32, i32* @O_WRONLY
%28 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %27, i32 0)
%29 = icmp ne i32 %28, 1
br i1 %29, label %30, label %31
30: ; preds = %25
store i32 -1, i32* %3
br label %38
31: ; preds = %25
%32 = call i32 @close(i32 2)
%33 = call i32 @dup(i32 1)
%34 = icmp ne i32 %33, 2
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 -1, i32* %3
br label %38
36: ; preds = %31
br label %37
37: ; preds = %36, %16
store i32 0, i32* %3
br label %38
38: ; preds = %37, %35, %30, %24, %14
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
define i32 @daemon(i32 %0, i32 %1) {
%3 = icmp eq i32 %0, 0
br i1 %3, label %7, label %4
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 %24
7: ; preds = %4, %2
%8 = icmp eq i32 %1, 0
br i1 %8, label %9, label %23
9: ; preds = %7
%10 = tail call i32 @close(i32 0)
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 0)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %9
%15 = tail call i32 @close(i32 1)
%16 = load i32, i32* @O_WRONLY
%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 %19, label %24
19: ; preds = %14
%20 = tail call i32 @close(i32 2)
%21 = tail call i32 @dup(i32 1)
%22 = icmp eq i32 %21, 2
br i1 %22, label %23, label %24
23: ; preds = %19, %7
br label %24
24: ; preds = %19, %14, %9, %4, %23
%25 = phi i32 [ 0, %23 ], [ -1, %4 ], [ -1, %9 ], [ -1, %14 ], [ -1, %19 ]
ret i32 %25
}