#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*/ SIGCHLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void skeleton_daemon() {
  pid_t pid;
  pid = fork();
  if (pid < 0)
    exit(EXIT_FAILURE);
  if (pid > 0)
    exit(EXIT_SUCCESS);
  if (setsid() < 0)
    exit(EXIT_FAILURE);
  signal(SIGCHLD, SIG_IGN);
  signal(SIGHUP, SIG_IGN);
  pid = fork();
  if (pid < 0)
    exit(EXIT_FAILURE);
  if (pid > 0)
    exit(EXIT_SUCCESS);
  umask(0);
  int x;
  for (x = sysconf(_SC_OPEN_MAX); x > 0; x--) {
    close(x);
  }
  openlog("Decent Deamon", 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    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11c5 <skeleton_daemon+0x45>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0x0,%rax
jge    11e4 <skeleton_daemon+0x64>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1236 <skeleton_daemon+0xb6>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    124f <skeleton_daemon+0xcf>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1287 <skeleton_daemon+0x107>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1267 <skeleton_daemon+0xe7>
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
mov    0x2d49(%rip),%rax
mov    (%rax),%edx
lea    0xd60(%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     1230 <skeleton_daemon+0xb0>
jne    123e <skeleton_daemon+0xbe>
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
js     1230 <skeleton_daemon+0xb0>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rbx
mov    (%rbx),%esi
call   1050 <signal@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1050 <signal@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1230 <skeleton_daemon+0xb0>
jne    123e <skeleton_daemon+0xbe>
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
test   %eax,%eax
jle    1211 <skeleton_daemon+0x91>
mov    %eax,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1030 <close@plt>
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
ja     1200 <skeleton_daemon+0x80>
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
lea    0xdd6(%rip),%rdi
pop    %rbx
jmp    1080 <openlog@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@_SC_OPEN_MAX = global i32 0
@.str = [14 x i8] c"Decent Deamon\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 = load i32, i32* @SIGCHLD
%23 = load i32, i32* @SIG_IGN
%24 = call i32 @signal(i32 %22, i32 %23)
%25 = load i32, i32* @SIGHUP
%26 = load i32, i32* @SIG_IGN
%27 = call i32 @signal(i32 %25, i32 %26)
%28 = call i64 (...) @fork()
store i64 %28, i64* %1
%29 = load i64, i64* %1
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %21
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %21
%35 = load i64, i64* %1
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* @EXIT_SUCCESS
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %34
%41 = call i32 @umask(i32 0)
%42 = load i32, i32* @_SC_OPEN_MAX
%43 = call i32 @sysconf(i32 %42)
store i32 %43, i32* %2
br label %44
44: ; preds = %50, %40
%45 = load i32, i32* %2
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %47
%51 = load i32, i32* %2
%52 = add nsw i32 %51, -1
store i32 %52, i32* %2
br label %44
53: ; preds = %44
%54 = load i32, i32* @LOG_PID
%55 = load i32, i32* @LOG_DAEMON
%56 = call i32 @openlog(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %54, i32 %55)
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@_SC_OPEN_MAX = global i32 0
@.str = [14 x i8] c"Decent Deamon\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 = load i32, i32* @SIGCHLD
%19 = load i32, i32* @SIG_IGN
%20 = tail call i32 @signal(i32 %18, i32 %19)
%21 = load i32, i32* @SIGHUP
%22 = load i32, i32* @SIG_IGN
%23 = tail call i32 @signal(i32 %21, i32 %22)
%24 = tail call i64 (...) @fork()
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %17
%30 = icmp eq i64 %24, 0
br i1 %30, label %34, label %31
31: ; preds = %29
%32 = load i32, i32* @EXIT_SUCCESS
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %29
%35 = tail call i32 @umask(i32 0)
%36 = load i32, i32* @_SC_OPEN_MAX
%37 = tail call i32 @sysconf(i32 %36)
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %34, %39
%40 = phi i32 [ %42, %39 ], [ %37, %34 ]
%41 = tail call i32 @close(i32 %40)
%42 = add nsw i32 %40, -1
%43 = icmp ugt i32 %40, 1
br i1 %43, label %39, label %44
44: ; preds = %39, %34
%45 = load i32, i32* @LOG_PID
%46 = load i32, i32* @LOG_DAEMON
%47 = tail call i32 @openlog(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %45, i32 %46)
ret void
}