#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();
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("/");
  int x;
  for (x = sysconf(_SC_OPEN_MAX); x > 0; x--) {
    close(x);
  }
}


x86 assembly (clang, -O0):
<skeleton_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    119c <skeleton_daemon+0x2c>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11b5 <skeleton_daemon+0x45>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jge    11d4 <skeleton_daemon+0x64>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11f8 <skeleton_daemon+0x88>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    1211 <skeleton_daemon+0xa1>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xde1(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1255 <skeleton_daemon+0xe5>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1235 <skeleton_daemon+0xc5>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<skeleton_daemon>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11d3 <skeleton_daemon+0x63>
jne    11e1 <skeleton_daemon+0x71>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     11d3 <skeleton_daemon+0x63>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11d3 <skeleton_daemon+0x63>
jne    11e1 <skeleton_daemon+0x71>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xe59(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
test   %eax,%eax
jle    11d1 <skeleton_daemon+0x61>
mov    %eax,%ebx
mov    %ebx,%edi
call   1040 <close@plt>
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
ja     11c0 <skeleton_daemon+0x50>
pop    %rbx
ret
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1080 <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
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 sgt 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
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
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, 0
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 ugt i32 %35, 1
br i1 %38, label %34, label %39
39: ; preds = %34, %28
ret void
}