#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getppid();
int /*<<< orphan*/ open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void make_daemon(const char *log_file) {
  pid_t did;
  fprintf(stdout, "Initializing daemon...");
  if (getppid() != 1) {
    did = fork();
    if (did > 0)
      exit(0);
    if (did < 0)
      fprintf(stderr, "could not fork() to make daemon");
    setsid();
  }
  close(0);
  close(1);
  close(2);
  (log_file) ? open(log_file, O_RDWR) : open("/dev/null", O_RDWR);
  dup(0);
  dup(0);
  return;
}


x86 assembly (clang, -O0):
<make_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x0,%al
call   1080 <getppid@plt>
cmp    $0x1,%eax
je     11f5 <make_daemon+0x75>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    11ce <make_daemon+0x4e>
xor    %edi,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jge    11ee <make_daemon+0x6e>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
cmpq   $0x0,-0x8(%rbp)
je     1232 <make_daemon+0xb2>
mov    -0x8(%rbp),%rdi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
jmp    1247 <make_daemon+0xc7>
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
lea    0xdf5(%rip),%rdi
call   1070 <open@plt>
xor    %edi,%edi
call   1030 <dup@plt>
xor    %edi,%edi
call   1030 <dup@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_daemon>:
push   %rbx
mov    %rdi,%rbx
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
call   1080 <getppid@plt>
cmp    $0x1,%eax
je     11cf <make_daemon+0x4f>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jg     1218 <make_daemon+0x98>
jns    11c8 <make_daemon+0x48>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
test   %rbx,%rbx
je     11fd <make_daemon+0x7d>
mov    %rbx,%rdi
jmp    1204 <make_daemon+0x84>
lea    0xe33(%rip),%rdi
call   1070 <open@plt>
xor    %edi,%edi
call   1030 <dup@plt>
xor    %edi,%edi
pop    %rbx
jmp    1030 <dup@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [23 x i8] c"Initializing daemon...\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"could not fork() to make daemon\00"
@O_RDWR = global i32 0
@.str.2 = [10 x i8] c"/dev/null\00"
define void @make_daemon(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
store i8* %0, i8** %2
%4 = load i32, i32* @stdout
%5 = call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (...) @getppid()
%7 = icmp ne i32 %6, 1
br i1 %7, label %8, label %22
8: ; preds = %1
%9 = call i64 (...) @fork()
store i64 %9, i64* %3
%10 = load i64, i64* %3
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %8
%15 = load i64, i64* %3
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %17, %14
%21 = call i32 (...) @setsid()
br label %22
22: ; preds = %20, %1
%23 = call i32 @close(i32 0)
%24 = call i32 @close(i32 1)
%25 = call i32 @close(i32 2)
%26 = load i8*, i8** %2
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i8*, i8** %2
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* %29, i32 %30)
br label %35
32: ; preds = %22
%33 = load i32, i32* @O_RDWR
%34 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %32, %28
%36 = phi i32 [ %31, %28 ], [ %34, %32 ]
%37 = call i32 @dup(i32 0)
%38 = call i32 @dup(i32 0)
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [23 x i8] c"Initializing daemon...\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"could not fork() to make daemon\00"
@O_RDWR = global i32 0
@.str.2 = [10 x i8] c"/dev/null\00"
define void @make_daemon(i8* %0) {
%2 = load i32, i32* @stdout
%3 = tail call i32 @fprintf(i32 %2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 (...) @getppid()
%5 = icmp eq i32 %4, 1
br i1 %5, label %18, label %6
6: ; preds = %1
%7 = tail call i64 (...) @fork()
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %6
%12 = icmp slt i64 %7, 0
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %13, %11
%17 = tail call i32 (...) @setsid()
br label %18
18: ; preds = %16, %1
%19 = tail call i32 @close(i32 0)
%20 = tail call i32 @close(i32 1)
%21 = tail call i32 @close(i32 2)
%22 = icmp eq i8* %0, null
%23 = load i32, i32* @O_RDWR
br i1 %22, label %26, label %24
24: ; preds = %18
%25 = tail call i32 @open(i8* %0, i32 %23)
br label %28
26: ; preds = %18
%27 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %28
28: ; preds = %26, %24
%29 = tail call i32 @dup(i32 0)
%30 = tail call i32 @dup(i32 0)
ret void
}