#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 STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ chdir(char *);
int close(int);
int dup2(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int setsid();
long sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int start_daemon() {
  int fd;
  switch (fork()) {
  case -1:
    printf("fork() failed\n");
    return -1;
  case 0:
    break;
  default:
    exit(0);
  }
  if (setsid() == -1) {
    printf("setsid() failed\n");
    return -1;
  }
  switch (fork()) {
  case -1:
    printf("fork() failed\n");
    return -1;
  case 0:
    break;
  default:
    exit(0);
  }
  umask(0);
  chdir("/");
  long maxfd;
  if ((maxfd = sysconf(_SC_OPEN_MAX)) != -1) {
    for (fd = 0; fd < maxfd; fd++) {
      close(fd);
    }
  }
  fd = open("/dev/null", O_RDWR);
  if (fd == -1) {
    printf("open(\"/dev/null\") failed\n");
    return -1;
  }
  if (dup2(fd, STDIN_FILENO) == -1) {
    printf("dup2(STDIN) failed\n");
    return -1;
  }
  if (dup2(fd, STDOUT_FILENO) == -1) {
    printf("dup2(STDOUT) failed\n");
    return -1;
  }
  if (dup2(fd, STDERR_FILENO) == -1) {
    printf("dup2(STDERR) failed\n");
    return -1;
  }
  if (fd > STDERR_FILENO) {
    if (close(fd) == -1) {
      printf("close() failed\n");
      return -1;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<start_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11d2 <start_daemon+0x32>
jmp    11c2 <start_daemon+0x22>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     11ea <start_daemon+0x4a>
jmp    11ef <start_daemon+0x4f>
lea    0xe27(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e2 <start_daemon+0x242>
jmp    11f6 <start_daemon+0x56>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    121e <start_daemon+0x7e>
lea    0xe02(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e2 <start_daemon+0x242>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
sub    $0xffffffff,%eax
je     1248 <start_daemon+0xa8>
jmp    1238 <start_daemon+0x98>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     1260 <start_daemon+0xc0>
jmp    1265 <start_daemon+0xc5>
lea    0xdb1(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e2 <start_daemon+0x242>
jmp    126c <start_daemon+0xcc>
xor    %edi,%edi
call   10b0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xda6(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %rax,-0x10(%rbp)
cmp    $0xffffffffffffffff,%rax
je     12cb <start_daemon+0x12b>
movl   $0x0,-0x8(%rbp)
movslq -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    12c6 <start_daemon+0x126>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12a2 <start_daemon+0x102>
jmp    12cb <start_daemon+0x12b>
mov    0x2d0e(%rip),%rax
mov    (%rax),%esi
lea    0xd47(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1305 <start_daemon+0x165>
lea    0xd38(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e2 <start_daemon+0x242>
mov    -0x8(%rbp),%edi
mov    0x2cb1(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1337 <start_daemon+0x197>
lea    0xd20(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e2 <start_daemon+0x242>
mov    -0x8(%rbp),%edi
mov    0x2caf(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1369 <start_daemon+0x1c9>
lea    0xd02(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e2 <start_daemon+0x242>
mov    -0x8(%rbp),%edi
mov    0x2c55(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    139b <start_daemon+0x1fb>
lea    0xce5(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e2 <start_daemon+0x242>
mov    -0x8(%rbp),%eax
mov    0x2c23(%rip),%rcx
cmp    (%rcx),%eax
jle    13db <start_daemon+0x23b>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    13d6 <start_daemon+0x236>
lea    0xcbf(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e2 <start_daemon+0x242>
jmp    13db <start_daemon+0x23b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_daemon>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   10c0 <fork@plt>
lea    0xecd(%rip),%rbx
cmp    $0xffffffff,%eax
je     12d6 <start_daemon+0x136>
test   %eax,%eax
jne    12ea <start_daemon+0x14a>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     12ab <start_daemon+0x10b>
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     12d6 <start_daemon+0x136>
test   %eax,%eax
jne    12ea <start_daemon+0x14a>
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xe07(%rip),%rdi
call   1040 <chdir@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
test   %rax,%rax
jle    1230 <start_daemon+0x90>
mov    %rax,%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1220 <start_daemon+0x80>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
lea    0xdc2(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12b4 <start_daemon+0x114>
mov    %eax,%ebx
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     12bd <start_daemon+0x11d>
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     12c6 <start_daemon+0x126>
mov    0x2d4b(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     12cf <start_daemon+0x12f>
mov    $0x1,%ebp
cmp    (%r14),%ebx
jle    12e3 <start_daemon+0x143>
mov    %ebx,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    12e3 <start_daemon+0x143>
lea    0xd63(%rip),%rbx
jmp    12d6 <start_daemon+0x136>
lea    0xdbd(%rip),%rbx
jmp    12d6 <start_daemon+0x136>
lea    0xd9b(%rip),%rbx
jmp    12d6 <start_daemon+0x136>
lea    0xd7f(%rip),%rbx
jmp    12d6 <start_daemon+0x136>
lea    0xd62(%rip),%rbx
jmp    12d6 <start_daemon+0x136>
lea    0xd45(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"fork() failed\0A\00"
@.str.1 = [17 x i8] c"setsid() failed\0A\00"
@.str.2 = [2 x i8] c"/\00"
@_SC_OPEN_MAX = global i32 0
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.4 = [26 x i8] c"open(\22/dev/null\22) failed\0A\00"
@STDIN_FILENO = global i32 0
@.str.5 = [20 x i8] c"dup2(STDIN) failed\0A\00"
@STDOUT_FILENO = global i32 0
@.str.6 = [21 x i8] c"dup2(STDOUT) failed\0A\00"
@STDERR_FILENO = global i32 0
@.str.7 = [21 x i8] c"dup2(STDERR) failed\0A\00"
@.str.8 = [16 x i8] c"close() failed\0A\00"
define i32 @start_daemon() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = call i32 (...) @fork()
switch i32 %4, label %8 [
i32 -1, label %5
i32 0, label %7
]
5: ; preds = %0
%6 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %81
7: ; preds = %0
br label %10
8: ; preds = %0
%9 = call i32 @exit(i32 0)
unreachable
10: ; preds = %7
%11 = call i32 (...) @setsid()
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %81
15: ; preds = %10
%16 = call i32 (...) @fork()
switch i32 %16, label %20 [
i32 -1, label %17
i32 0, label %19
]
17: ; preds = %15
%18 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %81
19: ; preds = %15
br label %22
20: ; preds = %15
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %19
%23 = call i32 @umask(i32 0)
%24 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @_SC_OPEN_MAX
%26 = call i64 @sysconf(i32 %25)
store i64 %26, i64* %3
%27 = icmp ne i64 %26, -1
br i1 %27, label %28, label %41
28: ; preds = %22
store i32 0, i32* %2
br label %29
29: ; preds = %37, %28
%30 = load i32, i32* %2
%31 = sext i32 %30 to i64
%32 = load i64, i64* %3
%33 = icmp slt i64 %31, %32
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = load i32, i32* %2
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %34
%38 = load i32, i32* %2
%39 = add nsw i32 %38, 1
store i32 %39, i32* %2
br label %29
40: ; preds = %29
br label %41
41: ; preds = %40, %22
%42 = load i32, i32* @O_RDWR
%43 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %42)
store i32 %43, i32* %2
%44 = load i32, i32* %2
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %81
48: ; preds = %41
%49 = load i32, i32* %2
%50 = load i32, i32* @STDIN_FILENO
%51 = call i32 @dup2(i32 %49, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %48
%54 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %1
br label %81
55: ; preds = %48
%56 = load i32, i32* %2
%57 = load i32, i32* @STDOUT_FILENO
%58 = call i32 @dup2(i32 %56, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %55
%61 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %1
br label %81
62: ; preds = %55
%63 = load i32, i32* %2
%64 = load i32, i32* @STDERR_FILENO
%65 = call i32 @dup2(i32 %63, i32 %64)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %69
67: ; preds = %62
%68 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %1
br label %81
69: ; preds = %62
%70 = load i32, i32* %2
%71 = load i32, i32* @STDERR_FILENO
%72 = icmp sgt i32 %70, %71
br i1 %72, label %73, label %80
73: ; preds = %69
%74 = load i32, i32* %2
%75 = call i32 @close(i32 %74)
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %79
77: ; preds = %73
%78 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %1
br label %81
79: ; preds = %73
br label %80
80: ; preds = %79, %69
store i32 1, i32* %1
br label %81
81: ; preds = %80, %77, %67, %60, %53, %46, %17, %13, %5
%82 = load i32, i32* %1
ret i32 %82
}

LLVM IR (-O2):
@.str.2 = [2 x i8] c"/\00"
@_SC_OPEN_MAX = global i32 0
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@str = [15 x i8] c"close() failed\00"
@str.9 = [20 x i8] c"dup2(STDERR) failed\00"
@str.10 = [20 x i8] c"dup2(STDOUT) failed\00"
@str.11 = [19 x i8] c"dup2(STDIN) failed\00"
@str.12 = [25 x i8] c"open(\22/dev/null\22) failed\00"
@str.14 = [16 x i8] c"setsid() failed\00"
@str.15 = [14 x i8] c"fork() failed\00"
define i32 @start_daemon() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %2 [
i32 -1, label %45
i32 0, label %4
]
2: ; preds = %0
%3 = tail call i32 @exit(i32 0)
unreachable
4: ; preds = %0
%5 = tail call i32 (...) @setsid()
%6 = icmp eq i32 %5, -1
br i1 %6, label %45, label %7
7: ; preds = %4
%8 = tail call i32 (...) @fork()
switch i32 %8, label %9 [
i32 -1, label %45
i32 0, label %11
]
9: ; preds = %7
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %7
%12 = tail call i32 @umask(i32 0)
%13 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* @_SC_OPEN_MAX
%15 = tail call i64 @sysconf(i32 %14)
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %11, %17
%18 = phi i64 [ %21, %17 ], [ 0, %11 ]
%19 = trunc i64 %18 to i32
%20 = tail call i32 @close(i32 %19)
%21 = add nuw nsw i64 %18, 1
%22 = icmp eq i64 %21, %15
br i1 %22, label %23, label %17
23: ; preds = %17, %11
%24 = load i32, i32* @O_RDWR
%25 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %45, label %27
27: ; preds = %23
%28 = load i32, i32* @STDIN_FILENO
%29 = tail call i32 @dup2(i32 %25, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %45, label %31
31: ; preds = %27
%32 = load i32, i32* @STDOUT_FILENO
%33 = tail call i32 @dup2(i32 %25, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %45, label %35
35: ; preds = %31
%36 = load i32, i32* @STDERR_FILENO
%37 = tail call i32 @dup2(i32 %25, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %45, label %39
39: ; preds = %35
%40 = load i32, i32* @STDERR_FILENO
%41 = icmp sgt i32 %25, %40
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = tail call i32 @close(i32 %25)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %42, %35, %31, %27, %23, %7, %4, %0
%46 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.15, i64 0, i64 0), %0 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.14, i64 0, i64 0), %4 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.15, i64 0, i64 0), %7 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @str.12, i64 0, i64 0), %23 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @str.11, i64 0, i64 0), %27 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0), %31 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0), %35 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0), %42 ]
%47 = tail call i32 @puts(i8* dereferenceable(1) %46)
br label %48
48: ; preds = %45, %42, %39
%49 = phi i32 [ 1, %42 ], [ 1, %39 ], [ -1, %45 ]
ret i32 %49
}