#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 int ssize_t;
typedef int pid_t;
typedef int /*<<< orphan*/ pid_buf;

/* Variables and functions */
int EEXIST;
int O_APPEND;
int O_CREAT;
int O_EXCL;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int dup2(int, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpid();
int /*<<< orphan*/ my_close(int *);
int open(char *, int, int);
scalar_t__ setsid();
int snprintf(char *, int, char *, long);
int /*<<< orphan*/ stderr;
char *strerror(int);
int unlink(char *);
int wait(int /*<<< orphan*/ *);
int write(int, char *, int);
int daemonize(char **argv, char *output_file, char *pid_file) {
  int pid_fd = -1;
  if (pid_file != NULL) {
    pid_fd = open(pid_file, O_CREAT | O_EXCL | O_WRONLY,
                  (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
    if (pid_fd == -1) {
      int errno_value = (errno);
      if (errno_value == EEXIST) {
        fprintf(stderr,
                "FATAL ERROR: Pid file %s already exists. This could mean "
                "another instance of this software is already running. Delete "
                "the pid file to resolve this problem.\n",
                pid_file);
      } else {
        fprintf(stderr, "FATAL ERROR: Could not open pid file %s: %s\n",
                pid_file, strerror(errno_value));
      }
      exit(1);
    }
  }
  int error = 0;
  pid_t fork_pid = fork();
  if (fork_pid == -1) {
    fprintf(stderr, "FATAL ERROR: Couldn't fork\n");
    error = 1;
  } else if (fork_pid == 0) {
    if (setsid() < 0) {
      fprintf(stderr, "FATAL ERROR: setsid() failed!\n");
      error = 1;
    } else {
      fork_pid = fork();
      if (fork_pid == -1) {
        fprintf(stderr, "FATAL ERROR: Couldn't fork a second time\n");
        error = 1;
      } else if (fork_pid == 0) {
        int output_fd = -1;
        if (output_file) {
          output_fd = open(output_file, O_CREAT | O_APPEND | O_WRONLY,
                           (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
          if (output_fd == -1) {
            fprintf(stderr, "FATAL ERROR: Unable to open file %s: %s\n",
                    output_file, strerror(errno));
            error = 1;
          }
        }
        char *input_file = "/dev/null";
        int input_fd =
            open(input_file, O_RDONLY, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
        if (input_fd == -1) {
          fprintf(stderr, "FATAL ERROR: Unable to open file %s: %s\n",
                  input_file, strerror(errno));
          error = 1;
        }
        if (!error) {
          int result0 = dup2(input_fd, STDIN_FILENO);
          int result1 = dup2(output_fd, STDOUT_FILENO);
          int result2 = dup2(output_fd, STDERR_FILENO);
          if (result0 == -1 || result1 == -1 || result2 == -1) {
            fprintf(stderr, "FATAL ERROR: Cannot redirect stdout, stdin and "
                            "stderr to a file!\n");
            error = 1;
          } else {
            my_close(&output_fd);
            my_close(&input_fd);
            fprintf(stderr, "---------------------------\n");
            fprintf(stderr, "- DAEMONIZE: NEW INSTANCE -\n");
            fprintf(stderr, "---------------------------\n");
            fork_pid = fork();
            if (fork_pid == -1) {
              fprintf(stderr, "FATAL ERROR: Couldn't fork a third time\n");
              error = 1;
            } else if (fork_pid == 0) {
              if (pid_file != NULL) {
                pid_t this_pid = getpid();
                char pid_buf[256];
                int pid_buf_len =
                    snprintf(pid_buf, sizeof(pid_buf), "%ld", (long)this_pid);
                if (pid_buf_len == -1 || pid_buf_len >= sizeof(pid_buf)) {
                  fprintf(
                      stderr,
                      "FATAL ERROR: nsprintf failed for some weird reason!\n");
                  error = 1;
                  my_close(&pid_fd);
                } else {
                  ssize_t write_pid_result =
                      write(pid_fd, pid_buf, pid_buf_len);
                  if (write_pid_result == -1) {
                    fprintf(stderr,
                            "FATAL ERROR: Could not write pid to pid file\n");
                    error = 1;
                  } else {
                    my_close(&pid_fd);
                  }
                }
              }
              if (!error) {
                fprintf(stderr, "Child: Calling execv\n");
                execv(argv[0], argv);
                int errno_value = (errno);
                fprintf(stderr,
                        "Child: FATAL ERROR: execve failed! errno = %s\n",
                        strerror(errno_value));
                error = 1;
              }
            } else {
              pid_t child_pid = wait(NULL);
              if (child_pid == -1) {
                int errno_value = (errno);
                fprintf(stderr, "Call to wait() failed. errno = %s\n",
                        strerror(errno_value));
              } else {
              }
            }
          }
        }
      } else {
        if (pid_fd != -1) {
          my_close(&pid_fd);
        }
      }
    }
  } else {
    if (pid_fd != -1) {
      my_close(&pid_fd);
    }
  }
  if (pid_fd != -1) {
    my_close(&pid_fd);
    if (unlink(pid_file) == -1) {
      int errno_value = (errno);
      fprintf(stderr, "Cannot remove pid file %s. errno=%s\n", pid_file,
              strerror(errno_value));
    }
  }
  return (error);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12eb <daemonize+0x10b>
mov    -0x18(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    0x2d8b(%rip),%rax
or     (%rax),%esi
mov    0x2d4a(%rip),%rax
or     (%rax),%esi
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
mov    0x2d90(%rip),%rax
or     (%rax),%edx
mov    0x2d4f(%rip),%rax
or     (%rax),%edx
mov    0x2d86(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12e6 <daemonize+0x106>
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2d44(%rip),%rcx
cmp    (%rcx),%eax
jne    129c <daemonize+0xbc>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    12dc <daemonize+0xfc>
mov    0x2d0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x174(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x170(%rbp)
mov    -0x20(%rbp),%edi
call   10d0 <strerror@plt>
mov    -0x174(%rbp),%edi
mov    -0x170(%rbp),%rdx
mov    %rax,%rcx
lea    0xdc8(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    12eb <daemonize+0x10b>
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1329 <daemonize+0x149>
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
jmp    183c <daemonize+0x65c>
cmpl   $0x0,-0x28(%rbp)
jne    181f <daemonize+0x63f>
mov    $0x0,%al
call   1080 <setsid@plt>
cmp    $0x0,%rax
jge    1367 <daemonize+0x187>
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
lea    0xd92(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
jmp    181a <daemonize+0x63a>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    139e <daemonize+0x1be>
mov    0x2c2e(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
jmp    1815 <daemonize+0x635>
cmpl   $0x0,-0x28(%rbp)
jne    17f8 <daemonize+0x618>
movl   $0xffffffff,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1461 <daemonize+0x281>
mov    -0x10(%rbp),%rdi
mov    0x2c33(%rip),%rax
mov    (%rax),%esi
mov    0x2b9a(%rip),%rax
or     (%rax),%esi
mov    0x2b99(%rip),%rax
or     (%rax),%esi
mov    0x2ba8(%rip),%rax
mov    (%rax),%edx
mov    0x2bdf(%rip),%rax
or     (%rax),%edx
mov    0x2b9e(%rip),%rax
or     (%rax),%edx
mov    0x2bd5(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    145c <daemonize+0x27c>
mov    0x2b9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x184(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x180(%rbp)
mov    0x2b70(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x184(%rbp),%edi
mov    -0x180(%rbp),%rdx
mov    %rax,%rcx
lea    0xce1(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
jmp    1461 <daemonize+0x281>
lea    0xcf0(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2b61(%rip),%rax
mov    (%rax),%esi
mov    0x2b08(%rip),%rax
mov    (%rax),%edx
mov    0x2b3f(%rip),%rax
or     (%rax),%edx
mov    0x2afe(%rip),%rax
or     (%rax),%edx
mov    0x2b35(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    14fc <daemonize+0x31c>
mov    0x2afa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x194(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x190(%rbp)
mov    0x2ad0(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x194(%rbp),%edi
mov    -0x190(%rbp),%rdx
mov    %rax,%rcx
lea    0xc41(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    17f3 <daemonize+0x613>
mov    -0x3c(%rbp),%edi
mov    0x2a70(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    %eax,-0x40(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2ac4(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    %eax,-0x44(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2a60(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
je     1560 <daemonize+0x380>
cmpl   $0xffffffff,-0x44(%rbp)
je     1560 <daemonize+0x380>
cmpl   $0xffffffff,-0x48(%rbp)
jne    1583 <daemonize+0x3a3>
mov    0x2a49(%rip),%rax
mov    (%rax),%edi
lea    0xbf2(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
jmp    17ee <daemonize+0x60e>
lea    -0x2c(%rbp),%rdi
call   1090 <my_close@plt>
lea    -0x3c(%rbp),%rdi
call   1090 <my_close@plt>
mov    0x2a14(%rip),%rax
mov    (%rax),%edi
lea    0xbff(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x29fd(%rip),%rax
mov    (%rax),%edi
lea    0xc05(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x29e6(%rip),%rax
mov    (%rax),%edi
lea    0xbd1(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1611 <daemonize+0x431>
mov    0x29bb(%rip),%rax
mov    (%rax),%edi
lea    0xbe0(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
jmp    17e9 <daemonize+0x609>
cmpl   $0x0,-0x28(%rbp)
jne    1779 <daemonize+0x599>
cmpq   $0x0,-0x18(%rbp)
je     16fc <daemonize+0x51c>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x4c(%rbp)
lea    -0x150(%rbp),%rdi
movslq -0x4c(%rbp),%rcx
mov    $0x100,%esi
lea    0xbc0(%rip),%rdx
call   1070 <snprintf@plt>
mov    %eax,-0x154(%rbp)
cmpl   $0xffffffff,-0x154(%rbp)
je     1672 <daemonize+0x492>
movslq -0x154(%rbp),%rax
cmp    $0x100,%rax
jb     169e <daemonize+0x4be>
mov    0x2937(%rip),%rax
mov    (%rax),%edi
lea    0xb89(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
lea    -0x1c(%rbp),%rdi
call   1090 <my_close@plt>
jmp    16f7 <daemonize+0x517>
mov    -0x1c(%rbp),%edi
lea    -0x150(%rbp),%rsi
mov    -0x154(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x158(%rbp)
cmpl   $0xffffffff,-0x158(%rbp)
jne    16e9 <daemonize+0x509>
mov    0x28e3(%rip),%rax
mov    (%rax),%edi
lea    0xb6a(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
jmp    16f2 <daemonize+0x512>
lea    -0x1c(%rbp),%rdi
call   1090 <my_close@plt>
jmp    16f7 <daemonize+0x517>
jmp    16fc <daemonize+0x51c>
cmpl   $0x0,-0x24(%rbp)
jne    1774 <daemonize+0x594>
mov    0x28a3(%rip),%rax
mov    (%rax),%edi
lea    0xb58(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   10f0 <execv@plt>
mov    0x286c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x15c(%rbp)
mov    0x286d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x198(%rbp)
mov    -0x15c(%rbp),%edi
call   10d0 <strerror@plt>
mov    -0x198(%rbp),%edi
mov    %rax,%rdx
lea    0xb1e(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
jmp    17e4 <daemonize+0x604>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <wait@plt>
mov    %eax,-0x160(%rbp)
cmpl   $0xffffffff,-0x160(%rbp)
jne    17da <daemonize+0x5fa>
mov    0x2804(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x164(%rbp)
mov    0x2805(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x19c(%rbp)
mov    -0x164(%rbp),%edi
call   10d0 <strerror@plt>
mov    -0x19c(%rbp),%edi
mov    %rax,%rdx
lea    0xae5(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    17df <daemonize+0x5ff>
jmp    17df <daemonize+0x5ff>
jmp    17e4 <daemonize+0x604>
jmp    17e9 <daemonize+0x609>
jmp    17ee <daemonize+0x60e>
jmp    17f3 <daemonize+0x613>
jmp    1810 <daemonize+0x630>
cmpl   $0xffffffff,-0x1c(%rbp)
je     180b <daemonize+0x62b>
lea    -0x1c(%rbp),%rdi
call   1090 <my_close@plt>
jmp    1810 <daemonize+0x630>
jmp    1815 <daemonize+0x635>
jmp    181a <daemonize+0x63a>
jmp    1837 <daemonize+0x657>
cmpl   $0xffffffff,-0x1c(%rbp)
je     1832 <daemonize+0x652>
lea    -0x1c(%rbp),%rdi
call   1090 <my_close@plt>
jmp    1837 <daemonize+0x657>
jmp    183c <daemonize+0x65c>
cmpl   $0xffffffff,-0x1c(%rbp)
je     18b8 <daemonize+0x6d8>
lea    -0x1c(%rbp),%rdi
call   1090 <my_close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    18b3 <daemonize+0x6d3>
mov    0x2738(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x168(%rbp)
mov    0x2739(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1ac(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x1a8(%rbp)
mov    -0x168(%rbp),%edi
call   10d0 <strerror@plt>
mov    -0x1ac(%rbp),%edi
mov    -0x1a8(%rbp),%rdx
mov    %rax,%rcx
lea    0xa2a(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    18b8 <daemonize+0x6d8>
mov    -0x24(%rbp),%eax
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
movl   $0xffffffff,(%rsp)
test   %rdx,%rdx
je     1299 <daemonize+0xb9>
mov    0x2de7(%rip),%rax
mov    0x2d90(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d4d(%rip),%rax
or     (%rax),%esi
mov    0x2d5c(%rip),%rax
mov    0x2d95(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d52(%rip),%rax
or     (%rax),%edx
mov    0x2d89(%rip),%rax
or     (%rax),%edx
mov    %r14,%rdi
call   10b0 <open@plt>
mov    %eax,%ebp
mov    %eax,(%rsp)
cmp    $0xffffffff,%eax
jne    129e <daemonize+0xbe>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
mov    0x2d4d(%rip),%rax
mov    0x2d3e(%rip),%rcx
mov    (%rcx),%ebx
cmp    (%rax),%edi
jne    1682 <daemonize+0x4a2>
lea    0xd7d(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    $0xffffffff,%ebp
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     12c0 <daemonize+0xe0>
cmp    $0xffffffff,%eax
jne    12f2 <daemonize+0x112>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
jmp    1317 <daemonize+0x137>
xor    %eax,%eax
call   1080 <setsid@plt>
test   %rax,%rax
js     1307 <daemonize+0x127>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     137f <daemonize+0x19f>
cmp    $0xffffffff,%eax
jne    12f2 <daemonize+0x112>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
jmp    1317 <daemonize+0x137>
cmp    $0xffffffff,%ebp
je     1303 <daemonize+0x123>
mov    %rsp,%rdi
call   1090 <my_close@plt>
xor    %ebx,%ebx
jmp    1323 <daemonize+0x143>
xor    %ebx,%ebx
jmp    136b <daemonize+0x18b>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%ebx
cmpl   $0xffffffff,(%rsp)
je     136b <daemonize+0x18b>
mov    %rsp,%rdi
call   1090 <my_close@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    136b <daemonize+0x18b>
mov    0x2c5b(%rip),%rax
mov    (%rax),%edi
mov    0x2c62(%rip),%rax
mov    (%rax),%ebp
call   10d0 <strerror@plt>
lea    0xf7a(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movl   $0xffffffff,0x4(%rsp)
ff
mov    $0x1,%r13b
mov    $0xffffffff,%r12d
test   %rbx,%rbx
je     1424 <daemonize+0x244>
mov    0x2c58(%rip),%rax
mov    0x2bc1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2bbe(%rip),%rax
or     (%rax),%esi
mov    0x2bcd(%rip),%rax
mov    0x2c06(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2bc3(%rip),%rax
or     (%rax),%edx
mov    0x2bfa(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
call   10b0 <open@plt>
mov    %eax,%r12d
mov    %eax,0x4(%rsp)
cmp    $0xffffffff,%eax
jne    1424 <daemonize+0x244>
mov    0x2bbd(%rip),%rax
mov    (%rax),%r12d
mov    0x2ba3(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd24(%rip),%rsi
xor    %r13d,%r13d
mov    %r12d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0xffffffff,%r12d
mov    0x2b5d(%rip),%rax
mov    0x2b96(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2b9b(%rip),%rax
mov    0x2b4c(%rip),%rcx
or     (%rcx),%edx
mov    (%rax),%esi
mov    0x2b81(%rip),%rax
or     (%rax),%edx
lea    0xd00(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,0xc(%rsp)
cmp    $0xffffffff,%eax
je     1540 <daemonize+0x360>
mov    $0x1,%ebx
test   %r13b,%r13b
je     1323 <daemonize+0x143>
mov    0x2b01(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1060 <dup2@plt>
mov    %eax,0x8(%rsp)
mov    0x2b55(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1060 <dup2@plt>
mov    %eax,%r13d
mov    0x2af1(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1060 <dup2@plt>
cmpl   $0xffffffff,0x8(%rsp)
je     1576 <daemonize+0x396>
cmp    $0xffffffff,%r13d
je     1576 <daemonize+0x396>
cmp    $0xffffffff,%eax
je     1576 <daemonize+0x396>
lea    0x4(%rsp),%rdi
call   1090 <my_close@plt>
lea    0xc(%rsp),%rdi
call   1090 <my_close@plt>
mov    0x2ac6(%rip),%r13
mov    0x0(%r13),%edi
lea    0xcaf(%rip),%r12
mov    %r12,%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x0(%r13),%edi
lea    0xcb7(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x0(%r13),%edi
mov    %r12,%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     1592 <daemonize+0x3b2>
cmp    $0xffffffff,%eax
jne    15e1 <daemonize+0x401>
mov    0x0(%r13),%edi
lea    0xca0(%rip),%rsi
jmp    1586 <daemonize+0x3a6>
mov    0x2a69(%rip),%rax
mov    (%rax),%ebx
mov    0x2a50(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xbd1(%rip),%rsi
lea    0xbf3(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    131e <daemonize+0x13e>
mov    0x2a33(%rip),%rax
mov    (%rax),%edi
lea    0xbdc(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    1323 <daemonize+0x143>
test   %r14,%r14
je     162b <daemonize+0x44b>
xor    %eax,%eax
call   1050 <getpid@plt>
movslq %eax,%rcx
lea    0xc5b(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1070 <snprintf@plt>
cmp    $0x100,%eax
jb     1610 <daemonize+0x430>
mov    0x0(%r13),%edi
lea    0xc3e(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %rsp,%rdi
call   1090 <my_close@plt>
jmp    1323 <daemonize+0x143>
xor    %ebx,%ebx
xor    %edi,%edi
call   10e0 <wait@plt>
cmp    $0xffffffff,%eax
jne    1323 <daemonize+0x143>
mov    0x29a6(%rip),%rax
mov    (%rax),%edi
mov    0x0(%r13),%ebp
call   10d0 <strerror@plt>
lea    0xca7(%rip),%rsi
xor    %ebx,%ebx
jmp    1661 <daemonize+0x481>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1672 <daemonize+0x492>
mov    %rsp,%rdi
call   1090 <my_close@plt>
mov    0x0(%r13),%edi
lea    0xc38(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    (%r15),%rdi
mov    %r15,%rsi
call   10f0 <execv@plt>
mov    0x2951(%rip),%rax
mov    (%rax),%edi
mov    0x0(%r13),%ebp
call   10d0 <strerror@plt>
lea    0xc23(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    1323 <daemonize+0x143>
mov    0x0(%r13),%edi
lea    0xbc3(%rip),%rsi
jmp    1586 <daemonize+0x3a6>
call   10d0 <strerror@plt>
lea    0xa0f(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@errno = global i32 0
@EEXIST = global i32 0
@stderr = global i32 0
@.str = [157 x i8] c"FATAL ERROR: Pid file %s already exists. This could mean another instance of this software is already running. Delete the pid file to resolve this problem.\0A\00"
@.str.1 = [45 x i8] c"FATAL ERROR: Could not open pid file %s: %s\0A\00"
@.str.2 = [28 x i8] c"FATAL ERROR: Couldn't fork\0A\00"
@.str.3 = [31 x i8] c"FATAL ERROR: setsid() failed!\0A\00"
@.str.4 = [42 x i8] c"FATAL ERROR: Couldn't fork a second time\0A\00"
@O_APPEND = global i32 0
@.str.5 = [41 x i8] c"FATAL ERROR: Unable to open file %s: %s\0A\00"
@.str.6 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.7 = [66 x i8] c"FATAL ERROR: Cannot redirect stdout, stdin and stderr to a file!\0A\00"
@.str.8 = [29 x i8] c"---------------------------\0A\00"
@.str.9 = [29 x i8] c"- DAEMONIZE: NEW INSTANCE -\0A\00"
@.str.10 = [41 x i8] c"FATAL ERROR: Couldn't fork a third time\0A\00"
@.str.11 = [4 x i8] c"%ld\00"
@.str.12 = [53 x i8] c"FATAL ERROR: nsprintf failed for some weird reason!\0A\00"
@.str.13 = [46 x i8] c"FATAL ERROR: Could not write pid to pid file\0A\00"
@.str.14 = [22 x i8] c"Child: Calling execv\0A\00"
@.str.15 = [47 x i8] c"Child: FATAL ERROR: execve failed! errno = %s\0A\00"
@.str.16 = [35 x i8] c"Call to wait() failed. errno = %s\0A\00"
@.str.17 = [37 x i8] c"Cannot remove pid file %s. errno=%s\0A\00"
define i32 @daemonize(i8** %0, i8* %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca [256 x i8]
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
%23 = alloca i32
%24 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 -1, i32* %7
%25 = load i8*, i8** %6
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %62
27: ; preds = %3
%28 = load i8*, i8** %6
%29 = load i32, i32* @O_CREAT
%30 = load i32, i32* @O_EXCL
%31 = or i32 %29, %30
%32 = load i32, i32* @O_WRONLY
%33 = or i32 %31, %32
%34 = load i32, i32* @S_IRUSR
%35 = load i32, i32* @S_IWUSR
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IRGRP
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IWGRP
%40 = or i32 %38, %39
%41 = call i32 @open(i8* %28, i32 %33, i32 %40)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %61
44: ; preds = %27
%45 = load i32, i32* @errno
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = load i32, i32* @EEXIST
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %53
49: ; preds = %44
%50 = load i32, i32* @stderr
%51 = load i8*, i8** %6
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([157 x i8], [157 x i8]* @.str, i64 0, i64 0), i8* %51)
br label %59
53: ; preds = %44
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %6
%56 = load i32, i32* %8
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %55, i8* %57)
br label %59
59: ; preds = %53, %49
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %27
br label %62
62: ; preds = %61, %3
store i32 0, i32* %9
%63 = call i32 (...) @fork()
store i32 %63, i32* %10
%64 = load i32, i32* %10
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = load i32, i32* @stderr
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %9
br label %258
69: ; preds = %62
%70 = load i32, i32* %10
%71 = icmp eq i32 %70, 0
br i1 %71, label %72, label %251
72: ; preds = %69
%73 = call i64 (...) @setsid()
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load i32, i32* @stderr
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %9
br label %250
78: ; preds = %72
%79 = call i32 (...) @fork()
store i32 %79, i32* %10
%80 = load i32, i32* %10
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %85
82: ; preds = %78
%83 = load i32, i32* @stderr
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %9
br label %249
85: ; preds = %78
%86 = load i32, i32* %10
%87 = icmp eq i32 %86, 0
br i1 %87, label %88, label %242
88: ; preds = %85
store i32 -1, i32* %11
%89 = load i8*, i8** %5
%90 = icmp ne i8* %89, null
br i1 %90, label %91, label %115
91: ; preds = %88
%92 = load i8*, i8** %5
%93 = load i32, i32* @O_CREAT
%94 = load i32, i32* @O_APPEND
%95 = or i32 %93, %94
%96 = load i32, i32* @O_WRONLY
%97 = or i32 %95, %96
%98 = load i32, i32* @S_IRUSR
%99 = load i32, i32* @S_IWUSR
%100 = or i32 %98, %99
%101 = load i32, i32* @S_IRGRP
%102 = or i32 %100, %101
%103 = load i32, i32* @S_IWGRP
%104 = or i32 %102, %103
%105 = call i32 @open(i8* %92, i32 %97, i32 %104)
store i32 %105, i32* %11
%106 = load i32, i32* %11
%107 = icmp eq i32 %106, -1
br i1 %107, label %108, label %114
108: ; preds = %91
%109 = load i32, i32* @stderr
%110 = load i8*, i8** %5
%111 = load i32, i32* @errno
%112 = call i8* @strerror(i32 %111)
%113 = call i32 (i32, i8*, ...) @fprintf(i32 %109, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %110, i8* %112)
store i32 1, i32* %9
br label %114
114: ; preds = %108, %91
br label %115
115: ; preds = %114, %88
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8** %12
%116 = load i8*, i8** %12
%117 = load i32, i32* @O_RDONLY
%118 = load i32, i32* @S_IRUSR
%119 = load i32, i32* @S_IWUSR
%120 = or i32 %118, %119
%121 = load i32, i32* @S_IRGRP
%122 = or i32 %120, %121
%123 = load i32, i32* @S_IWGRP
%124 = or i32 %122, %123
%125 = call i32 @open(i8* %116, i32 %117, i32 %124)
store i32 %125, i32* %13
%126 = load i32, i32* %13
%127 = icmp eq i32 %126, -1
br i1 %127, label %128, label %134
128: ; preds = %115
%129 = load i32, i32* @stderr
%130 = load i8*, i8** %12
%131 = load i32, i32* @errno
%132 = call i8* @strerror(i32 %131)
%133 = call i32 (i32, i8*, ...) @fprintf(i32 %129, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %130, i8* %132)
store i32 1, i32* %9
br label %134
134: ; preds = %128, %115
%135 = load i32, i32* %9
%136 = icmp ne i32 %135, 0
br i1 %136, label %241, label %137
137: ; preds = %134
%138 = load i32, i32* %13
%139 = load i32, i32* @STDIN_FILENO
%140 = call i32 @dup2(i32 %138, i32 %139)
store i32 %140, i32* %14
%141 = load i32, i32* %11
%142 = load i32, i32* @STDOUT_FILENO
%143 = call i32 @dup2(i32 %141, i32 %142)
store i32 %143, i32* %15
%144 = load i32, i32* %11
%145 = load i32, i32* @STDERR_FILENO
%146 = call i32 @dup2(i32 %144, i32 %145)
store i32 %146, i32* %16
%147 = load i32, i32* %14
%148 = icmp eq i32 %147, -1
br i1 %148, label %155, label %149
149: ; preds = %137
%150 = load i32, i32* %15
%151 = icmp eq i32 %150, -1
br i1 %151, label %155, label %152
152: ; preds = %149
%153 = load i32, i32* %16
%154 = icmp eq i32 %153, -1
br i1 %154, label %155, label %158
155: ; preds = %152, %149, %137
%156 = load i32, i32* @stderr
%157 = call i32 (i32, i8*, ...) @fprintf(i32 %156, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %9
br label %240
158: ; preds = %152
%159 = call i32 @my_close(i32* %11)
%160 = call i32 @my_close(i32* %13)
%161 = load i32, i32* @stderr
%162 = call i32 (i32, i8*, ...) @fprintf(i32 %161, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
%163 = load i32, i32* @stderr
%164 = call i32 (i32, i8*, ...) @fprintf(i32 %163, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
%165 = load i32, i32* @stderr
%166 = call i32 (i32, i8*, ...) @fprintf(i32 %165, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
%167 = call i32 (...) @fork()
store i32 %167, i32* %10
%168 = load i32, i32* %10
%169 = icmp eq i32 %168, -1
br i1 %169, label %170, label %173
170: ; preds = %158
%171 = load i32, i32* @stderr
%172 = call i32 (i32, i8*, ...) @fprintf(i32 %171, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.10, i64 0, i64 0))
store i32 1, i32* %9
br label %239
173: ; preds = %158
%174 = load i32, i32* %10
%175 = icmp eq i32 %174, 0
br i1 %175, label %176, label %226
176: ; preds = %173
%177 = load i8*, i8** %6
%178 = icmp ne i8* %177, null
br i1 %178, label %179, label %209
179: ; preds = %176
%180 = call i32 (...) @getpid()
store i32 %180, i32* %17
%181 = getelementptr inbounds [256 x i8], [256 x i8]* %18, i64 0, i64 0
%182 = load i32, i32* %17
%183 = sext i32 %182 to i64
%184 = call i32 @snprintf(i8* %181, i32 256, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0), i64 %183)
store i32 %184, i32* %19
%185 = load i32, i32* %19
%186 = icmp eq i32 %185, -1
br i1 %186, label %191, label %187
187: ; preds = %179
%188 = load i32, i32* %19
%189 = sext i32 %188 to i64
%190 = icmp uge i64 %189, 256
br i1 %190, label %191, label %195
191: ; preds = %187, %179
%192 = load i32, i32* @stderr
%193 = call i32 (i32, i8*, ...) @fprintf(i32 %192, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.12, i64 0, i64 0))
store i32 1, i32* %9
%194 = call i32 @my_close(i32* %7)
br label %208
195: ; preds = %187
%196 = load i32, i32* %7
%197 = getelementptr inbounds [256 x i8], [256 x i8]* %18, i64 0, i64 0
%198 = load i32, i32* %19
%199 = call i32 @write(i32 %196, i8* %197, i32 %198)
store i32 %199, i32* %20
%200 = load i32, i32* %20
%201 = icmp eq i32 %200, -1
br i1 %201, label %202, label %205
202: ; preds = %195
%203 = load i32, i32* @stderr
%204 = call i32 (i32, i8*, ...) @fprintf(i32 %203, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.13, i64 0, i64 0))
store i32 1, i32* %9
br label %207
205: ; preds = %195
%206 = call i32 @my_close(i32* %7)
br label %207
207: ; preds = %205, %202
br label %208
208: ; preds = %207, %191
br label %209
209: ; preds = %208, %176
%210 = load i32, i32* %9
%211 = icmp ne i32 %210, 0
br i1 %211, label %225, label %212
212: ; preds = %209
%213 = load i32, i32* @stderr
%214 = call i32 (i32, i8*, ...) @fprintf(i32 %213, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.14, i64 0, i64 0))
%215 = load i8**, i8*** %4
%216 = getelementptr inbounds i8*, i8** %215, i64 0
%217 = load i8*, i8** %216
%218 = load i8**, i8*** %4
%219 = call i32 @execv(i8* %217, i8** %218)
%220 = load i32, i32* @errno
store i32 %220, i32* %21
%221 = load i32, i32* @stderr
%222 = load i32, i32* %21
%223 = call i8* @strerror(i32 %222)
%224 = call i32 (i32, i8*, ...) @fprintf(i32 %221, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.15, i64 0, i64 0), i8* %223)
store i32 1, i32* %9
br label %225
225: ; preds = %212, %209
br label %238
226: ; preds = %173
%227 = call i32 @wait(i32* null)
store i32 %227, i32* %22
%228 = load i32, i32* %22
%229 = icmp eq i32 %228, -1
br i1 %229, label %230, label %236
230: ; preds = %226
%231 = load i32, i32* @errno
store i32 %231, i32* %23
%232 = load i32, i32* @stderr
%233 = load i32, i32* %23
%234 = call i8* @strerror(i32 %233)
%235 = call i32 (i32, i8*, ...) @fprintf(i32 %232, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.16, i64 0, i64 0), i8* %234)
br label %237
236: ; preds = %226
br label %237
237: ; preds = %236, %230
br label %238
238: ; preds = %237, %225
br label %239
239: ; preds = %238, %170
br label %240
240: ; preds = %239, %155
br label %241
241: ; preds = %240, %134
br label %248
242: ; preds = %85
%243 = load i32, i32* %7
%244 = icmp ne i32 %243, -1
br i1 %244, label %245, label %247
245: ; preds = %242
%246 = call i32 @my_close(i32* %7)
br label %247
247: ; preds = %245, %242
br label %248
248: ; preds = %247, %241
br label %249
249: ; preds = %248, %82
br label %250
250: ; preds = %249, %75
br label %257
251: ; preds = %69
%252 = load i32, i32* %7
%253 = icmp ne i32 %252, -1
br i1 %253, label %254, label %256
254: ; preds = %251
%255 = call i32 @my_close(i32* %7)
br label %256
256: ; preds = %254, %251
br label %257
257: ; preds = %256, %250
br label %258
258: ; preds = %257, %66
%259 = load i32, i32* %7
%260 = icmp ne i32 %259, -1
br i1 %260, label %261, label %274
261: ; preds = %258
%262 = call i32 @my_close(i32* %7)
%263 = load i8*, i8** %6
%264 = call i32 @unlink(i8* %263)
%265 = icmp eq i32 %264, -1
br i1 %265, label %266, label %273
266: ; preds = %261
%267 = load i32, i32* @errno
store i32 %267, i32* %24
%268 = load i32, i32* @stderr
%269 = load i8*, i8** %6
%270 = load i32, i32* %24
%271 = call i8* @strerror(i32 %270)
%272 = call i32 (i32, i8*, ...) @fprintf(i32 %268, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.17, i64 0, i64 0), i8* %269, i8* %271)
br label %273
273: ; preds = %266, %261
br label %274
274: ; preds = %273, %258
%275 = load i32, i32* %9
ret i32 %275
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@errno = global i32 0
@EEXIST = global i32 0
@stderr = global i32 0
@.str = [157 x i8] c"FATAL ERROR: Pid file %s already exists. This could mean another instance of this software is already running. Delete the pid file to resolve this problem.\0A\00"
@.str.1 = [45 x i8] c"FATAL ERROR: Could not open pid file %s: %s\0A\00"
@.str.2 = [28 x i8] c"FATAL ERROR: Couldn't fork\0A\00"
@.str.3 = [31 x i8] c"FATAL ERROR: setsid() failed!\0A\00"
@.str.4 = [42 x i8] c"FATAL ERROR: Couldn't fork a second time\0A\00"
@O_APPEND = global i32 0
@.str.5 = [41 x i8] c"FATAL ERROR: Unable to open file %s: %s\0A\00"
@.str.6 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.7 = [66 x i8] c"FATAL ERROR: Cannot redirect stdout, stdin and stderr to a file!\0A\00"
@.str.8 = [29 x i8] c"---------------------------\0A\00"
@.str.9 = [29 x i8] c"- DAEMONIZE: NEW INSTANCE -\0A\00"
@.str.10 = [41 x i8] c"FATAL ERROR: Couldn't fork a third time\0A\00"
@.str.11 = [4 x i8] c"%ld\00"
@.str.12 = [53 x i8] c"FATAL ERROR: nsprintf failed for some weird reason!\0A\00"
@.str.13 = [46 x i8] c"FATAL ERROR: Could not write pid to pid file\0A\00"
@.str.14 = [22 x i8] c"Child: Calling execv\0A\00"
@.str.15 = [47 x i8] c"Child: FATAL ERROR: execve failed! errno = %s\0A\00"
@.str.16 = [35 x i8] c"Call to wait() failed. errno = %s\0A\00"
@.str.17 = [37 x i8] c"Cannot remove pid file %s. errno=%s\0A\00"
define i32 @daemonize(i8** %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = bitcast i32* %4 to i8*
store i32 -1, i32* %4
%9 = icmp eq i8* %2, null
br i1 %9, label %37, label %10
10: ; preds = %3
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_EXCL
%13 = or i32 %12, %11
%14 = load i32, i32* @O_WRONLY
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %17, %16
%19 = load i32, i32* @S_IRGRP
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IWGRP
%22 = or i32 %20, %21
%23 = tail call i32 @open(i8* %2, i32 %15, i32 %22)
store i32 %23, i32* %4
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %37
25: ; preds = %10
%26 = load i32, i32* @errno
%27 = load i32, i32* @EEXIST
%28 = icmp eq i32 %26, %27
%29 = load i32, i32* @stderr
br i1 %28, label %30, label %32
30: ; preds = %25
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([157 x i8], [157 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %35
32: ; preds = %25
%33 = tail call i8* @strerror(i32 %26)
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %33)
br label %35
35: ; preds = %32, %30
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %10, %3
%38 = phi i32 [ %23, %10 ], [ -1, %3 ]
%39 = tail call i32 (...) @fork()
switch i32 %39, label %168 [
i32 -1, label %40
i32 0, label %43
]
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %172
43: ; preds = %37
%44 = tail call i64 (...) @setsid()
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32, i32* @stderr
%48 = tail call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %172
49: ; preds = %43
%50 = tail call i32 (...) @fork()
switch i32 %50, label %164 [
i32 -1, label %51
i32 0, label %54
]
51: ; preds = %49
%52 = load i32, i32* @stderr
%53 = tail call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
br label %172
54: ; preds = %49
%55 = bitcast i32* %5 to i8*
store i32 -1, i32* %5
%56 = icmp eq i8* %1, null
br i1 %56, label %77, label %57
57: ; preds = %54
%58 = load i32, i32* @O_CREAT
%59 = load i32, i32* @O_APPEND
%60 = or i32 %59, %58
%61 = load i32, i32* @O_WRONLY
%62 = or i32 %60, %61
%63 = load i32, i32* @S_IRUSR
%64 = load i32, i32* @S_IWUSR
%65 = or i32 %64, %63
%66 = load i32, i32* @S_IRGRP
%67 = or i32 %65, %66
%68 = load i32, i32* @S_IWGRP
%69 = or i32 %67, %68
%70 = tail call i32 @open(i8* %1, i32 %62, i32 %69)
store i32 %70, i32* %5
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %77
72: ; preds = %57
%73 = load i32, i32* @stderr
%74 = load i32, i32* @errno
%75 = tail call i8* @strerror(i32 %74)
%76 = tail call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %1, i8* %75)
br label %77
77: ; preds = %57, %72, %54
%78 = phi i32 [ %70, %57 ], [ -1, %72 ], [ -1, %54 ]
%79 = phi i1 [ true, %57 ], [ false, %72 ], [ true, %54 ]
%80 = bitcast i32* %6 to i8*
%81 = load i32, i32* @O_RDONLY
%82 = load i32, i32* @S_IRUSR
%83 = load i32, i32* @S_IWUSR
%84 = or i32 %83, %82
%85 = load i32, i32* @S_IRGRP
%86 = or i32 %84, %85
%87 = load i32, i32* @S_IWGRP
%88 = or i32 %86, %87
%89 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %81, i32 %88)
store i32 %89, i32* %6
%90 = icmp eq i32 %89, -1
br i1 %90, label %91, label %96
91: ; preds = %77
%92 = load i32, i32* @stderr
%93 = load i32, i32* @errno
%94 = tail call i8* @strerror(i32 %93)
%95 = tail call i32 (i32, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %94)
br label %162
96: ; preds = %77
br i1 %79, label %97, label %162
97: ; preds = %96
%98 = load i32, i32* @STDIN_FILENO
%99 = tail call i32 @dup2(i32 %89, i32 %98)
%100 = load i32, i32* @STDOUT_FILENO
%101 = tail call i32 @dup2(i32 %78, i32 %100)
%102 = load i32, i32* @STDERR_FILENO
%103 = tail call i32 @dup2(i32 %78, i32 %102)
%104 = icmp eq i32 %99, -1
%105 = icmp eq i32 %101, -1
%106 = select i1 %104, i1 true, i1 %105
%107 = icmp eq i32 %103, -1
%108 = select i1 %106, i1 true, i1 %107
br i1 %108, label %109, label %112
109: ; preds = %97
%110 = load i32, i32* @stderr
%111 = tail call i32 (i32, i8*, ...) @fprintf(i32 %110, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.7, i64 0, i64 0))
br label %162
112: ; preds = %97
%113 = call i32 @my_close(i32* %5)
%114 = call i32 @my_close(i32* %6)
%115 = load i32, i32* @stderr
%116 = call i32 (i32, i8*, ...) @fprintf(i32 %115, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
%117 = load i32, i32* @stderr
%118 = call i32 (i32, i8*, ...) @fprintf(i32 %117, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
%119 = load i32, i32* @stderr
%120 = call i32 (i32, i8*, ...) @fprintf(i32 %119, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
%121 = call i32 (...) @fork()
switch i32 %121, label %154 [
i32 -1, label %122
i32 0, label %125
]
122: ; preds = %112
%123 = load i32, i32* @stderr
%124 = call i32 (i32, i8*, ...) @fprintf(i32 %123, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.10, i64 0, i64 0))
br label %162
125: ; preds = %112
br i1 %9, label %145, label %126
126: ; preds = %125
%127 = call i32 (...) @getpid()
%128 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%129 = sext i32 %127 to i64
%130 = call i32 @snprintf(i8* %128, i32 256, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0), i64 %129)
%131 = icmp ugt i32 %130, 255
br i1 %131, label %132, label %136
132: ; preds = %126
%133 = load i32, i32* @stderr
%134 = call i32 (i32, i8*, ...) @fprintf(i32 %133, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.12, i64 0, i64 0))
%135 = call i32 @my_close(i32* %4)
br label %142
136: ; preds = %126
%137 = call i32 @write(i32 %38, i8* %128, i32 %130)
%138 = icmp eq i32 %137, -1
br i1 %138, label %139, label %143
139: ; preds = %136
%140 = load i32, i32* @stderr
%141 = call i32 (i32, i8*, ...) @fprintf(i32 %140, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.13, i64 0, i64 0))
br label %142
142: ; preds = %132, %139
br label %162
143: ; preds = %136
%144 = call i32 @my_close(i32* %4)
br label %145
145: ; preds = %125, %143
%146 = load i32, i32* @stderr
%147 = call i32 (i32, i8*, ...) @fprintf(i32 %146, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.14, i64 0, i64 0))
%148 = load i8*, i8** %0
%149 = call i32 @execv(i8* %148, i8** %0)
%150 = load i32, i32* @errno
%151 = load i32, i32* @stderr
%152 = call i8* @strerror(i32 %150)
%153 = call i32 (i32, i8*, ...) @fprintf(i32 %151, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.15, i64 0, i64 0), i8* %152)
br label %162
154: ; preds = %112
%155 = call i32 @wait(i32* null)
%156 = icmp eq i32 %155, -1
br i1 %156, label %157, label %162
157: ; preds = %154
%158 = load i32, i32* @errno
%159 = load i32, i32* @stderr
%160 = call i8* @strerror(i32 %158)
%161 = call i32 (i32, i8*, ...) @fprintf(i32 %159, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.16, i64 0, i64 0), i8* %160)
br label %162
162: ; preds = %142, %91, %109, %145, %122, %154, %157, %96
%163 = phi i32 [ 1, %96 ], [ 1, %109 ], [ 1, %122 ], [ 1, %145 ], [ 0, %154 ], [ 0, %157 ], [ 1, %91 ], [ 1, %142 ]
br label %172
164: ; preds = %49
%165 = icmp eq i32 %38, -1
br i1 %165, label %185, label %166
166: ; preds = %164
%167 = call i32 @my_close(i32* %4)
br label %172
168: ; preds = %37
%169 = icmp eq i32 %38, -1
br i1 %169, label %185, label %170
170: ; preds = %168
%171 = call i32 @my_close(i32* %4)
br label %172
172: ; preds = %51, %166, %162, %46, %170, %40
%173 = phi i32 [ 0, %170 ], [ 0, %166 ], [ %163, %162 ], [ 1, %51 ], [ 1, %46 ], [ 1, %40 ]
%174 = load i32, i32* %4
%175 = icmp eq i32 %174, -1
br i1 %175, label %185, label %176
176: ; preds = %172
%177 = call i32 @my_close(i32* %4)
%178 = call i32 @unlink(i8* %2)
%179 = icmp eq i32 %178, -1
br i1 %179, label %180, label %185
180: ; preds = %176
%181 = load i32, i32* @errno
%182 = load i32, i32* @stderr
%183 = call i8* @strerror(i32 %181)
%184 = call i32 (i32, i8*, ...) @fprintf(i32 %182, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.17, i64 0, i64 0), i8* %2, i8* %183)
br label %185
185: ; preds = %168, %164, %176, %180, %172
%186 = phi i32 [ %173, %176 ], [ %173, %180 ], [ %173, %172 ], [ 0, %164 ], [ 0, %168 ]
ret i32 %186
}