#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct stat {
  int st_size;
};
typedef int off_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EEXIST;
int EOF;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int getc(int /*<<< orphan*/ *);
char *malloc(int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char **argv) {
  int lockfd = -1;
  int ival;
  off_t hpos;
  struct stat statbuf;
  FILE *dbfp;
  char *dbfname, *lockfname, *buffer;
  static char usage[] = "halve-duplicatedb dbfile lockfile";
  if (argc != 3) {
    fprintf(stderr, "Usage:\t%s\n", usage);
    exit(1);
  }
  dbfname = argv[1];
  lockfname = argv[2];
  for (ival = 0; ival < 10; ++ival) {
    if ((lockfd = open(lockfname, O_CREAT | O_EXCL | O_RDWR, 0)) < 0) {
      if (errno == EEXIST) {
        sleep(1);
        continue;
      }
      fprintf(stderr, "Error creating lockfile: \"%s\"\t%s\n", lockfname,
              strerror(errno));
      exit(1);
    }
    break;
  }
  if (lockfd < 0) {
    fprintf(stderr, "Timeout gaining lockfile: \"%s\"\n", lockfname);
    exit(1);
  }
  close(lockfd);
  if (stat(dbfname, &statbuf) != 0) {
    fprintf(stderr, "Error statting file: \"%s\"\t%s\n", dbfname,
            strerror(errno));
    unlink(lockfname);
    exit(1);
  }
  hpos = statbuf.st_size / 2;
  if ((buffer = malloc(hpos)) == NULL) {
    fprintf(stderr, "Error allocating %ld bytes\t%s\n", (long)hpos,
            strerror(errno));
    unlink(lockfname);
    exit(1);
  }
  if ((dbfp = fopen(dbfname, "r")) == NULL) {
    fprintf(stderr, "Error opening file: \"%s\"\t%s\n", dbfname,
            strerror(errno));
    unlink(lockfname);
    exit(1);
  }
  if (fseek(dbfp, hpos, SEEK_SET) != 0) {
    fprintf(stderr, "Error seeking file: \"%s\"\t%s\n", dbfname,
            strerror(errno));
    unlink(lockfname);
    exit(1);
  }
  while ((ival = getc(dbfp)) != EOF) {
    ++hpos;
    if (ival == '\0')
      break;
  }
  if (ival == EOF) {
    fprintf(stderr, "No NULL character found\n");
    unlink(lockfname);
    exit(1);
  }
  if (fread(buffer, statbuf.st_size - hpos, 1, dbfp) != 1) {
    fprintf(stderr, "Error reading file: \"%s\"\t%s\n", dbfname,
            strerror(errno));
    unlink(lockfname);
    exit(1);
  }
  fclose(dbfp);
  if ((dbfp = fopen(dbfname, "w")) == NULL) {
    fprintf(stderr, "Error opening file: \"%s\"\t%s\n", dbfname,
            strerror(errno));
    unlink(lockfname);
    exit(1);
  }
  if (fwrite(buffer, statbuf.st_size - hpos, 1, dbfp) != 1) {
    fprintf(stderr, "Error writing file: \"%s\"\t%s\n", dbfname,
            strerror(errno));
    unlink(lockfname);
    exit(1);
  }
  fclose(dbfp);
  unlink(lockfname);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1242 <main+0x52>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
lea    0x2e6f(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jge    131e <main+0x12e>
mov    -0x38(%rbp),%rdi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
mov    0x2d39(%rip),%rax
or     (%rax),%esi
mov    0x2d58(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    130b <main+0x11b>
mov    0x2d0c(%rip),%rax
mov    (%rax),%rax
mov    0x2d1a(%rip),%rcx
cmp    (%rcx),%rax
jne    12c6 <main+0xd6>
mov    $0x1,%edi
call   1100 <sleep@plt>
jmp    1310 <main+0x120>
mov    0x2cf3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2ccf(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd11(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    131e <main+0x12e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1261 <main+0x71>
cmpl   $0x0,-0x14(%rbp)
jge    134d <main+0x15d>
mov    0x2c91(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xcf1(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     13ba <main+0x1ca>
mov    0x2c4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2c29(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xcad(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    142c <main+0x23c>
mov    0x2bdb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
movslq -0x1c(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2bb7(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rdx
mov    %rax,%rcx
lea    0xc59(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x30(%rbp),%rdi
lea    0xc53(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1498 <main+0x2a8>
mov    0x2b6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2b4b(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rdx
mov    %rax,%rcx
lea    0xc0e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2b02(%rip),%rax
mov    (%rax),%edx
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     1511 <main+0x321>
mov    0x2b02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    0x2ad8(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x8c(%rbp),%edi
mov    -0x88(%rbp),%rdx
mov    %rax,%rcx
lea    0xbb2(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    1516 <main+0x326>
mov    -0x28(%rbp),%rdi
call   1110 <getc@plt>
mov    %eax,-0x18(%rbp)
mov    0x2aaf(%rip),%rcx
cmp    (%rcx),%eax
je     154e <main+0x35e>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1549 <main+0x359>
jmp    154e <main+0x35e>
jmp    1516 <main+0x326>
mov    -0x18(%rbp),%eax
mov    0x2a80(%rip),%rcx
cmp    (%rcx),%eax
jne    158a <main+0x39a>
mov    0x2a59(%rip),%rax
mov    (%rax),%edi
lea    0xb56(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%esi
sub    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     1605 <main+0x415>
mov    0x2a0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x9c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    0x29e4(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x9c(%rbp),%edi
mov    -0x98(%rbp),%rdx
mov    %rax,%rcx
lea    0xaf4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
lea    0xae3(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1686 <main+0x496>
mov    0x298d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xac(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    0x2963(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0xac(%rbp),%edi
mov    -0xa8(%rbp),%rdx
mov    %rax,%rcx
lea    0xa20(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%esi
sub    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   10e0 <fwrite@plt>
cmp    $0x1,%eax
je     1701 <main+0x511>
mov    0x2912(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xbc(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    0x28e8(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0xbc(%rbp),%edi
mov    -0xb8(%rbp),%rdx
mov    %rax,%rcx
lea    0xa17(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x3,%edi
jne    1435 <main+0x245>
mov    0x8(%rsi),%rax
mov    %rax,0x10(%rsp)
mov    0x10(%rsi),%r12
mov    $0xa,%ebx
mov    0x2dd8(%rip),%r15
mov    0x2d91(%rip),%rbp
mov    0x2db2(%rip),%r14
mov    0x2d93(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%rbp),%esi
or     (%r15),%esi
or     (%r14),%esi
mov    %r12,%rdi
xor    %edx,%edx
call   10b0 <open@plt>
test   %eax,%eax
jns    129e <main+0xae>
mov    0x2d52(%rip),%rax
mov    (%rax),%rdi
cmp    0x0(%r13),%rdi
jne    13e3 <main+0x1f3>
mov    $0x1,%edi
call   1100 <sleep@plt>
add    $0xffffffff,%ebx
jne    1240 <main+0x50>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
lea    0xda3(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %eax,%edi
call   1060 <close@plt>
lea    0x8(%rsp),%rsi
mov    0x10(%rsp),%r13
mov    %r13,%rdi
call   1080 <stat@plt>
test   %rax,%rax
jne    1451 <main+0x261>
mov    0x8(%rsp),%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
mov    %ebp,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1475 <main+0x285>
mov    %rax,%r15
lea    0xda3(%rip),%rsi
mov    %r13,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1411 <main+0x221>
mov    %rax,%rbx
mov    0x2ca6(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
jne    149e <main+0x2ae>
neg    %ebp
mov    0x2cb8(%rip),%r14
mov    %rbx,%rdi
call   1110 <getc@plt>
cmp    (%r14),%eax
je     13ba <main+0x1ca>
add    $0xffffffff,%ebp
test   %eax,%eax
jne    1320 <main+0x130>
mov    0x8(%rsp),%esi
add    %ebp,%esi
mov    %r15,%rdi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    14bf <main+0x2cf>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xd96(%rip),%rsi
mov    %r13,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1411 <main+0x221>
mov    %rax,%rbx
add    0x8(%rsp),%ebp
mov    %r15,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10e0 <fwrite@plt>
cmp    $0x1,%eax
jne    14e0 <main+0x2f0>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2bff(%rip),%rax
mov    (%rax),%edi
lea    0xcfc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2bd6(%rip),%rax
mov    (%rax),%ebx
call   10f0 <strerror@plt>
lea    0xc13(%rip),%rsi
mov    %ebx,%edi
mov    %r12,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2ba8(%rip),%rax
mov    (%rax),%ebx
mov    0x2b8f(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xc5c(%rip),%rsi
jmp    14ff <main+0x30f>
mov    0x2b84(%rip),%rax
mov    (%rax),%edi
lea    0xbbb(%rip),%rsi
lea    0x2c54(%rip),%rdx
jmp    128d <main+0x9d>
mov    0x2b68(%rip),%rax
mov    (%rax),%ebx
mov    0x2b4f(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xbdd(%rip),%rsi
jmp    14ff <main+0x30f>
mov    0x2b44(%rip),%rax
mov    (%rax),%ebx
movslq %ebp,%rbp
mov    0x2b28(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xbd4(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
jmp    1504 <main+0x314>
mov    0x2b1b(%rip),%rax
mov    (%rax),%ebx
mov    0x2b02(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xbec(%rip),%rsi
jmp    14ff <main+0x30f>
mov    0x2afa(%rip),%rax
mov    (%rax),%ebx
mov    0x2ae1(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xc01(%rip),%rsi
jmp    14ff <main+0x30f>
mov    0x2ad9(%rip),%rax
mov    (%rax),%ebx
mov    0x2ac0(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xbff(%rip),%rsi
mov    %ebx,%edi
mov    %r13,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    13d1 <main+0x1e1>

LLVM IR (-O0):
%struct.stat = type { i32 }
@main.usage = internal global [34 x i8] c"halve-duplicatedb dbfile lockfile\00"
@stderr = global i32 0
@.str = [11 x i8] c"Usage:\09%s\0A\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [34 x i8] c"Error creating lockfile: \22%s\22\09%s\0A\00"
@.str.2 = [32 x i8] c"Timeout gaining lockfile: \22%s\22\0A\00"
@.str.3 = [30 x i8] c"Error statting file: \22%s\22\09%s\0A\00"
@.str.4 = [31 x i8] c"Error allocating %ld bytes\09%s\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [29 x i8] c"Error opening file: \22%s\22\09%s\0A\00"
@SEEK_SET = global i32 0
@.str.7 = [29 x i8] c"Error seeking file: \22%s\22\09%s\0A\00"
@EOF = global i32 0
@.str.8 = [25 x i8] c"No NULL character found\0A\00"
@.str.9 = [29 x i8] c"Error reading file: \22%s\22\09%s\0A\00"
@.str.10 = [2 x i8] c"w\00"
@.str.11 = [29 x i8] c"Error writing file: \22%s\22\09%s\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 %struct.stat
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([34 x i8], [34 x i8]* @main.usage, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %11
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
store i8* %26, i8** %12
store i32 0, i32* %7
br label %27
27: ; preds = %53, %20
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 10
br i1 %29, label %30, label %56
30: ; preds = %27
%31 = load i8*, i8** %12
%32 = load i32, i32* @O_CREAT
%33 = load i32, i32* @O_EXCL
%34 = or i32 %32, %33
%35 = load i32, i32* @O_RDWR
%36 = or i32 %34, %35
%37 = call i32 @open(i8* %31, i32 %36, i32 0)
store i32 %37, i32* %6
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %52
39: ; preds = %30
%40 = load i64, i64* @errno
%41 = load i64, i64* @EEXIST
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @sleep(i32 1)
br label %53
45: ; preds = %39
%46 = load i32, i32* @stderr
%47 = load i8*, i8** %12
%48 = load i64, i64* @errno
%49 = call i8* @strerror(i64 %48)
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %47, i8* %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %30
br label %56
53: ; preds = %43
%54 = load i32, i32* %7
%55 = add nsw i32 %54, 1
store i32 %55, i32* %7
br label %27
56: ; preds = %52, %27
%57 = load i32, i32* %6
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %56
%60 = load i32, i32* @stderr
%61 = load i8*, i8** %12
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %61)
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %56
%65 = load i32, i32* %6
%66 = call i32 @close(i32 %65)
%67 = load i8*, i8** %11
%68 = call i64 @stat(i8* %67, %struct.stat* %9)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %79
70: ; preds = %64
%71 = load i32, i32* @stderr
%72 = load i8*, i8** %11
%73 = load i64, i64* @errno
%74 = call i8* @strerror(i64 %73)
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %72, i8* %74)
%76 = load i8*, i8** %12
%77 = call i32 @unlink(i8* %76)
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %64
%80 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%81 = load i32, i32* %80
%82 = sdiv i32 %81, 2
store i32 %82, i32* %8
%83 = load i32, i32* %8
%84 = call i8* @malloc(i32 %83)
store i8* %84, i8** %13
%85 = icmp eq i8* %84, null
br i1 %85, label %86, label %96
86: ; preds = %79
%87 = load i32, i32* @stderr
%88 = load i32, i32* %8
%89 = sext i32 %88 to i64
%90 = load i64, i64* @errno
%91 = call i8* @strerror(i64 %90)
%92 = call i32 (i32, i8*, ...) @fprintf(i32 %87, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i64 %89, i8* %91)
%93 = load i8*, i8** %12
%94 = call i32 @unlink(i8* %93)
%95 = call i32 @exit(i32 1)
unreachable
96: ; preds = %79
%97 = load i8*, i8** %11
%98 = call i32* @fopen(i8* %97, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %98, i32** %10
%99 = icmp eq i32* %98, null
br i1 %99, label %100, label %109
100: ; preds = %96
%101 = load i32, i32* @stderr
%102 = load i8*, i8** %11
%103 = load i64, i64* @errno
%104 = call i8* @strerror(i64 %103)
%105 = call i32 (i32, i8*, ...) @fprintf(i32 %101, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %102, i8* %104)
%106 = load i8*, i8** %12
%107 = call i32 @unlink(i8* %106)
%108 = call i32 @exit(i32 1)
unreachable
109: ; preds = %96
%110 = load i32*, i32** %10
%111 = load i32, i32* %8
%112 = load i32, i32* @SEEK_SET
%113 = call i64 @fseek(i32* %110, i32 %111, i32 %112)
%114 = icmp ne i64 %113, 0
br i1 %114, label %115, label %124
115: ; preds = %109
%116 = load i32, i32* @stderr
%117 = load i8*, i8** %11
%118 = load i64, i64* @errno
%119 = call i8* @strerror(i64 %118)
%120 = call i32 (i32, i8*, ...) @fprintf(i32 %116, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i8* %117, i8* %119)
%121 = load i8*, i8** %12
%122 = call i32 @unlink(i8* %121)
%123 = call i32 @exit(i32 1)
unreachable
124: ; preds = %109
br label %125
125: ; preds = %136, %124
%126 = load i32*, i32** %10
%127 = call i32 @getc(i32* %126)
store i32 %127, i32* %7
%128 = load i32, i32* @EOF
%129 = icmp ne i32 %127, %128
br i1 %129, label %130, label %137
130: ; preds = %125
%131 = load i32, i32* %8
%132 = add nsw i32 %131, 1
store i32 %132, i32* %8
%133 = load i32, i32* %7
%134 = icmp eq i32 %133, 0
br i1 %134, label %135, label %136
135: ; preds = %130
br label %137
136: ; preds = %130
br label %125
137: ; preds = %135, %125
%138 = load i32, i32* %7
%139 = load i32, i32* @EOF
%140 = icmp eq i32 %138, %139
br i1 %140, label %141, label %147
141: ; preds = %137
%142 = load i32, i32* @stderr
%143 = call i32 (i32, i8*, ...) @fprintf(i32 %142, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
%144 = load i8*, i8** %12
%145 = call i32 @unlink(i8* %144)
%146 = call i32 @exit(i32 1)
unreachable
147: ; preds = %137
%148 = load i8*, i8** %13
%149 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%150 = load i32, i32* %149
%151 = load i32, i32* %8
%152 = sub nsw i32 %150, %151
%153 = load i32*, i32** %10
%154 = call i32 @fread(i8* %148, i32 %152, i32 1, i32* %153)
%155 = icmp ne i32 %154, 1
br i1 %155, label %156, label %165
156: ; preds = %147
%157 = load i32, i32* @stderr
%158 = load i8*, i8** %11
%159 = load i64, i64* @errno
%160 = call i8* @strerror(i64 %159)
%161 = call i32 (i32, i8*, ...) @fprintf(i32 %157, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0), i8* %158, i8* %160)
%162 = load i8*, i8** %12
%163 = call i32 @unlink(i8* %162)
%164 = call i32 @exit(i32 1)
unreachable
165: ; preds = %147
%166 = load i32*, i32** %10
%167 = call i32 @fclose(i32* %166)
%168 = load i8*, i8** %11
%169 = call i32* @fopen(i8* %168, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
store i32* %169, i32** %10
%170 = icmp eq i32* %169, null
br i1 %170, label %171, label %180
171: ; preds = %165
%172 = load i32, i32* @stderr
%173 = load i8*, i8** %11
%174 = load i64, i64* @errno
%175 = call i8* @strerror(i64 %174)
%176 = call i32 (i32, i8*, ...) @fprintf(i32 %172, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %173, i8* %175)
%177 = load i8*, i8** %12
%178 = call i32 @unlink(i8* %177)
%179 = call i32 @exit(i32 1)
unreachable
180: ; preds = %165
%181 = load i8*, i8** %13
%182 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%183 = load i32, i32* %182
%184 = load i32, i32* %8
%185 = sub nsw i32 %183, %184
%186 = load i32*, i32** %10
%187 = call i32 @fwrite(i8* %181, i32 %185, i32 1, i32* %186)
%188 = icmp ne i32 %187, 1
br i1 %188, label %189, label %198
189: ; preds = %180
%190 = load i32, i32* @stderr
%191 = load i8*, i8** %11
%192 = load i64, i64* @errno
%193 = call i8* @strerror(i64 %192)
%194 = call i32 (i32, i8*, ...) @fprintf(i32 %190, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.11, i64 0, i64 0), i8* %191, i8* %193)
%195 = load i8*, i8** %12
%196 = call i32 @unlink(i8* %195)
%197 = call i32 @exit(i32 1)
unreachable
198: ; preds = %180
%199 = load i32*, i32** %10
%200 = call i32 @fclose(i32* %199)
%201 = load i8*, i8** %12
%202 = call i32 @unlink(i8* %201)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@main.usage = internal global [34 x i8] c"halve-duplicatedb dbfile lockfile\00"
@stderr = global i32 0
@.str = [11 x i8] c"Usage:\09%s\0A\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [34 x i8] c"Error creating lockfile: \22%s\22\09%s\0A\00"
@.str.2 = [32 x i8] c"Timeout gaining lockfile: \22%s\22\0A\00"
@.str.3 = [30 x i8] c"Error statting file: \22%s\22\09%s\0A\00"
@.str.4 = [31 x i8] c"Error allocating %ld bytes\09%s\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [29 x i8] c"Error opening file: \22%s\22\09%s\0A\00"
@SEEK_SET = global i32 0
@.str.7 = [29 x i8] c"Error seeking file: \22%s\22\09%s\0A\00"
@EOF = global i32 0
@.str.8 = [25 x i8] c"No NULL character found\0A\00"
@.str.9 = [29 x i8] c"Error reading file: \22%s\22\09%s\0A\00"
@.str.10 = [2 x i8] c"w\00"
@.str.11 = [29 x i8] c"Error writing file: \22%s\22\09%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([34 x i8], [34 x i8]* @main.usage, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
br label %15
15: ; preds = %10, %28
%16 = phi i32 [ 0, %10 ], [ %30, %28 ]
%17 = load i32, i32* @O_CREAT
%18 = load i32, i32* @O_EXCL
%19 = or i32 %18, %17
%20 = load i32, i32* @O_RDWR
%21 = or i32 %19, %20
%22 = tail call i32 @open(i8* %14, i32 %21, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %41
24: ; preds = %15
%25 = load i64, i64* @errno
%26 = load i64, i64* @EEXIST
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = tail call i32 @sleep(i32 1)
%30 = add nuw nsw i32 %16, 1
%31 = icmp eq i32 %30, 10
br i1 %31, label %37, label %15
32: ; preds = %24
%33 = load i32, i32* @stderr
%34 = tail call i8* @strerror(i64 %25)
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %34)
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %28
%38 = load i32, i32* @stderr
%39 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %15
%42 = tail call i32 @close(i32 %22)
%43 = call i64 @stat(i8* %12, %struct.stat* %3)
%44 = icmp eq i64 %43, 0
br i1 %44, label %52, label %45
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = load i64, i64* @errno
%48 = call i8* @strerror(i64 %47)
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %12, i8* %48)
%50 = call i32 @unlink(i8* %14)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %41
%53 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%54 = load i32, i32* %53
%55 = sdiv i32 %54, 2
%56 = call i8* @malloc(i32 %55)
%57 = icmp eq i8* %56, null
br i1 %57, label %58, label %66
58: ; preds = %52
%59 = load i32, i32* @stderr
%60 = sext i32 %55 to i64
%61 = load i64, i64* @errno
%62 = call i8* @strerror(i64 %61)
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i64 %60, i8* %62)
%64 = call i32 @unlink(i8* %14)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %52
%67 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%68 = icmp eq i32* %67, null
br i1 %68, label %69, label %76
69: ; preds = %66
%70 = load i32, i32* @stderr
%71 = load i64, i64* @errno
%72 = call i8* @strerror(i64 %71)
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %12, i8* %72)
%74 = call i32 @unlink(i8* %14)
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %66
%77 = load i32, i32* @SEEK_SET
%78 = call i64 @fseek(i32* %67, i32 %55, i32 %77)
%79 = icmp eq i64 %78, 0
br i1 %79, label %87, label %80
80: ; preds = %76
%81 = load i32, i32* @stderr
%82 = load i64, i64* @errno
%83 = call i8* @strerror(i64 %82)
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i8* %12, i8* %83)
%85 = call i32 @unlink(i8* %14)
%86 = call i32 @exit(i32 1)
unreachable
87: ; preds = %76, %92
%88 = phi i32 [ %93, %92 ], [ %55, %76 ]
%89 = call i32 @getc(i32* %67)
%90 = load i32, i32* @EOF
%91 = icmp eq i32 %89, %90
br i1 %91, label %95, label %92
92: ; preds = %87
%93 = add nsw i32 %88, 1
%94 = icmp eq i32 %89, 0
br i1 %94, label %100, label %87
95: ; preds = %87
%96 = load i32, i32* @stderr
%97 = call i32 (i32, i8*, ...) @fprintf(i32 %96, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
%98 = call i32 @unlink(i8* %14)
%99 = call i32 @exit(i32 1)
unreachable
100: ; preds = %92
%101 = load i32, i32* %53
%102 = sub nsw i32 %101, %93
%103 = call i32 @fread(i8* %56, i32 %102, i32 1, i32* %67)
%104 = icmp eq i32 %103, 1
br i1 %104, label %112, label %105
105: ; preds = %100
%106 = load i32, i32* @stderr
%107 = load i64, i64* @errno
%108 = call i8* @strerror(i64 %107)
%109 = call i32 (i32, i8*, ...) @fprintf(i32 %106, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0), i8* %12, i8* %108)
%110 = call i32 @unlink(i8* %14)
%111 = call i32 @exit(i32 1)
unreachable
112: ; preds = %100
%113 = call i32 @fclose(i32* %67)
%114 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
%115 = icmp eq i32* %114, null
br i1 %115, label %116, label %123
116: ; preds = %112
%117 = load i32, i32* @stderr
%118 = load i64, i64* @errno
%119 = call i8* @strerror(i64 %118)
%120 = call i32 (i32, i8*, ...) @fprintf(i32 %117, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %12, i8* %119)
%121 = call i32 @unlink(i8* %14)
%122 = call i32 @exit(i32 1)
unreachable
123: ; preds = %112
%124 = load i32, i32* %53
%125 = sub nsw i32 %124, %93
%126 = call i32 @fwrite(i8* %56, i32 %125, i32 1, i32* %114)
%127 = icmp eq i32 %126, 1
br i1 %127, label %135, label %128
128: ; preds = %123
%129 = load i32, i32* @stderr
%130 = load i64, i64* @errno
%131 = call i8* @strerror(i64 %130)
%132 = call i32 (i32, i8*, ...) @fprintf(i32 %129, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.11, i64 0, i64 0), i8* %12, i8* %131)
%133 = call i32 @unlink(i8* %14)
%134 = call i32 @exit(i32 1)
unreachable
135: ; preds = %123
%136 = call i32 @fclose(i32* %114)
%137 = call i32 @unlink(i8* %14)
ret i32 0
}