#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*/ header;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
unsigned int fread(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(unsigned int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int, unsigned int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  FILE *fp;
  char header[16];
  char *buf;
  unsigned int size, checksum;
  int i;
  if (argc != 2) {
    printf("Usage: mkv210 <file>\n");
    return -1;
  }
  fp = fopen(argv[1], "r+b");
  if (fp == NULL) {
    printf("Can not open file '%s'\n", argv[1]);
    return -1;
  }
  fseek(fp, 0L, SEEK_END);
  if (ftell(fp) <= 16) {
    printf("The file '%s' is too small\n", argv[1]);
    fclose(fp);
    return -1;
  }
  fseek(fp, 0L, SEEK_SET);
  memset(header, 0, sizeof(header));
  if (fread(header, 1, sizeof(header), fp) != sizeof(header)) {
    printf("Read file header error\n", argv[1]);
    fclose(fp);
    return -1;
  }
  size = (header[3] << 24) | (header[2] << 16) | (header[1] << 8) |
         (header[0] << 0);
  if (size <= 0 || size > 0x4000) {
    printf("Not a valid image, header error\n", argv[1]);
    fclose(fp);
    return -1;
  }
  buf = (char *)malloc(size);
  if (buf == NULL) {
    printf("Can not malloc buffer\n", argv[1]);
    fclose(fp);
    return -1;
  }
  memset(buf, 0x00, size);
  memcpy(buf, header, sizeof(header));
  fseek(fp, 0L, SEEK_SET);
  if (fread(buf, 1, size, fp) != size) {
    fseek(fp, 0L, SEEK_SET);
    if (fwrite(buf, 1, size, fp) != size) {
      printf("Write body file error\n");
      free(buf);
      fclose(fp);
      return -1;
    }
  }
  for (i = 16, checksum = 0; i < size; i++) {
    checksum += buf[i] & 0xff;
  }
  header[11] = (checksum >> 24) & 0xff;
  header[10] = (checksum >> 16) & 0xff;
  header[9] = (checksum >> 8) & 0xff;
  header[8] = (checksum >> 0) & 0xff;
  fseek(fp, 0L, SEEK_SET);
  if (fwrite(header, 1, sizeof(header), fp) != sizeof(header)) {
    printf("Write file header error\n");
    free(buf);
    fclose(fp);
    return -1;
  }
  free(buf);
  fclose(fp);
  printf("the checksum is 0x%08x for %ld bytes [0x%08x ~ 0x%08x]\n", checksum,
         (size - sizeof(header)), (unsigned int)sizeof(header), size);
  return 0;
}


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     11ea <main+0x3a>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1570 <main+0x3c0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe1d(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    122f <main+0x7f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1570 <main+0x3c0>
mov    -0x18(%rbp),%rdi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
cmp    $0x10,%eax
jg     1282 <main+0xd2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1570 <main+0x3c0>
mov    -0x18(%rbp),%rdi
mov    0x2d4b(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1040 <fread@plt>
mov    %eax,%eax
cmp    $0x10,%rax
je     12f6 <main+0x146>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1570 <main+0x3c0>
movsbl -0x2d(%rbp),%eax
shl    $0x18,%eax
movsbl -0x2e(%rbp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
movsbl -0x2f(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movsbl -0x30(%rbp),%ecx
shl    $0x0,%ecx
or     %ecx,%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jbe    1332 <main+0x182>
cmpl   $0x4000,-0x3c(%rbp)
jbe    135d <main+0x1ad>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd25(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1570 <main+0x3c0>
mov    -0x3c(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    139f <main+0x1ef>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1570 <main+0x3c0>
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x38(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1090 <memcpy@plt>
mov    -0x18(%rbp),%rdi
mov    0x2c0e(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x3c(%rbp),%eax
je     1458 <main+0x2a8>
mov    -0x18(%rbp),%rdi
mov    0x2bda(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
cmp    -0x3c(%rbp),%eax
je     1453 <main+0x2a3>
lea    0xc70(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1570 <main+0x3c0>
jmp    1458 <main+0x2a8>
movl   $0x10,-0x44(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jae    1497 <main+0x2e7>
mov    -0x38(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
and    $0xff,%eax
add    -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1466 <main+0x2b6>
mov    -0x40(%rbp),%eax
shr    $0x18,%eax
and    $0xff,%eax
mov    %al,-0x25(%rbp)
mov    -0x40(%rbp),%eax
shr    $0x10,%eax
and    $0xff,%eax
mov    %al,-0x26(%rbp)
mov    -0x40(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x27(%rbp)
mov    -0x40(%rbp),%eax
shr    $0x0,%eax
and    $0xff,%eax
mov    %al,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2afe(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   10d0 <fwrite@plt>
cltq
cmp    $0x10,%rax
je     1534 <main+0x384>
lea    0xba6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1570 <main+0x3c0>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%esi
mov    -0x3c(%rbp),%eax
mov    %eax,%edx
sub    $0x10,%rdx
mov    -0x3c(%rbp),%r8d
lea    0xb71(%rip),%rdi
mov    $0x10,%ecx
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%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    $0x2,%edi
jne    1225 <main+0x65>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe1f(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1236 <main+0x76>
mov    %rax,%r12
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %r12,%rdi
call   1090 <ftell@plt>
cmp    $0x10,%eax
jg     124d <main+0x8d>
mov    0x8(%rbx),%rsi
lea    0xe03(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12d7 <main+0x117>
lea    0xebf(%rip),%rdi
call   1040 <puts@plt>
jmp    12df <main+0x11f>
mov    0x8(%rbx),%rsi
lea    0xdc3(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12df <main+0x11f>
mov    0x2d84(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
cmp    $0x10,%eax
jne    12cb <main+0x10b>
movzbl 0x3(%rsp),%eax
shl    $0x18,%rax
movsbl 0x2(%rsp),%ecx
shl    $0x10,%ecx
or     %rax,%rcx
movsbl 0x1(%rsp),%eax
shl    $0x8,%eax
movsbl (%rsp),%r14d
or     %rax,%r14
or     %rcx,%r14
lea    -0x4001(%r14),%eax
cmp    $0xffffbfff,%eax
ja     12f5 <main+0x135>
lea    0xdeb(%rip),%rdi
jmp    12d2 <main+0x112>
lea    0xe02(%rip),%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14d,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1394 <main+0x1d4>
mov    %rax,%r15
xor    %r13d,%r13d
mov    %rax,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1080 <memset@plt>
mov    %rsp,%rsi
mov    %r15,%rdi
mov    $0x10,%edx
call   10a0 <memcpy@plt>
mov    0x0(%rbp),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
cmp    %r14d,%eax
je     1373 <main+0x1b3>
mov    0x0(%rbp),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r12,%rcx
call   10e0 <fwrite@plt>
cmp    %r14d,%eax
jne    13a0 <main+0x1e0>
cmp    $0x11,%r14d
jb     14b1 <main+0x2f1>
lea    -0x10(%r14),%rcx
cmp    $0x8,%rcx
jae    13ac <main+0x1ec>
xor    %r13d,%r13d
mov    $0x10,%eax
jmp    14a0 <main+0x2e0>
lea    0xd03(%rip),%rdi
jmp    12d2 <main+0x112>
lea    0xce1(%rip),%rdi
jmp    151b <main+0x35b>
mov    %rcx,%rax
and    $0xfffffffffffffff8,%rax
lea    -0x8(%rax),%rsi
mov    %rsi,%rdx
shr    $0x3,%rdx
add    $0x1,%rdx
test   %rsi,%rsi
je     1436 <main+0x276>
mov    %rdx,%rdi
and    $0xfffffffffffffffe,%rdi
pxor   %xmm2,%xmm2
mov    $0x10,%esi
pxor   %xmm0,%xmm0
pxor   %xmm1,%xmm1
movd   (%r15,%rsi,1),%xmm3
movd   0x4(%r15,%rsi,1),%xmm4
punpcklbw %xmm2,%xmm3
punpcklwd %xmm2,%xmm3
paddd  %xmm0,%xmm3
punpcklbw %xmm2,%xmm4
punpcklwd %xmm2,%xmm4
paddd  %xmm1,%xmm4
movd   0x8(%r15,%rsi,1),%xmm0
movd   0xc(%r15,%rsi,1),%xmm1
punpcklbw %xmm2,%xmm0
punpcklwd %xmm2,%xmm0
paddd  %xmm3,%xmm0
punpcklbw %xmm2,%xmm1
punpcklwd %xmm2,%xmm1
paddd  %xmm4,%xmm1
add    $0x10,%rsi
add    $0xfffffffffffffffe,%rdi
jne    13df <main+0x21f>
jmp    1443 <main+0x283>
pxor   %xmm0,%xmm0
mov    $0x10,%esi
pxor   %xmm1,%xmm1
test   $0x1,%dl
je     1471 <main+0x2b1>
movd   (%r15,%rsi,1),%xmm2
movd   0x4(%r15,%rsi,1),%xmm3
pxor   %xmm4,%xmm4
punpcklbw %xmm4,%xmm2
punpcklwd %xmm4,%xmm2
paddd  %xmm2,%xmm0
punpcklbw %xmm4,%xmm3
punpcklwd %xmm4,%xmm3
paddd  %xmm3,%xmm1
paddd  %xmm1,%xmm0
pshufd $0xee,%xmm0,%xmm1
paddd  %xmm0,%xmm1
pshufd $0x55,%xmm1,%xmm0
paddd  %xmm1,%xmm0
movd   %xmm0,%r13d
cmp    %rax,%rcx
je     14b1 <main+0x2f1>
add    $0x10,%rax
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%r15,%rax,1),%ecx
add    %ecx,%r13d
add    $0x1,%rax
cmp    %rax,%r14
jne    14a0 <main+0x2e0>
mov    %r13d,0x8(%rsp)
mov    0x0(%rbp),%edx
xor    %ebx,%ebx
mov    %r12,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %r12,%rcx
call   10e0 <fwrite@plt>
cmp    $0x10,%eax
jne    1514 <main+0x354>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    %r14d,%edx
add    $0xfffffffffffffff0,%rdx
lea    0xb3b(%rip),%rdi
mov    %r13d,%esi
mov    $0x10,%ecx
mov    %r14d,%r8d
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12e4 <main+0x124>
lea    0xb55(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    12d7 <main+0x117>

LLVM IR (-O0):
@.str = [22 x i8] c"Usage: mkv210 <file>\0A\00"
@.str.1 = [4 x i8] c"r+b\00"
@.str.2 = [24 x i8] c"Can not open file '%s'\0A\00"
@SEEK_END = global i32 0
@.str.3 = [28 x i8] c"The file '%s' is too small\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [24 x i8] c"Read file header error\0A\00"
@.str.5 = [33 x i8] c"Not a valid image, header error\0A\00"
@.str.6 = [23 x i8] c"Can not malloc buffer\0A\00"
@.str.7 = [23 x i8] c"Write body file error\0A\00"
@.str.8 = [25 x i8] c"Write file header error\0A\00"
@.str.9 = [56 x i8] c"the checksum is 0x%08x for %ld bytes [0x%08x ~ 0x%08x]\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [16 x i8]
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %202
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %28
23: ; preds = %16
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %26)
store i32 -1, i32* %3
br label %202
28: ; preds = %16
%29 = load i32*, i32** %6
%30 = load i32, i32* @SEEK_END
%31 = call i32 @fseek(i32* %29, i64 0, i32 %30)
%32 = load i32*, i32** %6
%33 = call i32 @ftell(i32* %32)
%34 = icmp sle i32 %33, 16
br i1 %34, label %35, label %42
35: ; preds = %28
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %38)
%40 = load i32*, i32** %6
%41 = call i32 @fclose(i32* %40)
store i32 -1, i32* %3
br label %202
42: ; preds = %28
%43 = load i32*, i32** %6
%44 = load i32, i32* @SEEK_SET
%45 = call i32 @fseek(i32* %43, i64 0, i32 %44)
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%47 = call i32 @memset(i8* %46, i32 0, i32 16)
%48 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%49 = load i32*, i32** %6
%50 = call i32 @fread(i8* %48, i32 1, i32 16, i32* %49)
%51 = zext i32 %50 to i64
%52 = icmp ne i64 %51, 16
br i1 %52, label %53, label %60
53: ; preds = %42
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %56)
%58 = load i32*, i32** %6
%59 = call i32 @fclose(i32* %58)
store i32 -1, i32* %3
br label %202
60: ; preds = %42
%61 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 3
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = shl i32 %63, 24
%65 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 2
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = shl i32 %67, 16
%69 = or i32 %64, %68
%70 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 1
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = shl i32 %72, 8
%74 = or i32 %69, %73
%75 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%76 = load i8, i8* %75
%77 = sext i8 %76 to i32
%78 = shl i32 %77, 0
%79 = or i32 %74, %78
store i32 %79, i32* %9
%80 = load i32, i32* %9
%81 = icmp ule i32 %80, 0
br i1 %81, label %85, label %82
82: ; preds = %60
%83 = load i32, i32* %9
%84 = icmp ugt i32 %83, 16384
br i1 %84, label %85, label %92
85: ; preds = %82, %60
%86 = load i8**, i8*** %5
%87 = getelementptr inbounds i8*, i8** %86, i64 1
%88 = load i8*, i8** %87
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %88)
%90 = load i32*, i32** %6
%91 = call i32 @fclose(i32* %90)
store i32 -1, i32* %3
br label %202
92: ; preds = %82
%93 = load i32, i32* %9
%94 = call i64 @malloc(i32 %93)
%95 = inttoptr i64 %94 to i8*
store i8* %95, i8** %8
%96 = load i8*, i8** %8
%97 = icmp eq i8* %96, null
br i1 %97, label %98, label %105
98: ; preds = %92
%99 = load i8**, i8*** %5
%100 = getelementptr inbounds i8*, i8** %99, i64 1
%101 = load i8*, i8** %100
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %101)
%103 = load i32*, i32** %6
%104 = call i32 @fclose(i32* %103)
store i32 -1, i32* %3
br label %202
105: ; preds = %92
%106 = load i8*, i8** %8
%107 = load i32, i32* %9
%108 = call i32 @memset(i8* %106, i32 0, i32 %107)
%109 = load i8*, i8** %8
%110 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%111 = call i32 @memcpy(i8* %109, i8* %110, i32 16)
%112 = load i32*, i32** %6
%113 = load i32, i32* @SEEK_SET
%114 = call i32 @fseek(i32* %112, i64 0, i32 %113)
%115 = load i8*, i8** %8
%116 = load i32, i32* %9
%117 = load i32*, i32** %6
%118 = call i32 @fread(i8* %115, i32 1, i32 %116, i32* %117)
%119 = load i32, i32* %9
%120 = icmp ne i32 %118, %119
br i1 %120, label %121, label %138
121: ; preds = %105
%122 = load i32*, i32** %6
%123 = load i32, i32* @SEEK_SET
%124 = call i32 @fseek(i32* %122, i64 0, i32 %123)
%125 = load i8*, i8** %8
%126 = load i32, i32* %9
%127 = load i32*, i32** %6
%128 = call i32 @fwrite(i8* %125, i32 1, i32 %126, i32* %127)
%129 = load i32, i32* %9
%130 = icmp ne i32 %128, %129
br i1 %130, label %131, label %137
131: ; preds = %121
%132 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
%133 = load i8*, i8** %8
%134 = call i32 @free(i8* %133)
%135 = load i32*, i32** %6
%136 = call i32 @fclose(i32* %135)
store i32 -1, i32* %3
br label %202
137: ; preds = %121
br label %138
138: ; preds = %137, %105
store i32 16, i32* %11
store i32 0, i32* %10
br label %139
139: ; preds = %153, %138
%140 = load i32, i32* %11
%141 = load i32, i32* %9
%142 = icmp ult i32 %140, %141
br i1 %142, label %143, label %156
143: ; preds = %139
%144 = load i8*, i8** %8
%145 = load i32, i32* %11
%146 = sext i32 %145 to i64
%147 = getelementptr inbounds i8, i8* %144, i64 %146
%148 = load i8, i8* %147
%149 = sext i8 %148 to i32
%150 = and i32 %149, 255
%151 = load i32, i32* %10
%152 = add i32 %151, %150
store i32 %152, i32* %10
br label %153
153: ; preds = %143
%154 = load i32, i32* %11
%155 = add nsw i32 %154, 1
store i32 %155, i32* %11
br label %139
156: ; preds = %139
%157 = load i32, i32* %10
%158 = lshr i32 %157, 24
%159 = and i32 %158, 255
%160 = trunc i32 %159 to i8
%161 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 11
store i8 %160, i8* %161
%162 = load i32, i32* %10
%163 = lshr i32 %162, 16
%164 = and i32 %163, 255
%165 = trunc i32 %164 to i8
%166 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 10
store i8 %165, i8* %166
%167 = load i32, i32* %10
%168 = lshr i32 %167, 8
%169 = and i32 %168, 255
%170 = trunc i32 %169 to i8
%171 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 9
store i8 %170, i8* %171
%172 = load i32, i32* %10
%173 = lshr i32 %172, 0
%174 = and i32 %173, 255
%175 = trunc i32 %174 to i8
%176 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 8
store i8 %175, i8* %176
%177 = load i32*, i32** %6
%178 = load i32, i32* @SEEK_SET
%179 = call i32 @fseek(i32* %177, i64 0, i32 %178)
%180 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%181 = load i32*, i32** %6
%182 = call i32 @fwrite(i8* %180, i32 1, i32 16, i32* %181)
%183 = sext i32 %182 to i64
%184 = icmp ne i64 %183, 16
br i1 %184, label %185, label %191
185: ; preds = %156
%186 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
%187 = load i8*, i8** %8
%188 = call i32 @free(i8* %187)
%189 = load i32*, i32** %6
%190 = call i32 @fclose(i32* %189)
store i32 -1, i32* %3
br label %202
191: ; preds = %156
%192 = load i8*, i8** %8
%193 = call i32 @free(i8* %192)
%194 = load i32*, i32** %6
%195 = call i32 @fclose(i32* %194)
%196 = load i32, i32* %10
%197 = load i32, i32* %9
%198 = zext i32 %197 to i64
%199 = sub i64 %198, 16
%200 = load i32, i32* %9
%201 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.9, i64 0, i64 0), i32 %196, i64 %199, i32 16, i32 %200)
store i32 0, i32* %3
br label %202
202: ; preds = %191, %185, %131, %98, %85, %53, %35, %23, %14
%203 = load i32, i32* %3
ret i32 %203
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"r+b\00"
@.str.2 = [24 x i8] c"Can not open file '%s'\0A\00"
@SEEK_END = global i32 0
@.str.3 = [28 x i8] c"The file '%s' is too small\0A\00"
@SEEK_SET = global i32 0
@.str.9 = [56 x i8] c"the checksum is 0x%08x for %ld bytes [0x%08x ~ 0x%08x]\0A\00"
@str = [24 x i8] c"Write file header error\00"
@str.10 = [22 x i8] c"Write body file error\00"
@str.11 = [22 x i8] c"Can not malloc buffer\00"
@str.12 = [32 x i8] c"Not a valid image, header error\00"
@str.13 = [23 x i8] c"Read file header error\00"
@str.14 = [21 x i8] c"Usage: mkv210 <file>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.14, i64 0, i64 0))
br label %191
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = load i8*, i8** %9
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %191
16: ; preds = %8
%17 = load i32, i32* @SEEK_END
%18 = tail call i32 @fseek(i32* %11, i64 0, i32 %17)
%19 = tail call i32 @ftell(i32* %11)
%20 = icmp slt i32 %19, 17
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i8*, i8** %9
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = tail call i32 @fclose(i32* %11)
br label %191
25: ; preds = %16
%26 = load i32, i32* @SEEK_SET
%27 = tail call i32 @fseek(i32* %11, i64 0, i32 %26)
%28 = call i32 @memset(i8* %4, i32 0, i32 16)
%29 = call i32 @fread(i8* %4, i32 1, i32 16, i32* %11)
%30 = icmp eq i32 %29, 16
br i1 %30, label %34, label %31
31: ; preds = %25
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.13, i64 0, i64 0))
%33 = call i32 @fclose(i32* %11)
br label %191
34: ; preds = %25
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 3
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = shl nuw i32 %37, 24
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 2
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = shl nsw i32 %41, 16
%43 = or i32 %42, %38
%44 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 1
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = shl nsw i32 %46, 8
%48 = or i32 %43, %47
%49 = load i8, i8* %4
%50 = sext i8 %49 to i32
%51 = or i32 %48, %50
%52 = add i32 %51, -16385
%53 = icmp ult i32 %52, -16384
br i1 %53, label %54, label %57
54: ; preds = %34
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.12, i64 0, i64 0))
%56 = call i32 @fclose(i32* %11)
br label %191
57: ; preds = %34
%58 = call i64 @malloc(i32 %51)
%59 = inttoptr i64 %58 to i8*
%60 = icmp eq i64 %58, 0
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.11, i64 0, i64 0))
%63 = call i32 @fclose(i32* %11)
br label %191
64: ; preds = %57
%65 = call i32 @memset(i8* %59, i32 0, i32 %51)
%66 = call i32 @memcpy(i8* %59, i8* %4, i32 16)
%67 = load i32, i32* @SEEK_SET
%68 = call i32 @fseek(i32* %11, i64 0, i32 %67)
%69 = call i32 @fread(i8* %59, i32 1, i32 %51, i32* %11)
%70 = icmp eq i32 %69, %51
br i1 %70, label %80, label %71
71: ; preds = %64
%72 = load i32, i32* @SEEK_SET
%73 = call i32 @fseek(i32* %11, i64 0, i32 %72)
%74 = call i32 @fwrite(i8* %59, i32 1, i32 %51, i32* %11)
%75 = icmp eq i32 %74, %51
br i1 %75, label %80, label %76
76: ; preds = %71
%77 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.10, i64 0, i64 0))
%78 = call i32 @free(i8* %59)
%79 = call i32 @fclose(i32* %11)
br label %191
80: ; preds = %71, %64
%81 = icmp ugt i32 %51, 16
br i1 %81, label %82, label %164
82: ; preds = %80
%83 = zext i32 %51 to i64
%84 = add nsw i64 %83, -16
%85 = icmp ult i64 %84, 8
br i1 %85, label %152, label %86
86: ; preds = %82
%87 = and i64 %84, -8
%88 = add nsw i64 %87, 16
%89 = add nsw i64 %87, -8
%90 = lshr exact i64 %89, 3
%91 = add nuw nsw i64 %90, 1
%92 = and i64 %91, 1
%93 = icmp eq i64 %89, 0
br i1 %93, label %128, label %94
94: ; preds = %86
%95 = and i64 %91, 4611686018427387902
br label %96
96: ; preds = %96, %94
%97 = phi i64 [ 0, %94 ], [ %123, %96 ]
%98 = phi <4 x i32> [ zeroinitializer, %94 ], [ %121, %96 ]
%99 = phi <4 x i32> [ zeroinitializer, %94 ], [ %122, %96 ]
%100 = phi i64 [ 0, %94 ], [ %124, %96 ]
%101 = add i64 %97, 16
%102 = getelementptr inbounds i8, i8* %59, i64 %101
%103 = bitcast i8* %102 to <4 x i8>*
%104 = load <4 x i8>, <4 x i8>* %103
%105 = getelementptr inbounds i8, i8* %102, i64 4
%106 = bitcast i8* %105 to <4 x i8>*
%107 = load <4 x i8>, <4 x i8>* %106
%108 = zext <4 x i8> %104 to <4 x i32>
%109 = zext <4 x i8> %107 to <4 x i32>
%110 = add <4 x i32> %98, %108
%111 = add <4 x i32> %99, %109
%112 = add i64 %97, 24
%113 = getelementptr inbounds i8, i8* %59, i64 %112
%114 = bitcast i8* %113 to <4 x i8>*
%115 = load <4 x i8>, <4 x i8>* %114
%116 = getelementptr inbounds i8, i8* %113, i64 4
%117 = bitcast i8* %116 to <4 x i8>*
%118 = load <4 x i8>, <4 x i8>* %117
%119 = zext <4 x i8> %115 to <4 x i32>
%120 = zext <4 x i8> %118 to <4 x i32>
%121 = add <4 x i32> %110, %119
%122 = add <4 x i32> %111, %120
%123 = add nuw i64 %97, 16
%124 = add i64 %100, 2
%125 = icmp eq i64 %124, %95
br i1 %125, label %126, label %96
126: ; preds = %96
%127 = add i64 %97, 32
br label %128
128: ; preds = %126, %86
%129 = phi <4 x i32> [ undef, %86 ], [ %121, %126 ]
%130 = phi <4 x i32> [ undef, %86 ], [ %122, %126 ]
%131 = phi i64 [ 16, %86 ], [ %127, %126 ]
%132 = phi <4 x i32> [ zeroinitializer, %86 ], [ %121, %126 ]
%133 = phi <4 x i32> [ zeroinitializer, %86 ], [ %122, %126 ]
%134 = icmp eq i64 %92, 0
br i1 %134, label %146, label %135
135: ; preds = %128
%136 = getelementptr inbounds i8, i8* %59, i64 %131
%137 = bitcast i8* %136 to <4 x i8>*
%138 = load <4 x i8>, <4 x i8>* %137
%139 = getelementptr inbounds i8, i8* %136, i64 4
%140 = bitcast i8* %139 to <4 x i8>*
%141 = load <4 x i8>, <4 x i8>* %140
%142 = zext <4 x i8> %138 to <4 x i32>
%143 = zext <4 x i8> %141 to <4 x i32>
%144 = add <4 x i32> %132, %142
%145 = add <4 x i32> %133, %143
br label %146
146: ; preds = %128, %135
%147 = phi <4 x i32> [ %129, %128 ], [ %144, %135 ]
%148 = phi <4 x i32> [ %130, %128 ], [ %145, %135 ]
%149 = add <4 x i32> %148, %147
%150 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %149)
%151 = icmp eq i64 %84, %87
br i1 %151, label %164, label %152
152: ; preds = %82, %146
%153 = phi i64 [ 16, %82 ], [ %88, %146 ]
%154 = phi i32 [ 0, %82 ], [ %150, %146 ]
br label %155
155: ; preds = %152, %155
%156 = phi i64 [ %162, %155 ], [ %153, %152 ]
%157 = phi i32 [ %161, %155 ], [ %154, %152 ]
%158 = getelementptr inbounds i8, i8* %59, i64 %156
%159 = load i8, i8* %158
%160 = zext i8 %159 to i32
%161 = add i32 %157, %160
%162 = add nuw nsw i64 %156, 1
%163 = icmp eq i64 %162, %83
br i1 %163, label %164, label %155
164: ; preds = %155, %146, %80
%165 = phi i32 [ 0, %80 ], [ %150, %146 ], [ %161, %155 ]
%166 = lshr i32 %165, 24
%167 = trunc i32 %166 to i8
%168 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 11
store i8 %167, i8* %168
%169 = lshr i32 %165, 16
%170 = trunc i32 %169 to i8
%171 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 10
store i8 %170, i8* %171
%172 = lshr i32 %165, 8
%173 = trunc i32 %172 to i8
%174 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 9
store i8 %173, i8* %174
%175 = trunc i32 %165 to i8
%176 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 8
store i8 %175, i8* %176
%177 = load i32, i32* @SEEK_SET
%178 = call i32 @fseek(i32* %11, i64 0, i32 %177)
%179 = call i32 @fwrite(i8* %4, i32 1, i32 16, i32* %11)
%180 = icmp eq i32 %179, 16
br i1 %180, label %185, label %181
181: ; preds = %164
%182 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%183 = call i32 @free(i8* %59)
%184 = call i32 @fclose(i32* %11)
br label %191
185: ; preds = %164
%186 = call i32 @free(i8* %59)
%187 = call i32 @fclose(i32* %11)
%188 = zext i32 %51 to i64
%189 = add nsw i64 %188, -16
%190 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.9, i64 0, i64 0), i32 %165, i64 %189, i32 16, i32 %51)
br label %191
191: ; preds = %185, %181, %76, %61, %54, %31, %21, %13, %6
%192 = phi i32 [ -1, %6 ], [ -1, %13 ], [ -1, %21 ], [ -1, %31 ], [ -1, %54 ], [ -1, %61 ], [ -1, %76 ], [ -1, %181 ], [ 0, %185 ]
ret i32 %192
}