#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*/ _SC_OPEN_MAX;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ open_log_file();
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
scalar_t__ setsid();
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);
  pid = fork();
  if (pid < 0)
    exit(EXIT_FAILURE);
  if (pid > 0)
    exit(EXIT_SUCCESS);
  umask(0);
  chdir("/");
  printf("IN CASE OF ERROR KILL PID %ld\n", getpid());
  int x;
  for (x = sysconf(_SC_OPEN_MAX); x > 0; x--) {
    close(x);
  }
  open_log_file();
}


x86 assembly (clang, -O0):
<skeleton_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11cc <skeleton_daemon+0x2c>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11e5 <skeleton_daemon+0x45>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    1204 <skeleton_daemon+0x64>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1228 <skeleton_daemon+0x88>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    1241 <skeleton_daemon+0xa1>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xdb1(%rip),%rdi
call   1040 <chdir@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xd9e(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    129a <skeleton_daemon+0xfa>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    127a <skeleton_daemon+0xda>
mov    $0x0,%al
call   10b0 <open_log_file@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<skeleton_daemon>:
push   %rbx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1229 <skeleton_daemon+0x89>
jne    1237 <skeleton_daemon+0x97>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     1229 <skeleton_daemon+0x89>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1229 <skeleton_daemon+0x89>
jne    1237 <skeleton_daemon+0x97>
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xe25(%rip),%rdi
call   1040 <chdir@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe14(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
test   %eax,%eax
jle    1221 <skeleton_daemon+0x81>
mov    %eax,%ebx
nopl   0x0(%rax)
mov    %ebx,%edi
call   1060 <close@plt>
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
ja     1210 <skeleton_daemon+0x70>
xor    %eax,%eax
pop    %rbx
jmp    10b0 <open_log_file@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [31 x i8] c"IN CASE OF ERROR KILL PID %ld\0A\00"
@_SC_OPEN_MAX = 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 = call i32 (...) @getpid()
%38 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = load i32, i32* @_SC_OPEN_MAX
%40 = call i32 @sysconf(i32 %39)
store i32 %40, i32* %2
br label %41
41: ; preds = %47, %34
%42 = load i32, i32* %2
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = load i32, i32* %2
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %44
%48 = load i32, i32* %2
%49 = add nsw i32 %48, -1
store i32 %49, i32* %2
br label %41
50: ; preds = %41
%51 = call i32 (...) @open_log_file()
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [31 x i8] c"IN CASE OF ERROR KILL PID %ld\0A\00"
@_SC_OPEN_MAX = 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 = tail call i32 (...) @getpid()
%32 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @_SC_OPEN_MAX
%34 = tail call i32 @sysconf(i32 %33)
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %28, %36
%37 = phi i32 [ %39, %36 ], [ %34, %28 ]
%38 = tail call i32 @close(i32 %37)
%39 = add nsw i32 %37, -1
%40 = icmp ugt i32 %37, 1
br i1 %40, label %36, label %41
41: ; preds = %36, %28
%42 = tail call i32 (...) @open_log_file()
ret void
}