#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 {
  scalar_t__ st_size;
};
typedef int ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
scalar_t__ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fstat(int, struct stat *);
scalar_t__ ftruncate(int, scalar_t__);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ test_read_random(char *, int, unsigned char *, int);
scalar_t__ test_read_rest(char *, int, unsigned char *, int, int);
scalar_t__ test_write_random(char *, int, unsigned char *, int);
scalar_t__ unlink(char *);
int test_exercise(char *filename, ssize_t size) {
  int fd;
  ssize_t i;
  ssize_t n;
  ssize_t buflen;
  int ret = (1);
  ssize_t trunc_size = size / 2;
  struct stat statbuf;
  unsigned char buff[(65536)];
  unlink(filename);
  if ((fd = open(filename, O_RDWR | O_CREAT, 0600)) < 0) {
    fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
    return (1);
  }
  if (test_write_random(filename, fd, buff, size) < 0)
    goto done;
  if (test_read_random(filename, fd, buff, size) < 0)
    goto done;
  while (trunc_size > 0) {
    if (ftruncate(fd, (off_t)trunc_size) < 0) {
      fprintf(stderr, "ftruncate failed: %s %s\n", filename, strerror(errno));
      goto done;
    }
    if (test_read_random(filename, fd, buff, trunc_size) < 0)
      goto done;
    if (fstat(fd, &statbuf) < 0) {
      fprintf(stderr, "fstat failed: %s %s\n", filename, strerror(errno));
      goto done;
    }
    if (statbuf.st_size != (off_t)trunc_size) {
      fprintf(stderr, "truncated file size incorrect, got %lu, expected %lu\n",
              (unsigned long)statbuf.st_size, (unsigned long)trunc_size);
      goto done;
    }
    if (ftruncate(fd, (off_t)size) < 0) {
      fprintf(stderr, "ftruncate failed: %s %s\n", filename, strerror(errno));
      goto done;
    }
    if (fstat(fd, &statbuf) < 0) {
      fprintf(stderr, "fstat failed: %s %s\n", filename, strerror(errno));
      goto done;
    }
    if (statbuf.st_size != (off_t)size) {
      fprintf(stderr, "truncated file size incorrect, got %lu, expected %lu\n",
              (unsigned long)statbuf.st_size, (unsigned long)size);
      goto done;
    }
    if (test_read_random(filename, fd, buff, trunc_size) < 0)
      goto done;
    if (test_read_rest(filename, fd, buff, trunc_size, size) < 0)
      goto done;
    trunc_size >>= 1;
  }
  ret = (0);
done:
  if (close(fd) < 0) {
    fprintf(stderr, "close failed: %s: %s\n", filename, strerror(errno));
    return (1);
  }
  if (unlink(filename) < 0) {
    fprintf(stderr, "unlink failed: %s: %s\n", filename, strerror(errno));
    return (1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<test_exercise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100b0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    0x2e14(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    124e <test_exercise+0xae>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1004c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x10048(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1004c(%rbp),%edi
mov    -0x10048(%rbp),%rdx
mov    %rax,%rcx
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ec <test_exercise+0x44c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
lea    -0x10040(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   10c0 <test_write_random@plt>
cmp    $0x0,%rax
jge    1273 <test_exercise+0xd3>
jmp    151d <test_exercise+0x37d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
lea    -0x10040(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1080 <test_read_random@plt>
cmp    $0x0,%rax
jge    1298 <test_exercise+0xf8>
jmp    151d <test_exercise+0x37d>
jmp    129d <test_exercise+0xfd>
cmpl   $0x0,-0x2c(%rbp)
jle    1516 <test_exercise+0x376>
mov    -0x18(%rbp),%edi
movslq -0x2c(%rbp),%rsi
call   1040 <ftruncate@plt>
cmp    $0x0,%rax
jge    1308 <test_exercise+0x168>
mov    0x2d0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1005c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x10058(%rbp)
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1005c(%rbp),%edi
mov    -0x10058(%rbp),%rdx
mov    %rax,%rcx
lea    0xd1b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    151d <test_exercise+0x37d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
lea    -0x10040(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1080 <test_read_random@plt>
cmp    $0x0,%rax
jge    132d <test_exercise+0x18d>
jmp    151d <test_exercise+0x37d>
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
jge    138e <test_exercise+0x1ee>
mov    0x2c86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1006c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x10068(%rbp)
mov    0x2c64(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1006c(%rbp),%edi
mov    -0x10068(%rbp),%rdx
mov    %rax,%rcx
lea    0xcae(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    151d <test_exercise+0x37d>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmp    %rcx,%rax
je     13c3 <test_exercise+0x223>
mov    0x2c2a(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
movslq -0x2c(%rbp),%rcx
lea    0xc8e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    151d <test_exercise+0x37d>
mov    -0x18(%rbp),%edi
movslq -0x14(%rbp),%rsi
call   1040 <ftruncate@plt>
cmp    $0x0,%rax
jge    1424 <test_exercise+0x284>
mov    0x2bf0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1007c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x10078(%rbp)
mov    0x2bce(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1007c(%rbp),%edi
mov    -0x10078(%rbp),%rdx
mov    %rax,%rcx
lea    0xbff(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    151d <test_exercise+0x37d>
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
jge    1485 <test_exercise+0x2e5>
mov    0x2b8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1008c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x10088(%rbp)
mov    0x2b6d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1008c(%rbp),%edi
mov    -0x10088(%rbp),%rdx
mov    %rax,%rcx
lea    0xbb7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    151d <test_exercise+0x37d>
mov    -0x38(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
je     14ba <test_exercise+0x31a>
mov    0x2b33(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
movslq -0x14(%rbp),%rcx
lea    0xb97(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    151d <test_exercise+0x37d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
lea    -0x10040(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1080 <test_read_random@plt>
cmp    $0x0,%rax
jge    14df <test_exercise+0x33f>
jmp    151d <test_exercise+0x37d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
lea    -0x10040(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    -0x14(%rbp),%r8d
call   1070 <test_read_rest@plt>
cmp    $0x0,%rax
jge    1508 <test_exercise+0x368>
jmp    151d <test_exercise+0x37d>
mov    -0x2c(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    129d <test_exercise+0xfd>
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    1581 <test_exercise+0x3e1>
mov    0x2a9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1009c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x10098(%rbp)
mov    0x2a78(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1009c(%rbp),%edi
mov    -0x10098(%rbp),%rdx
mov    %rax,%rcx
lea    0xb0d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ec <test_exercise+0x44c>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    15e6 <test_exercise+0x446>
mov    0x2a35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x100ac(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x100a8(%rbp)
mov    0x2a13(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x100ac(%rbp),%edi
mov    -0x100a8(%rbp),%rdx
mov    %rax,%rcx
lea    0xabe(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ec <test_exercise+0x44c>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_exercise>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    %esi,%ebp
mov    %rdi,%r13
call   1030 <unlink@plt>
mov    0x2e1e(%rip),%rax
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r13,%rdi
mov    $0x180,%edx
call   1090 <open@plt>
test   %eax,%eax
js     131c <test_exercise+0x17c>
mov    %eax,%r14d
lea    0x10(%rsp),%rdx
mov    %r13,%rdi
mov    %eax,%esi
mov    %ebp,%ecx
call   10c0 <test_write_random@plt>
mov    $0x1,%ebx
test   %rax,%rax
js     13bb <test_exercise+0x21b>
lea    0x10(%rsp),%rdx
mov    %r13,%rdi
mov    %r14d,%esi
mov    %ebp,%ecx
call   1080 <test_read_random@plt>
test   %rax,%rax
js     13bb <test_exercise+0x21b>
cmp    $0x2,%ebp
jl     1315 <test_exercise+0x175>
mov    %ebp,%eax
shr    $0x1f,%eax
add    %ebp,%eax
sar    %eax
mov    %ebp,0x4(%rsp)
mov    %ebp,%r12d
lea    0x8(%rsp),%r15
mov    %eax,%ebx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <ftruncate@plt>
test   %rax,%rax
js     133f <test_exercise+0x19f>
mov    %r13,%rdi
mov    %r14d,%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%ecx
call   1080 <test_read_random@plt>
test   %rax,%rax
js     13b6 <test_exercise+0x216>
mov    %r14d,%edi
mov    %r15,%rsi
call   10b0 <fstat@plt>
test   %rax,%rax
js     135f <test_exercise+0x1bf>
mov    0x8(%rsp),%rdx
cmp    %rbx,%rdx
jne    1387 <test_exercise+0x1e7>
mov    %r14d,%edi
mov    %r12,%rsi
call   1040 <ftruncate@plt>
test   %rax,%rax
js     133f <test_exercise+0x19f>
mov    %r14d,%edi
mov    %r15,%rsi
call   10b0 <fstat@plt>
test   %rax,%rax
js     135f <test_exercise+0x1bf>
mov    0x8(%rsp),%rdx
cmp    %r12,%rdx
jne    139c <test_exercise+0x1fc>
mov    %r12,%rbp
mov    %r13,%rdi
mov    %r14d,%esi
lea    0x10(%rsp),%r12
mov    %r12,%rdx
mov    %ebx,%ecx
call   1080 <test_read_random@plt>
test   %rax,%rax
js     13b6 <test_exercise+0x216>
mov    %r13,%rdi
mov    %r14d,%esi
mov    %r12,%rdx
mov    %ebx,%ecx
mov    0x4(%rsp),%r8d
call   1070 <test_read_rest@plt>
test   %rax,%rax
js     13b6 <test_exercise+0x216>
mov    %ebx,%eax
shr    %eax
cmp    $0x1,%ebx
mov    %rbp,%r12
ja     123d <test_exercise+0x9d>
xor    %ebx,%ebx
jmp    13bb <test_exercise+0x21b>
mov    0x2cad(%rip),%rax
mov    (%rax),%ebx
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xcc6(%rip),%rsi
jmp    1413 <test_exercise+0x273>
mov    0x2c8a(%rip),%rax
mov    (%rax),%ebx
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xcba(%rip),%rsi
jmp    137d <test_exercise+0x1dd>
mov    0x2c6a(%rip),%rax
mov    (%rax),%ebx
mov    0x2c59(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xcb3(%rip),%rsi
mov    %ebx,%edi
mov    %r13,%rdx
mov    %rax,%rcx
jmp    13af <test_exercise+0x20f>
mov    0x2c42(%rip),%rax
mov    (%rax),%edi
lea    0xcae(%rip),%rsi
mov    %rbx,%rcx
jmp    13af <test_exercise+0x20f>
mov    0x2c2d(%rip),%rax
mov    (%rax),%edi
lea    0xc99(%rip),%rsi
mov    %r12,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%ebx
mov    %r14d,%edi
call   1050 <close@plt>
test   %rax,%rax
js     13f5 <test_exercise+0x255>
mov    %r13,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jns    1427 <test_exercise+0x287>
mov    0x2bf4(%rip),%rax
mov    (%rax),%ebx
mov    0x2be3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xc9e(%rip),%rsi
jmp    1413 <test_exercise+0x273>
mov    0x2bd4(%rip),%rax
mov    (%rax),%ebx
mov    0x2bc3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xc68(%rip),%rsi
mov    %ebx,%edi
mov    %r13,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"Failed to open %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [25 x i8] c"ftruncate failed: %s %s\0A\00"
@.str.2 = [21 x i8] c"fstat failed: %s %s\0A\00"
@.str.3 = [54 x i8] c"truncated file size incorrect, got %lu, expected %lu\0A\00"
@.str.4 = [22 x i8] c"close failed: %s: %s\0A\00"
@.str.5 = [23 x i8] c"unlink failed: %s: %s\0A\00"
define i32 @test_exercise(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.stat
%13 = alloca [65536 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %10
%14 = load i32, i32* %5
%15 = sdiv i32 %14, 2
store i32 %15, i32* %11
%16 = load i8*, i8** %4
%17 = call i64 @unlink(i8* %16)
%18 = load i8*, i8** %4
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %18, i32 %21, i32 384)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %2
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %4
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %28)
store i32 1, i32* %3
br label %171
30: ; preds = %2
%31 = load i8*, i8** %4
%32 = load i32, i32* %6
%33 = getelementptr inbounds [65536 x i8], [65536 x i8]* %13, i64 0, i64 0
%34 = load i32, i32* %5
%35 = call i64 @test_write_random(i8* %31, i32 %32, i8* %33, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %30
br label %149
38: ; preds = %30
%39 = load i8*, i8** %4
%40 = load i32, i32* %6
%41 = getelementptr inbounds [65536 x i8], [65536 x i8]* %13, i64 0, i64 0
%42 = load i32, i32* %5
%43 = call i64 @test_read_random(i8* %39, i32 %40, i8* %41, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %38
br label %149
46: ; preds = %38
br label %47
47: ; preds = %145, %46
%48 = load i32, i32* %11
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %148
50: ; preds = %47
%51 = load i32, i32* %6
%52 = load i32, i32* %11
%53 = sext i32 %52 to i64
%54 = call i64 @ftruncate(i32 %51, i64 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %50
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %4
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %58, i8* %60)
br label %149
62: ; preds = %50
%63 = load i8*, i8** %4
%64 = load i32, i32* %6
%65 = getelementptr inbounds [65536 x i8], [65536 x i8]* %13, i64 0, i64 0
%66 = load i32, i32* %11
%67 = call i64 @test_read_random(i8* %63, i32 %64, i8* %65, i32 %66)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %62
br label %149
70: ; preds = %62
%71 = load i32, i32* %6
%72 = call i64 @fstat(i32 %71, %struct.stat* %12)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %80
74: ; preds = %70
%75 = load i32, i32* @stderr
%76 = load i8*, i8** %4
%77 = load i32, i32* @errno
%78 = call i8* @strerror(i32 %77)
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %76, i8* %78)
br label %149
80: ; preds = %70
%81 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%82 = load i64, i64* %81
%83 = load i32, i32* %11
%84 = sext i32 %83 to i64
%85 = icmp ne i64 %82, %84
br i1 %85, label %86, label %93
86: ; preds = %80
%87 = load i32, i32* @stderr
%88 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%89 = load i64, i64* %88
%90 = load i32, i32* %11
%91 = sext i32 %90 to i64
%92 = call i32 (i32, i8*, ...) @fprintf(i32 %87, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %89, i64 %91)
br label %149
93: ; preds = %80
%94 = load i32, i32* %6
%95 = load i32, i32* %5
%96 = sext i32 %95 to i64
%97 = call i64 @ftruncate(i32 %94, i64 %96)
%98 = icmp slt i64 %97, 0
br i1 %98, label %99, label %105
99: ; preds = %93
%100 = load i32, i32* @stderr
%101 = load i8*, i8** %4
%102 = load i32, i32* @errno
%103 = call i8* @strerror(i32 %102)
%104 = call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %101, i8* %103)
br label %149
105: ; preds = %93
%106 = load i32, i32* %6
%107 = call i64 @fstat(i32 %106, %struct.stat* %12)
%108 = icmp slt i64 %107, 0
br i1 %108, label %109, label %115
109: ; preds = %105
%110 = load i32, i32* @stderr
%111 = load i8*, i8** %4
%112 = load i32, i32* @errno
%113 = call i8* @strerror(i32 %112)
%114 = call i32 (i32, i8*, ...) @fprintf(i32 %110, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %111, i8* %113)
br label %149
115: ; preds = %105
%116 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%117 = load i64, i64* %116
%118 = load i32, i32* %5
%119 = sext i32 %118 to i64
%120 = icmp ne i64 %117, %119
br i1 %120, label %121, label %128
121: ; preds = %115
%122 = load i32, i32* @stderr
%123 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%124 = load i64, i64* %123
%125 = load i32, i32* %5
%126 = sext i32 %125 to i64
%127 = call i32 (i32, i8*, ...) @fprintf(i32 %122, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %124, i64 %126)
br label %149
128: ; preds = %115
%129 = load i8*, i8** %4
%130 = load i32, i32* %6
%131 = getelementptr inbounds [65536 x i8], [65536 x i8]* %13, i64 0, i64 0
%132 = load i32, i32* %11
%133 = call i64 @test_read_random(i8* %129, i32 %130, i8* %131, i32 %132)
%134 = icmp slt i64 %133, 0
br i1 %134, label %135, label %136
135: ; preds = %128
br label %149
136: ; preds = %128
%137 = load i8*, i8** %4
%138 = load i32, i32* %6
%139 = getelementptr inbounds [65536 x i8], [65536 x i8]* %13, i64 0, i64 0
%140 = load i32, i32* %11
%141 = load i32, i32* %5
%142 = call i64 @test_read_rest(i8* %137, i32 %138, i8* %139, i32 %140, i32 %141)
%143 = icmp slt i64 %142, 0
br i1 %143, label %144, label %145
144: ; preds = %136
br label %149
145: ; preds = %136
%146 = load i32, i32* %11
%147 = ashr i32 %146, 1
store i32 %147, i32* %11
br label %47
148: ; preds = %47
store i32 0, i32* %10
br label %149
149: ; preds = %148, %144, %135, %121, %109, %99, %86, %74, %69, %56, %45, %37
%150 = load i32, i32* %6
%151 = call i64 @close(i32 %150)
%152 = icmp slt i64 %151, 0
br i1 %152, label %153, label %159
153: ; preds = %149
%154 = load i32, i32* @stderr
%155 = load i8*, i8** %4
%156 = load i32, i32* @errno
%157 = call i8* @strerror(i32 %156)
%158 = call i32 (i32, i8*, ...) @fprintf(i32 %154, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %155, i8* %157)
store i32 1, i32* %3
br label %171
159: ; preds = %149
%160 = load i8*, i8** %4
%161 = call i64 @unlink(i8* %160)
%162 = icmp slt i64 %161, 0
br i1 %162, label %163, label %169
163: ; preds = %159
%164 = load i32, i32* @stderr
%165 = load i8*, i8** %4
%166 = load i32, i32* @errno
%167 = call i8* @strerror(i32 %166)
%168 = call i32 (i32, i8*, ...) @fprintf(i32 %164, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %165, i8* %167)
store i32 1, i32* %3
br label %171
169: ; preds = %159
%170 = load i32, i32* %10
store i32 %170, i32* %3
br label %171
171: ; preds = %169, %163, %153, %24
%172 = load i32, i32* %3
ret i32 %172
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"Failed to open %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [25 x i8] c"ftruncate failed: %s %s\0A\00"
@.str.2 = [21 x i8] c"fstat failed: %s %s\0A\00"
@.str.3 = [54 x i8] c"truncated file size incorrect, got %lu, expected %lu\0A\00"
@.str.4 = [22 x i8] c"close failed: %s: %s\0A\00"
@.str.5 = [23 x i8] c"unlink failed: %s: %s\0A\00"
define i32 @test_exercise(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = alloca [65536 x i8]
%5 = sdiv i32 %1, 2
%6 = bitcast %struct.stat* %3 to i8*
%7 = getelementptr inbounds [65536 x i8], [65536 x i8]* %4, i64 0, i64 0
%8 = tail call i64 @unlink(i8* %0)
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %0, i32 %11, i32 384)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %17)
br label %106
19: ; preds = %2
%20 = call i64 @test_write_random(i8* %0, i32 %12, i8* %7, i32 %1)
%21 = icmp slt i64 %20, 0
br i1 %21, label %89, label %22
22: ; preds = %19
%23 = call i64 @test_read_random(i8* %0, i32 %12, i8* %7, i32 %1)
%24 = icmp slt i64 %23, 0
br i1 %24, label %89, label %25
25: ; preds = %22
%26 = icmp sgt i32 %1, 1
br i1 %26, label %27, label %89
27: ; preds = %25
%28 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%29 = zext i32 %1 to i64
br label %30
30: ; preds = %27, %86
%31 = phi i32 [ %5, %27 ], [ %87, %86 ]
%32 = zext i32 %31 to i64
%33 = call i64 @ftruncate(i32 %12, i64 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %38)
br label %89
40: ; preds = %30
%41 = call i64 @test_read_random(i8* %0, i32 %12, i8* %7, i32 %31)
%42 = icmp slt i64 %41, 0
br i1 %42, label %89, label %43
43: ; preds = %40
%44 = call i64 @fstat(i32 %12, %struct.stat* %3)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %43
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %49)
br label %89
51: ; preds = %43
%52 = load i64, i64* %28
%53 = icmp eq i64 %52, %32
br i1 %53, label %58, label %54
54: ; preds = %51
%55 = zext i32 %31 to i64
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %52, i64 %55)
br label %89
58: ; preds = %51
%59 = call i64 @ftruncate(i32 %12, i64 %29)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %58
%62 = load i32, i32* @stderr
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %64)
br label %89
66: ; preds = %58
%67 = call i64 @fstat(i32 %12, %struct.stat* %3)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %74
69: ; preds = %66
%70 = load i32, i32* @stderr
%71 = load i32, i32* @errno
%72 = call i8* @strerror(i32 %71)
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %72)
br label %89
74: ; preds = %66
%75 = load i64, i64* %28
%76 = icmp eq i64 %75, %29
br i1 %76, label %80, label %77
77: ; preds = %74
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %75, i64 %29)
br label %89
80: ; preds = %74
%81 = call i64 @test_read_random(i8* %0, i32 %12, i8* %7, i32 %31)
%82 = icmp slt i64 %81, 0
br i1 %82, label %89, label %83
83: ; preds = %80
%84 = call i64 @test_read_rest(i8* %0, i32 %12, i8* %7, i32 %31, i32 %1)
%85 = icmp slt i64 %84, 0
br i1 %85, label %89, label %86
86: ; preds = %83
%87 = lshr i32 %31, 1
%88 = icmp ult i32 %31, 2
br i1 %88, label %89, label %30
89: ; preds = %40, %80, %83, %86, %25, %22, %19, %77, %69, %61, %54, %46, %35
%90 = phi i32 [ 1, %19 ], [ 1, %22 ], [ 1, %35 ], [ 1, %46 ], [ 1, %54 ], [ 1, %61 ], [ 1, %69 ], [ 1, %77 ], [ 0, %25 ], [ 1, %40 ], [ 1, %80 ], [ 1, %83 ], [ 0, %86 ]
%91 = call i64 @close(i32 %12)
%92 = icmp slt i64 %91, 0
br i1 %92, label %93, label %98
93: ; preds = %89
%94 = load i32, i32* @stderr
%95 = load i32, i32* @errno
%96 = call i8* @strerror(i32 %95)
%97 = call i32 (i32, i8*, ...) @fprintf(i32 %94, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %96)
br label %106
98: ; preds = %89
%99 = call i64 @unlink(i8* %0)
%100 = icmp slt i64 %99, 0
br i1 %100, label %101, label %106
101: ; preds = %98
%102 = load i32, i32* @stderr
%103 = load i32, i32* @errno
%104 = call i8* @strerror(i32 %103)
%105 = call i32 (i32, i8*, ...) @fprintf(i32 %102, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* %104)
br label %106
106: ; preds = %98, %101, %93, %14
%107 = phi i32 [ 1, %14 ], [ 1, %93 ], [ 1, %101 ], [ %90, %98 ]
ret i32 %107
}