#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 */

/* Variables and functions */
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int getdtablesize();
int /*<<< orphan*/ printf(char *);
int setsid();
int /*<<< orphan*/ signal(int, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void start_daemon(void) {
  int n, pid;
  umask(0);
  n = getdtablesize();
  if ((pid = fork()) < 0) {
    printf("Cannot fork.\n");
    exit(1);
  } else if (pid != 0) {
    exit(0);
  }
  if (setsid() == -1)
    exit(1);
  if ((pid = fork()) < 0) {
    exit(1);
  } else if (pid != 0) {
    exit(0);
  }
  while (n)
    close(--n);
  for (n = 1; n <= 65; n++)
    signal(n, SIG_IGN);
}


x86 assembly (clang, -O0):
<start_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x0,%al
call   10a0 <getdtablesize@plt>
mov    %eax,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11c2 <start_daemon+0x42>
lea    0xe4d(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
je     11d3 <start_daemon+0x53>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    11d8 <start_daemon+0x58>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11f2 <start_daemon+0x72>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    120f <start_daemon+0x8f>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
je     1220 <start_daemon+0xa0>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1225 <start_daemon+0xa5>
jmp    122a <start_daemon+0xaa>
cmpl   $0x0,-0x4(%rbp)
je     1247 <start_daemon+0xc7>
mov    -0x4(%rbp),%edi
add    $0xffffffff,%edi
mov    %edi,-0x4(%rbp)
call   1040 <close@plt>
jmp    122a <start_daemon+0xaa>
movl   $0x1,-0x4(%rbp)
cmpl   $0x41,-0x4(%rbp)
jg     1277 <start_daemon+0xf7>
mov    -0x4(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    124e <start_daemon+0xce>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_daemon>:
push   %r14
push   %rbx
push   %rax
xor    %edi,%edi
call   1070 <umask@plt>
xor    %eax,%eax
call   10a0 <getdtablesize@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     121b <start_daemon+0x9b>
jne    1214 <start_daemon+0x94>
xor    %eax,%eax
call   1050 <setsid@plt>
cmp    $0xffffffff,%eax
je     120a <start_daemon+0x8a>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     120a <start_daemon+0x8a>
jne    1214 <start_daemon+0x94>
test   %ebx,%ebx
je     11dc <start_daemon+0x5c>
add    $0xffffffff,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1040 <close@plt>
add    $0xffffffff,%ebx
jb     11d0 <start_daemon+0x50>
mov    $0x1,%ebx
mov    0x2e00(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    (%r14),%esi
mov    %ebx,%edi
call   1060 <signal@plt>
add    $0x1,%ebx
cmp    $0x42,%ebx
jne    11f0 <start_daemon+0x70>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

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