#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int LOCK_EX;
int LOCK_NB;
int LOCK_SH;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
scalar_t__ close(int);
int /*<<< orphan*/ diod_log_init(char *);
int /*<<< orphan*/ err(char *);
int /*<<< orphan*/ err_exit(char *, ...);
int /*<<< orphan*/ exit(int);
scalar_t__ flock(int, int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ msg(char *);
int /*<<< orphan*/ msg_exit(char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd = -1;
  int fd2 = -1;
  pid_t pid;
  int status;
  diod_log_init(argv[0]);
  if (argc != 2) {
    msg("Usage: tflock file");
    exit(1);
  }
  msg("1. Conflicting write locks cannot be held by two processes");
  if ((fd = open(argv[1], O_RDWR)) < 0)
    err_exit("open %s", argv[1]);
  msg("fd: open");
  if (flock(fd, LOCK_EX | LOCK_NB) < 0)
    err_exit("fd: write-lock failed, aborting");
  msg("fd: write-locked");
  switch (pid = fork()) {
  case -1:
    err_exit("fork");
  case 0:
    msg("child forked");
    if ((fd2 = open(argv[1], O_RDWR)) < 0)
      err_exit("fd2 open %s", argv[1]);
    msg("fd2: open (child)");
    if (flock(fd2, LOCK_EX | LOCK_NB) < 0) {
      err("fd2: write-lock failed");
      exit(0);
    }
    msg_exit("fd2: write-locked");
  default:
    if (waitpid(pid, &status, 0) < 0)
      err_exit("waitpid");
    if (!WIFEXITED(status))
      msg_exit("child terminated without exit");
    if (WEXITSTATUS(status) != 0)
      msg_exit("child exited with %d, aborting", WEXITSTATUS(status));
    msg("child exited normally");
    break;
  }
  if (close(fd) < 0)
    err_exit("close fd");
  msg("fd: closed");
  msg("2. Conflicting read and write locks cannot be held by two processes");
  if ((fd = open(argv[1], O_RDWR)) < 0)
    err_exit("open %s", argv[1]);
  msg("fd: open");
  if (flock(fd, LOCK_EX | LOCK_NB) < 0)
    err_exit("fd: write-lock failed, aborting");
  msg("fd: write-locked");
  switch (pid = fork()) {
  case -1:
    err_exit("fork");
  case 0:
    msg("child forked");
    if ((fd2 = open(argv[1], O_RDWR)) < 0)
      err_exit("fd2 open %s", argv[1]);
    msg("fd2: open (child)");
    if (flock(fd2, LOCK_SH | LOCK_NB) < 0) {
      err("fd2: read-lock failed");
      exit(0);
    }
    msg_exit("fd2: read-locked");
  default:
    if (waitpid(pid, &status, 0) < 0)
      err_exit("waitpid");
    if (!WIFEXITED(status))
      msg_exit("child terminated without exit");
    if (WEXITSTATUS(status) != 0)
      msg_exit("child exited with %d, aborting", WEXITSTATUS(status));
    msg("child exited normally");
    break;
  }
  if (close(fd) < 0)
    err_exit("close fd");
  msg("fd: closed");
  msg("3. Conflicting read locks CAN be held by two processes");
  if ((fd = open(argv[1], O_RDWR)) < 0)
    err_exit("open %s", argv[1]);
  msg("fd: open");
  if (flock(fd, LOCK_SH | LOCK_NB) < 0)
    err_exit("fd: read-lock failed, aborting");
  msg("fd: read-locked");
  switch (pid = fork()) {
  case -1:
    err_exit("fork");
  case 0:
    msg("child forked");
    if ((fd2 = open(argv[1], O_RDWR)) < 0)
      err_exit("fd2 open %s", argv[1]);
    msg("fd2: open (child)");
    if (flock(fd, LOCK_SH | LOCK_NB) < 0)
      err_exit("fd2: read-lock failed");
    msg("fd2: read-locked");
    exit(0);
  default:
    if (waitpid(pid, &status, 0) < 0)
      err_exit("waitpid");
    if (!WIFEXITED(status))
      msg_exit("child terminated without exit");
    if (WEXITSTATUS(status) != 0)
      msg_exit("child exited with %d, aborting", WEXITSTATUS(status));
    msg("child exited normally");
    break;
  }
  if (close(fd) < 0)
    err_exit("close fd");
  msg("fd: closed");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <diod_log_init@plt>
cmpl   $0x2,-0x8(%rbp)
je     1220 <main+0x50>
lea    0xdef(%rip),%rdi
call   10f0 <msg@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdec(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1264 <main+0x94>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0xde3(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x14(%rbp),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    0x2d3d(%rip),%rax
or     (%rax),%esi
call   10a0 <flock@plt>
cmp    $0x0,%rax
jge    12a2 <main+0xd2>
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0xdce(%rip),%rdi
call   10f0 <msg@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,-0x1c(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
sub    $0xffffffff,%eax
je     12db <main+0x10b>
jmp    12cb <main+0xfb>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     12e9 <main+0x119>
jmp    137e <main+0x1ae>
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0xd9d(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2ce4(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    132d <main+0x15d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0xd72(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x18(%rbp),%edi
mov    0x2c9d(%rip),%rax
mov    (%rax),%esi
mov    0x2c74(%rip),%rax
or     (%rax),%esi
call   10a0 <flock@plt>
cmp    $0x0,%rax
jge    1370 <main+0x1a0>
lea    0xd54(%rip),%rdi
call   1050 <err@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0x0,%eax
jge    13a3 <main+0x1d3>
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
mov    -0x20(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    13c2 <main+0x1f2>
lea    0xd2e(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_exit@plt>
mov    -0x20(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     13eb <main+0x21b>
mov    -0x20(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd23(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_exit@plt>
lea    0xd34(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    1417 <main+0x247>
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0xd27(%rip),%rdi
call   10f0 <msg@plt>
lea    0xd26(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2baa(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1467 <main+0x297>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xc3e(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0xbe0(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x14(%rbp),%edi
mov    0x2b63(%rip),%rax
mov    (%rax),%esi
mov    0x2b3a(%rip),%rax
or     (%rax),%esi
call   10a0 <flock@plt>
cmp    $0x0,%rax
jge    14a5 <main+0x2d5>
lea    0xbb9(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0xbcb(%rip),%rdi
call   10f0 <msg@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,-0x1c(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0xffffffff,%eax
je     14de <main+0x30e>
jmp    14ce <main+0x2fe>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     14ec <main+0x31c>
jmp    1581 <main+0x3b1>
lea    0xba3(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0xb9a(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2ae1(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1530 <main+0x360>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xb71(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0xb6f(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x18(%rbp),%edi
mov    0x2a92(%rip),%rax
mov    (%rax),%esi
mov    0x2a71(%rip),%rax
or     (%rax),%esi
call   10a0 <flock@plt>
cmp    $0x0,%rax
jge    1573 <main+0x3a3>
lea    0xc2d(%rip),%rdi
call   1050 <err@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xc30(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0x0,%eax
jge    15a6 <main+0x3d6>
lea    0xb42(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
mov    -0x20(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    15c5 <main+0x3f5>
lea    0xb2b(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_exit@plt>
mov    -0x20(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     15ee <main+0x41e>
mov    -0x20(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xb20(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_exit@plt>
lea    0xb31(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    161a <main+0x44a>
lea    0xb29(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0xb24(%rip),%rdi
call   10f0 <msg@plt>
lea    0xb8e(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x29a7(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    166a <main+0x49a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xa3b(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0x9dd(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x14(%rbp),%edi
mov    0x2958(%rip),%rax
mov    (%rax),%esi
mov    0x2937(%rip),%rax
or     (%rax),%esi
call   10a0 <flock@plt>
cmp    $0x0,%rax
jge    16a8 <main+0x4d8>
lea    0xb51(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0xb62(%rip),%rdi
call   10f0 <msg@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,-0x1c(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     16e1 <main+0x511>
jmp    16d1 <main+0x501>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     16ef <main+0x51f>
jmp    1784 <main+0x5b4>
lea    0x9a0(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0x997(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x28de(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1733 <main+0x563>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0x96e(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0x96c(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x14(%rbp),%edi
mov    0x288f(%rip),%rax
mov    (%rax),%esi
mov    0x286e(%rip),%rax
or     (%rax),%esi
call   10a0 <flock@plt>
cmp    $0x0,%rax
jge    1771 <main+0x5a1>
lea    0xa2a(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0xa32(%rip),%rdi
call   10f0 <msg@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0x0,%eax
jge    17a9 <main+0x5d9>
lea    0x93f(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
mov    -0x20(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    17c8 <main+0x5f8>
lea    0x928(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_exit@plt>
mov    -0x20(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     17f1 <main+0x621>
mov    -0x20(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0x91d(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_exit@plt>
lea    0x92e(%rip),%rdi
call   10f0 <msg@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    181d <main+0x64d>
lea    0x926(%rip),%rdi
mov    $0x0,%al
call   1060 <err_exit@plt>
lea    0x921(%rip),%rdi
call   10f0 <msg@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebx
mov    (%rsi),%rdi
call   10d0 <diod_log_init@plt>
cmp    $0x2,%ebx
jne    12f7 <main+0x127>
lea    0xe1b(%rip),%rdi
call   10f0 <msg@plt>
mov    0x8(%r15),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    1228 <main+0x58>
mov    0x8(%r15),%rsi
lea    0xe7d(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xe1f(%rip),%rdi
call   10f0 <msg@plt>
mov    0x2da5(%rip),%r12
mov    0x2d7e(%rip),%r13
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %r14d,%edi
call   10a0 <flock@plt>
test   %rax,%rax
jns    1265 <main+0x95>
lea    0xdf9(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xe0b(%rip),%rdi
call   10f0 <msg@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     1295 <main+0xc5>
cmp    $0xffffffff,%ebx
jne    131b <main+0x14b>
lea    0xdfa(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xdf1(%rip),%rdi
call   10f0 <msg@plt>
mov    0x8(%r15),%rdi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12cb <main+0xfb>
mov    0x8(%r15),%rsi
lea    0xdd6(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xdd4(%rip),%rdi
call   10f0 <msg@plt>
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %ebp,%edi
call   10a0 <flock@plt>
test   %rax,%rax
jns    130d <main+0x13d>
lea    0xdc6(%rip),%rdi
jmp    14a7 <main+0x2d7>
lea    0xd02(%rip),%rdi
call   10f0 <msg@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdbb(%rip),%rdi
xor    %eax,%eax
call   1040 <msg_exit@plt>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
test   %eax,%eax
jns    133b <main+0x16b>
lea    0xdad(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
mov    0x4(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %eax,%eax
jne    1356 <main+0x186>
lea    0xd9a(%rip),%rdi
xor    %eax,%eax
call   1040 <msg_exit@plt>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
je     137c <main+0x1ac>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xd94(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <msg_exit@plt>
lea    0xda3(%rip),%rdi
call   10f0 <msg@plt>
mov    %r14d,%edi
call   1030 <close@plt>
test   %rax,%rax
jns    13a3 <main+0x1d3>
lea    0xda0(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xd9b(%rip),%rdi
call   10f0 <msg@plt>
lea    0xd9a(%rip),%rdi
call   10f0 <msg@plt>
mov    0x8(%r15),%rdi
mov    0x2c22(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    13e6 <main+0x216>
mov    0x8(%r15),%rsi
lea    0xcbf(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xc61(%rip),%rdi
call   10f0 <msg@plt>
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %r14d,%edi
call   10a0 <flock@plt>
test   %rax,%rax
jns    1415 <main+0x245>
lea    0xc49(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xc5b(%rip),%rdi
call   10f0 <msg@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1445 <main+0x275>
cmp    $0xffffffff,%ebp
jne    14c1 <main+0x2f1>
lea    0xc4a(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xc41(%rip),%rdi
call   10f0 <msg@plt>
mov    0x8(%r15),%rdi
mov    0x2b8c(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    147b <main+0x2ab>
mov    0x8(%r15),%rsi
lea    0xc26(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xc24(%rip),%rdi
call   10f0 <msg@plt>
mov    0x2b4a(%rip),%rax
mov    0x0(%r13),%esi
or     (%rax),%esi
mov    %ebx,%edi
call   10a0 <flock@plt>
test   %rax,%rax
jns    14b3 <main+0x2e3>
lea    0xced(%rip),%rdi
call   1050 <err@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xcf0(%rip),%rdi
xor    %eax,%eax
call   1040 <msg_exit@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
test   %eax,%eax
jns    14e1 <main+0x311>
lea    0xc07(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
mov    0x4(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %eax,%eax
jne    14fc <main+0x32c>
lea    0xbf4(%rip),%rdi
xor    %eax,%eax
call   1040 <msg_exit@plt>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
je     1522 <main+0x352>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xbee(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <msg_exit@plt>
lea    0xbfd(%rip),%rdi
call   10f0 <msg@plt>
mov    %r14d,%edi
call   1030 <close@plt>
test   %rax,%rax
jns    1549 <main+0x379>
lea    0xbfa(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xbf5(%rip),%rdi
call   10f0 <msg@plt>
lea    0xc5f(%rip),%rdi
call   10f0 <msg@plt>
mov    0x8(%r15),%rdi
mov    0x2a7c(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    158b <main+0x3bb>
mov    0x8(%r15),%rsi
lea    0xb1a(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xabc(%rip),%rdi
call   10f0 <msg@plt>
mov    0x2a3a(%rip),%rbx
mov    0x0(%r13),%esi
or     (%rbx),%esi
mov    %ebp,%edi
call   10a0 <flock@plt>
test   %rax,%rax
jns    15be <main+0x3ee>
lea    0xc3b(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xc4c(%rip),%rdi
call   10f0 <msg@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     15e8 <main+0x418>
cmp    $0xffffffff,%eax
jne    1654 <main+0x484>
lea    0xaa7(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xa9e(%rip),%rdi
call   10f0 <msg@plt>
mov    0x8(%r15),%rdi
mov    0x29e9(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
jns    161c <main+0x44c>
mov    0x8(%r15),%rsi
lea    0xa85(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xa83(%rip),%rdi
call   10f0 <msg@plt>
mov    0x0(%r13),%esi
or     (%rbx),%esi
mov    %ebp,%edi
call   10a0 <flock@plt>
test   %rax,%rax
jns    1648 <main+0x478>
lea    0xb53(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xb5b(%rip),%rdi
jmp    16e2 <main+0x512>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
test   %eax,%eax
jns    1674 <main+0x4a4>
lea    0xa74(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
mov    0x4(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %eax,%eax
jne    168f <main+0x4bf>
lea    0xa61(%rip),%rdi
xor    %eax,%eax
call   1040 <msg_exit@plt>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
je     16b5 <main+0x4e5>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xa5b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <msg_exit@plt>
lea    0xa6a(%rip),%rdi
call   10f0 <msg@plt>
mov    %ebp,%edi
call   1030 <close@plt>
test   %rax,%rax
jns    16db <main+0x50b>
lea    0xa68(%rip),%rdi
xor    %eax,%eax
call   1060 <err_exit@plt>
lea    0xa63(%rip),%rdi
call   10f0 <msg@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Usage: tflock file\00"
@.str.1 = [59 x i8] c"1. Conflicting write locks cannot be held by two processes\00"
@O_RDWR = global i32 0
@.str.2 = [8 x i8] c"open %s\00"
@.str.3 = [9 x i8] c"fd: open\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.4 = [32 x i8] c"fd: write-lock failed, aborting\00"
@.str.5 = [17 x i8] c"fd: write-locked\00"
@.str.6 = [5 x i8] c"fork\00"
@.str.7 = [13 x i8] c"child forked\00"
@.str.8 = [12 x i8] c"fd2 open %s\00"
@.str.9 = [18 x i8] c"fd2: open (child)\00"
@.str.10 = [23 x i8] c"fd2: write-lock failed\00"
@.str.11 = [18 x i8] c"fd2: write-locked\00"
@.str.12 = [8 x i8] c"waitpid\00"
@.str.13 = [30 x i8] c"child terminated without exit\00"
@.str.14 = [31 x i8] c"child exited with %d, aborting\00"
@.str.15 = [22 x i8] c"child exited normally\00"
@.str.16 = [9 x i8] c"close fd\00"
@.str.17 = [11 x i8] c"fd: closed\00"
@.str.18 = [68 x i8] c"2. Conflicting read and write locks cannot be held by two processes\00"
@LOCK_SH = global i32 0
@.str.19 = [22 x i8] c"fd2: read-lock failed\00"
@.str.20 = [17 x i8] c"fd2: read-locked\00"
@.str.21 = [55 x i8] c"3. Conflicting read locks CAN be held by two processes\00"
@.str.22 = [31 x i8] c"fd: read-lock failed, aborting\00"
@.str.23 = [16 x i8] c"fd: read-locked\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = call i32 @diod_log_init(i8* %12)
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 2
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i32 @msg(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0))
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %19
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %27, %19
%33 = call i32 @msg(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* %6
%35 = load i32, i32* @LOCK_EX
%36 = load i32, i32* @LOCK_NB
%37 = or i32 %35, %36
%38 = call i64 @flock(i32 %34, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %40, %32
%43 = call i32 @msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 (...) @fork()
store i32 %44, i32* %8
switch i32 %44, label %73 [
i32 -1, label %45
i32 0, label %47
]
45: ; preds = %42
%46 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %47
47: ; preds = %42, %45
%48 = call i32 @msg(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = load i32, i32* @O_RDWR
%53 = call i32 @open(i8* %51, i32 %52)
store i32 %53, i32* %7
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %47
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 1
%58 = load i8*, i8** %57
%59 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %55, %47
%61 = call i32 @msg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%62 = load i32, i32* %7
%63 = load i32, i32* @LOCK_EX
%64 = load i32, i32* @LOCK_NB
%65 = or i32 %63, %64
%66 = call i64 @flock(i32 %62, i32 %65)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %60
%69 = call i32 @err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0))
%70 = call i32 @exit(i32 0)
unreachable
71: ; preds = %60
%72 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0))
br label %73
73: ; preds = %42, %71
%74 = load i32, i32* %8
%75 = call i32 @waitpid(i32 %74, i32* %9, i32 0)
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %73
%78 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.12, i64 0, i64 0))
br label %79
79: ; preds = %77, %73
%80 = load i32, i32* %9
%81 = call i32 @WIFEXITED(i32 %80)
%82 = icmp ne i32 %81, 0
br i1 %82, label %85, label %83
83: ; preds = %79
%84 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.13, i64 0, i64 0))
br label %85
85: ; preds = %83, %79
%86 = load i32, i32* %9
%87 = call i32 @WEXITSTATUS(i32 %86)
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %93
89: ; preds = %85
%90 = load i32, i32* %9
%91 = call i32 @WEXITSTATUS(i32 %90)
%92 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.14, i64 0, i64 0), i32 %91)
br label %93
93: ; preds = %89, %85
%94 = call i32 @msg(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.15, i64 0, i64 0))
br label %95
95: ; preds = %93
%96 = load i32, i32* %6
%97 = call i64 @close(i32 %96)
%98 = icmp slt i64 %97, 0
br i1 %98, label %99, label %101
99: ; preds = %95
%100 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.16, i64 0, i64 0))
br label %101
101: ; preds = %99, %95
%102 = call i32 @msg(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.17, i64 0, i64 0))
%103 = call i32 @msg(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.18, i64 0, i64 0))
%104 = load i8**, i8*** %5
%105 = getelementptr inbounds i8*, i8** %104, i64 1
%106 = load i8*, i8** %105
%107 = load i32, i32* @O_RDWR
%108 = call i32 @open(i8* %106, i32 %107)
store i32 %108, i32* %6
%109 = icmp slt i32 %108, 0
br i1 %109, label %110, label %115
110: ; preds = %101
%111 = load i8**, i8*** %5
%112 = getelementptr inbounds i8*, i8** %111, i64 1
%113 = load i8*, i8** %112
%114 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %113)
br label %115
115: ; preds = %110, %101
%116 = call i32 @msg(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%117 = load i32, i32* %6
%118 = load i32, i32* @LOCK_EX
%119 = load i32, i32* @LOCK_NB
%120 = or i32 %118, %119
%121 = call i64 @flock(i32 %117, i32 %120)
%122 = icmp slt i64 %121, 0
br i1 %122, label %123, label %125
123: ; preds = %115
%124 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %125
125: ; preds = %123, %115
%126 = call i32 @msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%127 = call i32 (...) @fork()
store i32 %127, i32* %8
switch i32 %127, label %156 [
i32 -1, label %128
i32 0, label %130
]
128: ; preds = %125
%129 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %130
130: ; preds = %125, %128
%131 = call i32 @msg(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%132 = load i8**, i8*** %5
%133 = getelementptr inbounds i8*, i8** %132, i64 1
%134 = load i8*, i8** %133
%135 = load i32, i32* @O_RDWR
%136 = call i32 @open(i8* %134, i32 %135)
store i32 %136, i32* %7
%137 = icmp slt i32 %136, 0
br i1 %137, label %138, label %143
138: ; preds = %130
%139 = load i8**, i8*** %5
%140 = getelementptr inbounds i8*, i8** %139, i64 1
%141 = load i8*, i8** %140
%142 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %141)
br label %143
143: ; preds = %138, %130
%144 = call i32 @msg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%145 = load i32, i32* %7
%146 = load i32, i32* @LOCK_SH
%147 = load i32, i32* @LOCK_NB
%148 = or i32 %146, %147
%149 = call i64 @flock(i32 %145, i32 %148)
%150 = icmp slt i64 %149, 0
br i1 %150, label %151, label %154
151: ; preds = %143
%152 = call i32 @err(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.19, i64 0, i64 0))
%153 = call i32 @exit(i32 0)
unreachable
154: ; preds = %143
%155 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.20, i64 0, i64 0))
br label %156
156: ; preds = %125, %154
%157 = load i32, i32* %8
%158 = call i32 @waitpid(i32 %157, i32* %9, i32 0)
%159 = icmp slt i32 %158, 0
br i1 %159, label %160, label %162
160: ; preds = %156
%161 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.12, i64 0, i64 0))
br label %162
162: ; preds = %160, %156
%163 = load i32, i32* %9
%164 = call i32 @WIFEXITED(i32 %163)
%165 = icmp ne i32 %164, 0
br i1 %165, label %168, label %166
166: ; preds = %162
%167 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.13, i64 0, i64 0))
br label %168
168: ; preds = %166, %162
%169 = load i32, i32* %9
%170 = call i32 @WEXITSTATUS(i32 %169)
%171 = icmp ne i32 %170, 0
br i1 %171, label %172, label %176
172: ; preds = %168
%173 = load i32, i32* %9
%174 = call i32 @WEXITSTATUS(i32 %173)
%175 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.14, i64 0, i64 0), i32 %174)
br label %176
176: ; preds = %172, %168
%177 = call i32 @msg(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.15, i64 0, i64 0))
br label %178
178: ; preds = %176
%179 = load i32, i32* %6
%180 = call i64 @close(i32 %179)
%181 = icmp slt i64 %180, 0
br i1 %181, label %182, label %184
182: ; preds = %178
%183 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.16, i64 0, i64 0))
br label %184
184: ; preds = %182, %178
%185 = call i32 @msg(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.17, i64 0, i64 0))
%186 = call i32 @msg(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.21, i64 0, i64 0))
%187 = load i8**, i8*** %5
%188 = getelementptr inbounds i8*, i8** %187, i64 1
%189 = load i8*, i8** %188
%190 = load i32, i32* @O_RDWR
%191 = call i32 @open(i8* %189, i32 %190)
store i32 %191, i32* %6
%192 = icmp slt i32 %191, 0
br i1 %192, label %193, label %198
193: ; preds = %184
%194 = load i8**, i8*** %5
%195 = getelementptr inbounds i8*, i8** %194, i64 1
%196 = load i8*, i8** %195
%197 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %196)
br label %198
198: ; preds = %193, %184
%199 = call i32 @msg(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%200 = load i32, i32* %6
%201 = load i32, i32* @LOCK_SH
%202 = load i32, i32* @LOCK_NB
%203 = or i32 %201, %202
%204 = call i64 @flock(i32 %200, i32 %203)
%205 = icmp slt i64 %204, 0
br i1 %205, label %206, label %208
206: ; preds = %198
%207 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.22, i64 0, i64 0))
br label %208
208: ; preds = %206, %198
%209 = call i32 @msg(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.23, i64 0, i64 0))
%210 = call i32 (...) @fork()
store i32 %210, i32* %8
switch i32 %210, label %239 [
i32 -1, label %211
i32 0, label %213
]
211: ; preds = %208
%212 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %213
213: ; preds = %208, %211
%214 = call i32 @msg(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%215 = load i8**, i8*** %5
%216 = getelementptr inbounds i8*, i8** %215, i64 1
%217 = load i8*, i8** %216
%218 = load i32, i32* @O_RDWR
%219 = call i32 @open(i8* %217, i32 %218)
store i32 %219, i32* %7
%220 = icmp slt i32 %219, 0
br i1 %220, label %221, label %226
221: ; preds = %213
%222 = load i8**, i8*** %5
%223 = getelementptr inbounds i8*, i8** %222, i64 1
%224 = load i8*, i8** %223
%225 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %224)
br label %226
226: ; preds = %221, %213
%227 = call i32 @msg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%228 = load i32, i32* %6
%229 = load i32, i32* @LOCK_SH
%230 = load i32, i32* @LOCK_NB
%231 = or i32 %229, %230
%232 = call i64 @flock(i32 %228, i32 %231)
%233 = icmp slt i64 %232, 0
br i1 %233, label %234, label %236
234: ; preds = %226
%235 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.19, i64 0, i64 0))
br label %236
236: ; preds = %234, %226
%237 = call i32 @msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.20, i64 0, i64 0))
%238 = call i32 @exit(i32 0)
unreachable
239: ; preds = %208
%240 = load i32, i32* %8
%241 = call i32 @waitpid(i32 %240, i32* %9, i32 0)
%242 = icmp slt i32 %241, 0
br i1 %242, label %243, label %245
243: ; preds = %239
%244 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.12, i64 0, i64 0))
br label %245
245: ; preds = %243, %239
%246 = load i32, i32* %9
%247 = call i32 @WIFEXITED(i32 %246)
%248 = icmp ne i32 %247, 0
br i1 %248, label %251, label %249
249: ; preds = %245
%250 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.13, i64 0, i64 0))
br label %251
251: ; preds = %249, %245
%252 = load i32, i32* %9
%253 = call i32 @WEXITSTATUS(i32 %252)
%254 = icmp ne i32 %253, 0
br i1 %254, label %255, label %259
255: ; preds = %251
%256 = load i32, i32* %9
%257 = call i32 @WEXITSTATUS(i32 %256)
%258 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.14, i64 0, i64 0), i32 %257)
br label %259
259: ; preds = %255, %251
%260 = call i32 @msg(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.15, i64 0, i64 0))
br label %261
261: ; preds = %259
%262 = load i32, i32* %6
%263 = call i64 @close(i32 %262)
%264 = icmp slt i64 %263, 0
br i1 %264, label %265, label %267
265: ; preds = %261
%266 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.16, i64 0, i64 0))
br label %267
267: ; preds = %265, %261
%268 = call i32 @msg(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.17, i64 0, i64 0))
%269 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [19 x i8] c"Usage: tflock file\00"
@.str.1 = [59 x i8] c"1. Conflicting write locks cannot be held by two processes\00"
@O_RDWR = global i32 0
@.str.2 = [8 x i8] c"open %s\00"
@.str.3 = [9 x i8] c"fd: open\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.4 = [32 x i8] c"fd: write-lock failed, aborting\00"
@.str.5 = [17 x i8] c"fd: write-locked\00"
@.str.6 = [5 x i8] c"fork\00"
@.str.7 = [13 x i8] c"child forked\00"
@.str.8 = [12 x i8] c"fd2 open %s\00"
@.str.9 = [18 x i8] c"fd2: open (child)\00"
@.str.10 = [23 x i8] c"fd2: write-lock failed\00"
@.str.11 = [18 x i8] c"fd2: write-locked\00"
@.str.12 = [8 x i8] c"waitpid\00"
@.str.13 = [30 x i8] c"child terminated without exit\00"
@.str.14 = [31 x i8] c"child exited with %d, aborting\00"
@.str.15 = [22 x i8] c"child exited normally\00"
@.str.16 = [9 x i8] c"close fd\00"
@.str.17 = [11 x i8] c"fd: closed\00"
@.str.18 = [68 x i8] c"2. Conflicting read and write locks cannot be held by two processes\00"
@LOCK_SH = global i32 0
@.str.19 = [22 x i8] c"fd2: read-lock failed\00"
@.str.20 = [17 x i8] c"fd2: read-locked\00"
@.str.21 = [55 x i8] c"3. Conflicting read locks CAN be held by two processes\00"
@.str.22 = [31 x i8] c"fd: read-lock failed, aborting\00"
@.str.23 = [16 x i8] c"fd: read-locked\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i8*, i8** %1
%6 = tail call i32 @diod_log_init(i8* %5)
%7 = icmp eq i32 %0, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = tail call i32 @msg(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0))
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i8*, i8** %13
%20 = tail call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %18, %11
%22 = tail call i32 @msg(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @LOCK_EX
%24 = load i32, i32* @LOCK_NB
%25 = or i32 %24, %23
%26 = tail call i64 @flock(i32 %16, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = tail call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %30
30: ; preds = %28, %21
%31 = tail call i32 @msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%32 = tail call i32 (...) @fork()
switch i32 %32, label %56 [
i32 -1, label %33
i32 0, label %35
]
33: ; preds = %30
%34 = tail call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %35
35: ; preds = %30, %33
%36 = tail call i32 @msg(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%37 = load i8*, i8** %13
%38 = load i32, i32* @O_RDWR
%39 = tail call i32 @open(i8* %37, i32 %38)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = load i8*, i8** %13
%43 = tail call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %41, %35
%45 = tail call i32 @msg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%46 = load i32, i32* @LOCK_EX
%47 = load i32, i32* @LOCK_NB
%48 = or i32 %47, %46
%49 = tail call i64 @flock(i32 %39, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = tail call i32 @err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0))
%53 = tail call i32 @exit(i32 0)
unreachable
54: ; preds = %44
%55 = tail call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0))
br label %56
56: ; preds = %30, %54
%57 = call i32 @waitpid(i32 %32, i32* %3, i32 0)
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.12, i64 0, i64 0))
br label %61
61: ; preds = %59, %56
%62 = load i32, i32* %3
%63 = call i32 @WIFEXITED(i32 %62)
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.13, i64 0, i64 0))
br label %67
67: ; preds = %65, %61
%68 = load i32, i32* %3
%69 = call i32 @WEXITSTATUS(i32 %68)
%70 = icmp eq i32 %69, 0
br i1 %70, label %75, label %71
71: ; preds = %67
%72 = load i32, i32* %3
%73 = call i32 @WEXITSTATUS(i32 %72)
%74 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.14, i64 0, i64 0), i32 %73)
br label %75
75: ; preds = %71, %67
%76 = call i32 @msg(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.15, i64 0, i64 0))
%77 = call i64 @close(i32 %16)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %75
%80 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.16, i64 0, i64 0))
br label %81
81: ; preds = %79, %75
%82 = call i32 @msg(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.17, i64 0, i64 0))
%83 = call i32 @msg(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.18, i64 0, i64 0))
%84 = load i8*, i8** %13
%85 = load i32, i32* @O_RDWR
%86 = call i32 @open(i8* %84, i32 %85)
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %91
88: ; preds = %81
%89 = load i8*, i8** %13
%90 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %89)
br label %91
91: ; preds = %88, %81
%92 = call i32 @msg(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%93 = load i32, i32* @LOCK_EX
%94 = load i32, i32* @LOCK_NB
%95 = or i32 %94, %93
%96 = call i64 @flock(i32 %86, i32 %95)
%97 = icmp slt i64 %96, 0
br i1 %97, label %98, label %100
98: ; preds = %91
%99 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %100
100: ; preds = %98, %91
%101 = call i32 @msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%102 = call i32 (...) @fork()
switch i32 %102, label %126 [
i32 -1, label %103
i32 0, label %105
]
103: ; preds = %100
%104 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %105
105: ; preds = %100, %103
%106 = call i32 @msg(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%107 = load i8*, i8** %13
%108 = load i32, i32* @O_RDWR
%109 = call i32 @open(i8* %107, i32 %108)
%110 = icmp slt i32 %109, 0
br i1 %110, label %111, label %114
111: ; preds = %105
%112 = load i8*, i8** %13
%113 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %112)
br label %114
114: ; preds = %111, %105
%115 = call i32 @msg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%116 = load i32, i32* @LOCK_SH
%117 = load i32, i32* @LOCK_NB
%118 = or i32 %117, %116
%119 = call i64 @flock(i32 %109, i32 %118)
%120 = icmp slt i64 %119, 0
br i1 %120, label %121, label %124
121: ; preds = %114
%122 = call i32 @err(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.19, i64 0, i64 0))
%123 = call i32 @exit(i32 0)
unreachable
124: ; preds = %114
%125 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.20, i64 0, i64 0))
br label %126
126: ; preds = %100, %124
%127 = call i32 @waitpid(i32 %102, i32* %3, i32 0)
%128 = icmp slt i32 %127, 0
br i1 %128, label %129, label %131
129: ; preds = %126
%130 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.12, i64 0, i64 0))
br label %131
131: ; preds = %129, %126
%132 = load i32, i32* %3
%133 = call i32 @WIFEXITED(i32 %132)
%134 = icmp eq i32 %133, 0
br i1 %134, label %135, label %137
135: ; preds = %131
%136 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.13, i64 0, i64 0))
br label %137
137: ; preds = %135, %131
%138 = load i32, i32* %3
%139 = call i32 @WEXITSTATUS(i32 %138)
%140 = icmp eq i32 %139, 0
br i1 %140, label %145, label %141
141: ; preds = %137
%142 = load i32, i32* %3
%143 = call i32 @WEXITSTATUS(i32 %142)
%144 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.14, i64 0, i64 0), i32 %143)
br label %145
145: ; preds = %141, %137
%146 = call i32 @msg(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.15, i64 0, i64 0))
%147 = call i64 @close(i32 %86)
%148 = icmp slt i64 %147, 0
br i1 %148, label %149, label %151
149: ; preds = %145
%150 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.16, i64 0, i64 0))
br label %151
151: ; preds = %149, %145
%152 = call i32 @msg(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.17, i64 0, i64 0))
%153 = call i32 @msg(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.21, i64 0, i64 0))
%154 = load i8*, i8** %13
%155 = load i32, i32* @O_RDWR
%156 = call i32 @open(i8* %154, i32 %155)
%157 = icmp slt i32 %156, 0
br i1 %157, label %158, label %161
158: ; preds = %151
%159 = load i8*, i8** %13
%160 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %159)
br label %161
161: ; preds = %158, %151
%162 = call i32 @msg(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%163 = load i32, i32* @LOCK_SH
%164 = load i32, i32* @LOCK_NB
%165 = or i32 %164, %163
%166 = call i64 @flock(i32 %156, i32 %165)
%167 = icmp slt i64 %166, 0
br i1 %167, label %168, label %170
168: ; preds = %161
%169 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.22, i64 0, i64 0))
br label %170
170: ; preds = %168, %161
%171 = call i32 @msg(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.23, i64 0, i64 0))
%172 = call i32 (...) @fork()
switch i32 %172, label %196 [
i32 -1, label %173
i32 0, label %175
]
173: ; preds = %170
%174 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %175
175: ; preds = %170, %173
%176 = call i32 @msg(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%177 = load i8*, i8** %13
%178 = load i32, i32* @O_RDWR
%179 = call i32 @open(i8* %177, i32 %178)
%180 = icmp slt i32 %179, 0
br i1 %180, label %181, label %184
181: ; preds = %175
%182 = load i8*, i8** %13
%183 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %182)
br label %184
184: ; preds = %181, %175
%185 = call i32 @msg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%186 = load i32, i32* @LOCK_SH
%187 = load i32, i32* @LOCK_NB
%188 = or i32 %187, %186
%189 = call i64 @flock(i32 %156, i32 %188)
%190 = icmp slt i64 %189, 0
br i1 %190, label %191, label %193
191: ; preds = %184
%192 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.19, i64 0, i64 0))
br label %193
193: ; preds = %191, %184
%194 = call i32 @msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.20, i64 0, i64 0))
%195 = call i32 @exit(i32 0)
unreachable
196: ; preds = %170
%197 = call i32 @waitpid(i32 %172, i32* %3, i32 0)
%198 = icmp slt i32 %197, 0
br i1 %198, label %199, label %201
199: ; preds = %196
%200 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.12, i64 0, i64 0))
br label %201
201: ; preds = %199, %196
%202 = load i32, i32* %3
%203 = call i32 @WIFEXITED(i32 %202)
%204 = icmp eq i32 %203, 0
br i1 %204, label %205, label %207
205: ; preds = %201
%206 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.13, i64 0, i64 0))
br label %207
207: ; preds = %205, %201
%208 = load i32, i32* %3
%209 = call i32 @WEXITSTATUS(i32 %208)
%210 = icmp eq i32 %209, 0
br i1 %210, label %215, label %211
211: ; preds = %207
%212 = load i32, i32* %3
%213 = call i32 @WEXITSTATUS(i32 %212)
%214 = call i32 (i8*, ...) @msg_exit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.14, i64 0, i64 0), i32 %213)
br label %215
215: ; preds = %211, %207
%216 = call i32 @msg(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.15, i64 0, i64 0))
%217 = call i64 @close(i32 %156)
%218 = icmp slt i64 %217, 0
br i1 %218, label %219, label %221
219: ; preds = %215
%220 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.16, i64 0, i64 0))
br label %221
221: ; preds = %219, %215
%222 = call i32 @msg(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.17, i64 0, i64 0))
%223 = call i32 @exit(i32 0)
unreachable
}