#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 FD_SETSIZE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closelog();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ setsid();
int daemon_start(void) {
  int i;
  pid_t pid;
  if ((pid = fork()) < 0)
    return (-1);
  else if (pid != 0)
    exit(0);
  closelog();
  for (i = 0; i < FD_SETSIZE; i++)
    close(i);
  setsid();
  return 0;
}


x86 assembly (clang, -O0):
<daemon_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1179 <daemon_start+0x29>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d4 <daemon_start+0x84>
cmpq   $0x0,-0x10(%rbp)
je     118b <daemon_start+0x3b>
xor    %edi,%edi
call   1060 <exit@plt>
jmp    1190 <daemon_start+0x40>
mov    $0x0,%al
call   1030 <closelog@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e38(%rip),%rcx
cmp    (%rcx),%eax
jge    11c6 <daemon_start+0x76>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    119e <daemon_start+0x4e>
mov    $0x0,%al
call   1050 <setsid@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_start>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     1197 <daemon_start+0x47>
jne    11a5 <daemon_start+0x55>
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <closelog@plt>
mov    0x2e6d(%rip),%rbx
cmpl   $0x0,(%rbx)
jle    118e <daemon_start+0x3e>
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1040 <close@plt>
add    $0x1,%ebp
cmp    (%rbx),%ebp
jl     1180 <daemon_start+0x30>
xor    %eax,%eax
call   1050 <setsid@plt>
jmp    119d <daemon_start+0x4d>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@FD_SETSIZE = global i32 0
define i32 @daemon_start() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = call i64 (...) @fork()
store i64 %4, i64* %3
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %0
store i32 -1, i32* %1
br label %27
7: ; preds = %0
%8 = load i64, i64* %3
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = call i32 @exit(i32 0)
unreachable
12: ; preds = %7
br label %13
13: ; preds = %12
%14 = call i32 (...) @closelog()
store i32 0, i32* %2
br label %15
15: ; preds = %22, %13
%16 = load i32, i32* %2
%17 = load i32, i32* @FD_SETSIZE
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
br label %22
22: ; preds = %19
%23 = load i32, i32* %2
%24 = add nsw i32 %23, 1
store i32 %24, i32* %2
br label %15
25: ; preds = %15
%26 = call i32 (...) @setsid()
store i32 0, i32* %1
br label %27
27: ; preds = %25, %6
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@FD_SETSIZE = global i32 0
define i32 @daemon_start() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %19, label %3
3: ; preds = %0
%4 = icmp eq i64 %1, 0
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %3
%8 = tail call i32 (...) @closelog()
%9 = load i32, i32* @FD_SETSIZE
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %7, %11
%12 = phi i32 [ %14, %11 ], [ 0, %7 ]
%13 = tail call i32 @close(i32 %12)
%14 = add nuw nsw i32 %12, 1
%15 = load i32, i32* @FD_SETSIZE
%16 = icmp slt i32 %14, %15
br i1 %16, label %11, label %17
17: ; preds = %11, %7
%18 = tail call i32 (...) @setsid()
br label %19
19: ; preds = %0, %17
%20 = phi i32 [ 0, %17 ], [ -1, %0 ]
ret i32 %20
}