#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ printf(char *, scalar_t__);
int /*<<< orphan*/ puts(char *);
scalar_t__ setsid();
int /*<<< orphan*/ sleep(int);
void newDaemon(void (*cb)()) {
  pid_t proc_id = fork();
  if (proc_id < 0) {
    puts("Fork failed!");
    exit(1);
  } else if (proc_id > 0) {
    printf("Starting daemon process with pid %d.\n", proc_id);
    exit(0);
  }
  pid_t sid = setsid();
  if (sid < 0)
    exit(1);
  close(STDIN_FILENO);
  close(STDERR_FILENO);
  close(STDOUT_FILENO);
  FILE *fp = fopen("log.txt", "w+");
  while (1) {
    sleep(1);
    fprintf(fp, "Logging info... \n");
    fflush(fp);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<newDaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11d8 <newDaemon+0x38>
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    11fa <newDaemon+0x5a>
mov    -0x10(%rbp),%rsi
lea    0xe1f(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    11ff <newDaemon+0x5f>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    121f <newDaemon+0x7f>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
lea    0xde3(%rip),%rdi
lea    0xde4(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0x20(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1080 <fflush@plt>
jmp    1260 <newDaemon+0xc0>

x86 assembly (clang, -O2):
<newDaemon>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1249 <newDaemon+0xa9>
jne    125f <newDaemon+0xbf>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     1275 <newDaemon+0xd5>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
lea    0xe38(%rip),%rdi
lea    0xe39(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
lea    0xe2d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    $0x11,%esi
mov    $0x1,%edx
mov    %r14,%rdi
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <fflush@plt>
jmp    1220 <newDaemon+0x80>
lea    0xdb0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xda7(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"Fork failed!\00"
@.str.1 = [38 x i8] c"Starting daemon process with pid %d.\0A\00"
@STDIN_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [8 x i8] c"log.txt\00"
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [18 x i8] c"Logging info... \0A\00"
define void @newDaemon(void (...)* %0) {
%2 = alloca void (...)*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32*
store void (...)* %0, void (...)** %2
%6 = call i64 (...) @fork()
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = load i64, i64* %3
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i64, i64* %3
%17 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i64 %16)
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %12
br label %20
20: ; preds = %19
%21 = call i64 (...) @setsid()
store i64 %21, i64* %4
%22 = load i64, i64* %4
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %20
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @STDERR_FILENO
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @close(i32 %31)
%33 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %33, i32** %5
br label %34
34: ; preds = %26, %34
%35 = call i32 @sleep(i32 1)
%36 = load i32*, i32** %5
%37 = call i32 @fprintf(i32* %36, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%38 = load i32*, i32** %5
%39 = call i32 @fflush(i32* %38)
br label %34
}

LLVM IR (-O2):
@.str = [13 x i8] c"Fork failed!\00"
@.str.1 = [38 x i8] c"Starting daemon process with pid %d.\0A\00"
@STDIN_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [8 x i8] c"log.txt\00"
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [18 x i8] c"Logging info... \0A\00"
define void @newDaemon(void (...)* nocapture readnone %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1
%8 = icmp eq i64 %2, 0
br i1 %8, label %12, label %9
9: ; preds = %7
%10 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i64 %2)
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %7
%13 = tail call i64 (...) @setsid()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %12
%18 = load i32, i32* @STDIN_FILENO
%19 = tail call i32 @close(i32 %18)
%20 = load i32, i32* @STDERR_FILENO
%21 = tail call i32 @close(i32 %20)
%22 = load i32, i32* @STDOUT_FILENO
%23 = tail call i32 @close(i32 %22)
%24 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %25, %17
%26 = tail call i32 @sleep(i32 1)
%27 = tail call i64 @fwrite(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i64 17, i64 1, i32* %24)
%28 = tail call i32 @fflush(i32* %24)
br label %25
}