#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*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ chdir(char *);
scalar_t__ close(int);
int /*<<< orphan*/ closeall(int /*<<< orphan*/);
scalar_t__ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ error(char *);
int fork();
int open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int daemon(int nochdir, int noclose) {
  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);
  }
  if (!nochdir && chdir("/") < 0) {
    error("chdir(/): %m");
    return -1;
  }
  if (!noclose) {
    closeall(0);
    if (open("/dev/null", O_RDWR) != 0)
      error("Unable to open /dev/null on stdin: %m");
    dup2(0, STDOUT_FILENO);
    dup2(0, STDERR_FILENO);
  } else {
    int devnull = open("/dev/null", O_RDWR);
    if (devnull < 0)
      error("Unable to open /dev/null: %m");
    if (dup2(devnull, STDIN_FILENO) < 0)
      error("Unable to dup /dev/null onto stdin: %m");
    if (dup2(devnull, STDOUT_FILENO) < 0)
      error("Unable to dup /dev/null onto stdout: %m");
    if (dup2(devnull, STDERR_FILENO) < 0)
      error("Unable to dup /dev/null onto stderr: %m");
    if (close(devnull) < 0)
      error("Unable to close /dev/null: %m");
  }
  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   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11cd <daemon+0x3d>
jmp    11b8 <daemon+0x28>
mov    -0x14(%rbp),%eax
test   %eax,%eax
jne    11d9 <daemon+0x49>
jmp    11c8 <daemon+0x38>
jmp    11e0 <daemon+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <daemon+0x214>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    11fd <daemon+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <daemon+0x214>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
sub    $0xffffffff,%eax
je     122c <daemon+0x9c>
jmp    1217 <daemon+0x87>
mov    -0x18(%rbp),%eax
test   %eax,%eax
jne    1238 <daemon+0xa8>
jmp    1227 <daemon+0x97>
jmp    123f <daemon+0xaf>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <daemon+0x214>
xor    %edi,%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1277 <daemon+0xe7>
lea    0xdb0(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1277 <daemon+0xe7>
lea    0xd9c(%rip),%rdi
call   1090 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <daemon+0x214>
cmpl   $0x0,-0xc(%rbp)
jne    12d7 <daemon+0x147>
xor    %edi,%edi
call   1080 <closeall@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
lea    0xd77(%rip),%rdi
call   10a0 <open@plt>
cmp    $0x0,%eax
je     12b2 <daemon+0x122>
lea    0xd6c(%rip),%rdi
call   1090 <error@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1050 <dup2@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1050 <dup2@plt>
jmp    139d <daemon+0x20d>
mov    0x2d02(%rip),%rax
mov    (%rax),%esi
lea    0xd28(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1305 <daemon+0x175>
lea    0xd3f(%rip),%rdi
call   1090 <error@plt>
mov    -0x10(%rbp),%edi
mov    0x2cb9(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    132c <daemon+0x19c>
lea    0xd35(%rip),%rdi
call   1090 <error@plt>
mov    -0x10(%rbp),%edi
mov    0x2cba(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    1353 <daemon+0x1c3>
lea    0xd35(%rip),%rdi
call   1090 <error@plt>
mov    -0x10(%rbp),%edi
mov    0x2c73(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    137a <daemon+0x1ea>
lea    0xd36(%rip),%rdi
call   1090 <error@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    1398 <daemon+0x208>
lea    0xd40(%rip),%rdi
call   1090 <error@plt>
jmp    139d <daemon+0x20d>
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   10b0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     130b <daemon+0x17b>
test   %ebx,%ebx
jne    1312 <daemon+0x182>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     130b <daemon+0x17b>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     130b <daemon+0x17b>
test   %ebx,%ebx
jne    1312 <daemon+0x182>
test   %ebp,%ebp
jne    11fb <daemon+0x6b>
lea    0xe13(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     12fa <daemon+0x16a>
test   %r14d,%r14d
je     12aa <daemon+0x11a>
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
lea    0xdfb(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    122b <daemon+0x9b>
lea    0xe19(%rip),%rdi
call   1090 <error@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
test   %rax,%rax
jns    124c <daemon+0xbc>
lea    0xe15(%rip),%rdi
call   1090 <error@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
test   %rax,%rax
jns    126d <daemon+0xdd>
lea    0xe1b(%rip),%rdi
call   1090 <error@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
test   %rax,%rax
jns    128e <daemon+0xfe>
lea    0xe22(%rip),%rdi
call   1090 <error@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %ebx,%ebx
test   %rax,%rax
jns    130b <daemon+0x17b>
lea    0xe30(%rip),%rdi
call   1090 <error@plt>
jmp    130b <daemon+0x17b>
xor    %ebx,%ebx
xor    %edi,%edi
call   1080 <closeall@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
lea    0xd4c(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
je     12d8 <daemon+0x148>
lea    0xd46(%rip),%rdi
call   1090 <error@plt>
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1050 <dup2@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1050 <dup2@plt>
jmp    130b <daemon+0x17b>
lea    0xd01(%rip),%rdi
call   1090 <error@plt>
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 = [13 x i8] c"chdir(/): %m\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.3 = [38 x i8] c"Unable to open /dev/null on stdin: %m\00"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.4 = [29 x i8] c"Unable to open /dev/null: %m\00"
@STDIN_FILENO = global i32 0
@.str.5 = [39 x i8] c"Unable to dup /dev/null onto stdin: %m\00"
@.str.6 = [40 x i8] c"Unable to dup /dev/null onto stdout: %m\00"
@.str.7 = [40 x i8] c"Unable to dup /dev/null onto stderr: %m\00"
@.str.8 = [30 x i8] c"Unable to close /dev/null: %m\00"
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 0, label %8
i32 -1, label %9
]
8: ; preds = %2
br label %12
9: ; preds = %2
store i32 -1, i32* %3
br label %81
10: ; preds = %2
%11 = call i32 @_exit(i32 0)
unreachable
12: ; preds = %8
%13 = call i64 (...) @setsid()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 -1, i32* %3
br label %81
16: ; preds = %12
%17 = call i32 (...) @fork()
switch i32 %17, label %20 [
i32 0, label %18
i32 -1, label %19
]
18: ; preds = %16
br label %22
19: ; preds = %16
store i32 -1, i32* %3
br label %81
20: ; preds = %16
%21 = call i32 @_exit(i32 0)
unreachable
22: ; preds = %18
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %22
%26 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %81
30: ; preds = %25, %22
%31 = load i32, i32* %5
%32 = icmp ne i32 %31, 0
br i1 %32, label %45, label %33
33: ; preds = %30
%34 = call i32 @closeall(i32 0)
%35 = load i32, i32* @O_RDWR
%36 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %33
%41 = load i32, i32* @STDOUT_FILENO
%42 = call i64 @dup2(i32 0, i32 %41)
%43 = load i32, i32* @STDERR_FILENO
%44 = call i64 @dup2(i32 0, i32 %43)
br label %80
45: ; preds = %30
%46 = load i32, i32* @O_RDWR
%47 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %46)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %45
%53 = load i32, i32* %6
%54 = load i32, i32* @STDIN_FILENO
%55 = call i64 @dup2(i32 %53, i32 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
br label %59
59: ; preds = %57, %52
%60 = load i32, i32* %6
%61 = load i32, i32* @STDOUT_FILENO
%62 = call i64 @dup2(i32 %60, i32 %61)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %59
%65 = call i32 @error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
br label %66
66: ; preds = %64, %59
%67 = load i32, i32* %6
%68 = load i32, i32* @STDERR_FILENO
%69 = call i64 @dup2(i32 %67, i32 %68)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %66
%72 = call i32 @error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0))
br label %73
73: ; preds = %71, %66
%74 = load i32, i32* %6
%75 = call i64 @close(i32 %74)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %73
%78 = call i32 @error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0))
br label %79
79: ; preds = %77, %73
br label %80
80: ; preds = %79, %40
store i32 0, i32* %3
br label %81
81: ; preds = %80, %28, %19, %15, %9
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [13 x i8] c"chdir(/): %m\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.3 = [38 x i8] c"Unable to open /dev/null on stdin: %m\00"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.4 = [29 x i8] c"Unable to open /dev/null: %m\00"
@STDIN_FILENO = global i32 0
@.str.5 = [39 x i8] c"Unable to dup /dev/null onto stdin: %m\00"
@.str.6 = [40 x i8] c"Unable to dup /dev/null onto stdout: %m\00"
@.str.7 = [40 x i8] c"Unable to dup /dev/null onto stderr: %m\00"
@.str.8 = [30 x i8] c"Unable to close /dev/null: %m\00"
define i32 @daemon(i32 %0, i32 %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %4 [
i32 0, label %6
i32 -1, label %63
]
4: ; preds = %2
%5 = tail call i32 @_exit(i32 0)
unreachable
6: ; preds = %2
%7 = tail call i64 (...) @setsid()
%8 = icmp slt i64 %7, 0
br i1 %8, label %63, label %9
9: ; preds = %6
%10 = tail call i32 (...) @fork()
switch i32 %10, label %11 [
i32 0, label %13
i32 -1, label %63
]
11: ; preds = %9
%12 = tail call i32 @_exit(i32 0)
unreachable
13: ; preds = %9
%14 = icmp eq i32 %0, 0
br i1 %14, label %15, label %20
15: ; preds = %13
%16 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %63
20: ; preds = %15, %13
%21 = icmp eq i32 %1, 0
br i1 %21, label %22, label %34
22: ; preds = %20
%23 = tail call i32 @closeall(i32 0)
%24 = load i32, i32* @O_RDWR
%25 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %22
%28 = tail call i32 @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %22
%30 = load i32, i32* @STDOUT_FILENO
%31 = tail call i64 @dup2(i32 0, i32 %30)
%32 = load i32, i32* @STDERR_FILENO
%33 = tail call i64 @dup2(i32 0, i32 %32)
br label %63
34: ; preds = %20
%35 = load i32, i32* @O_RDWR
%36 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = tail call i32 @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = load i32, i32* @STDIN_FILENO
%42 = tail call i64 @dup2(i32 %36, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = tail call i32 @error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
br label %46
46: ; preds = %44, %40
%47 = load i32, i32* @STDOUT_FILENO
%48 = tail call i64 @dup2(i32 %36, i32 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = tail call i32 @error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = load i32, i32* @STDERR_FILENO
%54 = tail call i64 @dup2(i32 %36, i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = tail call i32 @error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0))
br label %58
58: ; preds = %56, %52
%59 = tail call i64 @close(i32 %36)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %58
%62 = tail call i32 @error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0))
br label %63
63: ; preds = %29, %61, %58, %9, %6, %2, %18
%64 = phi i32 [ -1, %18 ], [ %3, %2 ], [ -1, %6 ], [ %10, %9 ], [ 0, %58 ], [ 0, %61 ], [ 0, %29 ]
ret i32 %64
}