#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*/ LogMessage(char *);
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getppid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setsid();
void GoDaemon(void) {
  int exit_val = 0;
  pid_t cpid;
  int i;
  LogMessage("Initializing daemon mode\n");
  if (getppid() != 1) {
    printf("Spawning daemon child...\n");
    cpid = fork();
    if (cpid > 0) {
      printf("Daemon child %d lives...\n", cpid);
      printf("Daemon parent exiting (%d)\n", exit_val);
      exit(exit_val);
    }
    if (cpid < 0) {
      perror("fork");
      exit(1);
    }
  }
  setsid();
  close(0);
  close(1);
  close(2);
  i = open("/dev/null", O_RDWR);
  dup(i);
  dup(i);
}


x86 assembly (clang, -O0):
<GoDaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rdi
call   1080 <LogMessage@plt>
mov    $0x0,%al
call   10a0 <getppid@plt>
cmp    $0x1,%eax
je     1240 <GoDaemon+0xa0>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    121a <GoDaemon+0x7a>
mov    -0x10(%rbp),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%esi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jge    123b <GoDaemon+0x9b>
lea    0xe3e(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1240 <GoDaemon+0xa0>
mov    $0x0,%al
call   1060 <setsid@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
lea    0xdfd(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <dup@plt>
mov    -0x14(%rbp),%edi
call   1040 <dup@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GoDaemon>:
push   %rbx
lea    0xe48(%rip),%rdi
call   1090 <LogMessage@plt>
xor    %eax,%eax
call   10b0 <getppid@plt>
cmp    $0x1,%eax
je     11e3 <GoDaemon+0x33>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
jg     122b <GoDaemon+0x7b>
js     1253 <GoDaemon+0xa3>
xor    %eax,%eax
call   1070 <setsid@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
lea    0xe40(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1050 <dup@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1050 <dup@plt>
lea    0xde8(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdf1(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xdf6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

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

LLVM IR (-O2):
@.str = [26 x i8] c"Initializing daemon mode\0A\00"
@.str.2 = [26 x i8] c"Daemon child %d lives...\0A\00"
@.str.3 = [28 x i8] c"Daemon parent exiting (%d)\0A\00"
@.str.4 = [5 x i8] c"fork\00"
@.str.5 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@str = [25 x i8] c"Spawning daemon child...\00"
define void @GoDaemon() {
%1 = tail call i32 @LogMessage(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 (...) @getppid()
%3 = icmp eq i32 %2, 1
br i1 %3, label %17, label %4
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%6 = tail call i64 (...) @fork()
%7 = icmp sgt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %4
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i64 %6)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 0)
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %4
%13 = icmp slt i64 %6, 0
br i1 %13, label %14, label %17
14: ; preds = %12
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %12, %0
%18 = tail call i32 (...) @setsid()
%19 = tail call i32 @close(i32 0)
%20 = tail call i32 @close(i32 1)
%21 = tail call i32 @close(i32 2)
%22 = load i32, i32* @O_RDWR
%23 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %22)
%24 = tail call i32 @dup(i32 %23)
%25 = tail call i32 @dup(i32 %23)
ret void
}