#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setsid();
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void skeleton_daemon(void) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  if (setsid() < 0) {
    exit(EXIT_FAILURE);
  }
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  umask(0);
  chdir("/");
  int x;
  for (x = sysconf(_SC_OPEN_MAX); x >= 0; x--) {
    close(x);
  }
  openlog("cryptodaemon", LOG_PID, LOG_DAEMON);
}


x86 assembly (clang, -O0):
<skeleton_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11ac <skeleton_daemon+0x2c>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11c5 <skeleton_daemon+0x45>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jge    11e4 <skeleton_daemon+0x64>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1208 <skeleton_daemon+0x88>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    1221 <skeleton_daemon+0xa1>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xdd1(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1265 <skeleton_daemon+0xe5>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1245 <skeleton_daemon+0xc5>
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
lea    0xd84(%rip),%rdi
call   1080 <openlog@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<skeleton_daemon>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11fb <skeleton_daemon+0x7b>
jne    1209 <skeleton_daemon+0x89>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     11fb <skeleton_daemon+0x7b>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11fb <skeleton_daemon+0x7b>
jne    1209 <skeleton_daemon+0x89>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xe49(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
test   %eax,%eax
js     11dc <skeleton_daemon+0x5c>
mov    %eax,%ebx
mov    %ebx,%edi
call   1040 <close@plt>
add    $0xffffffff,%ebx
jb     11d0 <skeleton_daemon+0x50>
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
lea    0xe0d(%rip),%rdi
pop    %rbx
jmp    1080 <openlog@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@_SC_OPEN_MAX = global i32 0
@.str.1 = [13 x i8] c"cryptodaemon\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
define void @skeleton_daemon() {
%1 = alloca i64
%2 = alloca i32
%3 = call i64 (...) @fork()
store i64 %3, i64* %1
%4 = load i64, i64* %1
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @EXIT_FAILURE
%8 = call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = load i64, i64* %1
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @EXIT_SUCCESS
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %9
%16 = call i64 (...) @setsid()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %15
%22 = call i64 (...) @fork()
store i64 %22, i64* %1
%23 = load i64, i64* %1
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %21
%29 = load i64, i64* %1
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @EXIT_SUCCESS
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %28
%35 = call i32 @umask(i32 0)
%36 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* @_SC_OPEN_MAX
%38 = call i32 @sysconf(i32 %37)
store i32 %38, i32* %2
br label %39
39: ; preds = %45, %34
%40 = load i32, i32* %2
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = load i32, i32* %2
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %42
%46 = load i32, i32* %2
%47 = add nsw i32 %46, -1
store i32 %47, i32* %2
br label %39
48: ; preds = %39
%49 = load i32, i32* @LOG_PID
%50 = load i32, i32* @LOG_DAEMON
%51 = call i32 @openlog(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %49, i32 %50)
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@_SC_OPEN_MAX = global i32 0
@.str.1 = [13 x i8] c"cryptodaemon\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
define void @skeleton_daemon() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = load i32, i32* @EXIT_FAILURE
%5 = tail call i32 @exit(i32 %4)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %11, label %8
8: ; preds = %6
%9 = load i32, i32* @EXIT_SUCCESS
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %6
%12 = tail call i64 (...) @setsid()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %11
%18 = tail call i64 (...) @fork()
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %17
%24 = icmp eq i64 %18, 0
br i1 %24, label %28, label %25
25: ; preds = %23
%26 = load i32, i32* @EXIT_SUCCESS
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %23
%29 = tail call i32 @umask(i32 0)
%30 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* @_SC_OPEN_MAX
%32 = tail call i32 @sysconf(i32 %31)
%33 = icmp sgt i32 %32, -1
br i1 %33, label %34, label %39
34: ; preds = %28, %34
%35 = phi i32 [ %37, %34 ], [ %32, %28 ]
%36 = tail call i32 @close(i32 %35)
%37 = add nsw i32 %35, -1
%38 = icmp eq i32 %35, 0
br i1 %38, label %39, label %34
39: ; preds = %34, %28
%40 = load i32, i32* @LOG_PID
%41 = load i32, i32* @LOG_DAEMON
%42 = tail call i32 @openlog(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %40, i32 %41)
ret void
}