#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*/ chdir(char *);
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*/);
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);
  chdir("/home/dnabre/work/canary");
  openlog("canary", LOG_PID, LOG_DAEMON);
}


x86 assembly (clang, -O0):
<skeleton_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    118c <skeleton_daemon+0x2c>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11a5 <skeleton_daemon+0x45>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0x0,%rax
jge    11c4 <skeleton_daemon+0x64>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1216 <skeleton_daemon+0xb6>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    122f <skeleton_daemon+0xcf>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xdca(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    0x2d95(%rip),%rax
mov    (%rax),%edx
lea    0xdbe(%rip),%rdi
call   1060 <openlog@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<skeleton_daemon>:
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11db <skeleton_daemon+0x7b>
jne    11e9 <skeleton_daemon+0x89>
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
js     11db <skeleton_daemon+0x7b>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rbx
mov    (%rbx),%esi
call   1050 <signal@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1050 <signal@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11db <skeleton_daemon+0x7b>
jne    11e9 <skeleton_daemon+0x89>
lea    0xe49(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
lea    0xe3d(%rip),%rdi
pop    %rbx
jmp    1060 <openlog@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1070 <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
@.str = [25 x i8] c"/home/dnabre/work/canary\00"
@.str.1 = [7 x i8] c"canary\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
define void @skeleton_daemon() {
%1 = alloca i64
%2 = call i64 (...) @fork()
store i64 %2, i64* %1
%3 = load i64, i64* %1
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @EXIT_FAILURE
%7 = call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = load i64, i64* %1
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @EXIT_SUCCESS
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %8
%15 = call i64 (...) @setsid()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %14
%21 = load i32, i32* @SIGCHLD
%22 = load i32, i32* @SIG_IGN
%23 = call i32 @signal(i32 %21, i32 %22)
%24 = load i32, i32* @SIGHUP
%25 = load i32, i32* @SIG_IGN
%26 = call i32 @signal(i32 %24, i32 %25)
%27 = call i64 (...) @fork()
store i64 %27, i64* %1
%28 = load i64, i64* %1
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %20
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %20
%34 = load i64, i64* %1
%35 = icmp sgt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* @EXIT_SUCCESS
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %33
%40 = call i32 @chdir(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%41 = load i32, i32* @LOG_PID
%42 = load i32, i32* @LOG_DAEMON
%43 = call i32 @openlog(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %41, i32 %42)
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
@.str = [25 x i8] c"/home/dnabre/work/canary\00"
@.str.1 = [7 x i8] c"canary\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 @chdir(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%36 = load i32, i32* @LOG_PID
%37 = load i32, i32* @LOG_DAEMON
%38 = tail call i32 @openlog(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 %37)
ret void
}