#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, scalar_t__);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
void daemonize(char *pidfile) {
  if (!pidfile)
    return;
  printf("Daemonizing.\n");
  pid_t pid = fork();
  if (pid > 0) {
    FILE *F = fopen(pidfile, "w");
    if (F) {
      fprintf(F, "%i\n", pid);
      fclose(F);
    }
    exit(0);
  }
  setsid();
  if (!freopen("/dev/null", "r", stdin))
    return;
  if (!freopen("/dev/null", "w", stdout))
    return;
  if (!freopen("/dev/null", "w", stderr))
    return;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    119c <daemonize+0x1c>
jmp    1286 <daemonize+0x106>
lea    0xe5d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    1201 <daemonize+0x81>
mov    -0x8(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11fa <daemonize+0x7a>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe24(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
lea    0xdfc(%rip),%rdi
lea    0xdff(%rip),%rsi
call   1050 <freopen@plt>
cmp    $0x0,%eax
jne    1232 <daemonize+0xb2>
jmp    1286 <daemonize+0x106>
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
lea    0xdd2(%rip),%rdi
lea    0xdc5(%rip),%rsi
call   1050 <freopen@plt>
cmp    $0x0,%eax
jne    125c <daemonize+0xdc>
jmp    1286 <daemonize+0x106>
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
lea    0xda8(%rip),%rdi
lea    0xd9b(%rip),%rsi
call   1050 <freopen@plt>
cmp    $0x0,%eax
jne    1286 <daemonize+0x106>
jmp    1286 <daemonize+0x106>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1212 <daemonize+0x92>
mov    %rdi,%rbx
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jg     121a <daemonize+0x9a>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
lea    0xe47(%rip),%rdi
lea    0xe4a(%rip),%rsi
call   1050 <freopen@plt>
test   %eax,%eax
je     1212 <daemonize+0x92>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
lea    0xe27(%rip),%rdi
lea    0xe1a(%rip),%rsi
call   1050 <freopen@plt>
test   %eax,%eax
je     1212 <daemonize+0x92>
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
lea    0xe07(%rip),%rdi
lea    0xdfa(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <freopen@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rax,%r14
lea    0xddc(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     124e <daemonize+0xce>
mov    %rax,%rbx
lea    0xdc7(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"Daemonizing.\0A\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%i\0A\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@.str.4 = [2 x i8] c"r\00"
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
define void @daemonize(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
br label %42
8: ; preds = %1
%9 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%10 = call i64 (...) @fork()
store i64 %10, i64* %3
%11 = load i64, i64* %3
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %26
13: ; preds = %8
%14 = load i8*, i8** %2
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %4
%16 = load i32*, i32** %4
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %24
18: ; preds = %13
%19 = load i32*, i32** %4
%20 = load i64, i64* %3
%21 = call i32 @fprintf(i32* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 %20)
%22 = load i32*, i32** %4
%23 = call i32 @fclose(i32* %22)
br label %24
24: ; preds = %18, %13
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %8
%27 = call i32 (...) @setsid()
%28 = load i32, i32* @stdin
%29 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %26
br label %42
32: ; preds = %26
%33 = load i32, i32* @stdout
%34 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %32
br label %42
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %37
br label %42
42: ; preds = %7, %31, %36, %41, %37
ret void
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%i\0A\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@.str.4 = [2 x i8] c"r\00"
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@str = [13 x i8] c"Daemonizing.\00"
define void @daemonize(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %27, label %3
3: ; preds = %1
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%5 = tail call i64 (...) @fork()
%6 = icmp sgt i64 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %3
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = tail call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 %5)
%12 = tail call i32 @fclose(i32* %8)
br label %13
13: ; preds = %10, %7
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %3
%16 = tail call i32 (...) @setsid()
%17 = load i32, i32* @stdin
%18 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %15
%21 = load i32, i32* @stdout
%22 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %15, %20, %24, %1
ret void
}