#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 */
struct passwd {
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_gid;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ chdir(char *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ errno;
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*/ *);
struct passwd *getpwnam(char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setgroups(int, int /*<<< orphan*/ *);
scalar_t__ setsid();
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *, ...);
void daemonize(char *dir, char *user) {
  pid_t pid;
  FILE *pidfd;
  struct passwd *pw;
  if ((pidfd = fopen("/var/run/flashpolicyserv.pid", "w")) == NULL) {
    fprintf(stderr, "Failed to open pid file %s: %s\n",
            "/var/run/flashpolicyserv.pid", strerror(errno));
  }
  if ((pw = getpwnam(user)) == NULL) {
    fprintf(stderr, "getpwnam(%s) failed: %s\n", user, strerror(errno));
    exit(EXIT_FAILURE);
  }
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork failed: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (pid > 0)
    _exit(0);
  if (setsid() < 0) {
    syslog(LOG_ERR, "setsid() failed: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if ((pid = fork()) < 0) {
    syslog(LOG_ERR, "fork (2) failed: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    if (pidfd != NULL) {
      fprintf(pidfd, "%d\n", pid);
      fclose(pidfd);
    }
    _exit(0);
  }
  if (!freopen("/dev/null", "r", stdin) || !freopen("/dev/null", "w", stdout) ||
      !freopen("/dev/null", "w", stderr)) {
    syslog(LOG_WARNING, "close std fds %s failed: %s\n", dir, strerror(errno));
  }
  if (chroot(dir) < 0) {
    syslog(LOG_ERR, "chroot(%s) failed: %s\n", dir, strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (chdir("/") < 0) {
    syslog(LOG_ERR, "chdir(\"/\") failed: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (setgroups(1, &pw->pw_gid) < 0) {
    syslog(LOG_ERR, "setgroups() failed: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (setgid(pw->pw_gid)) {
    syslog(LOG_ERR, "setgid %i (user=%s) failed: %s\n", pw->pw_gid, user,
           strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (setuid(pw->pw_uid)) {
    syslog(LOG_ERR, "setuid %i (user=%s) failed: %s\n", pw->pw_uid, user,
           strerror(errno));
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xde6(%rip),%rdi
lea    0xdfc(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    126c <daemonize+0x6c>
mov    0x2d95(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rcx
lea    0xdc1(%rip),%rsi
lea    0xd9b(%rip),%rdx
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <getpwnam@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12ce <daemonize+0xce>
mov    0x2d46(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xd86(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1322 <daemonize+0x122>
mov    0x2ce6(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rdx
lea    0xd4b(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1334 <daemonize+0x134>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1090 <setsid@plt>
cmp    $0x0,%rax
jge    1381 <daemonize+0x181>
mov    0x2c5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xcfd(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    0x2c46(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    13d2 <daemonize+0x1d2>
mov    0x2c0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2c1f(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xcc1(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    0x2bf5(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    140e <daemonize+0x20e>
cmpq   $0x0,-0x20(%rbp)
je     1407 <daemonize+0x207>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xc9c(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    0x2bd3(%rip),%rax
mov    (%rax),%rdx
lea    0xc78(%rip),%rdi
lea    0xc7b(%rip),%rsi
call   1070 <freopen@plt>
cmp    $0x0,%eax
je     1480 <daemonize+0x280>
mov    0x2b7d(%rip),%rax
mov    (%rax),%rdx
lea    0xc52(%rip),%rdi
lea    0xbd1(%rip),%rsi
call   1070 <freopen@plt>
cmp    $0x0,%eax
je     1480 <daemonize+0x280>
mov    0x2b6f(%rip),%rax
mov    (%rax),%rdx
lea    0xc2c(%rip),%rdi
lea    0xbab(%rip),%rsi
call   1070 <freopen@plt>
cmp    $0x0,%eax
jne    14ba <daemonize+0x2ba>
mov    0x2b59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2b2d(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xbf0(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    -0x8(%rbp),%rdi
call   1040 <chroot@plt>
cmp    $0x0,%rax
jge    1515 <daemonize+0x315>
mov    0x2ad4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2ae0(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rdx
mov    %rax,%rcx
lea    0xbc0(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    0x2ab2(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    0xbbb(%rip),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
jge    1567 <daemonize+0x367>
mov    0x2a76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    0x2a8a(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x70(%rbp),%edi
mov    %rax,%rdx
lea    0xb87(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    0x2a60(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    $0x1,%edi
call   1080 <setgroups@plt>
cmp    $0x0,%rax
jge    15bf <daemonize+0x3bf>
mov    0x2a1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    0x2a32(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x74(%rbp),%edi
mov    %rax,%rdx
lea    0xb46(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    0x2a08(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   10d0 <setgid@plt>
cmp    $0x0,%rax
je     1639 <daemonize+0x439>
mov    0x29cc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x28(%rbp),%rax
movslq 0x4(%rax),%rax
mov    %rax,-0x88(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    0x29c6(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x8c(%rbp),%edi
mov    -0x88(%rbp),%rdx
mov    -0x80(%rbp),%rcx
mov    %rax,%r8
lea    0xae4(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    0x298e(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1100 <setuid@plt>
cmp    $0x0,%rax
je     16b7 <daemonize+0x4b7>
mov    0x2953(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa4(%rbp)
mov    -0x28(%rbp),%rax
movslq (%rax),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    0x294b(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0xa4(%rbp),%edi
mov    -0xa0(%rbp),%rdx
mov    -0x98(%rbp),%rcx
mov    %rax,%r8
lea    0xa86(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    0x2910(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xde8(%rip),%rdi
lea    0xdfe(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    125f <daemonize+0x5f>
mov    0x2d9d(%rip),%rax
mov    (%rax),%rbx
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xdd4(%rip),%rsi
lea    0xdae(%rip),%rdx
mov    %rbx,%rdi
mov    %rax,%rcx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    %r14,%rdi
call   10a0 <getpwnam@plt>
test   %rax,%rax
je     13b0 <daemonize+0x1b0>
mov    %rax,%r13
xor    %eax,%eax
call   1120 <fork@plt>
test   %rax,%rax
js     13e4 <daemonize+0x1e4>
jne    147c <daemonize+0x27c>
xor    %eax,%eax
call   1090 <setsid@plt>
test   %rax,%rax
js     1415 <daemonize+0x215>
xor    %eax,%eax
call   1120 <fork@plt>
test   %rax,%rax
js     1438 <daemonize+0x238>
jne    145b <daemonize+0x25b>
mov    0x2d32(%rip),%rax
mov    (%rax),%rdx
lea    0xdd7(%rip),%rdi
lea    0xdda(%rip),%rsi
call   1070 <freopen@plt>
test   %eax,%eax
je     1312 <daemonize+0x112>
mov    0x2ce1(%rip),%rax
mov    (%rax),%rdx
lea    0xdb6(%rip),%rdi
lea    0xd35(%rip),%rsi
call   1070 <freopen@plt>
test   %eax,%eax
je     1312 <daemonize+0x112>
mov    0x2cd8(%rip),%rax
mov    (%rax),%rdx
lea    0xd95(%rip),%rdi
lea    0xd14(%rip),%rsi
call   1070 <freopen@plt>
test   %eax,%eax
jne    133f <daemonize+0x13f>
mov    0x2cc7(%rip),%rax
mov    (%rax),%ebx
mov    0x2ca6(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xd73(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   10c0 <syslog@plt>
mov    %r15,%rdi
call   1040 <chroot@plt>
test   %rax,%rax
js     1483 <daemonize+0x283>
lea    0xd80(%rip),%rdi
call   1060 <chdir@plt>
test   %rax,%rax
js     14b5 <daemonize+0x2b5>
lea    0x4(%r13),%rbx
mov    $0x1,%edi
mov    %rbx,%rsi
call   1080 <setgroups@plt>
test   %rax,%rax
js     14d5 <daemonize+0x2d5>
mov    (%rbx),%edi
call   10d0 <setgid@plt>
test   %rax,%rax
jne    1501 <daemonize+0x301>
mov    0x0(%r13),%edi
call   1100 <setuid@plt>
test   %rax,%rax
jne    1529 <daemonize+0x329>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c19(%rip),%rax
mov    (%rax),%rbx
mov    0x2c07(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xc70(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   10b0 <fprintf@plt>
jmp    155d <daemonize+0x35d>
mov    0x2be5(%rip),%rax
mov    (%rax),%rbx
mov    0x2bd3(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xc55(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   10b0 <fprintf@plt>
jmp    155d <daemonize+0x35d>
mov    0x2b8c(%rip),%rax
mov    (%rax),%ebx
mov    0x2ba3(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xc36(%rip),%rsi
jmp    14f3 <daemonize+0x2f3>
mov    0x2b69(%rip),%rax
mov    (%rax),%ebx
mov    0x2b80(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xc28(%rip),%rsi
jmp    14f3 <daemonize+0x2f3>
test   %r12,%r12
je     147c <daemonize+0x27c>
lea    0xc2c(%rip),%rsi
mov    %r12,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    0x2b1e(%rip),%rax
mov    (%rax),%ebx
mov    0x2b35(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xc1f(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   10c0 <syslog@plt>
jmp    155d <daemonize+0x35d>
mov    0x2aec(%rip),%rax
mov    (%rax),%ebx
mov    0x2b03(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xc06(%rip),%rsi
jmp    14f3 <daemonize+0x2f3>
mov    0x2acc(%rip),%rax
mov    (%rax),%ebx
mov    0x2ae3(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xbfd(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   10c0 <syslog@plt>
jmp    155d <daemonize+0x35d>
mov    0x2aa0(%rip),%rax
mov    (%rax),%ebp
movslq (%rbx),%rbx
mov    0x2ab4(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xbe6(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
jmp    1550 <daemonize+0x350>
mov    0x2a78(%rip),%rax
mov    (%rax),%ebx
movslq 0x0(%r13),%rbp
mov    0x2a8b(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xbdd(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %r14,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   10c0 <syslog@plt>
mov    0x2a5c(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

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

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@.str = [29 x i8] c"/var/run/flashpolicyserv.pid\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32* null
@.str.2 = [32 x i8] c"Failed to open pid file %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [25 x i8] c"getpwnam(%s) failed: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [17 x i8] c"fork failed: %s\0A\00"
@LOG_ERR = global i32 0
@.str.5 = [21 x i8] c"setsid() failed: %s\0A\00"
@.str.6 = [21 x i8] c"fork (2) failed: %s\0A\00"
@.str.7 = [4 x i8] c"%d\0A\00"
@.str.8 = [10 x i8] c"/dev/null\00"
@.str.9 = [2 x i8] c"r\00"
@stdin = global i32* null
@stdout = global i32* null
@LOG_WARNING = global i32 0
@.str.10 = [29 x i8] c"close std fds %s failed: %s\0A\00"
@.str.11 = [23 x i8] c"chroot(%s) failed: %s\0A\00"
@.str.12 = [2 x i8] c"/\00"
@.str.13 = [23 x i8] c"chdir(\22/\22) failed: %s\0A\00"
@.str.14 = [24 x i8] c"setgroups() failed: %s\0A\00"
@.str.15 = [32 x i8] c"setgid %i (user=%s) failed: %s\0A\00"
@.str.16 = [32 x i8] c"setuid %i (user=%s) failed: %s\0A\00"
define void @daemonize(i8* %0, i8* %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32*, i32** @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i32*, i8*, i64, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 ptrtoint ([29 x i8]* @.str to i64), i8* %8)
br label %10
10: ; preds = %5, %2
%11 = tail call %struct.passwd* @getpwnam(i8* %1)
%12 = icmp eq %struct.passwd* %11, null
br i1 %12, label %13, label %21
13: ; preds = %10
%14 = load i32*, i32** @stderr
%15 = ptrtoint i8* %1 to i64
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 (i32*, i8*, i64, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %15, i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %10
%22 = tail call i64 (...) @fork()
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = load i32*, i32** @stderr
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = ptrtoint i8* %27 to i64
%29 = tail call i32 (i32*, i8*, i64, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i64 %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %21
%33 = icmp eq i64 %22, 0
br i1 %33, label %36, label %34
34: ; preds = %32
%35 = tail call i32 @_exit(i32 0)
unreachable
36: ; preds = %32
%37 = tail call i64 (...) @setsid()
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = load i32, i32* @LOG_ERR
%41 = load i32, i32* @errno
%42 = tail call i8* @strerror(i32 %41)
%43 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %40, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %42)
%44 = load i32, i32* @EXIT_FAILURE
%45 = tail call i32 @exit(i32 %44)
unreachable
46: ; preds = %36
%47 = tail call i64 (...) @fork()
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %46
%50 = load i32, i32* @LOG_ERR
%51 = load i32, i32* @errno
%52 = tail call i8* @strerror(i32 %51)
%53 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %50, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %52)
%54 = load i32, i32* @EXIT_FAILURE
%55 = tail call i32 @exit(i32 %54)
unreachable
56: ; preds = %46
%57 = icmp eq i64 %47, 0
br i1 %57, label %64, label %58
58: ; preds = %56
br i1 %4, label %62, label %59
59: ; preds = %58
%60 = tail call i32 (i32*, i8*, i64, ...) @fprintf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i64 %47)
%61 = tail call i32 @fclose(i32* %3)
br label %62
62: ; preds = %59, %58
%63 = tail call i32 @_exit(i32 0)
unreachable
64: ; preds = %56
%65 = load i32*, i32** @stdin
%66 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0), i32* %65)
%67 = icmp eq i32 %66, 0
br i1 %67, label %76, label %68
68: ; preds = %64
%69 = load i32*, i32** @stdout
%70 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32* %69)
%71 = icmp eq i32 %70, 0
br i1 %71, label %76, label %72
72: ; preds = %68
%73 = load i32*, i32** @stderr
%74 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32* %73)
%75 = icmp eq i32 %74, 0
br i1 %75, label %76, label %81
76: ; preds = %72, %68, %64
%77 = load i32, i32* @LOG_WARNING
%78 = load i32, i32* @errno
%79 = tail call i8* @strerror(i32 %78)
%80 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %77, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0), i8* %0, i8* %79)
br label %81
81: ; preds = %76, %72
%82 = tail call i64 @chroot(i8* %0)
%83 = icmp slt i64 %82, 0
br i1 %83, label %84, label %91
84: ; preds = %81
%85 = load i32, i32* @LOG_ERR
%86 = load i32, i32* @errno
%87 = tail call i8* @strerror(i32 %86)
%88 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %85, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.11, i64 0, i64 0), i8* %0, i8* %87)
%89 = load i32, i32* @EXIT_FAILURE
%90 = tail call i32 @exit(i32 %89)
unreachable
91: ; preds = %81
%92 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%93 = icmp slt i64 %92, 0
br i1 %93, label %94, label %101
94: ; preds = %91
%95 = load i32, i32* @LOG_ERR
%96 = load i32, i32* @errno
%97 = tail call i8* @strerror(i32 %96)
%98 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %95, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.13, i64 0, i64 0), i8* %97)
%99 = load i32, i32* @EXIT_FAILURE
%100 = tail call i32 @exit(i32 %99)
unreachable
101: ; preds = %91
%102 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i64 0, i32 1
%103 = tail call i64 @setgroups(i32 1, i32* %102)
%104 = icmp slt i64 %103, 0
br i1 %104, label %105, label %112
105: ; preds = %101
%106 = load i32, i32* @LOG_ERR
%107 = load i32, i32* @errno
%108 = tail call i8* @strerror(i32 %107)
%109 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %106, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.14, i64 0, i64 0), i8* %108)
%110 = load i32, i32* @EXIT_FAILURE
%111 = tail call i32 @exit(i32 %110)
unreachable
112: ; preds = %101
%113 = load i32, i32* %102
%114 = tail call i64 @setgid(i32 %113)
%115 = icmp eq i64 %114, 0
br i1 %115, label %126, label %116
116: ; preds = %112
%117 = load i32, i32* @LOG_ERR
%118 = load i32, i32* %102
%119 = sext i32 %118 to i64
%120 = inttoptr i64 %119 to i8*
%121 = load i32, i32* @errno
%122 = tail call i8* @strerror(i32 %121)
%123 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %117, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.15, i64 0, i64 0), i8* %120, i8* %1, i8* %122)
%124 = load i32, i32* @EXIT_FAILURE
%125 = tail call i32 @exit(i32 %124)
unreachable
126: ; preds = %112
%127 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i64 0, i32 0
%128 = load i32, i32* %127
%129 = tail call i64 @setuid(i32 %128)
%130 = icmp eq i64 %129, 0
br i1 %130, label %141, label %131
131: ; preds = %126
%132 = load i32, i32* @LOG_ERR
%133 = load i32, i32* %127
%134 = sext i32 %133 to i64
%135 = inttoptr i64 %134 to i8*
%136 = load i32, i32* @errno
%137 = tail call i8* @strerror(i32 %136)
%138 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %132, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.16, i64 0, i64 0), i8* %135, i8* %1, i8* %137)
%139 = load i32, i32* @EXIT_FAILURE
%140 = tail call i32 @exit(i32 %139)
unreachable
141: ; preds = %126
ret void
}