#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_SUCCESS;
int /*<<< orphan*/ console_print(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
void go_daemon() {
  console_print("Daemonizing...\n");
  pid_t pid = fork();
  if (pid != 0)
    exit(EXIT_SUCCESS);
  setsid();
  fclose(stdin);
  fclose(stdout);
  fclose(stderr);
}


x86 assembly (clang, -O0):
<go_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
call   1040 <console_print@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1188 <go_daemon+0x38>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<go_daemon>:
push   %rax
lea    0xea8(%rip),%rdi
call   1040 <console_print@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    119b <go_daemon+0x4b>
xor    %eax,%eax
call   1050 <setsid@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
pop    %rax
jmp    1030 <fclose@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"Daemonizing...\0A\00"
@EXIT_SUCCESS = global i32 0
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
define void @go_daemon() {
%1 = alloca i64
%2 = call i32 @console_print(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%3 = call i64 (...) @fork()
store i64 %3, i64* %1
%4 = load i64, i64* %1
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @EXIT_SUCCESS
%8 = call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = call i32 (...) @setsid()
%11 = load i32, i32* @stdin
%12 = call i32 @fclose(i32 %11)
%13 = load i32, i32* @stdout
%14 = call i32 @fclose(i32 %13)
%15 = load i32, i32* @stderr
%16 = call i32 @fclose(i32 %15)
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"Daemonizing...\0A\00"
@EXIT_SUCCESS = global i32 0
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
define void @go_daemon() {
%1 = tail call i32 @console_print(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%2 = tail call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %7, label %4
4: ; preds = %0
%5 = load i32, i32* @EXIT_SUCCESS
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = tail call i32 (...) @setsid()
%9 = load i32, i32* @stdin
%10 = tail call i32 @fclose(i32 %9)
%11 = load i32, i32* @stdout
%12 = tail call i32 @fclose(i32 %11)
%13 = load i32, i32* @stderr
%14 = tail call i32 @fclose(i32 %13)
ret void
}