#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*/ SIGCHLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
void setupDaemon() {
  pid_t pid;
  pid = fork();
  if (pid != 0)
    exit(0);
  signal(SIGCHLD, SIG_IGN);
  signal(SIGHUP, SIG_IGN);
  setsid();
  pid = fork();
  if (pid != 0)
    exit(0);
  fclose(stdin);
  fclose(stdout);
  fclose(stderr);
}


x86 assembly (clang, -O0):
<setupDaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1175 <setupDaemon+0x25>
xor    %edi,%edi
call   1060 <exit@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11c7 <setupDaemon+0x77>
xor    %edi,%edi
call   1060 <exit@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setupDaemon>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11c2 <setupDaemon+0x72>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
mov    0x2e63(%rip),%rbx
mov    (%rbx),%esi
call   1050 <signal@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1050 <signal@plt>
xor    %eax,%eax
call   1040 <setsid@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11c2 <setupDaemon+0x72>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
pop    %rbx
jmp    1030 <fclose@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
define void @setupDaemon() {
%1 = alloca i64
%2 = call i64 (...) @fork()
store i64 %2, i64* %1
%3 = load i64, i64* %1
%4 = icmp ne i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @exit(i32 0)
unreachable
7: ; preds = %0
%8 = load i32, i32* @SIGCHLD
%9 = load i32, i32* @SIG_IGN
%10 = call i32 @signal(i32 %8, i32 %9)
%11 = load i32, i32* @SIGHUP
%12 = load i32, i32* @SIG_IGN
%13 = call i32 @signal(i32 %11, i32 %12)
%14 = call i32 (...) @setsid()
%15 = call i64 (...) @fork()
store i64 %15, i64* %1
%16 = load i64, i64* %1
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %7
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %7
%21 = load i32, i32* @stdin
%22 = call i32 @fclose(i32 %21)
%23 = load i32, i32* @stdout
%24 = call i32 @fclose(i32 %23)
%25 = load i32, i32* @stderr
%26 = call i32 @fclose(i32 %25)
ret void
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
define void @setupDaemon() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %5, label %3
3: ; preds = %0
%4 = tail call i32 @exit(i32 0)
unreachable
5: ; preds = %0
%6 = load i32, i32* @SIGCHLD
%7 = load i32, i32* @SIG_IGN
%8 = tail call i32 @signal(i32 %6, i32 %7)
%9 = load i32, i32* @SIGHUP
%10 = load i32, i32* @SIG_IGN
%11 = tail call i32 @signal(i32 %9, i32 %10)
%12 = tail call i32 (...) @setsid()
%13 = tail call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %5
%16 = tail call i32 @exit(i32 0)
unreachable
17: ; preds = %5
%18 = load i32, i32* @stdin
%19 = tail call i32 @fclose(i32 %18)
%20 = load i32, i32* @stdout
%21 = tail call i32 @fclose(i32 %20)
%22 = load i32, i32* @stderr
%23 = tail call i32 @fclose(i32 %22)
ret void
}