#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*/);
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, int);
int fork();
int open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int daemon(int nochdir, int noclose) {
  if (!nochdir && chdir("/"))
    return -1;
  if (!noclose) {
    int fd, failed = 0;
    if ((fd = open("/dev/null", O_RDWR)) < 0)
      return -1;
    if (dup2(fd, 0) < 0 || dup2(fd, 1) < 0 || dup2(fd, 2) < 0)
      failed++;
    if (fd > 2)
      close(fd);
    if (failed)
      return -1;
  }
  switch (fork()) {
  case 0:
    break;
  case -1:
    return -1;
  default:
    _exit(0);
  }
  if (setsid() < 0)
    return -1;
  switch (fork()) {
  case 0:
    break;
  case -1:
    return -1;
  default:
    _exit(0);
  }
  return 0;
}


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    11aa <daemon+0x3a>
lea    0xe71(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     11aa <daemon+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1308 <daemon+0x198>
cmpl   $0x0,-0xc(%rbp)
jne    1260 <daemon+0xf0>
movl   $0x0,-0x14(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    0xe37(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    11e8 <daemon+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    1308 <daemon+0x198>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jl     122a <daemon+0xba>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jl     122a <daemon+0xba>
mov    -0x10(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    1233 <daemon+0xc3>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x10(%rbp)
jle    1245 <daemon+0xd5>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x14(%rbp)
je     125b <daemon+0xeb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1308 <daemon+0x198>
jmp    1260 <daemon+0xf0>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
sub    $0xffffffff,%eax
je     128f <daemon+0x11f>
jmp    127a <daemon+0x10a>
mov    -0x18(%rbp),%eax
test   %eax,%eax
jne    129b <daemon+0x12b>
jmp    128a <daemon+0x11a>
jmp    12a2 <daemon+0x132>
movl   $0xffffffff,-0x4(%rbp)
jmp    1308 <daemon+0x198>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    12bf <daemon+0x14f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1308 <daemon+0x198>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     12ee <daemon+0x17e>
jmp    12d9 <daemon+0x169>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
jne    12fa <daemon+0x18a>
jmp    12e9 <daemon+0x179>
jmp    1301 <daemon+0x191>
movl   $0xffffffff,-0x4(%rbp)
jmp    1308 <daemon+0x198>
xor    %edi,%edi
call   1030 <_exit@plt>
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,%ebp
test   %edi,%edi
je     11c9 <daemon+0x59>
test   %ebp,%ebp
je     11e5 <daemon+0x75>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1267 <daemon+0xf7>
test   %ebx,%ebx
jne    126e <daemon+0xfe>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     1262 <daemon+0xf2>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1267 <daemon+0xf7>
test   %ebx,%ebx
jne    126e <daemon+0xfe>
xor    %ebx,%ebx
jmp    1267 <daemon+0xf7>
lea    0xe30(%rip),%rdi
call   1040 <chdir@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1267 <daemon+0xf7>
jmp    117a <daemon+0xa>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0xe0d(%rip),%rdi
call   1080 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1267 <daemon+0xf7>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     1248 <daemon+0xd8>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     1248 <daemon+0xd8>
mov    %ebp,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    $0x1,%r14b
test   %rax,%rax
js     1248 <daemon+0xd8>
cmp    $0x3,%ebp
jae    1250 <daemon+0xe0>
test   %r14b,%r14b
jne    117e <daemon+0xe>
jmp    1267 <daemon+0xf7>
xor    %r14d,%r14d
cmp    $0x3,%ebp
jb     123d <daemon+0xcd>
mov    %ebp,%edi
call   1060 <close@plt>
test   %r14b,%r14b
jne    117e <daemon+0xe>
jmp    1267 <daemon+0xf7>
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
%7 = alloca i32
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 %14, 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 %65
14: ; preds = %10, %2
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %48, label %17
17: ; preds = %14
store i32 0, i32* %7
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -1, i32* %3
br label %65
22: ; preds = %17
%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 %37
34: ; preds = %30, %26, %22
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
store i32 %36, i32* %7
br label %37
37: ; preds = %34, %30
%38 = load i32, i32* %6
%39 = icmp sgt i32 %38, 2
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %40, %37
%44 = load i32, i32* %7
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 -1, i32* %3
br label %65
47: ; preds = %43
br label %48
48: ; preds = %47, %14
%49 = call i32 (...) @fork()
switch i32 %49, label %52 [
i32 0, label %50
i32 -1, label %51
]
50: ; preds = %48
br label %54
51: ; preds = %48
store i32 -1, i32* %3
br label %65
52: ; preds = %48
%53 = call i32 @_exit(i32 0)
unreachable
54: ; preds = %50
%55 = call i64 (...) @setsid()
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %54
store i32 -1, i32* %3
br label %65
58: ; preds = %54
%59 = call i32 (...) @fork()
switch i32 %59, label %62 [
i32 0, label %60
i32 -1, label %61
]
60: ; preds = %58
br label %64
61: ; preds = %58
store i32 -1, i32* %3
br label %65
62: ; preds = %58
%63 = call i32 @_exit(i32 0)
unreachable
64: ; preds = %60
store i32 0, i32* %3
br label %65
65: ; preds = %64, %61, %57, %51, %46, %21, %13
%66 = load i32, i32* %3
ret i32 %66
}

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 %41
7: ; preds = %4, %2
%8 = icmp eq i32 %1, 0
br i1 %8, label %9, label %29
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 %41, label %13
13: ; preds = %9
%14 = tail call i64 @dup2(i32 %11, i32 0)
%15 = icmp slt i64 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %13
%17 = tail call i64 @dup2(i32 %11, i32 1)
%18 = icmp slt i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = tail call i64 @dup2(i32 %11, i32 2)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19, %16, %13
br label %23
23: ; preds = %22, %19
%24 = phi i1 [ false, %22 ], [ true, %19 ]
%25 = icmp ugt i32 %11, 2
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = tail call i32 @close(i32 %11)
br label %28
28: ; preds = %26, %23
br i1 %24, label %29, label %41
29: ; preds = %28, %7
%30 = tail call i32 (...) @fork()
switch i32 %30, label %31 [
i32 0, label %33
i32 -1, label %41
]
31: ; preds = %29
%32 = tail call i32 @_exit(i32 0)
unreachable
33: ; preds = %29
%34 = tail call i64 (...) @setsid()
%35 = icmp slt i64 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %33
%37 = tail call i32 (...) @fork()
switch i32 %37, label %38 [
i32 0, label %40
i32 -1, label %41
]
38: ; preds = %36
%39 = tail call i32 @_exit(i32 0)
unreachable
40: ; preds = %36
br label %41
41: ; preds = %28, %9, %36, %33, %29, %4, %40
%42 = phi i32 [ 0, %40 ], [ -1, %4 ], [ %30, %29 ], [ -1, %33 ], [ %37, %36 ], [ -1, %9 ], [ -1, %28 ]
ret i32 %42
}