#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*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int our_daemon(int nochdir, int noclose) {
  pid_t p;
  int fd, i;
  if ((p = fork()) < 0)
    return -1;
  if (p > 0)
    exit(0);
  if ((p = setsid()) < 0)
    return -1;
  if (!nochdir)
    chdir("/");
  if (!noclose && !((fd = open("/dev/null", O_RDWR)) < 0)) {
    for (i = 0; i < 3; i++) {
      dup2(fd, i);
    }
    if (fd > 2)
      close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<our_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    119f <our_daemon+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    125b <our_daemon+0xeb>
cmpq   $0x0,-0x18(%rbp)
jle    11b1 <our_daemon+0x41>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11d2 <our_daemon+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    125b <our_daemon+0xeb>
cmpl   $0x0,-0x8(%rbp)
jne    11e8 <our_daemon+0x78>
lea    0xe1d(%rip),%rdi
call   1030 <chdir@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1254 <our_daemon+0xe4>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    0xe00(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jl     1254 <our_daemon+0xe4>
movl   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x20(%rbp)
jge    123d <our_daemon+0xcd>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <dup2@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    121a <our_daemon+0xaa>
cmpl   $0x2,-0x1c(%rbp)
jle    124f <our_daemon+0xdf>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    1254 <our_daemon+0xe4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<our_daemon>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <fork@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     11af <our_daemon+0x3f>
jne    1202 <our_daemon+0x92>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     11af <our_daemon+0x3f>
test   %ebp,%ebp
jne    11a8 <our_daemon+0x38>
lea    0xe5d(%rip),%rdi
call   1030 <chdir@plt>
xor    %ebx,%ebx
test   %r14d,%r14d
je     11b6 <our_daemon+0x46>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe3c(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11af <our_daemon+0x3f>
mov    %eax,%ebp
xor    %ebx,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
cmp    $0x3,%ebp
jb     11af <our_daemon+0x3f>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11af <our_daemon+0x3f>
xor    %edi,%edi
call   1080 <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 @our_daemon(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %52
12: ; preds = %2
%13 = load i64, i64* %6
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %12
%18 = call i64 (...) @setsid()
store i64 %18, i64* %6
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 -1, i32* %3
br label %52
21: ; preds = %17
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = load i32, i32* %5
%28 = icmp ne i32 %27, 0
br i1 %28, label %51, label %29
29: ; preds = %26
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %30)
store i32 %31, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %51, label %33
33: ; preds = %29
store i32 0, i32* %8
br label %34
34: ; preds = %41, %33
%35 = load i32, i32* %8
%36 = icmp slt i32 %35, 3
br i1 %36, label %37, label %44
37: ; preds = %34
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = call i32 @dup2(i32 %38, i32 %39)
br label %41
41: ; preds = %37
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %34
44: ; preds = %34
%45 = load i32, i32* %7
%46 = icmp sgt i32 %45, 2
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %47, %44
br label %51
51: ; preds = %50, %29, %26
store i32 0, i32* %3
br label %52
52: ; preds = %51, %20, %11
%53 = load i32, i32* %3
ret i32 %53
}

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