#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 NSIG;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getdtablesize();
int /*<<< orphan*/ my_signal(int, int /*<<< orphan*/);
int setsid();
void _daemon(int flag) {
  int n, i;
  chdir("/tmp");
  if (flag) {
    n = getdtablesize();
    for (i = 0; i <= n; i++)
      close(i);
  }
  if (fork())
    exit(0);
  if (setsid() == -1)
    exit(0);
  if (fork())
    exit(0);
  for (n = 1; n <= NSIG; n++)
    my_signal(n, SIG_IGN);
}


x86 assembly (clang, -O0):
<_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe7e(%rip),%rdi
call   1030 <chdir@plt>
cmpl   $0x0,-0x4(%rbp)
je     11c9 <_daemon+0x59>
mov    $0x0,%al
call   1090 <getdtablesize@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     11c4 <_daemon+0x54>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a2 <_daemon+0x32>
jmp    11c9 <_daemon+0x59>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
je     11e1 <_daemon+0x71>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11f8 <_daemon+0x88>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
je     1210 <_daemon+0xa0>
xor    %edi,%edi
call   1070 <exit@plt>
movl   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2db7(%rip),%rcx
cmp    (%rcx),%eax
jg     1248 <_daemon+0xd8>
mov    -0x8(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   1040 <my_signal@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1217 <_daemon+0xa7>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_daemon>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
lea    0xe83(%rip),%rdi
call   1030 <chdir@plt>
test   %ebx,%ebx
je     11ae <_daemon+0x3e>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <getdtablesize@plt>
test   %eax,%eax
js     11ae <_daemon+0x3e>
mov    %eax,%ebp
add    $0x1,%ebp
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    11a0 <_daemon+0x30>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    1207 <_daemon+0x97>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     1207 <_daemon+0x97>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    1207 <_daemon+0x97>
mov    0x2dff(%rip),%r14
cmpl   $0x0,(%r14)
jle    1202 <_daemon+0x92>
xor    %ebx,%ebx
mov    0x2e00(%rip),%rbp
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebx
mov    0x0(%rbp),%esi
mov    %ebx,%edi
call   1040 <my_signal@plt>
cmp    (%r14),%ebx
jl     11f0 <_daemon+0x80>
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1070 <exit@plt>

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

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