#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 */
struct sigaction {
  int /*<<< orphan*/ sa_flags;
  int /*<<< orphan*/ sa_mask;
  int /*<<< orphan*/ sa_handler;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SA_RESTART;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
scalar_t__ sigaction(int /*<<< orphan*/, struct sigaction *,
                     int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int __daemon() {
  int fd0, fd1, fd2;
  pid_t pid;
  struct sigaction sa;
  umask(0);
  if ((pid = fork()) < 0)
    return -1;
  else if (pid > 0)
    exit(0);
  if (setsid() < 0)
    return -1;
  sa.sa_handler = SIG_IGN;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = SA_RESTART;
  if (sigaction(SIGHUP, &sa, NULL) < 0)
    return -1;
  if ((pid = fork()) < 0)
    return -1;
  else if (pid > 0)
    exit(0);
  if (chdir("/") < 0)
    return -1;
  for (int i = 0; i < 1024; i++)
    close(i);
  fd0 = open("/dev/null", O_RDWR);
  fd1 = dup(fd0);
  fd2 = dup(fd0);
  if ((fd0 != STDIN_FILENO) || (fd1 != STDOUT_FILENO) ||
      (fd2 != STDERR_FILENO)) {
    close(fd0);
    close(fd1);
    close(fd2);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<__daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
xor    %edi,%edi
call   1090 <umask@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11d0 <__daemon+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <__daemon+0x1c8>
cmpq   $0x0,-0x18(%rbp)
jle    11e2 <__daemon+0x42>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    11e7 <__daemon+0x47>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    1204 <__daemon+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <__daemon+0x1c8>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x4,%rdi
call   1080 <sigemptyset@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    1255 <__daemon+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <__daemon+0x1c8>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1276 <__daemon+0xd6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <__daemon+0x1c8>
cmpq   $0x0,-0x18(%rbp)
jle    1288 <__daemon+0xe8>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    128d <__daemon+0xed>
lea    0xd6c(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    12af <__daemon+0x10f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <__daemon+0x1c8>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x400,-0x2c(%rbp)
jge    12d9 <__daemon+0x139>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12b6 <__daemon+0x116>
mov    0x2cf8(%rip),%rax
mov    (%rax),%esi
lea    0xd19(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2c9f(%rip),%rcx
cmp    (%rcx),%eax
jne    133d <__daemon+0x19d>
mov    -0xc(%rbp),%eax
mov    0x2cc5(%rip),%rcx
cmp    (%rcx),%eax
jne    133d <__daemon+0x19d>
mov    -0x10(%rbp),%eax
mov    0x2c83(%rip),%rcx
cmp    (%rcx),%eax
je     1361 <__daemon+0x1c1>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <__daemon+0x1c8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__daemon>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
xor    %edi,%edi
call   1090 <umask@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     12ce <__daemon+0x12e>
jne    12db <__daemon+0x13b>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     12ce <__daemon+0x12e>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0xc(%rsp),%rdi
call   1080 <sigemptyset@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
test   %rax,%rax
js     12ce <__daemon+0x12e>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     12ce <__daemon+0x12e>
jne    12db <__daemon+0x13b>
lea    0xdc5(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     12ce <__daemon+0x12e>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    $0x400,%ebx
jne    1250 <__daemon+0xb0>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
lea    0xd90(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1050 <dup@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1050 <dup@plt>
mov    %eax,%r15d
mov    0x2d1c(%rip),%rax
cmp    (%rax),%ebp
jne    12b2 <__daemon+0x112>
mov    0x2d49(%rip),%rax
cmp    (%rax),%r14d
jne    12b2 <__daemon+0x112>
xor    %ebx,%ebx
mov    0x2d0b(%rip),%rax
cmp    (%rax),%r15d
je     12ce <__daemon+0x12e>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sigaction = type { i32, i32, i32 }
@SIG_IGN = global i32 0
@SA_RESTART = global i32 0
@SIGHUP = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [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
define i32 @__daemon() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca %struct.sigaction
%7 = alloca i32
%8 = call i32 @umask(i32 0)
%9 = call i64 (...) @fork()
store i64 %9, i64* %5
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 -1, i32* %1
br label %83
12: ; preds = %0
%13 = load i64, i64* %5
%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
br label %18
18: ; preds = %17
%19 = call i64 (...) @setsid()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 -1, i32* %1
br label %83
22: ; preds = %18
%23 = load i32, i32* @SIG_IGN
%24 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
%26 = call i32 @sigemptyset(i32* %25)
%27 = load i32, i32* @SA_RESTART
%28 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* @SIGHUP
%30 = call i64 @sigaction(i32 %29, %struct.sigaction* %6, i32* null)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %22
store i32 -1, i32* %1
br label %83
33: ; preds = %22
%34 = call i64 (...) @fork()
store i64 %34, i64* %5
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 -1, i32* %1
br label %83
37: ; preds = %33
%38 = load i64, i64* %5
%39 = icmp sgt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %37
br label %43
43: ; preds = %42
%44 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 -1, i32* %1
br label %83
47: ; preds = %43
store i32 0, i32* %7
br label %48
48: ; preds = %54, %47
%49 = load i32, i32* %7
%50 = icmp slt i32 %49, 1024
br i1 %50, label %51, label %57
51: ; preds = %48
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %51
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %48
57: ; preds = %48
%58 = load i32, i32* @O_RDWR
%59 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %58)
store i32 %59, i32* %2
%60 = load i32, i32* %2
%61 = call i32 @dup(i32 %60)
store i32 %61, i32* %3
%62 = load i32, i32* %2
%63 = call i32 @dup(i32 %62)
store i32 %63, i32* %4
%64 = load i32, i32* %2
%65 = load i32, i32* @STDIN_FILENO
%66 = icmp ne i32 %64, %65
br i1 %66, label %75, label %67
67: ; preds = %57
%68 = load i32, i32* %3
%69 = load i32, i32* @STDOUT_FILENO
%70 = icmp ne i32 %68, %69
br i1 %70, label %75, label %71
71: ; preds = %67
%72 = load i32, i32* %4
%73 = load i32, i32* @STDERR_FILENO
%74 = icmp ne i32 %72, %73
br i1 %74, label %75, label %82
75: ; preds = %71, %67, %57
%76 = load i32, i32* %2
%77 = call i32 @close(i32 %76)
%78 = load i32, i32* %3
%79 = call i32 @close(i32 %78)
%80 = load i32, i32* %4
%81 = call i32 @close(i32 %80)
store i32 -1, i32* %1
br label %83
82: ; preds = %71
store i32 0, i32* %1
br label %83
83: ; preds = %82, %75, %46, %36, %32, %21, %11
%84 = load i32, i32* %1
ret i32 %84
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i32, i32 }
@SIG_IGN = global i32 0
@SA_RESTART = global i32 0
@SIGHUP = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [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
define i32 @__daemon() {
%1 = alloca %struct.sigaction
%2 = bitcast %struct.sigaction* %1 to i8*
%3 = tail call i32 @umask(i32 0)
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %55, label %6
6: ; preds = %0
%7 = icmp eq i64 %4, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %6
%11 = tail call i64 (...) @setsid()
%12 = icmp slt i64 %11, 0
br i1 %12, label %55, label %13
13: ; preds = %10
%14 = load i32, i32* @SIG_IGN
%15 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 1
%17 = call i32 @sigemptyset(i32* %16)
%18 = load i32, i32* @SA_RESTART
%19 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @SIGHUP
%21 = call i64 @sigaction(i32 %20, %struct.sigaction* %1, i32* null)
%22 = icmp slt i64 %21, 0
br i1 %22, label %55, label %23
23: ; preds = %13
%24 = call i64 (...) @fork()
%25 = icmp slt i64 %24, 0
br i1 %25, label %55, label %26
26: ; preds = %23
%27 = icmp eq i64 %24, 0
br i1 %27, label %30, label %28
28: ; preds = %26
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %26
%31 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%32 = icmp slt i64 %31, 0
br i1 %32, label %55, label %46
33: ; preds = %46
%34 = load i32, i32* @O_RDWR
%35 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = call i32 @dup(i32 %35)
%37 = call i32 @dup(i32 %35)
%38 = load i32, i32* @STDIN_FILENO
%39 = icmp eq i32 %35, %38
%40 = load i32, i32* @STDOUT_FILENO
%41 = icmp eq i32 %36, %40
%42 = select i1 %39, i1 %41, i1 false
%43 = load i32, i32* @STDERR_FILENO
%44 = icmp eq i32 %37, %43
%45 = select i1 %42, i1 %44, i1 false
br i1 %45, label %55, label %51
46: ; preds = %30, %46
%47 = phi i32 [ %49, %46 ], [ 0, %30 ]
%48 = call i32 @close(i32 %47)
%49 = add nuw nsw i32 %47, 1
%50 = icmp eq i32 %49, 1024
br i1 %50, label %33, label %46
51: ; preds = %33
%52 = call i32 @close(i32 %35)
%53 = call i32 @close(i32 %36)
%54 = call i32 @close(i32 %37)
br label %55
55: ; preds = %33, %30, %23, %13, %10, %0, %51
%56 = phi i32 [ -1, %51 ], [ -1, %0 ], [ -1, %10 ], [ -1, %13 ], [ -1, %23 ], [ -1, %30 ], [ 0, %33 ]
ret i32 %56
}