#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*/ 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: mkheader <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);
  fseek(fp, 0L, SEEK_SET);
  if (fread(buf, 1, size, fp) != size) {
    printf("Read body file error\n", argv[1]);
    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", argv[1]);
    free(buf);
    fclose(fp);
    return -1;
  }
  free(buf);
  fclose(fp);
  printf("the checksum is 0x%08x for %d bytes [0x%08x ~ 0x%08x]\n", checksum,
         (size - sizeof(header)), 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     11da <main+0x3a>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1525 <main+0x385>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe2f(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121f <main+0x7f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1525 <main+0x385>
mov    -0x18(%rbp),%rdi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
cmp    $0x10,%eax
jg     1272 <main+0xd2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1525 <main+0x385>
mov    -0x18(%rbp),%rdi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <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     12e6 <main+0x146>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1525 <main+0x385>
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    1322 <main+0x182>
cmpl   $0x4000,-0x3c(%rbp)
jbe    134d <main+0x1ad>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1525 <main+0x385>
mov    -0x3c(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    138f <main+0x1ef>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd16(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1525 <main+0x385>
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    0x2c30(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <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     1405 <main+0x265>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xcc0(%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    1525 <main+0x385>
movl   $0x10,-0x44(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jae    1444 <main+0x2a4>
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    1413 <main+0x273>
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    0x2b51(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   10c0 <fwrite@plt>
cltq
cmp    $0x10,%rax
je     14e9 <main+0x349>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xbf2(%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    1525 <main+0x385>
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    0xbbd(%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    1215 <main+0x65>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe2f(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1226 <main+0x76>
mov    %rax,%r12
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %r12,%rdi
call   1090 <ftell@plt>
cmp    $0x10,%eax
jg     123d <main+0x8d>
mov    0x8(%rbx),%rsi
lea    0xe13(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12c7 <main+0x117>
lea    0xecd(%rip),%rdi
call   1040 <puts@plt>
jmp    12cf <main+0x11f>
mov    0x8(%rbx),%rsi
lea    0xdd3(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12cf <main+0x11f>
mov    0x2d94(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10b0 <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    12bb <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     12e5 <main+0x135>
lea    0xdf9(%rip),%rdi
jmp    12c2 <main+0x112>
lea    0xe10(%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   10a0 <malloc@plt>
test   %rax,%rax
je     134b <main+0x19b>
mov    %rax,%r15
xor    %r13d,%r13d
mov    %rax,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1080 <memset@plt>
mov    0x0(%rbp),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
cmp    %r14d,%eax
jne    1357 <main+0x1a7>
cmp    $0x11,%r14d
jb     145d <main+0x2ad>
lea    -0x10(%r14),%rcx
cmp    $0x8,%rcx
jae    1363 <main+0x1b3>
xor    %r13d,%r13d
mov    $0x10,%eax
jmp    144c <main+0x29c>
lea    0xd4a(%rip),%rdi
jmp    12c2 <main+0x112>
lea    0xd29(%rip),%rdi
jmp    14c7 <main+0x317>
mov    %rcx,%rax
and    $0xfffffffffffffff8,%rax
lea    -0x8(%rax),%rsi
mov    %rsi,%rdx
shr    $0x3,%rdx
add    $0x1,%rdx
test   %rsi,%rsi
je     13ed <main+0x23d>
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    1396 <main+0x1e6>
jmp    13fa <main+0x24a>
pxor   %xmm0,%xmm0
mov    $0x10,%esi
pxor   %xmm1,%xmm1
test   $0x1,%dl
je     1428 <main+0x278>
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     145d <main+0x2ad>
add    $0x10,%rax
movzbl (%r15,%rax,1),%ecx
add    %ecx,%r13d
add    $0x1,%rax
cmp    %rax,%r14
jne    144c <main+0x29c>
mov    %r13d,0x8(%rsp)
mov    0x0(%rbp),%edx
xor    %ebx,%ebx
mov    %r12,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %r12,%rcx
call   10d0 <fwrite@plt>
cmp    $0x10,%eax
jne    14c0 <main+0x310>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    %r14d,%edx
add    $0xfffffffffffffff0,%rdx
lea    0xb8f(%rip),%rdi
mov    $0x10,%ecx
mov    %r13d,%esi
mov    %r14d,%r8d
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12d4 <main+0x124>
lea    0xba8(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    12c7 <main+0x117>

LLVM IR (-O0):
@.str = [24 x i8] c"Usage: mkheader <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 = [22 x i8] c"Read body file error\0A\00"
@.str.8 = [25 x i8] c"Write file header error\0A\00"
@.str.9 = [55 x i8] c"the checksum is 0x%08x for %d 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 ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %194
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 %194
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 %194
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 %194
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 %194
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 %194
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 i32*, i32** %6
%110 = load i32, i32* @SEEK_SET
%111 = call i32 @fseek(i32* %109, i64 0, i32 %110)
%112 = load i8*, i8** %8
%113 = load i32, i32* %9
%114 = load i32*, i32** %6
%115 = call i32 @fread(i8* %112, i32 1, i32 %113, i32* %114)
%116 = load i32, i32* %9
%117 = icmp ne i32 %115, %116
br i1 %117, label %118, label %127
118: ; preds = %105
%119 = load i8**, i8*** %5
%120 = getelementptr inbounds i8*, i8** %119, i64 1
%121 = load i8*, i8** %120
%122 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %121)
%123 = load i8*, i8** %8
%124 = call i32 @free(i8* %123)
%125 = load i32*, i32** %6
%126 = call i32 @fclose(i32* %125)
store i32 -1, i32* %3
br label %194
127: ; preds = %105
store i32 16, i32* %11
store i32 0, i32* %10
br label %128
128: ; preds = %142, %127
%129 = load i32, i32* %11
%130 = load i32, i32* %9
%131 = icmp ult i32 %129, %130
br i1 %131, label %132, label %145
132: ; preds = %128
%133 = load i8*, i8** %8
%134 = load i32, i32* %11
%135 = sext i32 %134 to i64
%136 = getelementptr inbounds i8, i8* %133, i64 %135
%137 = load i8, i8* %136
%138 = sext i8 %137 to i32
%139 = and i32 %138, 255
%140 = load i32, i32* %10
%141 = add i32 %140, %139
store i32 %141, i32* %10
br label %142
142: ; preds = %132
%143 = load i32, i32* %11
%144 = add nsw i32 %143, 1
store i32 %144, i32* %11
br label %128
145: ; preds = %128
%146 = load i32, i32* %10
%147 = lshr i32 %146, 24
%148 = and i32 %147, 255
%149 = trunc i32 %148 to i8
%150 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 11
store i8 %149, i8* %150
%151 = load i32, i32* %10
%152 = lshr i32 %151, 16
%153 = and i32 %152, 255
%154 = trunc i32 %153 to i8
%155 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 10
store i8 %154, i8* %155
%156 = load i32, i32* %10
%157 = lshr i32 %156, 8
%158 = and i32 %157, 255
%159 = trunc i32 %158 to i8
%160 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 9
store i8 %159, i8* %160
%161 = load i32, i32* %10
%162 = lshr i32 %161, 0
%163 = and i32 %162, 255
%164 = trunc i32 %163 to i8
%165 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 8
store i8 %164, i8* %165
%166 = load i32*, i32** %6
%167 = load i32, i32* @SEEK_SET
%168 = call i32 @fseek(i32* %166, i64 0, i32 %167)
%169 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%170 = load i32*, i32** %6
%171 = call i32 @fwrite(i8* %169, i32 1, i32 16, i32* %170)
%172 = sext i32 %171 to i64
%173 = icmp ne i64 %172, 16
br i1 %173, label %174, label %183
174: ; preds = %145
%175 = load i8**, i8*** %5
%176 = getelementptr inbounds i8*, i8** %175, i64 1
%177 = load i8*, i8** %176
%178 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* %177)
%179 = load i8*, i8** %8
%180 = call i32 @free(i8* %179)
%181 = load i32*, i32** %6
%182 = call i32 @fclose(i32* %181)
store i32 -1, i32* %3
br label %194
183: ; preds = %145
%184 = load i8*, i8** %8
%185 = call i32 @free(i8* %184)
%186 = load i32*, i32** %6
%187 = call i32 @fclose(i32* %186)
%188 = load i32, i32* %10
%189 = load i32, i32* %9
%190 = zext i32 %189 to i64
%191 = sub i64 %190, 16
%192 = load i32, i32* %9
%193 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.9, i64 0, i64 0), i32 %188, i64 %191, i64 16, i32 %192)
store i32 0, i32* %3
br label %194
194: ; preds = %183, %174, %118, %98, %85, %53, %35, %23, %14
%195 = load i32, i32* %3
ret i32 %195
}

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 = [55 x i8] c"the checksum is 0x%08x for %d bytes [0x%08x ~ 0x%08x]\0A\00"
@str = [24 x i8] c"Write file header error\00"
@str.10 = [21 x i8] c"Read 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 = [23 x i8] c"Usage: mkheader <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 ([23 x i8], [23 x i8]* @str.14, i64 0, i64 0))
br label %185
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 %185
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 %185
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 %185
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 %185
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 %185
64: ; preds = %57
%65 = call i32 @memset(i8* %59, i32 0, i32 %51)
%66 = load i32, i32* @SEEK_SET
%67 = call i32 @fseek(i32* %11, i64 0, i32 %66)
%68 = call i32 @fread(i8* %59, i32 1, i32 %51, i32* %11)
%69 = icmp eq i32 %68, %51
br i1 %69, label %70, label %145
70: ; preds = %64
%71 = icmp ugt i32 %51, 16
br i1 %71, label %72, label %158
72: ; preds = %70
%73 = zext i32 %51 to i64
%74 = add nsw i64 %73, -16
%75 = icmp ult i64 %74, 8
br i1 %75, label %142, label %76
76: ; preds = %72
%77 = and i64 %74, -8
%78 = add nsw i64 %77, 16
%79 = add nsw i64 %77, -8
%80 = lshr exact i64 %79, 3
%81 = add nuw nsw i64 %80, 1
%82 = and i64 %81, 1
%83 = icmp eq i64 %79, 0
br i1 %83, label %118, label %84
84: ; preds = %76
%85 = and i64 %81, 4611686018427387902
br label %86
86: ; preds = %86, %84
%87 = phi i64 [ 0, %84 ], [ %113, %86 ]
%88 = phi <4 x i32> [ zeroinitializer, %84 ], [ %111, %86 ]
%89 = phi <4 x i32> [ zeroinitializer, %84 ], [ %112, %86 ]
%90 = phi i64 [ 0, %84 ], [ %114, %86 ]
%91 = add i64 %87, 16
%92 = getelementptr inbounds i8, i8* %59, i64 %91
%93 = bitcast i8* %92 to <4 x i8>*
%94 = load <4 x i8>, <4 x i8>* %93
%95 = getelementptr inbounds i8, i8* %92, i64 4
%96 = bitcast i8* %95 to <4 x i8>*
%97 = load <4 x i8>, <4 x i8>* %96
%98 = zext <4 x i8> %94 to <4 x i32>
%99 = zext <4 x i8> %97 to <4 x i32>
%100 = add <4 x i32> %88, %98
%101 = add <4 x i32> %89, %99
%102 = add i64 %87, 24
%103 = getelementptr inbounds i8, i8* %59, i64 %102
%104 = bitcast i8* %103 to <4 x i8>*
%105 = load <4 x i8>, <4 x i8>* %104
%106 = getelementptr inbounds i8, i8* %103, i64 4
%107 = bitcast i8* %106 to <4 x i8>*
%108 = load <4 x i8>, <4 x i8>* %107
%109 = zext <4 x i8> %105 to <4 x i32>
%110 = zext <4 x i8> %108 to <4 x i32>
%111 = add <4 x i32> %100, %109
%112 = add <4 x i32> %101, %110
%113 = add nuw i64 %87, 16
%114 = add i64 %90, 2
%115 = icmp eq i64 %114, %85
br i1 %115, label %116, label %86
116: ; preds = %86
%117 = add i64 %87, 32
br label %118
118: ; preds = %116, %76
%119 = phi <4 x i32> [ undef, %76 ], [ %111, %116 ]
%120 = phi <4 x i32> [ undef, %76 ], [ %112, %116 ]
%121 = phi i64 [ 16, %76 ], [ %117, %116 ]
%122 = phi <4 x i32> [ zeroinitializer, %76 ], [ %111, %116 ]
%123 = phi <4 x i32> [ zeroinitializer, %76 ], [ %112, %116 ]
%124 = icmp eq i64 %82, 0
br i1 %124, label %136, label %125
125: ; preds = %118
%126 = getelementptr inbounds i8, i8* %59, i64 %121
%127 = bitcast i8* %126 to <4 x i8>*
%128 = load <4 x i8>, <4 x i8>* %127
%129 = getelementptr inbounds i8, i8* %126, i64 4
%130 = bitcast i8* %129 to <4 x i8>*
%131 = load <4 x i8>, <4 x i8>* %130
%132 = zext <4 x i8> %128 to <4 x i32>
%133 = zext <4 x i8> %131 to <4 x i32>
%134 = add <4 x i32> %122, %132
%135 = add <4 x i32> %123, %133
br label %136
136: ; preds = %118, %125
%137 = phi <4 x i32> [ %119, %118 ], [ %134, %125 ]
%138 = phi <4 x i32> [ %120, %118 ], [ %135, %125 ]
%139 = add <4 x i32> %138, %137
%140 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %139)
%141 = icmp eq i64 %74, %77
br i1 %141, label %158, label %142
142: ; preds = %72, %136
%143 = phi i64 [ 16, %72 ], [ %78, %136 ]
%144 = phi i32 [ 0, %72 ], [ %140, %136 ]
br label %149
145: ; preds = %64
%146 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.10, i64 0, i64 0))
%147 = call i32 @free(i8* %59)
%148 = call i32 @fclose(i32* %11)
br label %185
149: ; preds = %142, %149
%150 = phi i64 [ %156, %149 ], [ %143, %142 ]
%151 = phi i32 [ %155, %149 ], [ %144, %142 ]
%152 = getelementptr inbounds i8, i8* %59, i64 %150
%153 = load i8, i8* %152
%154 = zext i8 %153 to i32
%155 = add i32 %151, %154
%156 = add nuw nsw i64 %150, 1
%157 = icmp eq i64 %156, %73
br i1 %157, label %158, label %149
158: ; preds = %149, %136, %70
%159 = phi i32 [ 0, %70 ], [ %140, %136 ], [ %155, %149 ]
%160 = lshr i32 %159, 24
%161 = trunc i32 %160 to i8
%162 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 11
store i8 %161, i8* %162
%163 = lshr i32 %159, 16
%164 = trunc i32 %163 to i8
%165 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 10
store i8 %164, i8* %165
%166 = lshr i32 %159, 8
%167 = trunc i32 %166 to i8
%168 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 9
store i8 %167, i8* %168
%169 = trunc i32 %159 to i8
%170 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 8
store i8 %169, i8* %170
%171 = load i32, i32* @SEEK_SET
%172 = call i32 @fseek(i32* %11, i64 0, i32 %171)
%173 = call i32 @fwrite(i8* %4, i32 1, i32 16, i32* %11)
%174 = icmp eq i32 %173, 16
br i1 %174, label %179, label %175
175: ; preds = %158
%176 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%177 = call i32 @free(i8* %59)
%178 = call i32 @fclose(i32* %11)
br label %185
179: ; preds = %158
%180 = call i32 @free(i8* %59)
%181 = call i32 @fclose(i32* %11)
%182 = zext i32 %51 to i64
%183 = add nsw i64 %182, -16
%184 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.9, i64 0, i64 0), i32 %159, i64 %183, i64 16, i32 %51)
br label %185
185: ; preds = %179, %175, %145, %61, %54, %31, %21, %13, %6
%186 = phi i32 [ -1, %6 ], [ -1, %13 ], [ -1, %21 ], [ -1, %31 ], [ -1, %54 ], [ -1, %61 ], [ -1, %145 ], [ -1, %175 ], [ 0, %179 ]
ret i32 %186
}