#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*/ F_TLOCK;
int O_CREAT;
int O_WRONLY;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int getpid();
int getppid();
scalar_t__ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, int);
scalar_t__ pipe(int *);
int read(int, char *, int);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int write(int, char *, int);
void daemonize(const char *pidfile) {
  if (getppid() == 1) {
    return;
  }
  int notefd[2];
  if (pipe(notefd) < 0) {
    exit(1);
  }
  pid_t pid = fork();
  if (pid < 0) {
    exit(1);
  }
  if (pid > 0) {
    char retval = 0;
    if (read(notefd[0], &retval, 1) != 1) {
      exit(1);
    }
    exit(retval);
  }
  close(notefd[0]);
  umask(0);
  pid_t sid = setsid();
  if (sid < 0) {
    char retval = 2;
    if (write(notefd[1], &retval, 1) != 1) {
    }
    exit(1);
  }
  if (chdir("/") < 0) {
    char retval = 2;
    if (write(notefd[1], &retval, 1) != 1) {
    }
    exit(1);
  }
  if (!freopen("/dev/null", "r", stdin) || !freopen("/dev/null", "w", stdout) ||
      !freopen("/dev/null", "w", stderr)) {
    char retval = 2;
    if (write(notefd[1], &retval, 1) != 1) {
    }
    exit(1);
  }
  pid_t dpid = fork();
  if (dpid < 0) {
    char retval = 2;
    if (write(notefd[1], &retval, 1) != 1) {
    }
    exit(1);
  }
  if (dpid > 0) {
    exit(0);
  }
  if (pidfile) {
    int pidfd = open(pidfile, O_WRONLY | O_CREAT, 0640);
    if (pidfd < 0) {
      char retval = 2;
      if (write(notefd[1], &retval, 1) != 1) {
      }
      exit(1);
    }
    if (lockf(pidfd, F_TLOCK, 0) < 0) {
      char retval = 2;
      if (write(notefd[1], &retval, 1) != 1) {
      }
      exit(1);
    }
    FILE *pidfp = fdopen(pidfd, "w");
    if (!pidfp) {
      char retval = 2;
      if (write(notefd[1], &retval, 1) != 1) {
      }
      exit(1);
    }
    fprintf(pidfp, "%d\n", getpid());
    fflush(pidfp);
  }
  char retval = 0;
  if (write(notefd[1], &retval, 1) != 1) {
    close(notefd[1]);
    exit(1);
  }
  close(notefd[1]);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1110 <getppid@plt>
cmp    $0x1,%eax
jne    1231 <daemonize+0x21>
jmp    158c <daemonize+0x37c>
lea    -0x10(%rbp),%rdi
call   1090 <pipe@plt>
cmp    $0x0,%rax
jge    124e <daemonize+0x3e>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    126e <daemonize+0x5e>
mov    $0x1,%edi
call   1120 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    12aa <daemonize+0x9a>
movb   $0x0,-0x19(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   10b0 <read@plt>
cmp    $0x1,%eax
je     12a1 <daemonize+0x91>
mov    $0x1,%edi
call   1120 <exit@plt>
movsbl -0x19(%rbp),%edi
call   1120 <exit@plt>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   10d0 <umask@plt>
mov    $0x0,%al
call   10a0 <setsid@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    12fc <daemonize+0xec>
movb   $0x2,-0x29(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     12f2 <daemonize+0xe2>
jmp    12f2 <daemonize+0xe2>
mov    $0x1,%edi
call   1120 <exit@plt>
lea    0xcfd(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    133f <daemonize+0x12f>
movb   $0x2,-0x2a(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x2a(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1335 <daemonize+0x125>
jmp    1335 <daemonize+0x125>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    0x2c92(%rip),%rax
mov    (%rax),%edx
lea    0xcb3(%rip),%rdi
lea    0xcb6(%rip),%rsi
call   1070 <freopen@plt>
cmp    $0x0,%eax
je     13ae <daemonize+0x19e>
mov    0x2c55(%rip),%rax
mov    (%rax),%edx
lea    0xc8e(%rip),%rdi
lea    0xc93(%rip),%rsi
call   1070 <freopen@plt>
cmp    $0x0,%eax
je     13ae <daemonize+0x19e>
mov    0x2c38(%rip),%rax
mov    (%rax),%edx
lea    0xc69(%rip),%rdi
lea    0xc6e(%rip),%rsi
call   1070 <freopen@plt>
cmp    $0x0,%eax
jne    13db <daemonize+0x1cb>
movb   $0x2,-0x2b(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x2b(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     13d1 <daemonize+0x1c1>
jmp    13d1 <daemonize+0x1c1>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    141e <daemonize+0x20e>
movb   $0x2,-0x39(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x39(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1414 <daemonize+0x204>
jmp    1414 <daemonize+0x204>
mov    $0x1,%edi
call   1120 <exit@plt>
cmpq   $0x0,-0x38(%rbp)
jle    1430 <daemonize+0x220>
xor    %edi,%edi
call   1120 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     1554 <daemonize+0x344>
mov    -0x8(%rbp),%rdi
mov    0x2b6a(%rip),%rax
mov    (%rax),%esi
mov    0x2ba9(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
call   1100 <open@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    1495 <daemonize+0x285>
movb   $0x2,-0x41(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     148b <daemonize+0x27b>
jmp    148b <daemonize+0x27b>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x40(%rbp),%edi
mov    0x2b49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <lockf@plt>
cmp    $0x0,%rax
jge    14df <daemonize+0x2cf>
movb   $0x2,-0x42(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x42(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     14d5 <daemonize+0x2c5>
jmp    14d5 <daemonize+0x2c5>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x40(%rbp),%edi
lea    0xb25(%rip),%rsi
call   10f0 <fdopen@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    152a <daemonize+0x31a>
movb   $0x2,-0x51(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1520 <daemonize+0x310>
jmp    1520 <daemonize+0x310>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%edx
lea    0xaca(%rip),%rsi
call   10c0 <fprintf@plt>
mov    -0x50(%rbp),%rdi
call   10e0 <fflush@plt>
movb   $0x0,-0x52(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x52(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1584 <daemonize+0x374>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1110 <getppid@plt>
cmp    $0x1,%eax
je     13a7 <daemonize+0x197>
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
test   %rax,%rax
js     13f6 <daemonize+0x1e6>
xor    %eax,%eax
call   1130 <fork@plt>
test   %rax,%rax
js     13f6 <daemonize+0x1e6>
jne    13cf <daemonize+0x1bf>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   10d0 <umask@plt>
xor    %eax,%eax
call   10a0 <setsid@plt>
test   %rax,%rax
js     13ad <daemonize+0x19d>
lea    0xd88(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
js     13ad <daemonize+0x19d>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edx
lea    0xd6c(%rip),%rdi
lea    0xd6f(%rip),%rsi
call   1070 <freopen@plt>
test   %eax,%eax
je     13ad <daemonize+0x19d>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edx
lea    0xd48(%rip),%rdi
lea    0xd4d(%rip),%rsi
call   1070 <freopen@plt>
test   %eax,%eax
je     13ad <daemonize+0x19d>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edx
lea    0xd24(%rip),%rdi
lea    0xd29(%rip),%rsi
call   1070 <freopen@plt>
test   %eax,%eax
je     13ad <daemonize+0x19d>
xor    %eax,%eax
call   1130 <fork@plt>
test   %rax,%rax
js     13ad <daemonize+0x19d>
jne    1400 <daemonize+0x1f0>
test   %rbx,%rbx
je     137f <daemonize+0x16f>
mov    0x2c9c(%rip),%rax
mov    0x2cdd(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a0,%edx
call   1100 <open@plt>
test   %eax,%eax
js     13ad <daemonize+0x19d>
mov    %eax,%ebx
mov    0x2caf(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <lockf@plt>
test   %rax,%rax
js     13ad <daemonize+0x19d>
lea    0xcbe(%rip),%rsi
mov    %ebx,%edi
call   10f0 <fdopen@plt>
test   %rax,%rax
je     13ad <daemonize+0x19d>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xca3(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   10c0 <fprintf@plt>
mov    %rbx,%rdi
call   10e0 <fflush@plt>
movb   $0x0,0x7(%rsp)
mov    0xc(%rsp),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1080 <close@plt>
cmp    $0x1,%ebx
jne    13f6 <daemonize+0x1e6>
add    $0x10,%rsp
pop    %rbx
ret
movb   $0x2,0x7(%rsp)
mov    0xc(%rsp),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
movb   $0x0,0x7(%rsp)
mov    0x8(%rsp),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   10b0 <read@plt>
cmp    $0x1,%eax
jne    13f6 <daemonize+0x1e6>
movsbl 0x7(%rsp),%edi
call   1120 <exit@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
xor    %edi,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.3 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@F_TLOCK = global i32 0
@.str.4 = [4 x i8] c"%d\0A\00"
define void @daemonize(i8* %0) {
%2 = alloca i8*
%3 = alloca [2 x i32]
%4 = alloca i64
%5 = alloca i8
%6 = alloca i64
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i64
%11 = alloca i8
%12 = alloca i32
%13 = alloca i8
%14 = alloca i8
%15 = alloca i32*
%16 = alloca i8
%17 = alloca i8
store i8* %0, i8** %2
%18 = call i32 (...) @getppid()
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %21
20: ; preds = %1
br label %176
21: ; preds = %1
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%23 = call i64 @pipe(i32* %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = call i64 (...) @fork()
store i64 %28, i64* %4
%29 = load i64, i64* %4
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = load i64, i64* %4
%35 = icmp sgt i64 %34, 0
br i1 %35, label %36, label %47
36: ; preds = %33
store i8 0, i8* %5
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%38 = load i32, i32* %37
%39 = call i32 @read(i32 %38, i8* %5, i32 1)
%40 = icmp ne i32 %39, 1
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %36
%44 = load i8, i8* %5
%45 = sext i8 %44 to i32
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %33
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = call i32 @umask(i32 0)
%52 = call i64 (...) @setsid()
store i64 %52, i64* %6
%53 = load i64, i64* %6
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %63
55: ; preds = %47
store i8 2, i8* %7
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%57 = load i32, i32* %56
%58 = call i32 @write(i32 %57, i8* %7, i32 1)
%59 = icmp ne i32 %58, 1
br i1 %59, label %60, label %61
60: ; preds = %55
br label %61
61: ; preds = %60, %55
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %47
%64 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %74
66: ; preds = %63
store i8 2, i8* %8
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @write(i32 %68, i8* %8, i32 1)
%70 = icmp ne i32 %69, 1
br i1 %70, label %71, label %72
71: ; preds = %66
br label %72
72: ; preds = %71, %66
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %63
%75 = load i32, i32* @stdin
%76 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %75)
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %86
78: ; preds = %74
%79 = load i32, i32* @stdout
%80 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %79)
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %86
82: ; preds = %78
%83 = load i32, i32* @stderr
%84 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %83)
%85 = icmp ne i32 %84, 0
br i1 %85, label %94, label %86
86: ; preds = %82, %78, %74
store i8 2, i8* %9
%87 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%88 = load i32, i32* %87
%89 = call i32 @write(i32 %88, i8* %9, i32 1)
%90 = icmp ne i32 %89, 1
br i1 %90, label %91, label %92
91: ; preds = %86
br label %92
92: ; preds = %91, %86
%93 = call i32 @exit(i32 1)
unreachable
94: ; preds = %82
%95 = call i64 (...) @fork()
store i64 %95, i64* %10
%96 = load i64, i64* %10
%97 = icmp slt i64 %96, 0
br i1 %97, label %98, label %106
98: ; preds = %94
store i8 2, i8* %11
%99 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%100 = load i32, i32* %99
%101 = call i32 @write(i32 %100, i8* %11, i32 1)
%102 = icmp ne i32 %101, 1
br i1 %102, label %103, label %104
103: ; preds = %98
br label %104
104: ; preds = %103, %98
%105 = call i32 @exit(i32 1)
unreachable
106: ; preds = %94
%107 = load i64, i64* %10
%108 = icmp sgt i64 %107, 0
br i1 %108, label %109, label %111
109: ; preds = %106
%110 = call i32 @exit(i32 0)
unreachable
111: ; preds = %106
%112 = load i8*, i8** %2
%113 = icmp ne i8* %112, null
br i1 %113, label %114, label %162
114: ; preds = %111
%115 = load i8*, i8** %2
%116 = load i32, i32* @O_WRONLY
%117 = load i32, i32* @O_CREAT
%118 = or i32 %116, %117
%119 = call i32 @open(i8* %115, i32 %118, i32 416)
store i32 %119, i32* %12
%120 = load i32, i32* %12
%121 = icmp slt i32 %120, 0
br i1 %121, label %122, label %130
122: ; preds = %114
store i8 2, i8* %13
%123 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%124 = load i32, i32* %123
%125 = call i32 @write(i32 %124, i8* %13, i32 1)
%126 = icmp ne i32 %125, 1
br i1 %126, label %127, label %128
127: ; preds = %122
br label %128
128: ; preds = %127, %122
%129 = call i32 @exit(i32 1)
unreachable
130: ; preds = %114
%131 = load i32, i32* %12
%132 = load i32, i32* @F_TLOCK
%133 = call i64 @lockf(i32 %131, i32 %132, i32 0)
%134 = icmp slt i64 %133, 0
br i1 %134, label %135, label %143
135: ; preds = %130
store i8 2, i8* %14
%136 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%137 = load i32, i32* %136
%138 = call i32 @write(i32 %137, i8* %14, i32 1)
%139 = icmp ne i32 %138, 1
br i1 %139, label %140, label %141
140: ; preds = %135
br label %141
141: ; preds = %140, %135
%142 = call i32 @exit(i32 1)
unreachable
143: ; preds = %130
%144 = load i32, i32* %12
%145 = call i32* @fdopen(i32 %144, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %145, i32** %15
%146 = load i32*, i32** %15
%147 = icmp ne i32* %146, null
br i1 %147, label %156, label %148
148: ; preds = %143
store i8 2, i8* %16
%149 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%150 = load i32, i32* %149
%151 = call i32 @write(i32 %150, i8* %16, i32 1)
%152 = icmp ne i32 %151, 1
br i1 %152, label %153, label %154
153: ; preds = %148
br label %154
154: ; preds = %153, %148
%155 = call i32 @exit(i32 1)
unreachable
156: ; preds = %143
%157 = load i32*, i32** %15
%158 = call i32 (...) @getpid()
%159 = call i32 @fprintf(i32* %157, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %158)
%160 = load i32*, i32** %15
%161 = call i32 @fflush(i32* %160)
br label %162
162: ; preds = %156, %111
store i8 0, i8* %17
%163 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%164 = load i32, i32* %163
%165 = call i32 @write(i32 %164, i8* %17, i32 1)
%166 = icmp ne i32 %165, 1
br i1 %166, label %167, label %172
167: ; preds = %162
%168 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%169 = load i32, i32* %168
%170 = call i32 @close(i32 %169)
%171 = call i32 @exit(i32 1)
unreachable
172: ; preds = %162
%173 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%174 = load i32, i32* %173
%175 = call i32 @close(i32 %174)
br label %176
176: ; preds = %172, %20
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.3 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@F_TLOCK = global i32 0
@.str.4 = [4 x i8] c"%d\0A\00"
define void @daemonize(i8* %0) {
%2 = alloca [2 x i32]
%3 = alloca i8
%4 = alloca i8
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
%12 = tail call i32 (...) @getppid()
%13 = icmp eq i32 %12, 1
br i1 %13, label %130, label %14
14: ; preds = %1
%15 = bitcast [2 x i32]* %2 to i8*
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%17 = call i64 @pipe(i32* %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = call i64 (...) @fork()
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %21
%27 = icmp eq i64 %22, 0
br i1 %27, label %38, label %28
28: ; preds = %26
store i8 0, i8* %3
%29 = load i32, i32* %16
%30 = call i32 @read(i32 %29, i8* %3, i32 1)
%31 = icmp eq i32 %30, 1
br i1 %31, label %34, label %32
32: ; preds = %28
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %28
%35 = load i8, i8* %3
%36 = sext i8 %35 to i32
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %26
%39 = load i32, i32* %16
%40 = call i32 @close(i32 %39)
%41 = call i32 @umask(i32 0)
%42 = call i64 (...) @setsid()
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %38
store i8 2, i8* %4
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @write(i32 %46, i8* %4, i32 1)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %38
%50 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %49
store i8 2, i8* %5
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @write(i32 %54, i8* %5, i32 1)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %49
%58 = load i32, i32* @stdin
%59 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %58)
%60 = icmp eq i32 %59, 0
br i1 %60, label %69, label %61
61: ; preds = %57
%62 = load i32, i32* @stdout
%63 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %62)
%64 = icmp eq i32 %63, 0
br i1 %64, label %69, label %65
65: ; preds = %61
%66 = load i32, i32* @stderr
%67 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %66)
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %74
69: ; preds = %65, %61, %57
store i8 2, i8* %6
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%71 = load i32, i32* %70
%72 = call i32 @write(i32 %71, i8* %6, i32 1)
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %65
%75 = call i64 (...) @fork()
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %82
77: ; preds = %74
store i8 2, i8* %7
%78 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%79 = load i32, i32* %78
%80 = call i32 @write(i32 %79, i8* %7, i32 1)
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %74
%83 = icmp eq i64 %75, 0
br i1 %83, label %86, label %84
84: ; preds = %82
%85 = call i32 @exit(i32 0)
unreachable
86: ; preds = %82
%87 = icmp eq i8* %0, null
br i1 %87, label %120, label %88
88: ; preds = %86
%89 = load i32, i32* @O_WRONLY
%90 = load i32, i32* @O_CREAT
%91 = or i32 %90, %89
%92 = call i32 @open(i8* %0, i32 %91, i32 416)
%93 = icmp slt i32 %92, 0
br i1 %93, label %94, label %99
94: ; preds = %88
store i8 2, i8* %8
%95 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%96 = load i32, i32* %95
%97 = call i32 @write(i32 %96, i8* %8, i32 1)
%98 = call i32 @exit(i32 1)
unreachable
99: ; preds = %88
%100 = load i32, i32* @F_TLOCK
%101 = call i64 @lockf(i32 %92, i32 %100, i32 0)
%102 = icmp slt i64 %101, 0
br i1 %102, label %103, label %108
103: ; preds = %99
store i8 2, i8* %9
%104 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%105 = load i32, i32* %104
%106 = call i32 @write(i32 %105, i8* %9, i32 1)
%107 = call i32 @exit(i32 1)
unreachable
108: ; preds = %99
%109 = call i32* @fdopen(i32 %92, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%110 = icmp eq i32* %109, null
br i1 %110, label %111, label %116
111: ; preds = %108
store i8 2, i8* %10
%112 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%113 = load i32, i32* %112
%114 = call i32 @write(i32 %113, i8* %10, i32 1)
%115 = call i32 @exit(i32 1)
unreachable
116: ; preds = %108
%117 = call i32 (...) @getpid()
%118 = call i32 @fprintf(i32* %109, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %117)
%119 = call i32 @fflush(i32* %109)
br label %120
120: ; preds = %116, %86
store i8 0, i8* %11
%121 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%122 = load i32, i32* %121
%123 = call i32 @write(i32 %122, i8* %11, i32 1)
%124 = icmp eq i32 %123, 1
%125 = load i32, i32* %121
%126 = call i32 @close(i32 %125)
br i1 %124, label %129, label %127
127: ; preds = %120
%128 = call i32 @exit(i32 1)
unreachable
129: ; preds = %120
br label %130
130: ; preds = %1, %129
ret void
}