#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 */
scalar_t__ EOF;
scalar_t__ chdir(char *);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, scalar_t__);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
scalar_t__ getpid();
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(char *pidfile) {
  FILE *pidfile_h;
  pid_t pid;
  pid = fork();
  if (pid < 0)
    err(1, "first fork");
  if (pid > 0)
    exit(0);
  if (setsid() < 0)
    err(1, "setsid");
  if (chdir("/") < 0)
    err(1, "chdir");
  umask(0);
  freopen("/dev/null", "r", stdin);
  freopen("/dev/null", "w", stdout);
  freopen("/dev/null", "w", stderr);
  pid = fork();
  if (pid < 0)
    err(1, "second fork");
  if (pid > 0)
    exit(0);
  pid = getpid();
  if ((pidfile_h = fopen(pidfile, "w+")) == NULL)
    err(1, "fopen");
  if (fprintf(pidfile_h, "%d", pid) < 0)
    err(1, "fprintf");
  if (fclose(pidfile_h) == EOF)
    err(1, "fclose");
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11e3 <daemonize+0x33>
mov    $0x1,%edi
lea    0xe22(%rip),%rsi
call   10a0 <err@plt>
cmpq   $0x0,-0x18(%rbp)
jle    11f5 <daemonize+0x45>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    1217 <daemonize+0x67>
mov    $0x1,%edi
lea    0xdf9(%rip),%rsi
call   10a0 <err@plt>
lea    0xdf4(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    123e <daemonize+0x8e>
mov    $0x1,%edi
lea    0xddb(%rip),%rsi
call   10a0 <err@plt>
xor    %edi,%edi
call   1090 <umask@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
lea    0xdc5(%rip),%rdi
lea    0xdbc(%rip),%rsi
call   1060 <freopen@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
lea    0xda9(%rip),%rdi
lea    0xdac(%rip),%rsi
call   1060 <freopen@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edx
lea    0xd8d(%rip),%rdi
lea    0xd90(%rip),%rsi
call   1060 <freopen@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    12c0 <daemonize+0x110>
mov    $0x1,%edi
lea    0xd6b(%rip),%rsi
call   10a0 <err@plt>
cmpq   $0x0,-0x18(%rbp)
jle    12d2 <daemonize+0x122>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xd4a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    130c <daemonize+0x15c>
mov    $0x1,%edi
lea    0xd2e(%rip),%rsi
call   10a0 <err@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd20(%rip),%rsi
call   1080 <fprintf@plt>
cmp    $0x0,%rax
jge    133b <daemonize+0x18b>
mov    $0x1,%edi
lea    0xd08(%rip),%rsi
call   10a0 <err@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2c95(%rip),%rcx
cmp    (%rcx),%rax
jne    1365 <daemonize+0x1b5>
mov    $0x1,%edi
lea    0xce6(%rip),%rsi
call   10a0 <err@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     11cb <daemonize+0x1b>
je     11dc <daemonize+0x2c>
jmp    127e <daemonize+0xce>
lea    0xe2e(%rip),%rsi
mov    $0x1,%edi
call   10a0 <err@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
jns    11f9 <daemonize+0x49>
lea    0xe1c(%rip),%rsi
mov    $0x1,%edi
call   10a0 <err@plt>
lea    0xe12(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jns    121b <daemonize+0x6b>
lea    0xe03(%rip),%rsi
mov    $0x1,%edi
call   10a0 <err@plt>
xor    %edi,%edi
call   1090 <umask@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
lea    0xde8(%rip),%rbx
lea    0xddf(%rip),%rsi
mov    %rbx,%rdi
call   1060 <freopen@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
lea    0xdd3(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <freopen@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <freopen@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     1285 <daemonize+0xd5>
je     1296 <daemonize+0xe6>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xd9a(%rip),%rsi
mov    $0x1,%edi
call   10a0 <err@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r14
lea    0xd8b(%rip),%rsi
mov    %r15,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12c8 <daemonize+0x118>
lea    0xd77(%rip),%rsi
mov    $0x1,%edi
call   10a0 <err@plt>
lea    0xd6c(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1080 <fprintf@plt>
test   %rax,%rax
jns    12f0 <daemonize+0x140>
lea    0xd58(%rip),%rsi
mov    $0x1,%edi
call   10a0 <err@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2ce1(%rip),%rcx
cmp    (%rcx),%rax
jne    131a <daemonize+0x16a>
lea    0xd3b(%rip),%rsi
mov    $0x1,%edi
pop    %rbx
pop    %r14
pop    %r15
jmp    10a0 <err@plt>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"first fork\00"
@.str.1 = [7 x i8] c"setsid\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [6 x i8] c"chdir\00"
@.str.4 = [10 x i8] c"/dev/null\00"
@.str.5 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.6 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
@.str.7 = [12 x i8] c"second fork\00"
@.str.8 = [3 x i8] c"w+\00"
@.str.9 = [6 x i8] c"fopen\00"
@.str.10 = [3 x i8] c"%d\00"
@.str.11 = [8 x i8] c"fprintf\00"
@EOF = global i64 0
@.str.12 = [7 x i8] c"fclose\00"
define void @daemonize(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
store i8* %0, i8** %2
%5 = call i64 (...) @fork()
store i64 %5, i64* %4
%6 = load i64, i64* %4
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = load i64, i64* %4
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %10
%16 = call i64 (...) @setsid()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
%26 = call i32 @umask(i32 0)
%27 = load i32, i32* @stdin
%28 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%29 = load i32, i32* @stdout
%30 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @stderr
%32 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %31)
%33 = call i64 (...) @fork()
store i64 %33, i64* %4
%34 = load i64, i64* %4
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %25
%37 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %38
38: ; preds = %36, %25
%39 = load i64, i64* %4
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @exit(i32 0)
unreachable
43: ; preds = %38
%44 = call i64 (...) @getpid()
store i64 %44, i64* %4
%45 = load i8*, i8** %2
%46 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
store i32* %46, i32** %3
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
br label %50
50: ; preds = %48, %43
%51 = load i32*, i32** %3
%52 = load i64, i64* %4
%53 = call i64 @fprintf(i32* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i64 %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.11, i64 0, i64 0))
br label %57
57: ; preds = %55, %50
%58 = load i32*, i32** %3
%59 = call i64 @fclose(i32* %58)
%60 = load i64, i64* @EOF
%61 = icmp eq i64 %59, %60
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.12, i64 0, i64 0))
br label %64
64: ; preds = %62, %57
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"first fork\00"
@.str.1 = [7 x i8] c"setsid\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [6 x i8] c"chdir\00"
@.str.4 = [10 x i8] c"/dev/null\00"
@.str.5 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.6 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
@.str.7 = [12 x i8] c"second fork\00"
@.str.8 = [3 x i8] c"w+\00"
@.str.9 = [6 x i8] c"fopen\00"
@.str.10 = [3 x i8] c"%d\00"
@.str.11 = [8 x i8] c"fprintf\00"
@EOF = global i64 0
@.str.12 = [7 x i8] c"fclose\00"
define void @daemonize(i8* nocapture %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %10
6: ; preds = %1
%7 = icmp eq i64 %2, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %4, %6
%11 = tail call i64 (...) @setsid()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = tail call i32 @umask(i32 0)
%22 = load i32, i32* @stdin
%23 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %22)
%24 = load i32, i32* @stdout
%25 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %24)
%26 = load i32, i32* @stderr
%27 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 %26)
%28 = tail call i64 (...) @fork()
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %36
32: ; preds = %20
%33 = icmp eq i64 %28, 0
br i1 %33, label %36, label %34
34: ; preds = %32
%35 = tail call i32 @exit(i32 0)
unreachable
36: ; preds = %30, %32
%37 = tail call i64 (...) @getpid()
%38 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = tail call i64 @fprintf(i32* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i64 %37)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.11, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
%48 = tail call i64 @fclose(i32* %38)
%49 = load i64, i64* @EOF
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.12, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
ret void
}