#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/);
scalar_t__ fork();
char *get_argument_as_string(char);
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
char *strerror(int);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void init_daemon() {
  const char *stdout_filename = get_argument_as_string('d');
  if (!stdout_filename || !*stdout_filename)
    return;
  printf("Stdout Filename: %s\n", stdout_filename);
  int fd;
  if ((fd = open(stdout_filename, O_WRONLY | O_CREAT, 0)) < 0) {
    printf("Failed to open file write daemon stdout %s: %d %s\n",
           stdout_filename, errno, strerror(errno));
    exit(1);
  }
  pid_t pid = fork();
  if (pid < 0) {
    printf("Failed to init as daemon\n");
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    printf("Daemon: started\n");
    exit(EXIT_SUCCESS);
  }
  setsid();
  chdir("/");
  umask(0);
  fclose(stdin);
  fclose(stdout);
  fclose(stderr);
  dup2(fd, STDOUT_FILENO);
  close(fd);
}


x86 assembly (clang, -O0):
<init_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x64,%edi
call   1070 <get_argument_as_string@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11ee <init_daemon+0x2e>
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11f3 <init_daemon+0x33>
jmp    1328 <init_daemon+0x168>
mov    -0x8(%rbp),%rsi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2ddf(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   10b0 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1272 <init_daemon+0xb2>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    %rax,%rcx
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    12a4 <init_daemon+0xe4>
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    12cb <init_daemon+0x10b>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   1090 <setsid@plt>
lea    0xd9a(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   10a0 <umask@plt>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    -0xc(%rbp),%edi
mov    0x2ccf(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_daemon>:
push   %rbp
push   %rbx
push   %rax
mov    $0x64,%edi
call   1080 <get_argument_as_string@plt>
test   %rax,%rax
je     12a0 <init_daemon+0xd0>
mov    %rax,%rbx
cmpb   $0x0,(%rax)
je     12a0 <init_daemon+0xd0>
lea    0xe07(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2d8e(%rip),%rax
mov    0x2de7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   10c0 <open@plt>
test   %eax,%eax
js     12a7 <init_daemon+0xd7>
mov    %eax,%ebp
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     12d7 <init_daemon+0x107>
jne    12f1 <init_daemon+0x121>
xor    %eax,%eax
call   10a0 <setsid@plt>
lea    0xdfb(%rip),%rdi
call   1050 <chdir@plt>
xor    %edi,%edi
call   10b0 <umask@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1040 <fclose@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   1040 <fclose@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   1040 <fclose@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1060 <dup2@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1090 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d12(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10e0 <strerror@plt>
lea    0xd57(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd7c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xd52(%rip),%rdi
call   1030 <puts@plt>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"Stdout Filename: %s\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [51 x i8] c"Failed to open file write daemon stdout %s: %d %s\0A\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Failed to init as daemon\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [17 x i8] c"Daemon: started\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [2 x i8] c"/\00"
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@STDOUT_FILENO = global i32 0
define void @init_daemon() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i64
%4 = call i8* @get_argument_as_string(i8 signext 100)
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = load i8*, i8** %1
%9 = load i8, i8* %8
%10 = icmp ne i8 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %7, %0
br label %58
12: ; preds = %7
%13 = load i8*, i8** %1
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i8*, i8** %1
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %15, i32 %18, i32 0)
store i32 %19, i32* %2
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %12
%22 = load i8*, i8** %1
%23 = load i32, i32* @errno
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %22, i32 %23, i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %12
%29 = call i64 (...) @fork()
store i64 %29, i64* %3
%30 = load i64, i64* %3
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %28
%37 = load i64, i64* %3
%38 = icmp sgt i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* @EXIT_SUCCESS
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %36
%44 = call i32 (...) @setsid()
%45 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%46 = call i32 @umask(i32 0)
%47 = load i32, i32* @stdin
%48 = call i32 @fclose(i32 %47)
%49 = load i32, i32* @stdout
%50 = call i32 @fclose(i32 %49)
%51 = load i32, i32* @stderr
%52 = call i32 @fclose(i32 %51)
%53 = load i32, i32* %2
%54 = load i32, i32* @STDOUT_FILENO
%55 = call i32 @dup2(i32 %53, i32 %54)
%56 = load i32, i32* %2
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %43, %11
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"Stdout Filename: %s\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [51 x i8] c"Failed to open file write daemon stdout %s: %d %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.4 = [2 x i8] c"/\00"
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@STDOUT_FILENO = global i32 0
@str = [16 x i8] c"Daemon: started\00"
@str.5 = [25 x i8] c"Failed to init as daemon\00"
define void @init_daemon() {
%1 = tail call i8* @get_argument_as_string(i8 signext 100)
%2 = icmp eq i8* %1, null
br i1 %2, label %44, label %3
3: ; preds = %0
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
br i1 %5, label %44, label %6
6: ; preds = %3
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %1)
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* %1, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %6
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %14, i8* %15)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %6
%19 = tail call i64 (...) @fork()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %18
%26 = icmp eq i64 %19, 0
br i1 %26, label %31, label %27
27: ; preds = %25
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%29 = load i32, i32* @EXIT_SUCCESS
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %25
%32 = tail call i32 (...) @setsid()
%33 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%34 = tail call i32 @umask(i32 0)
%35 = load i32, i32* @stdin
%36 = tail call i32 @fclose(i32 %35)
%37 = load i32, i32* @stdout
%38 = tail call i32 @fclose(i32 %37)
%39 = load i32, i32* @stderr
%40 = tail call i32 @fclose(i32 %39)
%41 = load i32, i32* @STDOUT_FILENO
%42 = tail call i32 @dup2(i32 %11, i32 %41)
%43 = tail call i32 @close(i32 %11)
br label %44
44: ; preds = %0, %3, %31
ret void
}