#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ FIGETBSZ;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ ftruncate(int, long);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
unsigned char *malloc(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, unsigned char *, int);
int /*<<< orphan*/ stderr;
int write(int, unsigned char *, int);
int main(int argc, char **argv) {
  int fd2;
  int block_size;
  int i;
  int zero_blocks;
  int copied_blocks;
  long filesize;
  unsigned char *buffer1;
  if (argc != 2) {
    fprintf(stderr, "%s a fast sparse-aware binary file writer.\n", argv[0]);
    fprintf(stderr, "Usage: %s FILE\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((fd2 = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0777)) < 0) {
    fprintf(stderr, "Cannot open %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  if (ioctl(fd2, FIGETBSZ, &block_size) < 0) {
    fprintf(stderr, "Cannot get block size\n");
    exit(EXIT_FAILURE);
  }
  buffer1 = malloc(block_size);
  if (!buffer1) {
    fprintf(stderr, "Cannot allocate buffer\n");
    close(fd2);
    exit(EXIT_FAILURE);
  }
  zero_blocks = 0;
  copied_blocks = 0;
  filesize = 0;
  i = 0;
  while (1 == 1) {
    int nread = 0;
    int tread = 0;
    while (tread < block_size) {
      nread = read(STDIN_FILENO, buffer1 + tread, block_size - tread);
      tread += nread;
      if (nread == 0)
        break;
    }
    filesize += tread;
    if (tread == 0) {
      break;
    } else {
      i++;
      int n;
      for (n = 0; n < tread; n++) {
        if (buffer1[n])
          break;
      }
      if (n == tread) {
        if (lseek(fd2, tread, SEEK_CUR) < 0) {
          fprintf(stderr, "Seek failed.\n");
          close(fd2);
          exit(EXIT_FAILURE);
        }
        zero_blocks++;
        continue;
      } else {
        if (write(fd2, buffer1, tread) != tread) {
          fprintf(stderr, "Cannot write block %d on %s\n", i, argv[3]);
        } else {
          copied_blocks++;
          continue;
        }
      }
    }
  }
  free(buffer1);
  ftruncate(fd2, filesize);
  close(fd2);
  printf("\n");
  printf("Report:\n");
  printf("%d byte block size\n", block_size);
  printf("  total %08d blocks\n", i);
  printf("  found %08d zero blocks (now sparse)\n", zero_blocks);
  printf(" copied %08d blocks\n", copied_blocks);
  printf("  ------------------------------------\n");
  printf("  Sum:  %08d blocks accounted for.\n", (zero_blocks + copied_blocks));
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     122a <main+0x6a>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
mov    0x2db6(%rip),%rax
or     (%rax),%esi
mov    0x2da5(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   10d0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1290 <main+0xd0>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdc1(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1080 <ioctl@plt>
cmp    $0x0,%rax
jge    12d4 <main+0x114>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%edi
call   10c0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1318 <main+0x158>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
lea    0xd68(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    138e <main+0x1ce>
mov    0x2c52(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
movslq -0x40(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
sub    -0x40(%rbp),%edx
call   10a0 <read@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    1389 <main+0x1c9>
jmp    138e <main+0x1ce>
jmp    1343 <main+0x183>
movslq -0x40(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x40(%rbp)
jne    13a9 <main+0x1e9>
jmp    14b0 <main+0x2f0>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    13ef <main+0x22f>
mov    -0x38(%rbp),%rax
movslq -0x44(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     13dc <main+0x21c>
jmp    13ef <main+0x22f>
jmp    13e1 <main+0x221>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    13b9 <main+0x1f9>
mov    -0x44(%rbp),%eax
cmp    -0x40(%rbp),%eax
jne    1454 <main+0x294>
mov    -0x14(%rbp),%edi
mov    -0x40(%rbp),%esi
mov    0x2bd0(%rip),%rax
mov    (%rax),%edx
call   1070 <lseek@plt>
cmp    $0x0,%rax
jge    1446 <main+0x286>
mov    0x2ba0(%rip),%rax
mov    (%rax),%edi
lea    0xc52(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    0x2b79(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1335 <main+0x175>
mov    -0x14(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%edx
call   1040 <write@plt>
cmp    -0x40(%rbp),%eax
je     1493 <main+0x2d3>
mov    0x2b4d(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rcx
lea    0xc02(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
jmp    14a1 <main+0x2e1>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1335 <main+0x175>
jmp    14a6 <main+0x2e6>
jmp    14ab <main+0x2eb>
jmp    1335 <main+0x175>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1060 <ftruncate@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
lea    0xc29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xbc4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xbbc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xbbf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%esi
lea    0xbc3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%esi
lea    0xbd9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xbe0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%esi
add    -0x24(%rbp),%esi
lea    0xbf4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2a5a(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1239 <main+0x59>
mov    0x2d9b(%rip),%rax
mov    0x2df4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbx),%rdi
mov    0x2ddd(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   10f0 <open@plt>
test   %eax,%eax
jns    1276 <main+0x96>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe05(%rip),%rsi
jmp    1261 <main+0x81>
mov    0x2d80(%rip),%rbp
mov    0x0(%rbp),%edi
mov    (%rbx),%rdx
lea    0xdb3(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
mov    0x0(%rbp),%edi
mov    (%rbx),%rdx
lea    0xdcb(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    %eax,%r13d
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %r13d,%edi
call   10a0 <ioctl@plt>
test   %rax,%rax
jns    12ad <main+0xcd>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
jmp    1268 <main+0x88>
mov    0x4(%rsp),%edi
call   10e0 <malloc@plt>
test   %rax,%rax
jne    12dc <main+0xfc>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
lea    0xd98(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
mov    %r13d,%edi
call   10b0 <close@plt>
jmp    1268 <main+0x88>
mov    %rax,%rbp
mov    %rbx,0x10(%rsp)
mov    0x2cbd(%rip),%r12
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
xor    %r14d,%r14d
xor    %ebx,%ebx
jmp    1332 <main+0x152>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rsp),%rax
mov    0x18(%rax),%rcx
lea    0xd60(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   10d0 <fprintf@plt>
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x4(%rsp),%edx
sub    %r15d,%edx
jle    135f <main+0x17f>
mov    (%r12),%edi
movslq %r15d,%rsi
add    %rbp,%rsi
call   10c0 <read@plt>
add    %eax,%r15d
test   %eax,%eax
jne    1340 <main+0x160>
movslq %r15d,%rax
add    %rax,%r14
test   %eax,%eax
je     13e8 <main+0x208>
add    $0x1,%ebx
test   %r15d,%r15d
jle    13c0 <main+0x1e0>
mov    %r15d,%ecx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
cmpb   $0x0,0x0(%rbp,%rax,1)
jne    13c2 <main+0x1e2>
add    $0x1,%rax
cmp    %rax,%rcx
jne    1380 <main+0x1a0>
mov    0x2c41(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    %r15d,%esi
call   1090 <lseek@plt>
test   %rax,%rax
js     148f <main+0x2af>
addl   $0x1,0xc(%rsp)
jmp    1332 <main+0x152>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
cmp    %r15d,%eax
je     1390 <main+0x1b0>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1060 <write@plt>
cmp    %r15d,%eax
jne    1310 <main+0x130>
addl   $0x1,0x8(%rsp)
jmp    1332 <main+0x152>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r13d,%edi
mov    %r14,%rsi
call   1080 <ftruncate@plt>
mov    %r13d,%edi
call   10b0 <close@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
lea    0xd1b(%rip),%rdi
call   1050 <puts@plt>
mov    0x4(%rsp),%esi
lea    0xc82(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xc88(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xc8d(%rip),%rdi
mov    0xc(%rsp),%ebp
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xca0(%rip),%rdi
mov    0x8(%rsp),%ebx
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xccd(%rip),%rdi
call   1050 <puts@plt>
add    %ebp,%ebx
lea    0xc93(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2b28(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    0x2b2a(%rip),%rax
mov    (%rax),%edi
lea    0xbdc(%rip),%rsi
jmp    12cb <main+0xeb>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [44 x i8] c"%s a fast sparse-aware binary file writer.\0A\00"
@.str.1 = [16 x i8] c"Usage: %s FILE\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [16 x i8] c"Cannot open %s\0A\00"
@FIGETBSZ = global i32 0
@.str.3 = [23 x i8] c"Cannot get block size\0A\00"
@.str.4 = [24 x i8] c"Cannot allocate buffer\0A\00"
@STDIN_FILENO = global i32 0
@SEEK_CUR = global i32 0
@.str.5 = [14 x i8] c"Seek failed.\0A\00"
@.str.6 = [29 x i8] c"Cannot write block %d on %s\0A\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [9 x i8] c"Report:\0A\00"
@.str.9 = [20 x i8] c"%d byte block size\0A\00"
@.str.10 = [21 x i8] c" total %08d blocks\0A\00"
@.str.11 = [39 x i8] c" found %08d zero blocks (now sparse)\0A\00"
@.str.12 = [21 x i8] c" copied %08d blocks\0A\00"
@.str.13 = [40 x i8] c" ------------------------------------\0A\00"
@.str.14 = [36 x i8] c" Sum: %08d blocks accounted for.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 2
br i1 %17, label %18, label %31
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @stderr
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %2
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = load i32, i32* @O_WRONLY
%36 = load i32, i32* @O_CREAT
%37 = or i32 %35, %36
%38 = load i32, i32* @O_TRUNC
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %34, i32 %39, i32 511)
store i32 %40, i32* %6
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %31
%43 = load i32, i32* @stderr
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %31
%51 = load i32, i32* %6
%52 = load i32, i32* @FIGETBSZ
%53 = call i64 @ioctl(i32 %51, i32 %52, i32* %7)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %50
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %50
%61 = load i32, i32* %7
%62 = call i8* @malloc(i32 %61)
store i8* %62, i8** %12
%63 = load i8*, i8** %12
%64 = icmp ne i8* %63, null
br i1 %64, label %72, label %65
65: ; preds = %60
%66 = load i32, i32* @stderr
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* @EXIT_FAILURE
%71 = call i32 @exit(i32 %70)
unreachable
72: ; preds = %60
store i32 0, i32* %9
store i32 0, i32* %10
store i64 0, i64* %11
store i32 0, i32* %8
br label %73
73: ; preds = %72, %139, %156, %161
store i32 0, i32* %13
store i32 0, i32* %14
br label %74
74: ; preds = %94, %73
%75 = load i32, i32* %14
%76 = load i32, i32* %7
%77 = icmp slt i32 %75, %76
br i1 %77, label %78, label %95
78: ; preds = %74
%79 = load i32, i32* @STDIN_FILENO
%80 = load i8*, i8** %12
%81 = load i32, i32* %14
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %80, i64 %82
%84 = load i32, i32* %7
%85 = load i32, i32* %14
%86 = sub nsw i32 %84, %85
%87 = call i32 @read(i32 %79, i8* %83, i32 %86)
store i32 %87, i32* %13
%88 = load i32, i32* %13
%89 = load i32, i32* %14
%90 = add nsw i32 %89, %88
store i32 %90, i32* %14
%91 = load i32, i32* %13
%92 = icmp eq i32 %91, 0
br i1 %92, label %93, label %94
93: ; preds = %78
br label %95
94: ; preds = %78
br label %74
95: ; preds = %93, %74
%96 = load i32, i32* %14
%97 = sext i32 %96 to i64
%98 = load i64, i64* %11
%99 = add nsw i64 %98, %97
store i64 %99, i64* %11
%100 = load i32, i32* %14
%101 = icmp eq i32 %100, 0
br i1 %101, label %102, label %103
102: ; preds = %95
br label %162
103: ; preds = %95
%104 = load i32, i32* %8
%105 = add nsw i32 %104, 1
store i32 %105, i32* %8
store i32 0, i32* %15
br label %106
106: ; preds = %119, %103
%107 = load i32, i32* %15
%108 = load i32, i32* %14
%109 = icmp slt i32 %107, %108
br i1 %109, label %110, label %122
110: ; preds = %106
%111 = load i8*, i8** %12
%112 = load i32, i32* %15
%113 = sext i32 %112 to i64
%114 = getelementptr inbounds i8, i8* %111, i64 %113
%115 = load i8, i8* %114
%116 = icmp ne i8 %115, 0
br i1 %116, label %117, label %118
117: ; preds = %110
br label %122
118: ; preds = %110
br label %119
119: ; preds = %118
%120 = load i32, i32* %15
%121 = add nsw i32 %120, 1
store i32 %121, i32* %15
br label %106
122: ; preds = %117, %106
%123 = load i32, i32* %15
%124 = load i32, i32* %14
%125 = icmp eq i32 %123, %124
br i1 %125, label %126, label %142
126: ; preds = %122
%127 = load i32, i32* %6
%128 = load i32, i32* %14
%129 = load i32, i32* @SEEK_CUR
%130 = call i64 @lseek(i32 %127, i32 %128, i32 %129)
%131 = icmp slt i64 %130, 0
br i1 %131, label %132, label %139
132: ; preds = %126
%133 = load i32, i32* @stderr
%134 = call i32 (i32, i8*, ...) @fprintf(i32 %133, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%135 = load i32, i32* %6
%136 = call i32 @close(i32 %135)
%137 = load i32, i32* @EXIT_FAILURE
%138 = call i32 @exit(i32 %137)
unreachable
139: ; preds = %126
%140 = load i32, i32* %9
%141 = add nsw i32 %140, 1
store i32 %141, i32* %9
br label %73
142: ; preds = %122
%143 = load i32, i32* %6
%144 = load i8*, i8** %12
%145 = load i32, i32* %14
%146 = call i32 @write(i32 %143, i8* %144, i32 %145)
%147 = load i32, i32* %14
%148 = icmp ne i32 %146, %147
br i1 %148, label %149, label %156
149: ; preds = %142
%150 = load i32, i32* @stderr
%151 = load i32, i32* %8
%152 = load i8**, i8*** %5
%153 = getelementptr inbounds i8*, i8** %152, i64 3
%154 = load i8*, i8** %153
%155 = call i32 (i32, i8*, ...) @fprintf(i32 %150, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %151, i8* %154)
br label %159
156: ; preds = %142
%157 = load i32, i32* %10
%158 = add nsw i32 %157, 1
store i32 %158, i32* %10
br label %73
159: ; preds = %149
br label %160
160: ; preds = %159
br label %161
161: ; preds = %160
br label %73
162: ; preds = %102
%163 = load i8*, i8** %12
%164 = call i32 @free(i8* %163)
%165 = load i32, i32* %6
%166 = load i64, i64* %11
%167 = call i32 @ftruncate(i32 %165, i64 %166)
%168 = load i32, i32* %6
%169 = call i32 @close(i32 %168)
%170 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%171 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
%172 = load i32, i32* %7
%173 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 %172)
%174 = load i32, i32* %8
%175 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0), i32 %174)
%176 = load i32, i32* %9
%177 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.11, i64 0, i64 0), i32 %176)
%178 = load i32, i32* %10
%179 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0), i32 %178)
%180 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.13, i64 0, i64 0))
%181 = load i32, i32* %9
%182 = load i32, i32* %10
%183 = add nsw i32 %181, %182
%184 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.14, i64 0, i64 0), i32 %183)
%185 = load i32, i32* @EXIT_SUCCESS
%186 = call i32 @exit(i32 %185)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [44 x i8] c"%s a fast sparse-aware binary file writer.\0A\00"
@.str.1 = [16 x i8] c"Usage: %s FILE\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [16 x i8] c"Cannot open %s\0A\00"
@FIGETBSZ = global i32 0
@.str.3 = [23 x i8] c"Cannot get block size\0A\00"
@.str.4 = [24 x i8] c"Cannot allocate buffer\0A\00"
@STDIN_FILENO = global i32 0
@SEEK_CUR = global i32 0
@.str.5 = [14 x i8] c"Seek failed.\0A\00"
@.str.6 = [29 x i8] c"Cannot write block %d on %s\0A\00"
@.str.9 = [20 x i8] c"%d byte block size\0A\00"
@.str.10 = [21 x i8] c" total %08d blocks\0A\00"
@.str.11 = [39 x i8] c" found %08d zero blocks (now sparse)\0A\00"
@.str.12 = [21 x i8] c" copied %08d blocks\0A\00"
@.str.14 = [36 x i8] c" Sum: %08d blocks accounted for.\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [8 x i8] c"Report:\00"
@str.15 = [39 x i8] c" ------------------------------------\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %15, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %1
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = tail call i32 @open(i8* %17, i32 %22, i32 511)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %15
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %16
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %15
%32 = load i32, i32* @FIGETBSZ
%33 = call i64 @ioctl(i32 %23, i32 %32, i32* %3)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %31
%41 = load i32, i32* %3
%42 = call i8* @malloc(i32 %41)
%43 = icmp eq i8* %42, null
br i1 %43, label %46, label %44
44: ; preds = %40
%45 = getelementptr inbounds i8*, i8** %1, i64 3
br label %102
46: ; preds = %40
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%49 = call i32 @close(i32 %23)
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %107, %114
%53 = phi i64 [ %70, %114 ], [ %109, %107 ]
%54 = phi i32 [ %73, %114 ], [ %110, %107 ]
br label %55
55: ; preds = %59, %52
%56 = phi i32 [ 0, %52 ], [ %65, %59 ]
%57 = load i32, i32* %3
%58 = icmp sgt i32 %57, %56
br i1 %58, label %59, label %67
59: ; preds = %55
%60 = load i32, i32* @STDIN_FILENO
%61 = sext i32 %56 to i64
%62 = getelementptr inbounds i8, i8* %42, i64 %61
%63 = sub nsw i32 %57, %56
%64 = call i32 @read(i32 %60, i8* %62, i32 %63)
%65 = add nsw i32 %64, %56
%66 = icmp eq i32 %64, 0
br i1 %66, label %67, label %55
67: ; preds = %59, %55
%68 = phi i32 [ %65, %59 ], [ %56, %55 ]
%69 = sext i32 %68 to i64
%70 = add nsw i64 %53, %69
%71 = icmp eq i32 %68, 0
br i1 %71, label %120, label %72
72: ; preds = %67
%73 = add nuw nsw i32 %54, 1
%74 = icmp sgt i32 %68, 0
br i1 %74, label %75, label %87
75: ; preds = %72
%76 = zext i32 %68 to i64
br label %77
77: ; preds = %75, %82
%78 = phi i64 [ 0, %75 ], [ %83, %82 ]
%79 = getelementptr inbounds i8, i8* %42, i64 %78
%80 = load i8, i8* %79
%81 = icmp eq i8 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %77
%83 = add nuw nsw i64 %78, 1
%84 = icmp eq i64 %83, %76
br i1 %84, label %90, label %77
85: ; preds = %77
%86 = trunc i64 %78 to i32
br label %87
87: ; preds = %85, %72
%88 = phi i32 [ 0, %72 ], [ %86, %85 ]
%89 = icmp eq i32 %88, %68
br i1 %89, label %90, label %111
90: ; preds = %87, %82
%91 = load i32, i32* @SEEK_CUR
%92 = call i64 @lseek(i32 %23, i32 %68, i32 %91)
%93 = icmp slt i64 %92, 0
br i1 %93, label %94, label %100
94: ; preds = %90
%95 = load i32, i32* @stderr
%96 = call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%97 = call i32 @close(i32 %23)
%98 = load i32, i32* @EXIT_FAILURE
%99 = call i32 @exit(i32 %98)
unreachable
100: ; preds = %90
%101 = add nuw nsw i32 %103, 1
br label %102
102: ; preds = %100, %44
%103 = phi i32 [ %101, %100 ], [ 0, %44 ]
%104 = phi i32 [ %108, %100 ], [ 0, %44 ]
%105 = phi i64 [ %70, %100 ], [ 0, %44 ]
%106 = phi i32 [ %73, %100 ], [ 0, %44 ]
br label %107
107: ; preds = %102, %118
%108 = phi i32 [ %104, %102 ], [ %119, %118 ]
%109 = phi i64 [ %105, %102 ], [ %70, %118 ]
%110 = phi i32 [ %106, %102 ], [ %73, %118 ]
br label %52
111: ; preds = %87
%112 = call i32 @write(i32 %23, i8* %42, i32 %68)
%113 = icmp eq i32 %112, %68
br i1 %113, label %118, label %114
114: ; preds = %111
%115 = load i32, i32* @stderr
%116 = load i8*, i8** %45
%117 = call i32 (i32, i8*, ...) @fprintf(i32 %115, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %73, i8* %116)
br label %52
118: ; preds = %111
%119 = add nsw i32 %108, 1
br label %107
120: ; preds = %67
%121 = call i32 @free(i8* %42)
%122 = call i32 @ftruncate(i32 %23, i64 %70)
%123 = call i32 @close(i32 %23)
%124 = call i32 @putchar(i32 10)
%125 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%126 = load i32, i32* %3
%127 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 %126)
%128 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0), i32 %54)
%129 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.11, i64 0, i64 0), i32 %103)
%130 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0), i32 %108)
%131 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.15, i64 0, i64 0))
%132 = add nsw i32 %108, %103
%133 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.14, i64 0, i64 0), i32 %132)
%134 = load i32, i32* @EXIT_SUCCESS
%135 = call i32 @exit(i32 %134)
unreachable
}