#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 */
typedef struct TYPE_9__ TYPE_2__;
typedef struct TYPE_8__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ pthread_mutexattr_t;
struct TYPE_8__ {
  int __owner;
};
struct TYPE_9__ {
  TYPE_1__ __data;
};
typedef TYPE_2__ pthread_mutex_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int EBUSY;
int EOWNERDEAD;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int MAP_FILE;
int MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ PTHREAD_MUTEX_ERRORCHECK;
int /*<<< orphan*/ PTHREAD_MUTEX_ROBUST;
int /*<<< orphan*/ PTHREAD_PROCESS_SHARED;
int /*<<< orphan*/ SEEK_SET;
int atoi(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ftruncate(int, int);
int getpid();
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int pthread_mutex_consistent(TYPE_2__ *);
int pthread_mutex_init(TYPE_2__ *, int /*<<< orphan*/ *);
int pthread_mutex_lock(TYPE_2__ *);
int pthread_mutex_trylock(TYPE_2__ *);
int pthread_mutex_unlock(TYPE_2__ *);
int pthread_mutexattr_init(int /*<<< orphan*/ *);
int pthread_mutexattr_setpshared(int /*<<< orphan*/ *, int /*<<< orphan*/);
int pthread_mutexattr_setrobust(int /*<<< orphan*/ *, int /*<<< orphan*/);
int pthread_mutexattr_settype(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ set_priority(int);
int /*<<< orphan*/ setpgid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ usage(char const *);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
int /*<<< orphan*/ worker(char const *);
int main(int argc, const char **argv) {
  int i, fd, ret, num_children, mode = -1, priority = 0;
  const char *mode_str;
  const char *file;
  char *addr;
  pthread_mutex_t *mutex;
  pthread_mutexattr_t mattr;
  pid_t pid;
  if (argc < 3 || argc > 5)
    usage(argv[0]);
  file = argv[1];
  mode_str = argv[2];
  if (strcmp("init", mode_str) == 0) {
    mode = 0;
  } else if (strcmp("worker", mode_str) == 0) {
    mode = 1;
  } else if (strcmp("debug", mode_str) == 0) {
    mode = 2;
  } else {
    usage(argv[0]);
  }
  if (mode == 1) {
    priority = atoi(argv[4]);
    set_priority(priority);
  }
  fd = open(argv[1], O_CREAT | O_RDWR, 0600);
  if (fd == -1) {
    perror("FAIL: open");
    exit(EXIT_FAILURE);
  }
  ret = lseek(fd, 0, SEEK_SET);
  if (ret != 0) {
    perror("FAIL: lseek");
    exit(EXIT_FAILURE);
  }
  if (mode == 0) {
    ret = ftruncate(fd, sizeof(pthread_mutex_t));
    if (ret != 0) {
      perror("FAIL: ftruncate");
      exit(EXIT_FAILURE);
    }
  }
  addr = mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE,
              MAP_SHARED | MAP_FILE, fd, 0);
  if (addr == NULL) {
    perror("FAIL: mmap");
    exit(EXIT_FAILURE);
  }
  mutex = (pthread_mutex_t *)(void *)addr;
  if (mode == 2) {
    ret = pthread_mutex_trylock(mutex);
    if (ret == EOWNERDEAD) {
      ret = pthread_mutex_consistent(mutex);
      if (ret == 0) {
        pthread_mutex_unlock(mutex);
      } else {
        fprintf(stderr, "FAIL: pthread_mutex_consistent "
                        "failed\n");
        exit(EXIT_FAILURE);
      }
    } else if (ret == EBUSY) {
      printf("INFO: pid=%u\n", mutex->__data.__owner);
    } else if (ret == 0) {
      pthread_mutex_unlock(mutex);
    }
    exit(EXIT_SUCCESS);
  }
  if (mode == 0) {
    ret = pthread_mutexattr_init(&mattr);
    if (ret != 0) {
      fprintf(stderr, "FAIL: pthread_mutexattr_init failed\n");
      exit(EXIT_FAILURE);
    }
    ret = pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_ERRORCHECK);
    if (ret != 0) {
      fprintf(stderr, "FAIL: pthread_mutexattr_settype failed\n");
      exit(EXIT_FAILURE);
    }
    ret = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
    if (ret != 0) {
      fprintf(stderr, "FAIL: pthread_mutexattr_setpshared failed\n");
      exit(EXIT_FAILURE);
    }
    ret = pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
    if (ret != 0) {
      fprintf(stderr, "FAIL: pthread_mutexattr_setrobust failed\n");
      exit(EXIT_FAILURE);
    }
    ret = pthread_mutex_init(mutex, &mattr);
    if (ret != 0) {
      fprintf(stderr, "FAIL: pthread_mutex_init failed\n");
      exit(EXIT_FAILURE);
    }
    printf("INFO: init: Mutex initialization complete.\n");
    for (;;)
      sleep(1);
  }
  fprintf(stderr, "INFO: parent: Acquiring mutex (pid = %d).\n", getpid());
  do {
    ret = pthread_mutex_lock(mutex);
    if (ret == EOWNERDEAD) {
      int rc;
      rc = pthread_mutex_consistent(mutex);
      if (rc == 0) {
        pthread_mutex_unlock(mutex);
      } else {
        fprintf(stderr, "FAIL: pthread_mutex_consistent "
                        "failed\n");
        exit(EXIT_FAILURE);
      }
      fprintf(stderr, "INFO: parent: Unlock recovery ret = %d\n", ret);
    }
  } while (ret != 0);
  setpgid(0, 0);
  fprintf(stderr, "INFO: parent: Creating children\n");
  num_children = atoi(argv[3]);
  for (i = 0; i < num_children; i++) {
    pid = fork();
    if (pid < 0) {
      fprintf(stderr, "FAIL: fork() failed\n");
      exit(EXIT_FAILURE);
    }
    if (pid == 0) {
      close(fd);
      worker(file);
      exit(EXIT_FAILURE);
    }
  }
  fprintf(stderr, "INFO: parent: Waiting for children\n");
  ret = pthread_mutex_unlock(mutex);
  if (ret != 0) {
    fprintf(stderr, "FAIL: pthread_mutex_unlock failed\n");
    exit(EXIT_FAILURE);
  }
  for (i = 0; i < num_children; i++) {
    int status;
    pid = waitpid(-1, &status, 0);
    if (pid <= 0) {
      fprintf(stderr, "FAIL: waitpid() failed\n");
      exit(EXIT_FAILURE);
    }
    fprintf(stderr, "INFO: parent: Reaped %u\n", (unsigned int)pid);
  }
  close(fd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     12f8 <main+0x38>
cmpl   $0x5,-0x8(%rbp)
jle    1304 <main+0x44>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1120 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xcd9(%rip),%rdi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
jne    1342 <main+0x82>
movl   $0x0,-0x24(%rbp)
jmp    13a4 <main+0xe4>
mov    -0x30(%rbp),%rsi
lea    0xcb8(%rip),%rdi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
jne    1368 <main+0xa8>
movl   $0x1,-0x24(%rbp)
jmp    139f <main+0xdf>
mov    -0x30(%rbp),%rsi
lea    0xc99(%rip),%rdi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
jne    138e <main+0xce>
movl   $0x2,-0x24(%rbp)
jmp    139a <main+0xda>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1120 <usage@plt>
jmp    139f <main+0xdf>
jmp    13a4 <main+0xe4>
cmpl   $0x1,-0x24(%rbp)
jne    13c6 <main+0x106>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1160 <atoi@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   11a0 <set_priority@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2c23(%rip),%rax
mov    (%rax),%esi
mov    0x2bf2(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1140 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1411 <main+0x151>
lea    0xc14(%rip),%rdi
call   1150 <perror@plt>
mov    0x2b8e(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2b85(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <lseek@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     144b <main+0x18b>
lea    0xbe5(%rip),%rdi
call   1150 <perror@plt>
mov    0x2b54(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jne    148e <main+0x1ce>
mov    -0x18(%rbp),%edi
mov    $0x4,%esi
call   1070 <ftruncate@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1489 <main+0x1c9>
lea    0xbb3(%rip),%rdi
call   1150 <perror@plt>
mov    0x2b16(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
jmp    148e <main+0x1ce>
mov    0x2b5b(%rip),%rax
mov    (%rax),%edx
mov    0x2af2(%rip),%rax
or     (%rax),%edx
mov    0x2b39(%rip),%rax
mov    (%rax),%ecx
mov    0x2b18(%rip),%rax
or     (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    14f0 <main+0x230>
lea    0xb5c(%rip),%rdi
call   1150 <perror@plt>
mov    0x2aaf(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpl   $0x2,-0x24(%rbp)
jne    15c4 <main+0x304>
mov    -0x48(%rbp),%rdi
call   10b0 <pthread_mutex_trylock@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2a90(%rip),%rcx
cmp    (%rcx),%eax
jne    156e <main+0x2ae>
mov    -0x48(%rbp),%rdi
call   10e0 <pthread_mutex_consistent@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1544 <main+0x284>
mov    -0x48(%rbp),%rdi
call   1110 <pthread_mutex_unlock@plt>
jmp    1569 <main+0x2a9>
mov    0x2a65(%rip),%rax
mov    (%rax),%edi
lea    0xaf0(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    0x2a36(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
jmp    15b6 <main+0x2f6>
mov    -0x1c(%rbp),%eax
mov    0x2a00(%rip),%rcx
cmp    (%rcx),%eax
jne    1599 <main+0x2d9>
mov    -0x48(%rbp),%rax
mov    (%rax),%esi
lea    0xade(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    15b1 <main+0x2f1>
cmpl   $0x0,-0x1c(%rbp)
jne    15ac <main+0x2ec>
mov    -0x48(%rbp),%rdi
call   1110 <pthread_mutex_unlock@plt>
jmp    15b1 <main+0x2f1>
jmp    15b6 <main+0x2f6>
mov    0x29c3(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jne    1731 <main+0x471>
lea    -0x4c(%rbp),%rdi
call   1190 <pthread_mutexattr_init@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1609 <main+0x349>
mov    0x29c5(%rip),%rax
mov    (%rax),%edi
lea    0xa85(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    0x2996(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
mov    0x2958(%rip),%rax
mov    (%rax),%esi
lea    -0x4c(%rbp),%rdi
call   1060 <pthread_mutexattr_settype@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     164d <main+0x38d>
mov    0x2981(%rip),%rax
mov    (%rax),%edi
lea    0xa66(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    0x2952(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
mov    0x2934(%rip),%rax
mov    (%rax),%esi
lea    -0x4c(%rbp),%rdi
call   1100 <pthread_mutexattr_setpshared@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1691 <main+0x3d1>
mov    0x293d(%rip),%rax
mov    (%rax),%edi
lea    0xa4a(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    0x290e(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
mov    0x2920(%rip),%rax
mov    (%rax),%esi
lea    -0x4c(%rbp),%rdi
call   10a0 <pthread_mutexattr_setrobust@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     16d5 <main+0x415>
mov    0x28f9(%rip),%rax
mov    (%rax),%edi
lea    0xa31(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    0x28ca(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
mov    -0x48(%rbp),%rdi
lea    -0x4c(%rbp),%rsi
call   11c0 <pthread_mutex_init@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1714 <main+0x454>
mov    0x28ba(%rip),%rax
mov    (%rax),%edi
lea    0xa1c(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    0x288b(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
lea    0xa21(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   11b0 <sleep@plt>
jmp    1722 <main+0x462>
mov    0x2878(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x64(%rbp),%edi
mov    %eax,%edx
lea    0xa18(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    -0x48(%rbp),%rdi
call   11e0 <pthread_mutex_lock@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x283b(%rip),%rcx
cmp    (%rcx),%eax
jne    17d8 <main+0x518>
mov    -0x48(%rbp),%rdi
call   10e0 <pthread_mutex_consistent@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jne    1799 <main+0x4d9>
mov    -0x48(%rbp),%rdi
call   1110 <pthread_mutex_unlock@plt>
jmp    17be <main+0x4fe>
mov    0x2810(%rip),%rax
mov    (%rax),%edi
lea    0x89b(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    0x27e1(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
mov    0x27eb(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0x9c2(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
jmp    17dd <main+0x51d>
cmpl   $0x0,-0x1c(%rbp)
jne    1757 <main+0x497>
xor    %esi,%esi
mov    %esi,%edi
call   1170 <setpgid@plt>
mov    0x27b9(%rip),%rax
mov    (%rax),%edi
lea    0x9bb(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1160 <atoi@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    18a2 <main+0x5e2>
mov    $0x0,%al
call   11d0 <fork@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jge    1865 <main+0x5a5>
mov    0x2769(%rip),%rax
mov    (%rax),%edi
lea    0x98c(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    0x273a(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
cmpq   $0x0,-0x58(%rbp)
jne    188f <main+0x5cf>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    -0x38(%rbp),%rdi
call   10d0 <worker@plt>
mov    0x2710(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
jmp    1894 <main+0x5d4>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    181e <main+0x55e>
mov    0x2707(%rip),%rax
mov    (%rax),%edi
lea    0x93f(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    -0x48(%rbp),%rdi
call   1110 <pthread_mutex_unlock@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     18f4 <main+0x634>
mov    0x26da(%rip),%rax
mov    (%rax),%edi
lea    0x936(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    0x26ab(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1976 <main+0x6b6>
mov    $0xffffffff,%edi
lea    -0x60(%rbp),%rsi
xor    %edx,%edx
call   1130 <waitpid@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jg     194b <main+0x68b>
mov    0x2683(%rip),%rax
mov    (%rax),%edi
lea    0x902(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    0x2654(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>
mov    0x265e(%rip),%rax
mov    (%rax),%edi
mov    -0x58(%rbp),%rax
mov    %eax,%edx
lea    0x8ef(%rip),%rsi
mov    $0x0,%al
call   10f0 <fprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    18fb <main+0x63b>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    0x25fb(%rip),%rax
mov    (%rax),%edi
call   1180 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
add    $0xfffffffa,%edi
cmp    $0xfffffffc,%edi
ja     12ee <main+0x1e>
mov    (%rbx),%rdi
call   1130 <usage@plt>
mov    0x8(%rbx),%r14
mov    0x10(%rbx),%rbp
lea    0xd03(%rip),%rdi
mov    %rbp,%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     133c <main+0x6c>
lea    0xcf4(%rip),%rdi
mov    %rbp,%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     1344 <main+0x74>
lea    0xce7(%rip),%rdi
mov    %rbp,%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     1359 <main+0x89>
mov    (%rbx),%rdi
call   1130 <usage@plt>
jmp    1354 <main+0x84>
mov    $0x1,%bpl
xor    %r12d,%r12d
jmp    135e <main+0x8e>
mov    0x20(%rbx),%rdi
call   1170 <atoi@plt>
mov    %eax,%edi
call   11b0 <set_priority@plt>
xor    %r12d,%r12d
jmp    135c <main+0x8c>
mov    $0x1,%r12b
xor    %ebp,%ebp
mov    0x8(%rbx),%rdi
mov    0x2c8f(%rip),%rax
mov    0x2c60(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x180,%edx
call   1150 <open@plt>
cmp    $0xffffffff,%eax
je     149c <main+0x1cc>
mov    %eax,%r15d
mov    0x2c0f(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1090 <lseek@plt>
test   %eax,%eax
jne    14a8 <main+0x1d8>
test   %bpl,%bpl
je     13bf <main+0xef>
mov    %r15d,%edi
mov    $0x4,%esi
call   1080 <ftruncate@plt>
test   %eax,%eax
jne    169a <main+0x3ca>
mov    0x2c2a(%rip),%rax
mov    0x2bc3(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2c08(%rip),%rax
mov    0x2be9(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
test   %rax,%rax
je     14b4 <main+0x1e4>
mov    %rax,%r13
test   %r12b,%r12b
jne    14c0 <main+0x1f0>
test   %bpl,%bpl
je     14ec <main+0x21c>
mov    %rsp,%rdi
call   11a0 <pthread_mutexattr_init@plt>
test   %eax,%eax
jne    1577 <main+0x2a7>
mov    0x2b3e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1070 <pthread_mutexattr_settype@plt>
test   %eax,%eax
jne    1589 <main+0x2b9>
mov    0x2b45(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1110 <pthread_mutexattr_setpshared@plt>
test   %eax,%eax
jne    159b <main+0x2cb>
mov    0x2b5c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   10b0 <pthread_mutexattr_setrobust@plt>
test   %eax,%eax
jne    15ad <main+0x2dd>
mov    %rsp,%rsi
mov    %r13,%rdi
call   11d0 <pthread_mutex_init@plt>
test   %eax,%eax
jne    15bf <main+0x2ef>
lea    0xdb5(%rip),%rdi
call   1030 <puts@plt>
nopl   (%rax)
mov    $0x1,%edi
call   11c0 <sleep@plt>
jmp    1490 <main+0x1c0>
lea    0xb6f(%rip),%rdi
jmp    16a1 <main+0x3d1>
lea    0xb6e(%rip),%rdi
jmp    16a1 <main+0x3d1>
lea    0xb7e(%rip),%rdi
jmp    16a1 <main+0x3d1>
mov    %r13,%rdi
call   10c0 <pthread_mutex_trylock@plt>
mov    0x2ad9(%rip),%rcx
cmp    (%rcx),%eax
jne    16b4 <main+0x3e4>
mov    %r13,%rdi
call   10f0 <pthread_mutex_consistent@plt>
test   %eax,%eax
jne    1565 <main+0x295>
jmp    16d7 <main+0x407>
mov    0x2abd(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xc39(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1100 <fprintf@plt>
mov    0x2a93(%rip),%r12
jmp    1549 <main+0x279>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1120 <pthread_mutex_unlock@plt>
mov    0x2a81(%rip),%rax
mov    (%rax),%edi
lea    0xc2f(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1100 <fprintf@plt>
test   %ebp,%ebp
je     15db <main+0x30b>
mov    %r13,%rdi
call   11f0 <pthread_mutex_lock@plt>
mov    %eax,%ebp
cmp    (%r12),%eax
jne    1541 <main+0x271>
mov    %r13,%rdi
call   10f0 <pthread_mutex_consistent@plt>
test   %eax,%eax
je     1520 <main+0x250>
mov    0x2a44(%rip),%rax
mov    (%rax),%edi
lea    0xacf(%rip),%rsi
jmp    15cf <main+0x2ff>
mov    0x2a32(%rip),%rax
mov    (%rax),%edi
lea    0xaf2(%rip),%rsi
jmp    15cf <main+0x2ff>
mov    0x2a20(%rip),%rax
mov    (%rax),%edi
lea    0xb05(%rip),%rsi
jmp    15cf <main+0x2ff>
mov    0x2a0e(%rip),%rax
mov    (%rax),%edi
lea    0xb1b(%rip),%rsi
jmp    15cf <main+0x2ff>
mov    0x29fc(%rip),%rax
mov    (%rax),%edi
lea    0xb34(%rip),%rsi
jmp    15cf <main+0x2ff>
mov    0x29ea(%rip),%rax
mov    (%rax),%edi
lea    0xb4c(%rip),%rsi
xor    %eax,%eax
call   1100 <fprintf@plt>
jmp    16a6 <main+0x3d6>
xor    %edi,%edi
xor    %esi,%esi
call   1180 <setpgid@plt>
mov    0x29c5(%rip),%rax
mov    (%rax),%edi
lea    0xb9b(%rip),%rsi
xor    %eax,%eax
call   1100 <fprintf@plt>
mov    0x18(%rbx),%rdi
call   1170 <atoi@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1627 <main+0x357>
mov    %ebx,%ebp
xor    %eax,%eax
call   11e0 <fork@plt>
test   %rax,%rax
js     1702 <main+0x432>
je     1717 <main+0x447>
add    $0xffffffff,%ebp
jne    160c <main+0x33c>
mov    0x2982(%rip),%rax
mov    (%rax),%edi
lea    0xb8e(%rip),%rsi
xor    %eax,%eax
call   1100 <fprintf@plt>
mov    %r13,%rdi
call   1120 <pthread_mutex_unlock@plt>
test   %eax,%eax
jne    16ed <main+0x41d>
test   %ebx,%ebx
jle    1690 <main+0x3c0>
lea    0x4(%rsp),%r14
lea    0xbc6(%rip),%rbp
mov    $0xffffffff,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1140 <waitpid@plt>
mov    0x293c(%rip),%rcx
mov    (%rcx),%edi
test   %rax,%rax
jle    172c <main+0x45c>
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1100 <fprintf@plt>
add    $0xffffffff,%ebx
jne    165e <main+0x38e>
mov    %r15d,%edi
call   10a0 <close@plt>
jmp    16df <main+0x40f>
lea    0x988(%rip),%rdi
call   1160 <perror@plt>
mov    0x28eb(%rip),%rax
mov    (%rax),%edi
call   1190 <exit@plt>
mov    0x28bd(%rip),%rcx
cmp    (%rcx),%eax
jne    16d3 <main+0x403>
mov    0x0(%r13),%esi
lea    0x9a1(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    16df <main+0x40f>
test   %eax,%eax
jne    16df <main+0x40f>
mov    %r13,%rdi
call   1120 <pthread_mutex_unlock@plt>
mov    0x289a(%rip),%rax
mov    (%rax),%edi
call   1190 <exit@plt>
mov    0x28bc(%rip),%rax
mov    (%rax),%edi
lea    0xaec(%rip),%rsi
jmp    15cf <main+0x2ff>
mov    0x28a7(%rip),%rax
mov    (%rax),%edi
lea    0xa9e(%rip),%rsi
jmp    15cf <main+0x2ff>
mov    %r15d,%edi
call   10a0 <close@plt>
mov    %r14,%rdi
call   10e0 <worker@plt>
jmp    16a6 <main+0x3d6>
lea    0xad9(%rip),%rsi
jmp    15cf <main+0x2ff>

LLVM IR (-O0):
%struct.TYPE_9__ = type { %struct.TYPE_8__ }
%struct.TYPE_8__ = type { i32 }
@.str = [5 x i8] c"init\00"
@.str.1 = [7 x i8] c"worker\00"
@.str.2 = [6 x i8] c"debug\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.3 = [11 x i8] c"FAIL: open\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@.str.4 = [12 x i8] c"FAIL: lseek\00"
@.str.5 = [16 x i8] c"FAIL: ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FILE = global i32 0
@.str.6 = [11 x i8] c"FAIL: mmap\00"
@EOWNERDEAD = global i32 0
@stderr = global i32 0
@.str.7 = [39 x i8] c"FAIL: pthread_mutex_consistent failed\0A\00"
@EBUSY = global i32 0
@.str.8 = [14 x i8] c"INFO: pid=%u\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.9 = [37 x i8] c"FAIL: pthread_mutexattr_init failed\0A\00"
@PTHREAD_MUTEX_ERRORCHECK = global i32 0
@.str.10 = [40 x i8] c"FAIL: pthread_mutexattr_settype failed\0A\00"
@PTHREAD_PROCESS_SHARED = global i32 0
@.str.11 = [43 x i8] c"FAIL: pthread_mutexattr_setpshared failed\0A\00"
@PTHREAD_MUTEX_ROBUST = global i32 0
@.str.12 = [42 x i8] c"FAIL: pthread_mutexattr_setrobust failed\0A\00"
@.str.13 = [33 x i8] c"FAIL: pthread_mutex_init failed\0A\00"
@.str.14 = [44 x i8] c"INFO: init: Mutex initialization complete.\0A\00"
@.str.15 = [43 x i8] c"INFO: parent: Acquiring mutex (pid = %d).\0A\00"
@.str.16 = [40 x i8] c"INFO: parent: Unlock recovery ret = %d\0A\00"
@.str.17 = [33 x i8] c"INFO: parent: Creating children\0A\00"
@.str.18 = [21 x i8] c"FAIL: fork() failed\0A\00"
@.str.19 = [36 x i8] c"INFO: parent: Waiting for children\0A\00"
@.str.20 = [35 x i8] c"FAIL: pthread_mutex_unlock failed\0A\00"
@.str.21 = [24 x i8] c"FAIL: waitpid() failed\0A\00"
@.str.22 = [25 x i8] c"INFO: parent: Reaped %u\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca %struct.TYPE_9__*
%16 = alloca i32
%17 = alloca i64
%18 = alloca i32
%19 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %10
store i32 0, i32* %11
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 3
br i1 %21, label %25, label %22
22: ; preds = %2
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, 5
br i1 %24, label %25, label %30
25: ; preds = %22, %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 0
%28 = load i8*, i8** %27
%29 = call i32 @usage(i8* %28)
br label %30
30: ; preds = %25, %22
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
store i8* %33, i8** %13
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
store i8* %36, i8** %12
%37 = load i8*, i8** %12
%38 = call i64 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %30
store i32 0, i32* %10
br label %58
41: ; preds = %30
%42 = load i8*, i8** %12
%43 = call i64 @strcmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %41
store i32 1, i32* %10
br label %57
46: ; preds = %41
%47 = load i8*, i8** %12
%48 = call i64 @strcmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 2, i32* %10
br label %56
51: ; preds = %46
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 0
%54 = load i8*, i8** %53
%55 = call i32 @usage(i8* %54)
br label %56
56: ; preds = %51, %50
br label %57
57: ; preds = %56, %45
br label %58
58: ; preds = %57, %40
%59 = load i32, i32* %10
%60 = icmp eq i32 %59, 1
br i1 %60, label %61, label %68
61: ; preds = %58
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 4
%64 = load i8*, i8** %63
%65 = call i32 @atoi(i8* %64)
store i32 %65, i32* %11
%66 = load i32, i32* %11
%67 = call i32 @set_priority(i32 %66)
br label %68
68: ; preds = %61, %58
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 1
%71 = load i8*, i8** %70
%72 = load i32, i32* @O_CREAT
%73 = load i32, i32* @O_RDWR
%74 = or i32 %72, %73
%75 = call i32 @open(i8* %71, i32 %74, i32 384)
store i32 %75, i32* %7
%76 = load i32, i32* %7
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %82
78: ; preds = %68
%79 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%80 = load i32, i32* @EXIT_FAILURE
%81 = call i32 @exit(i32 %80)
unreachable
82: ; preds = %68
%83 = load i32, i32* %7
%84 = load i32, i32* @SEEK_SET
%85 = call i32 @lseek(i32 %83, i32 0, i32 %84)
store i32 %85, i32* %8
%86 = load i32, i32* %8
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %92
88: ; preds = %82
%89 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%90 = load i32, i32* @EXIT_FAILURE
%91 = call i32 @exit(i32 %90)
unreachable
92: ; preds = %82
%93 = load i32, i32* %10
%94 = icmp eq i32 %93, 0
br i1 %94, label %95, label %105
95: ; preds = %92
%96 = load i32, i32* %7
%97 = call i32 @ftruncate(i32 %96, i32 4)
store i32 %97, i32* %8
%98 = load i32, i32* %8
%99 = icmp ne i32 %98, 0
br i1 %99, label %100, label %104
100: ; preds = %95
%101 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%102 = load i32, i32* @EXIT_FAILURE
%103 = call i32 @exit(i32 %102)
unreachable
104: ; preds = %95
br label %105
105: ; preds = %104, %92
%106 = load i32, i32* @PROT_READ
%107 = load i32, i32* @PROT_WRITE
%108 = or i32 %106, %107
%109 = load i32, i32* @MAP_SHARED
%110 = load i32, i32* @MAP_FILE
%111 = or i32 %109, %110
%112 = load i32, i32* %7
%113 = call i8* @mmap(i32* null, i32 4, i32 %108, i32 %111, i32 %112, i32 0)
store i8* %113, i8** %14
%114 = load i8*, i8** %14
%115 = icmp eq i8* %114, null
br i1 %115, label %116, label %120
116: ; preds = %105
%117 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%118 = load i32, i32* @EXIT_FAILURE
%119 = call i32 @exit(i32 %118)
unreachable
120: ; preds = %105
%121 = load i8*, i8** %14
%122 = bitcast i8* %121 to %struct.TYPE_9__*
store %struct.TYPE_9__* %122, %struct.TYPE_9__** %15
%123 = load i32, i32* %10
%124 = icmp eq i32 %123, 2
br i1 %124, label %125, label %166
125: ; preds = %120
%126 = load %struct.TYPE_9__*, %struct.TYPE_9__** %15
%127 = call i32 @pthread_mutex_trylock(%struct.TYPE_9__* %126)
store i32 %127, i32* %8
%128 = load i32, i32* %8
%129 = load i32, i32* @EOWNERDEAD
%130 = icmp eq i32 %128, %129
br i1 %130, label %131, label %145
131: ; preds = %125
%132 = load %struct.TYPE_9__*, %struct.TYPE_9__** %15
%133 = call i32 @pthread_mutex_consistent(%struct.TYPE_9__* %132)
store i32 %133, i32* %8
%134 = load i32, i32* %8
%135 = icmp eq i32 %134, 0
br i1 %135, label %136, label %139
136: ; preds = %131
%137 = load %struct.TYPE_9__*, %struct.TYPE_9__** %15
%138 = call i32 @pthread_mutex_unlock(%struct.TYPE_9__* %137)
br label %144
139: ; preds = %131
%140 = load i32, i32* @stderr
%141 = call i32 (i32, i8*, ...) @fprintf(i32 %140, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%142 = load i32, i32* @EXIT_FAILURE
%143 = call i32 @exit(i32 %142)
unreachable
144: ; preds = %136
br label %163
145: ; preds = %125
%146 = load i32, i32* %8
%147 = load i32, i32* @EBUSY
%148 = icmp eq i32 %146, %147
br i1 %148, label %149, label %155
149: ; preds = %145
%150 = load %struct.TYPE_9__*, %struct.TYPE_9__** %15
%151 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %150, i32 0, i32 0
%152 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %151, i32 0, i32 0
%153 = load i32, i32* %152
%154 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i32 %153)
br label %162
155: ; preds = %145
%156 = load i32, i32* %8
%157 = icmp eq i32 %156, 0
br i1 %157, label %158, label %161
158: ; preds = %155
%159 = load %struct.TYPE_9__*, %struct.TYPE_9__** %15
%160 = call i32 @pthread_mutex_unlock(%struct.TYPE_9__* %159)
br label %161
161: ; preds = %158, %155
br label %162
162: ; preds = %161, %149
br label %163
163: ; preds = %162, %144
%164 = load i32, i32* @EXIT_SUCCESS
%165 = call i32 @exit(i32 %164)
unreachable
166: ; preds = %120
%167 = load i32, i32* %10
%168 = icmp eq i32 %167, 0
br i1 %168, label %169, label %222
169: ; preds = %166
%170 = call i32 @pthread_mutexattr_init(i32* %16)
store i32 %170, i32* %8
%171 = load i32, i32* %8
%172 = icmp ne i32 %171, 0
br i1 %172, label %173, label %178
173: ; preds = %169
%174 = load i32, i32* @stderr
%175 = call i32 (i32, i8*, ...) @fprintf(i32 %174, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.9, i64 0, i64 0))
%176 = load i32, i32* @EXIT_FAILURE
%177 = call i32 @exit(i32 %176)
unreachable
178: ; preds = %169
%179 = load i32, i32* @PTHREAD_MUTEX_ERRORCHECK
%180 = call i32 @pthread_mutexattr_settype(i32* %16, i32 %179)
store i32 %180, i32* %8
%181 = load i32, i32* %8
%182 = icmp ne i32 %181, 0
br i1 %182, label %183, label %188
183: ; preds = %178
%184 = load i32, i32* @stderr
%185 = call i32 (i32, i8*, ...) @fprintf(i32 %184, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.10, i64 0, i64 0))
%186 = load i32, i32* @EXIT_FAILURE
%187 = call i32 @exit(i32 %186)
unreachable
188: ; preds = %178
%189 = load i32, i32* @PTHREAD_PROCESS_SHARED
%190 = call i32 @pthread_mutexattr_setpshared(i32* %16, i32 %189)
store i32 %190, i32* %8
%191 = load i32, i32* %8
%192 = icmp ne i32 %191, 0
br i1 %192, label %193, label %198
193: ; preds = %188
%194 = load i32, i32* @stderr
%195 = call i32 (i32, i8*, ...) @fprintf(i32 %194, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.11, i64 0, i64 0))
%196 = load i32, i32* @EXIT_FAILURE
%197 = call i32 @exit(i32 %196)
unreachable
198: ; preds = %188
%199 = load i32, i32* @PTHREAD_MUTEX_ROBUST
%200 = call i32 @pthread_mutexattr_setrobust(i32* %16, i32 %199)
store i32 %200, i32* %8
%201 = load i32, i32* %8
%202 = icmp ne i32 %201, 0
br i1 %202, label %203, label %208
203: ; preds = %198
%204 = load i32, i32* @stderr
%205 = call i32 (i32, i8*, ...) @fprintf(i32 %204, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.12, i64 0, i64 0))
%206 = load i32, i32* @EXIT_FAILURE
%207 = call i32 @exit(i32 %206)
unreachable
208: ; preds = %198
%209 = load %struct.TYPE_9__*, %struct.TYPE_9__** %15
%210 = call i32 @pthread_mutex_init(%struct.TYPE_9__* %209, i32* %16)
store i32 %210, i32* %8
%211 = load i32, i32* %8
%212 = icmp ne i32 %211, 0
br i1 %212, label %213, label %218
213: ; preds = %208
%214 = load i32, i32* @stderr
%215 = call i32 (i32, i8*, ...) @fprintf(i32 %214, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.13, i64 0, i64 0))
%216 = load i32, i32* @EXIT_FAILURE
%217 = call i32 @exit(i32 %216)
unreachable
218: ; preds = %208
%219 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.14, i64 0, i64 0))
br label %220
220: ; preds = %220, %218
%221 = call i32 @sleep(i32 1)
br label %220
222: ; preds = %166
%223 = load i32, i32* @stderr
%224 = call i32 (...) @getpid()
%225 = call i32 (i32, i8*, ...) @fprintf(i32 %223, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.15, i64 0, i64 0), i32 %224)
br label %226
226: ; preds = %250, %222
%227 = load %struct.TYPE_9__*, %struct.TYPE_9__** %15
%228 = call i32 @pthread_mutex_lock(%struct.TYPE_9__* %227)
store i32 %228, i32* %8
%229 = load i32, i32* %8
%230 = load i32, i32* @EOWNERDEAD
%231 = icmp eq i32 %229, %230
br i1 %231, label %232, label %249
232: ; preds = %226
%233 = load %struct.TYPE_9__*, %struct.TYPE_9__** %15
%234 = call i32 @pthread_mutex_consistent(%struct.TYPE_9__* %233)
store i32 %234, i32* %18
%235 = load i32, i32* %18
%236 = icmp eq i32 %235, 0
br i1 %236, label %237, label %240
237: ; preds = %232
%238 = load %struct.TYPE_9__*, %struct.TYPE_9__** %15
%239 = call i32 @pthread_mutex_unlock(%struct.TYPE_9__* %238)
br label %245
240: ; preds = %232
%241 = load i32, i32* @stderr
%242 = call i32 (i32, i8*, ...) @fprintf(i32 %241, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%243 = load i32, i32* @EXIT_FAILURE
%244 = call i32 @exit(i32 %243)
unreachable
245: ; preds = %237
%246 = load i32, i32* @stderr
%247 = load i32, i32* %8
%248 = call i32 (i32, i8*, ...) @fprintf(i32 %246, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.16, i64 0, i64 0), i32 %247)
br label %249
249: ; preds = %245, %226
br label %250
250: ; preds = %249
%251 = load i32, i32* %8
%252 = icmp ne i32 %251, 0
br i1 %252, label %226, label %253
253: ; preds = %250
%254 = call i32 @setpgid(i32 0, i32 0)
%255 = load i32, i32* @stderr
%256 = call i32 (i32, i8*, ...) @fprintf(i32 %255, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.17, i64 0, i64 0))
%257 = load i8**, i8*** %5
%258 = getelementptr inbounds i8*, i8** %257, i64 3
%259 = load i8*, i8** %258
%260 = call i32 @atoi(i8* %259)
store i32 %260, i32* %9
store i32 0, i32* %6
br label %261
261: ; preds = %285, %253
%262 = load i32, i32* %6
%263 = load i32, i32* %9
%264 = icmp slt i32 %262, %263
br i1 %264, label %265, label %288
265: ; preds = %261
%266 = call i64 (...) @fork()
store i64 %266, i64* %17
%267 = load i64, i64* %17
%268 = icmp slt i64 %267, 0
br i1 %268, label %269, label %274
269: ; preds = %265
%270 = load i32, i32* @stderr
%271 = call i32 (i32, i8*, ...) @fprintf(i32 %270, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.18, i64 0, i64 0))
%272 = load i32, i32* @EXIT_FAILURE
%273 = call i32 @exit(i32 %272)
unreachable
274: ; preds = %265
%275 = load i64, i64* %17
%276 = icmp eq i64 %275, 0
br i1 %276, label %277, label %284
277: ; preds = %274
%278 = load i32, i32* %7
%279 = call i32 @close(i32 %278)
%280 = load i8*, i8** %13
%281 = call i32 @worker(i8* %280)
%282 = load i32, i32* @EXIT_FAILURE
%283 = call i32 @exit(i32 %282)
unreachable
284: ; preds = %274
br label %285
285: ; preds = %284
%286 = load i32, i32* %6
%287 = add nsw i32 %286, 1
store i32 %287, i32* %6
br label %261
288: ; preds = %261
%289 = load i32, i32* @stderr
%290 = call i32 (i32, i8*, ...) @fprintf(i32 %289, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.19, i64 0, i64 0))
%291 = load %struct.TYPE_9__*, %struct.TYPE_9__** %15
%292 = call i32 @pthread_mutex_unlock(%struct.TYPE_9__* %291)
store i32 %292, i32* %8
%293 = load i32, i32* %8
%294 = icmp ne i32 %293, 0
br i1 %294, label %295, label %300
295: ; preds = %288
%296 = load i32, i32* @stderr
%297 = call i32 (i32, i8*, ...) @fprintf(i32 %296, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.20, i64 0, i64 0))
%298 = load i32, i32* @EXIT_FAILURE
%299 = call i32 @exit(i32 %298)
unreachable
300: ; preds = %288
store i32 0, i32* %6
br label %301
301: ; preds = %319, %300
%302 = load i32, i32* %6
%303 = load i32, i32* %9
%304 = icmp slt i32 %302, %303
br i1 %304, label %305, label %322
305: ; preds = %301
%306 = call i64 @waitpid(i32 -1, i32* %19, i32 0)
store i64 %306, i64* %17
%307 = load i64, i64* %17
%308 = icmp sle i64 %307, 0
br i1 %308, label %309, label %314
309: ; preds = %305
%310 = load i32, i32* @stderr
%311 = call i32 (i32, i8*, ...) @fprintf(i32 %310, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.21, i64 0, i64 0))
%312 = load i32, i32* @EXIT_FAILURE
%313 = call i32 @exit(i32 %312)
unreachable
314: ; preds = %305
%315 = load i32, i32* @stderr
%316 = load i64, i64* %17
%317 = trunc i64 %316 to i32
%318 = call i32 (i32, i8*, ...) @fprintf(i32 %315, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.22, i64 0, i64 0), i32 %317)
br label %319
319: ; preds = %314
%320 = load i32, i32* %6
%321 = add nsw i32 %320, 1
store i32 %321, i32* %6
br label %301
322: ; preds = %301
%323 = load i32, i32* %7
%324 = call i32 @close(i32 %323)
%325 = load i32, i32* @EXIT_SUCCESS
%326 = call i32 @exit(i32 %325)
unreachable
}

LLVM IR (-O2):
%struct.TYPE_9__ = type { %struct.TYPE_8__ }
%struct.TYPE_8__ = type { i32 }
@.str = [5 x i8] c"init\00"
@.str.1 = [7 x i8] c"worker\00"
@.str.2 = [6 x i8] c"debug\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.3 = [11 x i8] c"FAIL: open\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@.str.4 = [12 x i8] c"FAIL: lseek\00"
@.str.5 = [16 x i8] c"FAIL: ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FILE = global i32 0
@.str.6 = [11 x i8] c"FAIL: mmap\00"
@EOWNERDEAD = global i32 0
@stderr = global i32 0
@.str.7 = [39 x i8] c"FAIL: pthread_mutex_consistent failed\0A\00"
@EBUSY = global i32 0
@.str.8 = [14 x i8] c"INFO: pid=%u\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.9 = [37 x i8] c"FAIL: pthread_mutexattr_init failed\0A\00"
@PTHREAD_MUTEX_ERRORCHECK = global i32 0
@.str.10 = [40 x i8] c"FAIL: pthread_mutexattr_settype failed\0A\00"
@PTHREAD_PROCESS_SHARED = global i32 0
@.str.11 = [43 x i8] c"FAIL: pthread_mutexattr_setpshared failed\0A\00"
@PTHREAD_MUTEX_ROBUST = global i32 0
@.str.12 = [42 x i8] c"FAIL: pthread_mutexattr_setrobust failed\0A\00"
@.str.13 = [33 x i8] c"FAIL: pthread_mutex_init failed\0A\00"
@.str.15 = [43 x i8] c"INFO: parent: Acquiring mutex (pid = %d).\0A\00"
@.str.16 = [40 x i8] c"INFO: parent: Unlock recovery ret = %d\0A\00"
@.str.17 = [33 x i8] c"INFO: parent: Creating children\0A\00"
@.str.18 = [21 x i8] c"FAIL: fork() failed\0A\00"
@.str.19 = [36 x i8] c"INFO: parent: Waiting for children\0A\00"
@.str.20 = [35 x i8] c"FAIL: pthread_mutex_unlock failed\0A\00"
@.str.21 = [24 x i8] c"FAIL: waitpid() failed\0A\00"
@.str.22 = [25 x i8] c"INFO: parent: Reaped %u\0A\00"
@str = [43 x i8] c"INFO: init: Mutex initialization complete.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = add i32 %0, -6
%7 = icmp ult i32 %6, -3
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @usage(i8* %9)
br label %11
11: ; preds = %2, %8
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i64 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %32, label %18
18: ; preds = %11
%19 = tail call i64 @strcmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%20 = icmp eq i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %18
%22 = tail call i64 @strcmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%23 = icmp eq i64 %22, 0
br i1 %23, label %32, label %24
24: ; preds = %21
%25 = load i8*, i8** %1
%26 = tail call i32 @usage(i8* %25)
br label %32
27: ; preds = %18
%28 = getelementptr inbounds i8*, i8** %1, i64 4
%29 = load i8*, i8** %28
%30 = tail call i32 @atoi(i8* %29)
%31 = tail call i32 @set_priority(i32 %30)
br label %32
32: ; preds = %21, %11, %24, %27
%33 = phi i1 [ false, %27 ], [ true, %21 ], [ false, %11 ], [ false, %24 ]
%34 = phi i1 [ false, %27 ], [ false, %21 ], [ true, %11 ], [ false, %24 ]
%35 = load i8*, i8** %12
%36 = load i32, i32* @O_CREAT
%37 = load i32, i32* @O_RDWR
%38 = or i32 %37, %36
%39 = tail call i32 @open(i8* %35, i32 %38, i32 384)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %32
%42 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = tail call i32 @exit(i32 %43)
unreachable
45: ; preds = %32
%46 = load i32, i32* @SEEK_SET
%47 = tail call i32 @lseek(i32 %39, i32 0, i32 %46)
%48 = icmp eq i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %45
%50 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = tail call i32 @exit(i32 %51)
unreachable
53: ; preds = %45
br i1 %34, label %54, label %61
54: ; preds = %53
%55 = tail call i32 @ftruncate(i32 %39, i32 4)
%56 = icmp eq i32 %55, 0
br i1 %56, label %61, label %57
57: ; preds = %54
%58 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%59 = load i32, i32* @EXIT_FAILURE
%60 = tail call i32 @exit(i32 %59)
unreachable
61: ; preds = %54, %53
%62 = load i32, i32* @PROT_READ
%63 = load i32, i32* @PROT_WRITE
%64 = or i32 %63, %62
%65 = load i32, i32* @MAP_SHARED
%66 = load i32, i32* @MAP_FILE
%67 = or i32 %66, %65
%68 = tail call i8* @mmap(i32* null, i32 4, i32 %64, i32 %67, i32 %39, i32 0)
%69 = icmp eq i8* %68, null
br i1 %69, label %70, label %74
70: ; preds = %61
%71 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%72 = load i32, i32* @EXIT_FAILURE
%73 = tail call i32 @exit(i32 %72)
unreachable
74: ; preds = %61
%75 = bitcast i8* %68 to %struct.TYPE_9__*
br i1 %33, label %76, label %104
76: ; preds = %74
%77 = tail call i32 @pthread_mutex_trylock(%struct.TYPE_9__* %75)
%78 = load i32, i32* @EOWNERDEAD
%79 = icmp eq i32 %77, %78
br i1 %79, label %80, label %90
80: ; preds = %76
%81 = tail call i32 @pthread_mutex_consistent(%struct.TYPE_9__* %75)
%82 = icmp eq i32 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %80
%84 = tail call i32 @pthread_mutex_unlock(%struct.TYPE_9__* %75)
br label %101
85: ; preds = %80
%86 = load i32, i32* @stderr
%87 = tail call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%88 = load i32, i32* @EXIT_FAILURE
%89 = tail call i32 @exit(i32 %88)
unreachable
90: ; preds = %76
%91 = load i32, i32* @EBUSY
%92 = icmp eq i32 %77, %91
br i1 %92, label %93, label %97
93: ; preds = %90
%94 = bitcast i8* %68 to i32*
%95 = load i32, i32* %94
%96 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i32 %95)
br label %101
97: ; preds = %90
%98 = icmp eq i32 %77, 0
br i1 %98, label %99, label %101
99: ; preds = %97
%100 = tail call i32 @pthread_mutex_unlock(%struct.TYPE_9__* %75)
br label %101
101: ; preds = %93, %99, %97, %83
%102 = load i32, i32* @EXIT_SUCCESS
%103 = tail call i32 @exit(i32 %102)
unreachable
104: ; preds = %74
br i1 %34, label %105, label %152
105: ; preds = %104
%106 = call i32 @pthread_mutexattr_init(i32* %3)
%107 = icmp eq i32 %106, 0
br i1 %107, label %113, label %108
108: ; preds = %105
%109 = load i32, i32* @stderr
%110 = call i32 (i32, i8*, ...) @fprintf(i32 %109, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.9, i64 0, i64 0))
%111 = load i32, i32* @EXIT_FAILURE
%112 = call i32 @exit(i32 %111)
unreachable
113: ; preds = %105
%114 = load i32, i32* @PTHREAD_MUTEX_ERRORCHECK
%115 = call i32 @pthread_mutexattr_settype(i32* %3, i32 %114)
%116 = icmp eq i32 %115, 0
br i1 %116, label %122, label %117
117: ; preds = %113
%118 = load i32, i32* @stderr
%119 = call i32 (i32, i8*, ...) @fprintf(i32 %118, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.10, i64 0, i64 0))
%120 = load i32, i32* @EXIT_FAILURE
%121 = call i32 @exit(i32 %120)
unreachable
122: ; preds = %113
%123 = load i32, i32* @PTHREAD_PROCESS_SHARED
%124 = call i32 @pthread_mutexattr_setpshared(i32* %3, i32 %123)
%125 = icmp eq i32 %124, 0
br i1 %125, label %131, label %126
126: ; preds = %122
%127 = load i32, i32* @stderr
%128 = call i32 (i32, i8*, ...) @fprintf(i32 %127, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.11, i64 0, i64 0))
%129 = load i32, i32* @EXIT_FAILURE
%130 = call i32 @exit(i32 %129)
unreachable
131: ; preds = %122
%132 = load i32, i32* @PTHREAD_MUTEX_ROBUST
%133 = call i32 @pthread_mutexattr_setrobust(i32* %3, i32 %132)
%134 = icmp eq i32 %133, 0
br i1 %134, label %140, label %135
135: ; preds = %131
%136 = load i32, i32* @stderr
%137 = call i32 (i32, i8*, ...) @fprintf(i32 %136, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.12, i64 0, i64 0))
%138 = load i32, i32* @EXIT_FAILURE
%139 = call i32 @exit(i32 %138)
unreachable
140: ; preds = %131
%141 = call i32 @pthread_mutex_init(%struct.TYPE_9__* %75, i32* %3)
%142 = icmp eq i32 %141, 0
br i1 %142, label %148, label %143
143: ; preds = %140
%144 = load i32, i32* @stderr
%145 = call i32 (i32, i8*, ...) @fprintf(i32 %144, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.13, i64 0, i64 0))
%146 = load i32, i32* @EXIT_FAILURE
%147 = call i32 @exit(i32 %146)
unreachable
148: ; preds = %140
%149 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
br label %150
150: ; preds = %150, %148
%151 = call i32 @sleep(i32 1)
br label %150
152: ; preds = %104
%153 = load i32, i32* @stderr
%154 = tail call i32 (...) @getpid()
%155 = tail call i32 (i32, i8*, ...) @fprintf(i32 %153, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.15, i64 0, i64 0), i32 %154)
br label %156
156: ; preds = %172, %152
%157 = tail call i32 @pthread_mutex_lock(%struct.TYPE_9__* %75)
%158 = load i32, i32* @EOWNERDEAD
%159 = icmp eq i32 %157, %158
br i1 %159, label %160, label %172
160: ; preds = %156
%161 = tail call i32 @pthread_mutex_consistent(%struct.TYPE_9__* %75)
%162 = icmp eq i32 %161, 0
br i1 %162, label %163, label %167
163: ; preds = %160
%164 = tail call i32 @pthread_mutex_unlock(%struct.TYPE_9__* %75)
%165 = load i32, i32* @stderr
%166 = tail call i32 (i32, i8*, ...) @fprintf(i32 %165, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.16, i64 0, i64 0), i32 %157)
br label %172
167: ; preds = %160
%168 = load i32, i32* @stderr
%169 = tail call i32 (i32, i8*, ...) @fprintf(i32 %168, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%170 = load i32, i32* @EXIT_FAILURE
%171 = tail call i32 @exit(i32 %170)
unreachable
172: ; preds = %156, %163
%173 = icmp eq i32 %157, 0
br i1 %173, label %174, label %156
174: ; preds = %172
%175 = tail call i32 @setpgid(i32 0, i32 0)
%176 = load i32, i32* @stderr
%177 = tail call i32 (i32, i8*, ...) @fprintf(i32 %176, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.17, i64 0, i64 0))
%178 = getelementptr inbounds i8*, i8** %1, i64 3
%179 = load i8*, i8** %178
%180 = tail call i32 @atoi(i8* %179)
%181 = icmp sgt i32 %180, 0
br i1 %181, label %184, label %201
182: ; preds = %193
%183 = icmp eq i32 %195, %180
br i1 %183, label %201, label %184
184: ; preds = %174, %182
%185 = phi i32 [ %195, %182 ], [ 0, %174 ]
%186 = tail call i64 (...) @fork()
%187 = icmp slt i64 %186, 0
br i1 %187, label %188, label %193
188: ; preds = %184
%189 = load i32, i32* @stderr
%190 = tail call i32 (i32, i8*, ...) @fprintf(i32 %189, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.18, i64 0, i64 0))
%191 = load i32, i32* @EXIT_FAILURE
%192 = tail call i32 @exit(i32 %191)
unreachable
193: ; preds = %184
%194 = icmp eq i64 %186, 0
%195 = add nuw nsw i32 %185, 1
br i1 %194, label %196, label %182
196: ; preds = %193
%197 = tail call i32 @close(i32 %39)
%198 = tail call i32 @worker(i8* %13)
%199 = load i32, i32* @EXIT_FAILURE
%200 = tail call i32 @exit(i32 %199)
unreachable
201: ; preds = %182, %174
%202 = load i32, i32* @stderr
%203 = tail call i32 (i32, i8*, ...) @fprintf(i32 %202, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.19, i64 0, i64 0))
%204 = tail call i32 @pthread_mutex_unlock(%struct.TYPE_9__* %75)
%205 = icmp eq i32 %204, 0
br i1 %205, label %206, label %209
206: ; preds = %201
br i1 %181, label %207, label %228
207: ; preds = %206
%208 = bitcast i32* %4 to i8*
br label %214
209: ; preds = %201
%210 = load i32, i32* @stderr
%211 = tail call i32 (i32, i8*, ...) @fprintf(i32 %210, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.20, i64 0, i64 0))
%212 = load i32, i32* @EXIT_FAILURE
%213 = tail call i32 @exit(i32 %212)
unreachable
214: ; preds = %207, %223
%215 = phi i32 [ 0, %207 ], [ %226, %223 ]
%216 = call i64 @waitpid(i32 -1, i32* %4, i32 0)
%217 = icmp slt i64 %216, 1
%218 = load i32, i32* @stderr
br i1 %217, label %219, label %223
219: ; preds = %214
%220 = call i32 (i32, i8*, ...) @fprintf(i32 %218, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.21, i64 0, i64 0))
%221 = load i32, i32* @EXIT_FAILURE
%222 = call i32 @exit(i32 %221)
unreachable
223: ; preds = %214
%224 = trunc i64 %216 to i32
%225 = call i32 (i32, i8*, ...) @fprintf(i32 %218, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.22, i64 0, i64 0), i32 %224)
%226 = add nuw nsw i32 %215, 1
%227 = icmp eq i32 %226, %180
br i1 %227, label %228, label %214
228: ; preds = %223, %206
%229 = call i32 @close(i32 %39)
%230 = load i32, i32* @EXIT_SUCCESS
%231 = call i32 @exit(i32 %230)
unreachable
}