#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 flock {
  scalar_t__ l_pid;
  scalar_t__ l_start;
  scalar_t__ l_len;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ ESRCH;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int LOG_CONS;
int /*<<< orphan*/ LOG_LOCAL0;
int LOG_NDELAY;
int LOG_NOWAIT;
int LOG_PID;
int /*<<< orphan*/ LOG_UPTO(int const);
int O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fcntl(int, int /*<<< orphan*/, struct flock *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *const, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getpid();
int kill(unsigned long, int /*<<< orphan*/);
int open(char const *const, int, ...);
int /*<<< orphan*/ openlog(char const *const, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *const, unsigned long);
int /*<<< orphan*/ setlogmask(int /*<<< orphan*/);
int /*<<< orphan*/ setpgrp();
scalar_t__ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int strlen(char *);
unsigned long strtoul(char *, char **, int);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int BecomeDaemonProcess(const char *const lockFileName,
                        const char *const logPrefix, const int logLevel,
                        int *const lockFileDesc, pid_t *const thisPID) {
  int curPID, stdioFD, lockResult, killResult, lockFD, i, numFiles;
  char pidBuf[17], *lfs, pidStr[7];
  FILE *lfp;
  unsigned long lockPID;
  struct flock exclusiveLock;
  chdir("/");
  lockFD = open(lockFileName, O_RDWR | O_CREAT | O_EXCL, 0644);
  if (lockFD == -1) {
    lfp = fopen(lockFileName, "r");
    if (lfp == 0) {
      perror("Can't get lockfile");
      return -1;
    }
    lfs = fgets(pidBuf, 16, lfp);
    if (lfs != 0) {
      if (pidBuf[strlen(pidBuf) - 1] == '\n')
        pidBuf[strlen(pidBuf) - 1] = 0;
      lockPID = strtoul(pidBuf, (char **)0, 10);
      killResult = kill(lockPID, 0);
      if (killResult == 0) {
        printf("\n\nERROR\n\nA lock file %s has been detected. It appears it "
               "is owned\nby the (active) process with PID %ld.\n\n",
               lockFileName, lockPID);
      } else {
        if (errno == ESRCH) {
          printf("\n\nERROR\n\nA lock file %s has been detected. It appears it "
                 "is owned\nby the process with PID %ld, which is now defunct. "
                 "Delete the lock file\nand try again.\n\n",
                 lockFileName, lockPID);
        } else {
          perror("Could not acquire exclusive lock on lock file");
        }
      }
    } else
      perror("Could not read lock file");
    fclose(lfp);
    return -1;
  }
  exclusiveLock.l_type = F_WRLCK;
  exclusiveLock.l_whence = SEEK_SET;
  exclusiveLock.l_len = exclusiveLock.l_start = 0;
  exclusiveLock.l_pid = 0;
  lockResult = fcntl(lockFD, F_SETLK, &exclusiveLock);
  if (lockResult < 0) {
    close(lockFD);
    perror("Can't get lockfile");
    return -1;
  }
  curPID = fork();
  switch (curPID) {
  case 0:
    break;
  case -1:
    fprintf(stderr, "Error: initial fork failed: %s\n", strerror(errno));
    return -1;
    break;
  default:
    exit(0);
    break;
  }
  if (setsid() < 0)
    return -1;
  signal(SIGHUP, SIG_IGN);
  curPID = fork();
  switch (curPID) {
  case 0:
    break;
  case -1:
    return -1;
    break;
  default:
    exit(0);
    break;
  }
  if (ftruncate(lockFD, 0) < 0)
    return -1;
  sprintf(pidStr, "%d\n", (int)getpid());
  write(lockFD, pidStr, strlen(pidStr));
  *lockFileDesc = lockFD;
  numFiles = sysconf(_SC_OPEN_MAX);
  if (numFiles < 0)
    numFiles = 256;
  for (i = numFiles - 1; i >= 0; --i) {
    if (i != lockFD)
      close(i);
  }
  umask(0);
  stdioFD = open("/dev/null", O_RDWR);
  dup(stdioFD);
  dup(stdioFD);
  openlog(logPrefix, LOG_PID | LOG_CONS | LOG_NDELAY | LOG_NOWAIT, LOG_LOCAL0);
  (void)setlogmask(LOG_UPTO(logLevel));
  setpgrp();
  return 0;
}


x86 assembly (clang, -O0):
<BecomeDaemonProcess>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
lea    0xd0b(%rip),%rdi
call   1090 <chdir@plt>
mov    -0x10(%rbp),%rdi
mov    0x2cd3(%rip),%rax
mov    (%rax),%esi
mov    0x2cea(%rip),%rax
or     (%rax),%esi
mov    0x2c81(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1150 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    146a <BecomeDaemonProcess+0x19a>
mov    -0x10(%rbp),%rdi
lea    0xcc5(%rip),%rsi
call   1160 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jne    1369 <BecomeDaemonProcess+0x99>
lea    0xcac(%rip),%rdi
call   1170 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    174e <BecomeDaemonProcess+0x47e>
lea    -0x60(%rbp),%rdi
mov    -0x78(%rbp),%rdx
mov    $0x10,%esi
call   10f0 <fgets@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
je     1449 <BecomeDaemonProcess+0x179>
lea    -0x60(%rbp),%rdi
call   1080 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x60(%rbp,%rax,1),%eax
cmp    $0xa,%eax
jne    13b9 <BecomeDaemonProcess+0xe9>
lea    -0x60(%rbp),%rdi
call   1080 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x60(%rbp,%rax,1)
lea    -0x60(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1190 <strtoul@plt>
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rdi
xor    %esi,%esi
call   1130 <kill@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jne    1400 <BecomeDaemonProcess+0x130>
mov    -0x10(%rbp),%rsi
mov    -0x80(%rbp),%rdx
lea    0xc21(%rip),%rdi
call   10a0 <printf@plt>
jmp    1444 <BecomeDaemonProcess+0x174>
mov    0x2b89(%rip),%rax
mov    (%rax),%rax
mov    0x2b47(%rip),%rcx
cmp    (%rcx),%rax
jne    1433 <BecomeDaemonProcess+0x163>
mov    -0x10(%rbp),%rsi
mov    -0x80(%rbp),%rdx
lea    0xc58(%rip),%rdi
call   10a0 <printf@plt>
jmp    143f <BecomeDaemonProcess+0x16f>
lea    0xce2(%rip),%rdi
call   1170 <perror@plt>
jmp    1444 <BecomeDaemonProcess+0x174>
jmp    1455 <BecomeDaemonProcess+0x185>
lea    0xcfa(%rip),%rdi
call   1170 <perror@plt>
mov    -0x78(%rbp),%rdi
call   1070 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    174e <BecomeDaemonProcess+0x47e>
mov    0x2aff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
mov    0x2b08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x88(%rbp)
movq   $0x0,-0x98(%rbp)
movq   $0x0,-0x90(%rbp)
movq   $0x0,-0xa0(%rbp)
mov    -0x44(%rbp),%edi
mov    0x2b15(%rip),%rax
mov    (%rax),%esi
lea    -0xa0(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    14ee <BecomeDaemonProcess+0x21e>
mov    -0x44(%rbp),%edi
call   10d0 <close@plt>
lea    0xb27(%rip),%rdi
call   1170 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    174e <BecomeDaemonProcess+0x47e>
xor    %eax,%eax
call   11f0 <fork@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0xa4(%rbp)
sub    $0xffffffff,%eax
je     1527 <BecomeDaemonProcess+0x257>
jmp    150f <BecomeDaemonProcess+0x23f>
mov    -0xa4(%rbp),%eax
test   %eax,%eax
jne    1566 <BecomeDaemonProcess+0x296>
jmp    1522 <BecomeDaemonProcess+0x252>
jmp    156d <BecomeDaemonProcess+0x29d>
mov    0x2a72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa8(%rbp)
mov    0x2a53(%rip),%rax
mov    (%rax),%rdi
call   11d0 <strerror@plt>
mov    -0xa8(%rbp),%edi
mov    %rax,%rdx
lea    0xc0e(%rip),%rsi
call   1110 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    174e <BecomeDaemonProcess+0x47e>
xor    %edi,%edi
call   11c0 <exit@plt>
mov    $0x0,%al
call   10e0 <setsid@plt>
cmp    $0x0,%rax
jge    158a <BecomeDaemonProcess+0x2ba>
movl   $0xffffffff,-0x4(%rbp)
jmp    174e <BecomeDaemonProcess+0x47e>
mov    0x2a57(%rip),%rax
mov    (%rax),%edi
mov    0x2a26(%rip),%rax
mov    (%rax),%esi
call   1100 <signal@plt>
xor    %eax,%eax
call   11f0 <fork@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0xac(%rbp)
sub    $0xffffffff,%eax
je     15da <BecomeDaemonProcess+0x30a>
jmp    15c2 <BecomeDaemonProcess+0x2f2>
mov    -0xac(%rbp),%eax
test   %eax,%eax
jne    15e6 <BecomeDaemonProcess+0x316>
jmp    15d5 <BecomeDaemonProcess+0x305>
jmp    15ed <BecomeDaemonProcess+0x31d>
movl   $0xffffffff,-0x4(%rbp)
jmp    174e <BecomeDaemonProcess+0x47e>
xor    %edi,%edi
call   11c0 <exit@plt>
mov    -0x44(%rbp),%edi
xor    %esi,%esi
call   10b0 <ftruncate@plt>
cmp    $0x0,%rax
jge    160d <BecomeDaemonProcess+0x33d>
movl   $0xffffffff,-0x4(%rbp)
jmp    174e <BecomeDaemonProcess+0x47e>
lea    -0x6f(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0xc8(%rbp),%rdi
mov    %eax,%edx
lea    0xb54(%rip),%rsi
call   11b0 <sprintf@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0xbc(%rbp)
lea    -0x6f(%rbp),%rax
mov    %rax,-0xb8(%rbp)
lea    -0x6f(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0xbc(%rbp),%edi
mov    -0xb8(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x44(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    0x293b(%rip),%rax
mov    (%rax),%edi
call   1180 <sysconf@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1690 <BecomeDaemonProcess+0x3c0>
movl   $0x100,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jl     16ca <BecomeDaemonProcess+0x3fa>
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
je     16b7 <BecomeDaemonProcess+0x3e7>
mov    -0x48(%rbp),%edi
call   10d0 <close@plt>
jmp    16bc <BecomeDaemonProcess+0x3ec>
mov    -0x48(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x48(%rbp)
jmp    1699 <BecomeDaemonProcess+0x3c9>
xor    %edi,%edi
call   1120 <umask@plt>
mov    0x2900(%rip),%rax
mov    (%rax),%esi
lea    0xaa6(%rip),%rdi
mov    $0x0,%al
call   1150 <open@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
call   10c0 <dup@plt>
mov    -0x38(%rbp),%edi
call   10c0 <dup@plt>
mov    -0x18(%rbp),%rdi
mov    0x28a2(%rip),%rax
mov    (%rax),%esi
mov    0x2871(%rip),%rax
or     (%rax),%esi
mov    0x2848(%rip),%rax
or     (%rax),%esi
mov    0x2857(%rip),%rax
or     (%rax),%esi
mov    0x28a6(%rip),%rax
mov    (%rax),%edx
call   11a0 <openlog@plt>
mov    -0x1c(%rbp),%edi
call   1060 <LOG_UPTO@plt>
mov    %eax,%edi
call   1140 <setlogmask@plt>
mov    $0x0,%al
call   11e0 <setpgrp@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BecomeDaemonProcess>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%r12
mov    %edx,%r13d
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xd0f(%rip),%rdi
call   1090 <chdir@plt>
mov    0x2cdb(%rip),%r14
mov    0x2cf4(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    0x2c88(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1150 <open@plt>
cmp    $0xffffffff,%eax
je     13ba <BecomeDaemonProcess+0xea>
mov    %eax,%ebp
mov    0x2c3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2c48(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2c6a(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
js     15ec <BecomeDaemonProcess+0x31c>
xor    %eax,%eax
call   11f0 <fork@plt>
test   %eax,%eax
je     145f <BecomeDaemonProcess+0x18f>
cmp    $0xffffffff,%eax
jne    1618 <BecomeDaemonProcess+0x348>
mov    0x2c0d(%rip),%rax
mov    (%rax),%ebx
mov    0x2bf4(%rip),%rax
mov    (%rax),%rdi
call   11d0 <strerror@plt>
lea    0xdb8(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1110 <fprintf@plt>
jmp    15d4 <BecomeDaemonProcess+0x304>
lea    0xc41(%rip),%rsi
mov    %rbx,%rdi
call   1160 <fopen@plt>
test   %rax,%rax
je     15f3 <BecomeDaemonProcess+0x323>
mov    %rax,%r14
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x10,%esi
mov    %rax,%rdx
call   10f0 <fgets@plt>
test   %rax,%rax
je     1601 <BecomeDaemonProcess+0x331>
mov    %rbp,%rdi
call   1080 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbp,1)
jne    1413 <BecomeDaemonProcess+0x143>
mov    %rbp,%rdi
call   1080 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbp,1)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1190 <strtoul@plt>
mov    %rax,%rbp
mov    %rax,%rdi
xor    %esi,%esi
call   1130 <kill@plt>
test   %eax,%eax
je     15ba <BecomeDaemonProcess+0x2ea>
mov    0x2b50(%rip),%rax
mov    (%rax),%rax
mov    0x2b0e(%rip),%rcx
cmp    (%rcx),%rax
jne    160a <BecomeDaemonProcess+0x33a>
lea    0xc27(%rip),%rdi
jmp    15c1 <BecomeDaemonProcess+0x2f1>
mov    %r13d,0xc(%rsp)
xor    %eax,%eax
call   10e0 <setsid@plt>
mov    $0xffffffff,%r13d
test   %rax,%rax
js     15da <BecomeDaemonProcess+0x30a>
mov    0x2b67(%rip),%rax
mov    (%rax),%edi
mov    0x2b36(%rip),%rax
mov    (%rax),%esi
call   1100 <signal@plt>
xor    %eax,%eax
call   11f0 <fork@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
je     15da <BecomeDaemonProcess+0x30a>
test   %r13d,%r13d
jne    1618 <BecomeDaemonProcess+0x348>
mov    %ebp,%edi
xor    %esi,%esi
call   10b0 <ftruncate@plt>
mov    $0xffffffff,%r13d
test   %rax,%rax
js     15da <BecomeDaemonProcess+0x30a>
xor    %r13d,%r13d
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xcad(%rip),%rsi
lea    0x31(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   11b0 <sprintf@plt>
mov    %rbx,%rdi
call   1080 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,(%r12)
mov    0x2aac(%rip),%rax
mov    (%rax),%edi
call   1180 <sysconf@plt>
lea    -0x1(%rax),%ecx
test   %eax,%eax
mov    $0xff,%ebx
cmovns %ecx,%ebx
test   %ebx,%ebx
jns    15ad <BecomeDaemonProcess+0x2dd>
xor    %edi,%edi
call   1120 <umask@plt>
mov    (%r14),%esi
lea    0xc56(%rip),%rdi
xor    %eax,%eax
call   1150 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   10c0 <dup@plt>
mov    %ebx,%edi
call   10c0 <dup@plt>
mov    0x2a59(%rip),%rax
mov    0x2a2a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x29ff(%rip),%rax
or     (%rax),%esi
mov    0x2a0e(%rip),%rax
or     (%rax),%esi
mov    0x2a5d(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
call   11a0 <openlog@plt>
mov    0xc(%rsp),%edi
call   1060 <LOG_UPTO@plt>
mov    %eax,%edi
call   1140 <setlogmask@plt>
xor    %eax,%eax
call   11e0 <setpgrp@plt>
jmp    15da <BecomeDaemonProcess+0x30a>
cs nopw 0x0(%rax,%rax,1)
lea    -0x1(%rbx),%eax
test   %ebx,%ebx
mov    %eax,%ebx
jle    1520 <BecomeDaemonProcess+0x250>
cmp    %ebx,%ebp
je     15a0 <BecomeDaemonProcess+0x2d0>
mov    %ebx,%edi
call   10d0 <close@plt>
jmp    15a0 <BecomeDaemonProcess+0x2d0>
lea    0xa56(%rip),%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   10a0 <printf@plt>
mov    %r14,%rdi
call   1070 <fclose@plt>
mov    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   10d0 <close@plt>
lea    0xa0a(%rip),%rdi
call   1170 <perror@plt>
jmp    15d4 <BecomeDaemonProcess+0x304>
lea    0xb42(%rip),%rdi
jmp    1611 <BecomeDaemonProcess+0x341>
lea    0xb0b(%rip),%rdi
call   1170 <perror@plt>
jmp    15cc <BecomeDaemonProcess+0x2fc>
xor    %edi,%edi
call   11c0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i64, i64, i64, i32, i32 }
@.str = [2 x i8] c"/\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"Can't get lockfile\00"
@.str.3 = [106 x i8] c"\0A\0AERROR\0A\0AA lock file %s has been detected. It appears it is owned\0Aby the (active) process with PID %ld.\0A\0A\00"
@errno = global i64 0
@ESRCH = global i64 0
@.str.4 = [155 x i8] c"\0A\0AERROR\0A\0AA lock file %s has been detected. It appears it is owned\0Aby the process with PID %ld, which is now defunct. Delete the lock file\0Aand try again.\0A\0A\00"
@.str.5 = [46 x i8] c"Could not acquire exclusive lock on lock file\00"
@.str.6 = [25 x i8] c"Could not read lock file\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@stderr = global i32 0
@.str.7 = [32 x i8] c"Error: initial fork failed: %s\0A\00"
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.8 = [4 x i8] c"%d\0A\00"
@_SC_OPEN_MAX = global i32 0
@.str.9 = [10 x i8] c"/dev/null\00"
@LOG_PID = global i32 0
@LOG_CONS = global i32 0
@LOG_NDELAY = global i32 0
@LOG_NOWAIT = global i32 0
@LOG_LOCAL0 = global i32 0
define i32 @BecomeDaemonProcess(i8* %0, i8* %1, i32 %2, i32* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca [17 x i8]
%20 = alloca i8*
%21 = alloca [7 x i8]
%22 = alloca i32*
%23 = alloca i64
%24 = alloca %struct.flock
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32* %3, i32** %10
store i32* %4, i32** %11
%25 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%26 = load i8*, i8** %7
%27 = load i32, i32* @O_RDWR
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @O_EXCL
%31 = or i32 %29, %30
%32 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %31, i32 420)
store i32 %32, i32* %16
%33 = load i32, i32* %16
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %91
35: ; preds = %5
%36 = load i8*, i8** %7
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %37, i32** %22
%38 = load i32*, i32** %22
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
br label %194
42: ; preds = %35
%43 = getelementptr inbounds [17 x i8], [17 x i8]* %19, i64 0, i64 0
%44 = load i32*, i32** %22
%45 = call i8* @fgets(i8* %43, i32 16, i32* %44)
store i8* %45, i8** %20
%46 = load i8*, i8** %20
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %86
48: ; preds = %42
%49 = getelementptr inbounds [17 x i8], [17 x i8]* %19, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = sub nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [17 x i8], [17 x i8]* %19, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 10
br i1 %56, label %57, label %63
57: ; preds = %48
%58 = getelementptr inbounds [17 x i8], [17 x i8]* %19, i64 0, i64 0
%59 = call i32 @strlen(i8* %58)
%60 = sub nsw i32 %59, 1
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [17 x i8], [17 x i8]* %19, i64 0, i64 %61
store i8 0, i8* %62
br label %63
63: ; preds = %57, %48
%64 = getelementptr inbounds [17 x i8], [17 x i8]* %19, i64 0, i64 0
%65 = call i64 @strtoul(i8* %64, i8** null, i32 10)
store i64 %65, i64* %23
%66 = load i64, i64* %23
%67 = call i32 @kill(i64 %66, i32 0)
store i32 %67, i32* %15
%68 = load i32, i32* %15
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %63
%71 = load i8*, i8** %7
%72 = load i64, i64* %23
%73 = call i32 @printf(i8* getelementptr inbounds ([106 x i8], [106 x i8]* @.str.3, i64 0, i64 0), i8* %71, i64 %72)
br label %85
74: ; preds = %63
%75 = load i64, i64* @errno
%76 = load i64, i64* @ESRCH
%77 = icmp eq i64 %75, %76
br i1 %77, label %78, label %82
78: ; preds = %74
%79 = load i8*, i8** %7
%80 = load i64, i64* %23
%81 = call i32 @printf(i8* getelementptr inbounds ([155 x i8], [155 x i8]* @.str.4, i64 0, i64 0), i8* %79, i64 %80)
br label %84
82: ; preds = %74
%83 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0))
br label %84
84: ; preds = %82, %78
br label %85
85: ; preds = %84, %70
br label %88
86: ; preds = %42
%87 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %88
88: ; preds = %86, %85
%89 = load i32*, i32** %22
%90 = call i32 @fclose(i32* %89)
store i32 -1, i32* %6
br label %194
91: ; preds = %5
%92 = load i32, i32* @F_WRLCK
%93 = getelementptr inbounds %struct.flock, %struct.flock* %24, i32 0, i32 4
store i32 %92, i32* %93
%94 = load i32, i32* @SEEK_SET
%95 = getelementptr inbounds %struct.flock, %struct.flock* %24, i32 0, i32 3
store i32 %94, i32* %95
%96 = getelementptr inbounds %struct.flock, %struct.flock* %24, i32 0, i32 1
store i64 0, i64* %96
%97 = getelementptr inbounds %struct.flock, %struct.flock* %24, i32 0, i32 2
store i64 0, i64* %97
%98 = getelementptr inbounds %struct.flock, %struct.flock* %24, i32 0, i32 0
store i64 0, i64* %98
%99 = load i32, i32* %16
%100 = load i32, i32* @F_SETLK
%101 = call i32 @fcntl(i32 %99, i32 %100, %struct.flock* %24)
store i32 %101, i32* %14
%102 = load i32, i32* %14
%103 = icmp slt i32 %102, 0
br i1 %103, label %104, label %108
104: ; preds = %91
%105 = load i32, i32* %16
%106 = call i32 @close(i32 %105)
%107 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
br label %194
108: ; preds = %91
%109 = call i32 (...) @fork()
store i32 %109, i32* %12
%110 = load i32, i32* %12
switch i32 %110, label %117 [
i32 0, label %111
i32 -1, label %112
]
111: ; preds = %108
br label %119
112: ; preds = %108
%113 = load i32, i32* @stderr
%114 = load i64, i64* @errno
%115 = call i8* @strerror(i64 %114)
%116 = call i32 @fprintf(i32 %113, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %115)
store i32 -1, i32* %6
br label %194
117: ; preds = %108
%118 = call i32 @exit(i32 0)
unreachable
119: ; preds = %111
%120 = call i64 (...) @setsid()
%121 = icmp slt i64 %120, 0
br i1 %121, label %122, label %123
122: ; preds = %119
store i32 -1, i32* %6
br label %194
123: ; preds = %119
%124 = load i32, i32* @SIGHUP
%125 = load i32, i32* @SIG_IGN
%126 = call i32 @signal(i32 %124, i32 %125)
%127 = call i32 (...) @fork()
store i32 %127, i32* %12
%128 = load i32, i32* %12
switch i32 %128, label %131 [
i32 0, label %129
i32 -1, label %130
]
129: ; preds = %123
br label %133
130: ; preds = %123
store i32 -1, i32* %6
br label %194
131: ; preds = %123
%132 = call i32 @exit(i32 0)
unreachable
133: ; preds = %129
%134 = load i32, i32* %16
%135 = call i64 @ftruncate(i32 %134, i32 0)
%136 = icmp slt i64 %135, 0
br i1 %136, label %137, label %138
137: ; preds = %133
store i32 -1, i32* %6
br label %194
138: ; preds = %133
%139 = getelementptr inbounds [7 x i8], [7 x i8]* %21, i64 0, i64 0
%140 = call i64 (...) @getpid()
%141 = trunc i64 %140 to i32
%142 = call i32 @sprintf(i8* %139, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i32 %141)
%143 = load i32, i32* %16
%144 = getelementptr inbounds [7 x i8], [7 x i8]* %21, i64 0, i64 0
%145 = getelementptr inbounds [7 x i8], [7 x i8]* %21, i64 0, i64 0
%146 = call i32 @strlen(i8* %145)
%147 = call i32 @write(i32 %143, i8* %144, i32 %146)
%148 = load i32, i32* %16
%149 = load i32*, i32** %10
store i32 %148, i32* %149
%150 = load i32, i32* @_SC_OPEN_MAX
%151 = call i32 @sysconf(i32 %150)
store i32 %151, i32* %18
%152 = load i32, i32* %18
%153 = icmp slt i32 %152, 0
br i1 %153, label %154, label %155
154: ; preds = %138
store i32 256, i32* %18
br label %155
155: ; preds = %154, %138
%156 = load i32, i32* %18
%157 = sub nsw i32 %156, 1
store i32 %157, i32* %17
br label %158
158: ; preds = %169, %155
%159 = load i32, i32* %17
%160 = icmp sge i32 %159, 0
br i1 %160, label %161, label %172
161: ; preds = %158
%162 = load i32, i32* %17
%163 = load i32, i32* %16
%164 = icmp ne i32 %162, %163
br i1 %164, label %165, label %168
165: ; preds = %161
%166 = load i32, i32* %17
%167 = call i32 @close(i32 %166)
br label %168
168: ; preds = %165, %161
br label %169
169: ; preds = %168
%170 = load i32, i32* %17
%171 = add nsw i32 %170, -1
store i32 %171, i32* %17
br label %158
172: ; preds = %158
%173 = call i32 @umask(i32 0)
%174 = load i32, i32* @O_RDWR
%175 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i32 %174)
store i32 %175, i32* %13
%176 = load i32, i32* %13
%177 = call i32 @dup(i32 %176)
%178 = load i32, i32* %13
%179 = call i32 @dup(i32 %178)
%180 = load i8*, i8** %8
%181 = load i32, i32* @LOG_PID
%182 = load i32, i32* @LOG_CONS
%183 = or i32 %181, %182
%184 = load i32, i32* @LOG_NDELAY
%185 = or i32 %183, %184
%186 = load i32, i32* @LOG_NOWAIT
%187 = or i32 %185, %186
%188 = load i32, i32* @LOG_LOCAL0
%189 = call i32 @openlog(i8* %180, i32 %187, i32 %188)
%190 = load i32, i32* %9
%191 = call i32 @LOG_UPTO(i32 %190)
%192 = call i32 @setlogmask(i32 %191)
%193 = call i32 (...) @setpgrp()
store i32 0, i32* %6
br label %194
194: ; preds = %172, %137, %130, %122, %112, %104, %88, %40
%195 = load i32, i32* %6
ret i32 %195
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i64, i32, i32 }
@.str = [2 x i8] c"/\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"Can't get lockfile\00"
@.str.3 = [106 x i8] c"\0A\0AERROR\0A\0AA lock file %s has been detected. It appears it is owned\0Aby the (active) process with PID %ld.\0A\0A\00"
@errno = global i64 0
@ESRCH = global i64 0
@.str.4 = [155 x i8] c"\0A\0AERROR\0A\0AA lock file %s has been detected. It appears it is owned\0Aby the process with PID %ld, which is now defunct. Delete the lock file\0Aand try again.\0A\0A\00"
@.str.5 = [46 x i8] c"Could not acquire exclusive lock on lock file\00"
@.str.6 = [25 x i8] c"Could not read lock file\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@stderr = global i32 0
@.str.7 = [32 x i8] c"Error: initial fork failed: %s\0A\00"
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.8 = [4 x i8] c"%d\0A\00"
@_SC_OPEN_MAX = global i32 0
@.str.9 = [10 x i8] c"/dev/null\00"
@LOG_PID = global i32 0
@LOG_CONS = global i32 0
@LOG_NDELAY = global i32 0
@LOG_NOWAIT = global i32 0
@LOG_LOCAL0 = global i32 0
define i32 @BecomeDaemonProcess(i8* %0, i8* %1, i32 %2, i32* nocapture writeonly %3, i32* nocapture readnone %4) {
%6 = alloca [17 x i8]
%7 = alloca [7 x i8]
%8 = alloca %struct.flock
%9 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 0
%10 = getelementptr inbounds [7 x i8], [7 x i8]* %7, i64 0, i64 0
%11 = bitcast %struct.flock* %8 to i8*
%12 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = load i32, i32* @O_EXCL
%17 = or i32 %15, %16
%18 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %17, i32 420)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %58
20: ; preds = %5
%21 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %130
25: ; preds = %20
%26 = call i8* @fgets(i8* %9, i32 16, i32* %21)
%27 = icmp eq i8* %26, null
br i1 %27, label %54, label %28
28: ; preds = %25
%29 = call i32 @strlen(i8* %9)
%30 = add nsw i32 %29, -1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 10
br i1 %34, label %35, label %40
35: ; preds = %28
%36 = call i32 @strlen(i8* %9)
%37 = add nsw i32 %36, -1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 %38
store i8 0, i8* %39
br label %40
40: ; preds = %35, %28
%41 = call i64 @strtoul(i8* nocapture %9, i8** null, i32 10)
%42 = call i32 @kill(i64 %41, i32 0)
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @printf(i8* getelementptr inbounds ([106 x i8], [106 x i8]* @.str.3, i64 0, i64 0), i8* %0, i64 %41)
br label %56
46: ; preds = %40
%47 = load i64, i64* @errno
%48 = load i64, i64* @ESRCH
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @printf(i8* getelementptr inbounds ([155 x i8], [155 x i8]* @.str.4, i64 0, i64 0), i8* %0, i64 %41)
br label %56
52: ; preds = %46
%53 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0))
br label %56
54: ; preds = %25
%55 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %56
56: ; preds = %44, %52, %50, %54
%57 = call i32 @fclose(i32* %21)
br label %130
58: ; preds = %5
%59 = load i32, i32* @F_WRLCK
%60 = getelementptr inbounds %struct.flock, %struct.flock* %8, i64 0, i32 4
store i32 %59, i32* %60
%61 = load i32, i32* @SEEK_SET
%62 = getelementptr inbounds %struct.flock, %struct.flock* %8, i64 0, i32 3
store i32 %61, i32* %62
%63 = bitcast %struct.flock* %8 to i8*
%64 = load i32, i32* @F_SETLK
%65 = call i32 @fcntl(i32 %18, i32 %64, %struct.flock* %8)
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %58
%68 = call i32 @close(i32 %18)
%69 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %130
70: ; preds = %58
%71 = call i32 (...) @fork()
switch i32 %71, label %77 [
i32 0, label %79
i32 -1, label %72
]
72: ; preds = %70
%73 = load i32, i32* @stderr
%74 = load i64, i64* @errno
%75 = call i8* @strerror(i64 %74)
%76 = call i32 @fprintf(i32 %73, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %75)
br label %130
77: ; preds = %70
%78 = call i32 @exit(i32 0)
unreachable
79: ; preds = %70
%80 = call i64 (...) @setsid()
%81 = icmp slt i64 %80, 0
br i1 %81, label %130, label %82
82: ; preds = %79
%83 = load i32, i32* @SIGHUP
%84 = load i32, i32* @SIG_IGN
%85 = call i32 @signal(i32 %83, i32 %84)
%86 = call i32 (...) @fork()
switch i32 %86, label %87 [
i32 0, label %89
i32 -1, label %130
]
87: ; preds = %82
%88 = call i32 @exit(i32 0)
unreachable
89: ; preds = %82
%90 = call i64 @ftruncate(i32 %18, i32 0)
%91 = icmp slt i64 %90, 0
br i1 %91, label %130, label %92
92: ; preds = %89
%93 = call i64 (...) @getpid()
%94 = trunc i64 %93 to i32
%95 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i32 %94)
%96 = call i32 @strlen(i8* %10)
%97 = call i32 @write(i32 %18, i8* %10, i32 %96)
store i32 %18, i32* %3
%98 = load i32, i32* @_SC_OPEN_MAX
%99 = call i32 @sysconf(i32 %98)
%100 = icmp slt i32 %99, 0
%101 = add nsw i32 %99, -1
%102 = select i1 %100, i32 255, i32 %101
%103 = icmp sgt i32 %102, -1
br i1 %103, label %104, label %112
104: ; preds = %92, %109
%105 = phi i32 [ %110, %109 ], [ %102, %92 ]
%106 = icmp eq i32 %105, %18
br i1 %106, label %109, label %107
107: ; preds = %104
%108 = call i32 @close(i32 %105)
br label %109
109: ; preds = %104, %107
%110 = add nsw i32 %105, -1
%111 = icmp sgt i32 %105, 0
br i1 %111, label %104, label %112
112: ; preds = %109, %92
%113 = call i32 @umask(i32 0)
%114 = load i32, i32* @O_RDWR
%115 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i32 %114)
%116 = call i32 @dup(i32 %115)
%117 = call i32 @dup(i32 %115)
%118 = load i32, i32* @LOG_PID
%119 = load i32, i32* @LOG_CONS
%120 = or i32 %119, %118
%121 = load i32, i32* @LOG_NDELAY
%122 = or i32 %120, %121
%123 = load i32, i32* @LOG_NOWAIT
%124 = or i32 %122, %123
%125 = load i32, i32* @LOG_LOCAL0
%126 = call i32 @openlog(i8* %1, i32 %124, i32 %125)
%127 = call i32 @LOG_UPTO(i32 %2)
%128 = call i32 @setlogmask(i32 %127)
%129 = call i32 (...) @setpgrp()
br label %130
130: ; preds = %89, %82, %79, %112, %72, %67, %56, %23
%131 = phi i32 [ -1, %23 ], [ -1, %56 ], [ -1, %67 ], [ -1, %72 ], [ 0, %112 ], [ -1, %79 ], [ %86, %82 ], [ -1, %89 ]
ret i32 %131
}