#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*/ uint8_t;
typedef scalar_t__ uint32_t;
typedef int /*<<< orphan*/ out;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ btol(int /*<<< orphan*/ *);
int calculate_crc(int /*<<< orphan*/ *, scalar_t__ *);
int /*<<< orphan*/ *calloc(size_t, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
long get_file_length_and_rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ ltob(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *);
scalar_t__ safe_fread(int /*<<< orphan*/ *, long, int /*<<< orphan*/ *);
scalar_t__ safe_fwrite(int /*<<< orphan*/ *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, const char *argv[]) {
  size_t min_sz = 2 * 1024 * 1024;
  uint8_t *buffer, out[8];
  uint32_t crc[2], c_crc[2];
  long fsz;
  int status;
  FILE *f;
  if (argc != 3) {
    printf("Usage: %s <header> <filename>\n", argv[0]);
    return EXIT_SUCCESS;
  }
  if ((buffer = calloc(min_sz, 1)) == NULL) {
    perror("calloc");
    return EXIT_FAILURE;
  }
  if ((f = fopen(argv[1], "rb")) == NULL) {
    perror("fopen");
    free(buffer);
    return EXIT_FAILURE;
  }
  if (safe_fread(buffer, 0x00001000, f)) {
    fprintf(stderr, "Unable to read contents of: %s.\n", argv[1]);
    free(buffer);
    fclose(f);
    return EXIT_FAILURE;
  }
  fclose(f);
  if ((f = fopen(argv[2], "rb")) == NULL) {
    perror("fopen");
    free(buffer);
    return EXIT_FAILURE;
  }
  if ((fsz = get_file_length_and_rewind(f)) < 0) {
    fprintf(stderr, "Unable to determine file size of: %s.\n", argv[2]);
    free(buffer);
    fclose(f);
    return EXIT_FAILURE;
  }
  if (safe_fread(buffer + 0x00001000,
                 ((0x00100000 - 0x00001000) > fsz) ? fsz
                                                   : 0x00100000 - 0x00001000,
                 f)) {
    fprintf(stderr, "Unable to read contents of: %s.\n", argv[2]);
    free(buffer);
    fclose(f);
    return EXIT_FAILURE;
  }
  c_crc[0] = btol(buffer + 0x00000010);
  c_crc[1] = btol(buffer + 0x00000010 + 4);
  status = calculate_crc(buffer, crc);
  fclose(f);
  if (status) {
    free(buffer);
    return EXIT_FAILURE;
  }
  if ((c_crc[0] == crc[0] && c_crc[1] == crc[1]) && ((size_t)fsz >= min_sz))
    return EXIT_SUCCESS;
  ltob(crc[0], out);
  ltob(crc[1], out + 4);
  if ((f = fopen(argv[2], "rb+")) == NULL) {
    perror("fopen");
    free(buffer);
    return EXIT_FAILURE;
  }
  size_t wr_len = min_sz > 0x00100000 ? 0x00100000 : min_sz;
  if (safe_fwrite(buffer, wr_len, f)) {
    fprintf(stderr, "Unable to write contents to: %s.\n", argv[2]);
    free(buffer);
    fclose(f);
    return EXIT_FAILURE;
  }
  free(buffer);
  if (fseek(f, 0x00000010, SEEK_SET)) {
    perror("fseek");
    fclose(f);
    return EXIT_FAILURE;
  }
  if (safe_fwrite(out, sizeof(out), f)) {
    fprintf(stderr, "Unable to write CRC to: %s.\n", argv[2]);
    fclose(f);
    return EXIT_FAILURE;
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x200000,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     122f <main+0x4f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xde7(%rip),%rdi
call   1070 <printf@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1268 <main+0x88>
lea    0xdcd(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdaf(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmp    $0x0,%rax
jne    12b0 <main+0xd0>
lea    0xd98(%rip),%rdi
call   10f0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
mov    -0x20(%rbp),%rdi
mov    -0x78(%rbp),%rdx
mov    $0x1000,%esi
call   1100 <safe_fread@plt>
cmp    $0x0,%rax
je     130c <main+0x12c>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd4b(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x78(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
mov    -0x78(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xd02(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmp    $0x0,%rax
jne    135d <main+0x17d>
lea    0xceb(%rip),%rdi
call   10f0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2c7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
mov    -0x78(%rbp),%rdi
call   1040 <get_file_length_and_rewind@plt>
mov    %rax,-0x68(%rbp)
cmp    $0x0,%rax
jge    13b4 <main+0x1d4>
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xcc4(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x78(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2c26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
mov    -0x20(%rbp),%rax
add    $0x4000,%rax
mov    %rax,-0x88(%rbp)
mov    $0xff000,%eax
cmp    -0x68(%rbp),%rax
jle    13e4 <main+0x204>
mov    -0x68(%rbp),%rax
mov    %rax,-0x90(%rbp)
jmp    13f5 <main+0x215>
mov    $0xff000,%eax
mov    %rax,-0x90(%rbp)
jmp    13f5 <main+0x215>
mov    -0x88(%rbp),%rdi
mov    -0x90(%rbp),%rsi
mov    -0x78(%rbp),%rdx
call   1100 <safe_fread@plt>
cmp    $0x0,%rax
je     1456 <main+0x276>
mov    0x2bc3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xc01(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x78(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2b84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
mov    -0x20(%rbp),%rdi
add    $0x40,%rdi
call   1090 <btol@plt>
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x40,%rdi
add    $0x10,%rdi
call   1090 <btol@plt>
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1050 <calculate_crc@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x78(%rbp),%rdi
call   1060 <fclose@plt>
cmpl   $0x0,-0x6c(%rbp)
je     14b9 <main+0x2d9>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2b21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
mov    -0x60(%rbp),%rax
cmp    -0x50(%rbp),%rax
jne    14f4 <main+0x314>
mov    -0x58(%rbp),%rax
cmp    -0x48(%rbp),%rax
jne    14f4 <main+0x314>
mov    -0x68(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     14f4 <main+0x314>
mov    0x2ade(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
mov    -0x50(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   10b0 <ltob@plt>
mov    -0x48(%rbp),%rdi
lea    -0x40(%rbp),%rsi
add    $0x10,%rsi
call   10b0 <ltob@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xb56(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmp    $0x0,%rax
jne    155a <main+0x37a>
lea    0xaee(%rip),%rdi
call   10f0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2a80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
cmpq   $0x100000,-0x18(%rbp)
jbe    1579 <main+0x399>
mov    $0x100000,%eax
mov    %rax,-0x98(%rbp)
jmp    1584 <main+0x3a4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x80(%rbp),%rax
mov    %eax,%esi
mov    -0x78(%rbp),%rdx
call   10d0 <safe_fwrite@plt>
cmp    $0x0,%rax
je     15ec <main+0x40c>
mov    0x2a2d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xab7(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x78(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x29ee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x78(%rbp),%rdi
mov    0x29d8(%rip),%rax
mov    (%rax),%edx
mov    $0x10,%esi
call   10c0 <fseek@plt>
cmp    $0x0,%rax
je     163c <main+0x45c>
lea    0xa80(%rip),%rdi
call   10f0 <perror@plt>
mov    -0x78(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x299e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
lea    -0x40(%rbp),%rdi
mov    -0x78(%rbp),%rdx
mov    $0x20,%esi
call   10d0 <safe_fwrite@plt>
cmp    $0x0,%rax
je     168f <main+0x4af>
mov    0x2981(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xa33(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x78(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x294b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    169f <main+0x4bf>
mov    -0x78(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r13
cmp    $0x3,%edi
jne    126b <main+0x8b>
mov    $0x200000,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
test   %rax,%rax
je     1406 <main+0x226>
mov    %rax,%r14
mov    0x8(%r13),%rdi
lea    0xe0a(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     13ed <main+0x20d>
mov    %rax,%rbx
mov    $0x1000,%esi
mov    %r14,%rdi
mov    %rax,%rdx
call   1100 <safe_fread@plt>
test   %rax,%rax
je     1287 <main+0xa7>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r13),%rdx
lea    0xdd9(%rip),%rsi
call   10a0 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
jmp    1318 <main+0x138>
mov    0x0(%r13),%rsi
lea    0xd8a(%rip),%rdi
call   1070 <printf@plt>
mov    0x2d46(%rip),%rax
jmp    1324 <main+0x144>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    0x10(%r13),%rdi
lea    0xd8c(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     13ed <main+0x20d>
mov    %rax,%r15
mov    %rax,%rdi
call   1040 <get_file_length_and_rewind@plt>
test   %rax,%rax
js     12f4 <main+0x114>
mov    %rax,%r12
lea    0x4000(%r14),%rdi
cmp    $0xff000,%rax
mov    $0xff000,%esi
cmovb  %rax,%rsi
mov    %r15,%rdx
call   1100 <safe_fread@plt>
test   %rax,%rax
je     1335 <main+0x155>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
mov    0x10(%r13),%rdx
lea    0xd3d(%rip),%rsi
jmp    1308 <main+0x128>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
mov    0x10(%r13),%rdx
lea    0xd48(%rip),%rsi
call   10a0 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    0x2cac(%rip),%rax
mov    (%rax),%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x40(%r14),%rdi
call   1090 <btol@plt>
mov    %rax,0x18(%rsp)
mov    %r14,%rdi
add    $0x50,%rdi
call   1090 <btol@plt>
mov    %rax,%rbp
mov    %rsp,%rsi
mov    %r14,%rdi
call   1050 <calculate_crc@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1060 <fclose@plt>
test   %ebx,%ebx
jne    13f9 <main+0x219>
mov    (%rsp),%rdi
cmp    %rdi,0x18(%rsp)
jne    138e <main+0x1ae>
cmp    0x8(%rsp),%rbp
jne    138e <main+0x1ae>
cmp    $0x200000,%r12
jae    127b <main+0x9b>
lea    0x20(%rsp),%rsi
call   10b0 <ltob@plt>
mov    0x8(%rsp),%rdi
lea    0x30(%rsp),%rsi
call   10b0 <ltob@plt>
mov    0x10(%r13),%rdi
lea    0xcc5(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     13ed <main+0x20d>
mov    %rax,%r15
mov    %r14,%rdi
mov    $0x100000,%esi
mov    %rax,%rdx
call   10d0 <safe_fwrite@plt>
test   %rax,%rax
je     1417 <main+0x237>
mov    0x2c05(%rip),%rax
mov    (%rax),%edi
mov    0x10(%r13),%rdx
lea    0xc93(%rip),%rsi
jmp    1308 <main+0x128>
lea    0xc35(%rip),%rdi
call   10f0 <perror@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    131d <main+0x13d>
lea    0xc12(%rip),%rdi
call   10f0 <perror@plt>
jmp    131d <main+0x13d>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x2bb2(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    $0x10,%esi
call   10c0 <fseek@plt>
test   %rax,%rax
jne    147e <main+0x29e>
lea    0x20(%rsp),%rdi
mov    $0x20,%esi
mov    %r15,%rdx
call   10d0 <safe_fwrite@plt>
test   %rax,%rax
je     146f <main+0x28f>
mov    0x2b88(%rip),%rax
mov    (%rax),%edi
mov    0x10(%r13),%rdx
lea    0xc3e(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    1315 <main+0x135>
mov    %r15,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
jmp    1326 <main+0x146>
lea    0xc18(%rip),%rdi
call   10f0 <perror@plt>
jmp    1315 <main+0x135>

LLVM IR (-O0):
@.str = [31 x i8] c"Usage: %s <header> <filename>\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [7 x i8] c"calloc\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.4 = [33 x i8] c"Unable to read contents of: %s.\0A\00"
@.str.5 = [39 x i8] c"Unable to determine file size of: %s.\0A\00"
@.str.6 = [4 x i8] c"rb+\00"
@.str.7 = [34 x i8] c"Unable to write contents to: %s.\0A\00"
@SEEK_SET = global i32 0
@.str.8 = [6 x i8] c"fseek\00"
@.str.9 = [29 x i8] c"Unable to write CRC to: %s.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32*
%8 = alloca [8 x i32]
%9 = alloca [2 x i64]
%10 = alloca [2 x i64]
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 2097152, i64* %6
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 3
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_SUCCESS
store i32 %22, i32* %3
br label %222
23: ; preds = %2
%24 = load i64, i64* %6
%25 = call i32* @calloc(i64 %24, i32 1)
store i32* %25, i32** %7
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
store i32 %29, i32* %3
br label %222
30: ; preds = %23
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %34, i32** %13
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %41
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32*, i32** %7
%39 = call i32 @free(i32* %38)
%40 = load i32, i32* @EXIT_FAILURE
store i32 %40, i32* %3
br label %222
41: ; preds = %30
%42 = load i32*, i32** %7
%43 = load i32*, i32** %13
%44 = call i64 @safe_fread(i32* %42, i64 4096, i32* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %57
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %50)
%52 = load i32*, i32** %7
%53 = call i32 @free(i32* %52)
%54 = load i32*, i32** %13
%55 = call i32 @fclose(i32* %54)
%56 = load i32, i32* @EXIT_FAILURE
store i32 %56, i32* %3
br label %222
57: ; preds = %41
%58 = load i32*, i32** %13
%59 = call i32 @fclose(i32* %58)
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 2
%62 = load i8*, i8** %61
%63 = call i32* @fopen(i8* %62, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %63, i32** %13
%64 = icmp eq i32* %63, null
br i1 %64, label %65, label %70
65: ; preds = %57
%66 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%67 = load i32*, i32** %7
%68 = call i32 @free(i32* %67)
%69 = load i32, i32* @EXIT_FAILURE
store i32 %69, i32* %3
br label %222
70: ; preds = %57
%71 = load i32*, i32** %13
%72 = call i64 @get_file_length_and_rewind(i32* %71)
store i64 %72, i64* %11
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %85
74: ; preds = %70
%75 = load i32, i32* @stderr
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i64 2
%78 = load i8*, i8** %77
%79 = call i32 @fprintf(i32 %75, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i8* %78)
%80 = load i32*, i32** %7
%81 = call i32 @free(i32* %80)
%82 = load i32*, i32** %13
%83 = call i32 @fclose(i32* %82)
%84 = load i32, i32* @EXIT_FAILURE
store i32 %84, i32* %3
br label %222
85: ; preds = %70
%86 = load i32*, i32** %7
%87 = getelementptr inbounds i32, i32* %86, i64 4096
%88 = load i64, i64* %11
%89 = icmp sgt i64 1044480, %88
br i1 %89, label %90, label %92
90: ; preds = %85
%91 = load i64, i64* %11
br label %93
92: ; preds = %85
br label %93
93: ; preds = %92, %90
%94 = phi i64 [ %91, %90 ], [ 1044480, %92 ]
%95 = load i32*, i32** %13
%96 = call i64 @safe_fread(i32* %87, i64 %94, i32* %95)
%97 = icmp ne i64 %96, 0
br i1 %97, label %98, label %109
98: ; preds = %93
%99 = load i32, i32* @stderr
%100 = load i8**, i8*** %5
%101 = getelementptr inbounds i8*, i8** %100, i64 2
%102 = load i8*, i8** %101
%103 = call i32 @fprintf(i32 %99, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %102)
%104 = load i32*, i32** %7
%105 = call i32 @free(i32* %104)
%106 = load i32*, i32** %13
%107 = call i32 @fclose(i32* %106)
%108 = load i32, i32* @EXIT_FAILURE
store i32 %108, i32* %3
br label %222
109: ; preds = %93
%110 = load i32*, i32** %7
%111 = getelementptr inbounds i32, i32* %110, i64 16
%112 = call i64 @btol(i32* %111)
%113 = getelementptr inbounds [2 x i64], [2 x i64]* %10, i64 0, i64 0
store i64 %112, i64* %113
%114 = load i32*, i32** %7
%115 = getelementptr inbounds i32, i32* %114, i64 16
%116 = getelementptr inbounds i32, i32* %115, i64 4
%117 = call i64 @btol(i32* %116)
%118 = getelementptr inbounds [2 x i64], [2 x i64]* %10, i64 0, i64 1
store i64 %117, i64* %118
%119 = load i32*, i32** %7
%120 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 0
%121 = call i32 @calculate_crc(i32* %119, i64* %120)
store i32 %121, i32* %12
%122 = load i32*, i32** %13
%123 = call i32 @fclose(i32* %122)
%124 = load i32, i32* %12
%125 = icmp ne i32 %124, 0
br i1 %125, label %126, label %130
126: ; preds = %109
%127 = load i32*, i32** %7
%128 = call i32 @free(i32* %127)
%129 = load i32, i32* @EXIT_FAILURE
store i32 %129, i32* %3
br label %222
130: ; preds = %109
%131 = getelementptr inbounds [2 x i64], [2 x i64]* %10, i64 0, i64 0
%132 = load i64, i64* %131
%133 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 0
%134 = load i64, i64* %133
%135 = icmp eq i64 %132, %134
br i1 %135, label %136, label %148
136: ; preds = %130
%137 = getelementptr inbounds [2 x i64], [2 x i64]* %10, i64 0, i64 1
%138 = load i64, i64* %137
%139 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 1
%140 = load i64, i64* %139
%141 = icmp eq i64 %138, %140
br i1 %141, label %142, label %148
142: ; preds = %136
%143 = load i64, i64* %11
%144 = load i64, i64* %6
%145 = icmp uge i64 %143, %144
br i1 %145, label %146, label %148
146: ; preds = %142
%147 = load i32, i32* @EXIT_SUCCESS
store i32 %147, i32* %3
br label %222
148: ; preds = %142, %136, %130
%149 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 0
%150 = load i64, i64* %149
%151 = getelementptr inbounds [8 x i32], [8 x i32]* %8, i64 0, i64 0
%152 = call i32 @ltob(i64 %150, i32* %151)
%153 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 1
%154 = load i64, i64* %153
%155 = getelementptr inbounds [8 x i32], [8 x i32]* %8, i64 0, i64 0
%156 = getelementptr inbounds i32, i32* %155, i64 4
%157 = call i32 @ltob(i64 %154, i32* %156)
%158 = load i8**, i8*** %5
%159 = getelementptr inbounds i8*, i8** %158, i64 2
%160 = load i8*, i8** %159
%161 = call i32* @fopen(i8* %160, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
store i32* %161, i32** %13
%162 = icmp eq i32* %161, null
br i1 %162, label %163, label %168
163: ; preds = %148
%164 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%165 = load i32*, i32** %7
%166 = call i32 @free(i32* %165)
%167 = load i32, i32* @EXIT_FAILURE
store i32 %167, i32* %3
br label %222
168: ; preds = %148
%169 = load i64, i64* %6
%170 = icmp ugt i64 %169, 1048576
br i1 %170, label %171, label %172
171: ; preds = %168
br label %174
172: ; preds = %168
%173 = load i64, i64* %6
br label %174
174: ; preds = %172, %171
%175 = phi i64 [ 1048576, %171 ], [ %173, %172 ]
store i64 %175, i64* %14
%176 = load i32*, i32** %7
%177 = load i64, i64* %14
%178 = trunc i64 %177 to i32
%179 = load i32*, i32** %13
%180 = call i64 @safe_fwrite(i32* %176, i32 %178, i32* %179)
%181 = icmp ne i64 %180, 0
br i1 %181, label %182, label %193
182: ; preds = %174
%183 = load i32, i32* @stderr
%184 = load i8**, i8*** %5
%185 = getelementptr inbounds i8*, i8** %184, i64 2
%186 = load i8*, i8** %185
%187 = call i32 @fprintf(i32 %183, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i8* %186)
%188 = load i32*, i32** %7
%189 = call i32 @free(i32* %188)
%190 = load i32*, i32** %13
%191 = call i32 @fclose(i32* %190)
%192 = load i32, i32* @EXIT_FAILURE
store i32 %192, i32* %3
br label %222
193: ; preds = %174
%194 = load i32*, i32** %7
%195 = call i32 @free(i32* %194)
%196 = load i32*, i32** %13
%197 = load i32, i32* @SEEK_SET
%198 = call i64 @fseek(i32* %196, i32 16, i32 %197)
%199 = icmp ne i64 %198, 0
br i1 %199, label %200, label %205
200: ; preds = %193
%201 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%202 = load i32*, i32** %13
%203 = call i32 @fclose(i32* %202)
%204 = load i32, i32* @EXIT_FAILURE
store i32 %204, i32* %3
br label %222
205: ; preds = %193
%206 = getelementptr inbounds [8 x i32], [8 x i32]* %8, i64 0, i64 0
%207 = load i32*, i32** %13
%208 = call i64 @safe_fwrite(i32* %206, i32 32, i32* %207)
%209 = icmp ne i64 %208, 0
br i1 %209, label %210, label %219
210: ; preds = %205
%211 = load i32, i32* @stderr
%212 = load i8**, i8*** %5
%213 = getelementptr inbounds i8*, i8** %212, i64 2
%214 = load i8*, i8** %213
%215 = call i32 @fprintf(i32 %211, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0), i8* %214)
%216 = load i32*, i32** %13
%217 = call i32 @fclose(i32* %216)
%218 = load i32, i32* @EXIT_FAILURE
store i32 %218, i32* %3
br label %222
219: ; preds = %205
%220 = load i32*, i32** %13
%221 = call i32 @fclose(i32* %220)
store i32 0, i32* %3
br label %222
222: ; preds = %219, %210, %200, %182, %163, %146, %126, %98, %74, %65, %46, %36, %27, %17
%223 = load i32, i32* %3
ret i32 %223
}

LLVM IR (-O2):
@.str = [31 x i8] c"Usage: %s <header> <filename>\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [7 x i8] c"calloc\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.4 = [33 x i8] c"Unable to read contents of: %s.\0A\00"
@.str.5 = [39 x i8] c"Unable to determine file size of: %s.\0A\00"
@.str.6 = [4 x i8] c"rb+\00"
@.str.7 = [34 x i8] c"Unable to write contents to: %s.\0A\00"
@SEEK_SET = global i32 0
@.str.8 = [6 x i8] c"fseek\00"
@.str.9 = [29 x i8] c"Unable to write CRC to: %s.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8 x i32]
%4 = alloca [2 x i64]
%5 = bitcast [8 x i32]* %3 to i8*
%6 = bitcast [2 x i64]* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_SUCCESS
br label %136
12: ; preds = %2
%13 = tail call i32* @calloc(i64 2097152, i32 1)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
br label %136
18: ; preds = %12
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @free(i32* %13)
%26 = load i32, i32* @EXIT_FAILURE
br label %136
27: ; preds = %18
%28 = tail call i64 @safe_fread(i32* %13, i64 4096, i32* %21)
%29 = icmp eq i64 %28, 0
br i1 %29, label %37, label %30
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %19
%33 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = tail call i32 @free(i32* %13)
%35 = tail call i32 @fclose(i32* %21)
%36 = load i32, i32* @EXIT_FAILURE
br label %136
37: ; preds = %27
%38 = tail call i32 @fclose(i32* %21)
%39 = getelementptr inbounds i8*, i8** %1, i64 2
%40 = load i8*, i8** %39
%41 = tail call i32* @fopen(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %47
43: ; preds = %37
%44 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%45 = tail call i32 @free(i32* %13)
%46 = load i32, i32* @EXIT_FAILURE
br label %136
47: ; preds = %37
%48 = tail call i64 @get_file_length_and_rewind(i32* %41)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %57
50: ; preds = %47
%51 = load i32, i32* @stderr
%52 = load i8*, i8** %39
%53 = tail call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i8* %52)
%54 = tail call i32 @free(i32* %13)
%55 = tail call i32 @fclose(i32* %41)
%56 = load i32, i32* @EXIT_FAILURE
br label %136
57: ; preds = %47
%58 = getelementptr inbounds i32, i32* %13, i64 4096
%59 = icmp ult i64 %48, 1044480
%60 = select i1 %59, i64 %48, i64 1044480
%61 = tail call i64 @safe_fread(i32* %58, i64 %60, i32* %41)
%62 = icmp eq i64 %61, 0
br i1 %62, label %70, label %63
63: ; preds = %57
%64 = load i32, i32* @stderr
%65 = load i8*, i8** %39
%66 = tail call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %65)
%67 = tail call i32 @free(i32* %13)
%68 = tail call i32 @fclose(i32* %41)
%69 = load i32, i32* @EXIT_FAILURE
br label %136
70: ; preds = %57
%71 = getelementptr inbounds i32, i32* %13, i64 16
%72 = tail call i64 @btol(i32* %71)
%73 = getelementptr inbounds i32, i32* %13, i64 20
%74 = tail call i64 @btol(i32* %73)
%75 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 0
%76 = call i32 @calculate_crc(i32* %13, i64* %75)
%77 = call i32 @fclose(i32* %41)
%78 = icmp eq i32 %76, 0
br i1 %78, label %82, label %79
79: ; preds = %70
%80 = call i32 @free(i32* %13)
%81 = load i32, i32* @EXIT_FAILURE
br label %136
82: ; preds = %70
%83 = load i64, i64* %75
%84 = icmp eq i64 %72, %83
%85 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 1
%86 = load i64, i64* %85
%87 = icmp eq i64 %74, %86
%88 = select i1 %84, i1 %87, i1 false
%89 = icmp ugt i64 %48, 2097151
%90 = select i1 %88, i1 %89, i1 false
br i1 %90, label %91, label %93
91: ; preds = %82
%92 = load i32, i32* @EXIT_SUCCESS
br label %136
93: ; preds = %82
%94 = getelementptr inbounds [8 x i32], [8 x i32]* %3, i64 0, i64 0
%95 = call i32 @ltob(i64 %83, i32* %94)
%96 = load i64, i64* %85
%97 = getelementptr inbounds [8 x i32], [8 x i32]* %3, i64 0, i64 4
%98 = call i32 @ltob(i64 %96, i32* %97)
%99 = load i8*, i8** %39
%100 = call i32* @fopen(i8* %99, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%101 = icmp eq i32* %100, null
br i1 %101, label %102, label %106
102: ; preds = %93
%103 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%104 = call i32 @free(i32* %13)
%105 = load i32, i32* @EXIT_FAILURE
br label %136
106: ; preds = %93
%107 = call i64 @safe_fwrite(i32* %13, i32 1048576, i32* %100)
%108 = icmp eq i64 %107, 0
br i1 %108, label %116, label %109
109: ; preds = %106
%110 = load i32, i32* @stderr
%111 = load i8*, i8** %39
%112 = call i32 @fprintf(i32 %110, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i8* %111)
%113 = call i32 @free(i32* %13)
%114 = call i32 @fclose(i32* %100)
%115 = load i32, i32* @EXIT_FAILURE
br label %136
116: ; preds = %106
%117 = call i32 @free(i32* %13)
%118 = load i32, i32* @SEEK_SET
%119 = call i64 @fseek(i32* %100, i32 16, i32 %118)
%120 = icmp eq i64 %119, 0
br i1 %120, label %125, label %121
121: ; preds = %116
%122 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%123 = call i32 @fclose(i32* %100)
%124 = load i32, i32* @EXIT_FAILURE
br label %136
125: ; preds = %116
%126 = call i64 @safe_fwrite(i32* %94, i32 32, i32* %100)
%127 = icmp eq i64 %126, 0
br i1 %127, label %134, label %128
128: ; preds = %125
%129 = load i32, i32* @stderr
%130 = load i8*, i8** %39
%131 = call i32 @fprintf(i32 %129, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0), i8* %130)
%132 = call i32 @fclose(i32* %100)
%133 = load i32, i32* @EXIT_FAILURE
br label %136
134: ; preds = %125
%135 = call i32 @fclose(i32* %100)
br label %136
136: ; preds = %109, %121, %128, %134, %102, %91, %79, %63, %50, %43, %30, %23, %15, %8
%137 = phi i32 [ %11, %8 ], [ %17, %15 ], [ %26, %23 ], [ %36, %30 ], [ %46, %43 ], [ %56, %50 ], [ %69, %63 ], [ %81, %79 ], [ %92, %91 ], [ %105, %102 ], [ %115, %109 ], [ %124, %121 ], [ %133, %128 ], [ 0, %134 ]
ret i32 %137
}