#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 uint32_t;
struct stat {
  int st_size;
};

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int S_IRGRP;
int S_IRWXU;
int /*<<< orphan*/ be_wr(char *, int);
int /*<<< orphan*/ close(int);
int crc32(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ memset(char *, int, size_t);
int open(char const *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, char *, int);
int stat(char const *, struct stat *);
int /*<<< orphan*/ strncpy(char *, char *, int);
size_t write(int, char *, size_t);
int main(int argc, char **argv) {
  int fdin;
  int fdout;
  struct stat sb;
  uint32_t filesize;
  int ret = 0;
  const char *pathin;
  const char *pathout;
  char *buffer;
  uint32_t sum;
  size_t bufsize;
  size_t bytes;
  if (argc < 3) {
    printf("Too few arguments.\n");
    printf("%s <infile> <outfile>\n", argv[0]);
  }
  pathin = argv[1];
  pathout = argv[2];
  ret = stat(pathin, &sb);
  if (ret < 0)
    return ret;
  filesize = sb.st_size;
  printf("INFILE: %s, size: %08x bytes\n", pathin, filesize);
  bufsize = filesize + 0x68;
  printf("Allocate %08zx bytes\n", bufsize);
  buffer = malloc(bufsize);
  if (!buffer) {
    printf("OOM: could not allocate buffer\n");
    return 0;
  }
  memset(buffer, 0x00, bufsize);
  fdin = open(pathin, O_RDONLY);
  if (!fdin) {
    printf("ERROR: could not open input file\n");
    free(buffer);
    return 0;
  }
  bytes = read(fdin, buffer + 0x68, filesize);
  if (bytes < filesize) {
    printf("ERROR: could not read entire file\n");
    free(buffer);
    close(fdin);
    return 0;
  }
  close(fdin);
  be_wr(buffer + 0x00, 0x27051956);
  be_wr(buffer + 0x08, 0x4c06738c);
  be_wr(buffer + 0x0c, filesize);
  be_wr(buffer + 0x10, 0x00008000);
  be_wr(buffer + 0x14, 0x00008000);
  buffer[0x1c] = 0x05;
  buffer[0x1d] = 0x02;
  buffer[0x1e] = 0x02;
  buffer[0x1f] = 0x01;
  strncpy(buffer + 0x20, "kernel.img", 0x20);
  buffer[0x20 + 0x20 - 1] = '\0';
  strncpy(buffer + 0x40, "dns-313v3", 0x10);
  buffer[0x40 + 0x10 - 1] = '\0';
  strncpy(buffer + 0x50, "2.01b04", 0x10);
  buffer[0x50 + 0x10 - 1] = '\0';
  buffer[0x60] = 0x00;
  buffer[0x60 + 1] = 0x80;
  buffer[0x60 + 2] = 0xc8;
  buffer[0x60 + 3] = 0x16;
  buffer[0x60 + 4] = 0x81;
  buffer[0x60 + 5] = 0x68;
  sum = crc32(0, buffer + 0x68, filesize);
  be_wr(buffer + 0x18, sum);
  printf("data checksum: 0x%08x\n", sum);
  sum = crc32(0, buffer, 0x68);
  be_wr(buffer + 0x04, sum);
  printf("header checksum: 0x%08x\n", sum);
  printf("OUTFILE: %s, size: %08zx bytes\n", pathout, bufsize);
  fdout = open(pathout, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRGRP);
  if (!fdout) {
    printf("ERROR: could not open output file\n");
    return 0;
  }
  bytes = write(fdout, buffer, bufsize);
  if (bytes < bufsize) {
    printf("ERROR: could not write complete output file\n");
    return 0;
  }
  close(fdout);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    120a <main+0x4a>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10b0 <stat@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1247 <main+0x87>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    159a <main+0x3da>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x68,%eax
cltq
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rsi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x50(%rbp),%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12b2 <main+0xf2>
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    159a <main+0x3da>
mov    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%rdx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    0x2cfc(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10e0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1305 <main+0x145>
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    159a <main+0x3da>
mov    -0x14(%rbp),%edi
mov    -0x40(%rbp),%rsi
add    $0x68,%rsi
mov    -0x24(%rbp),%edx
call   10a0 <read@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmp    %rcx,%rax
jae    1358 <main+0x198>
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    159a <main+0x3da>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    -0x40(%rbp),%rdi
mov    $0x27051956,%esi
call   10d0 <be_wr@plt>
mov    -0x40(%rbp),%rdi
add    $0x8,%rdi
mov    $0x4c06738c,%esi
call   10d0 <be_wr@plt>
mov    -0x40(%rbp),%rdi
add    $0xc,%rdi
mov    -0x24(%rbp),%esi
call   10d0 <be_wr@plt>
mov    -0x40(%rbp),%rdi
add    $0x10,%rdi
mov    $0x8000,%esi
call   10d0 <be_wr@plt>
mov    -0x40(%rbp),%rdi
add    $0x14,%rdi
mov    $0x8000,%esi
call   10d0 <be_wr@plt>
mov    -0x40(%rbp),%rax
movb   $0x5,0x1c(%rax)
mov    -0x40(%rbp),%rax
movb   $0x2,0x1d(%rax)
mov    -0x40(%rbp),%rax
movb   $0x2,0x1e(%rax)
mov    -0x40(%rbp),%rax
movb   $0x1,0x1f(%rax)
mov    -0x40(%rbp),%rdi
add    $0x20,%rdi
lea    0xce1(%rip),%rsi
mov    $0x20,%edx
call   1040 <strncpy@plt>
mov    -0x40(%rbp),%rax
movb   $0x0,0x3f(%rax)
mov    -0x40(%rbp),%rdi
add    $0x40,%rdi
lea    0xccb(%rip),%rsi
mov    $0x10,%edx
call   1040 <strncpy@plt>
mov    -0x40(%rbp),%rax
movb   $0x0,0x4f(%rax)
mov    -0x40(%rbp),%rdi
add    $0x50,%rdi
lea    0xcb4(%rip),%rsi
mov    $0x10,%edx
call   1040 <strncpy@plt>
mov    -0x40(%rbp),%rax
movb   $0x0,0x5f(%rax)
mov    -0x40(%rbp),%rax
movb   $0x0,0x60(%rax)
mov    -0x40(%rbp),%rax
movb   $0x80,0x61(%rax)
mov    -0x40(%rbp),%rax
movb   $0xc8,0x62(%rax)
mov    -0x40(%rbp),%rax
movb   $0x16,0x63(%rax)
mov    -0x40(%rbp),%rax
movb   $0x81,0x64(%rax)
mov    -0x40(%rbp),%rax
movb   $0x68,0x65(%rax)
mov    -0x40(%rbp),%rsi
add    $0x68,%rsi
mov    -0x24(%rbp),%edx
xor    %edi,%edi
call   1050 <crc32@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
add    $0x18,%rdi
mov    -0x44(%rbp),%esi
call   10d0 <be_wr@plt>
mov    -0x44(%rbp),%esi
lea    0xc4b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x40(%rbp),%rsi
xor    %edi,%edi
mov    $0x68,%edx
call   1050 <crc32@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
add    $0x4,%rdi
mov    -0x44(%rbp),%esi
call   10d0 <be_wr@plt>
mov    -0x44(%rbp),%esi
lea    0xc2e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x38(%rbp),%rsi
mov    -0x50(%rbp),%rdx
lea    0xc31(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2ade(%rip),%rax
mov    (%rax),%esi
mov    0x2afd(%rip),%rax
or     (%rax),%esi
mov    0x2aec(%rip),%rax
or     (%rax),%esi
mov    0x2acb(%rip),%rax
mov    (%rax),%edx
mov    0x2aa2(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   10e0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1546 <main+0x386>
lea    0xbfe(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    159a <main+0x3da>
mov    -0x18(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1060 <write@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    -0x50(%rbp),%rax
jae    1582 <main+0x3c2>
lea    0xbe5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    159a <main+0x3da>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
cmp    $0x2,%edi
jg     1204 <main+0x34>
lea    0xf7b(%rip),%rdi
call   1050 <puts@plt>
mov    0x0(%rbp),%rsi
lea    0xe03(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x8(%rbp),%rbx
mov    0x10(%rbp),%rbp
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10c0 <stat@plt>
test   %eax,%eax
js     12c8 <main+0xf8>
mov    %rbp,0x10(%rsp)
mov    0x8(%rsp),%ebp
movslq %ebp,%r14
lea    0xde3(%rip),%rdi
xor    %r12d,%r12d
mov    %rbx,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1080 <printf@plt>
lea    0x68(%r14),%r15
lea    0xde6(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r15,%rdi
call   10d0 <malloc@plt>
test   %rax,%rax
je     12d0 <main+0x100>
mov    %rax,%r13
xor    %r12d,%r12d
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1090 <memset@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   10f0 <open@plt>
test   %eax,%eax
je     12e1 <main+0x111>
mov    %eax,%ebx
lea    0x68(%r13),%r12
mov    %eax,%edi
mov    %r12,%rsi
mov    %r14d,%edx
call   10b0 <read@plt>
cmp    %r14,%rax
jae    12fa <main+0x12a>
lea    0xe97(%rip),%rdi
call   1050 <puts@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
jmp    1496 <main+0x2c6>
mov    %eax,%r12d
jmp    1499 <main+0x2c9>
lea    0xde1(%rip),%rdi
call   1050 <puts@plt>
jmp    1499 <main+0x2c9>
lea    0xdef(%rip),%rdi
call   1050 <puts@plt>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    1499 <main+0x2c9>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    %r13,%rdi
mov    $0x27051956,%esi
call   10e0 <be_wr@plt>
lea    0x8(%r13),%rdi
mov    $0x4c06738c,%esi
call   10e0 <be_wr@plt>
lea    0xc(%r13),%rdi
mov    %ebp,%esi
call   10e0 <be_wr@plt>
lea    0x10(%r13),%rdi
mov    $0x8000,%esi
call   10e0 <be_wr@plt>
lea    0x14(%r13),%rdi
mov    $0x8000,%esi
call   10e0 <be_wr@plt>
movl   $0x1020205,0x1c(%r13)
01
lea    0x20(%r13),%rdi
lea    0xcf5(%rip),%rsi
mov    $0x20,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x3f(%r13)
lea    0x40(%r13),%rdi
lea    0xce6(%rip),%rsi
mov    $0x10,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x4f(%r13)
lea    0x50(%r13),%rdi
lea    0xcd6(%rip),%rsi
mov    $0x10,%edx
call   1040 <strncpy@plt>
movl   $0xc8800000,0x5f(%r13)
c8
movw   $0x8116,0x63(%r13)
movb   $0x68,0x65(%r13)
mov    %r12,%rsi
xor    %r12d,%r12d
xor    %edi,%edi
mov    %ebp,%edx
call   1060 <crc32@plt>
mov    %eax,%ebx
lea    0x18(%r13),%rdi
mov    %eax,%esi
call   10e0 <be_wr@plt>
lea    0xc9d(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %edi,%edi
mov    %r13,%rsi
mov    $0x68,%edx
call   1060 <crc32@plt>
mov    %eax,%ebx
mov    %r13,%rdi
add    $0x4,%rdi
mov    %eax,%esi
call   10e0 <be_wr@plt>
lea    0xc85(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xc8e(%rip),%rdi
mov    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2bad(%rip),%rax
mov    0x2bce(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2bbb(%rip),%rax
or     (%rax),%esi
mov    0x2b9a(%rip),%rax
mov    0x2b73(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   10f0 <open@plt>
test   %eax,%eax
je     1479 <main+0x2a9>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r13,%rsi
mov    %r15,%rdx
call   1070 <write@plt>
cmp    %r15,%rax
jae    1487 <main+0x2b7>
lea    0xca8(%rip),%rdi
call   1050 <puts@plt>
jmp    1496 <main+0x2c6>
lea    0xc78(%rip),%rdi
call   1050 <puts@plt>
jmp    1499 <main+0x2c9>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [20 x i8] c"Too few arguments.\0A\00"
@.str.1 = [23 x i8] c"%s <infile> <outfile>\0A\00"
@.str.2 = [30 x i8] c"INFILE: %s, size: %08x bytes\0A\00"
@.str.3 = [22 x i8] c"Allocate %08zx bytes\0A\00"
@.str.4 = [32 x i8] c"OOM: could not allocate buffer\0A\00"
@O_RDONLY = global i32 0
@.str.5 = [34 x i8] c"ERROR: could not open input file\0A\00"
@.str.6 = [35 x i8] c"ERROR: could not read entire file\0A\00"
@.str.7 = [11 x i8] c"kernel.img\00"
@.str.8 = [10 x i8] c"dns-313v3\00"
@.str.9 = [8 x i8] c"2.01b04\00"
@.str.10 = [23 x i8] c"data checksum: 0x%08x\0A\00"
@.str.11 = [25 x i8] c"header checksum: 0x%08x\0A\00"
@.str.12 = [32 x i8] c"OUTFILE: %s, size: %08zx bytes\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@S_IRGRP = global i32 0
@.str.13 = [35 x i8] c"ERROR: could not open output file\0A\00"
@.str.14 = [45 x i8] c"ERROR: could not write complete output file\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i64
%16 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 3
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %19, %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %11
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
store i8* %31, i8** %12
%32 = load i8*, i8** %11
%33 = call i32 @stat(i8* %32, %struct.stat* %8)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %25
%37 = load i32, i32* %10
store i32 %37, i32* %3
br label %188
38: ; preds = %25
%39 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%40 = load i32, i32* %39
store i32 %40, i32* %9
%41 = load i8*, i8** %11
%42 = load i32, i32* %9
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %41, i32 %42)
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 104
%46 = sext i32 %45 to i64
store i64 %46, i64* %15
%47 = load i64, i64* %15
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %47)
%49 = load i64, i64* %15
%50 = call i8* @malloc(i64 %49)
store i8* %50, i8** %13
%51 = load i8*, i8** %13
%52 = icmp ne i8* %51, null
br i1 %52, label %55, label %53
53: ; preds = %38
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %188
55: ; preds = %38
%56 = load i8*, i8** %13
%57 = load i64, i64* %15
%58 = call i32 @memset(i8* %56, i32 0, i64 %57)
%59 = load i8*, i8** %11
%60 = load i32, i32* @O_RDONLY
%61 = call i32 (i8*, i32, ...) @open(i8* %59, i32 %60)
store i32 %61, i32* %6
%62 = load i32, i32* %6
%63 = icmp ne i32 %62, 0
br i1 %63, label %68, label %64
64: ; preds = %55
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
%66 = load i8*, i8** %13
%67 = call i32 @free(i8* %66)
store i32 0, i32* %3
br label %188
68: ; preds = %55
%69 = load i32, i32* %6
%70 = load i8*, i8** %13
%71 = getelementptr inbounds i8, i8* %70, i64 104
%72 = load i32, i32* %9
%73 = call i64 @read(i32 %69, i8* %71, i32 %72)
store i64 %73, i64* %16
%74 = load i64, i64* %16
%75 = load i32, i32* %9
%76 = sext i32 %75 to i64
%77 = icmp ult i64 %74, %76
br i1 %77, label %78, label %84
78: ; preds = %68
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%80 = load i8*, i8** %13
%81 = call i32 @free(i8* %80)
%82 = load i32, i32* %6
%83 = call i32 @close(i32 %82)
store i32 0, i32* %3
br label %188
84: ; preds = %68
%85 = load i32, i32* %6
%86 = call i32 @close(i32 %85)
%87 = load i8*, i8** %13
%88 = getelementptr inbounds i8, i8* %87, i64 0
%89 = call i32 @be_wr(i8* %88, i32 654645590)
%90 = load i8*, i8** %13
%91 = getelementptr inbounds i8, i8* %90, i64 8
%92 = call i32 @be_wr(i8* %91, i32 1275491212)
%93 = load i8*, i8** %13
%94 = getelementptr inbounds i8, i8* %93, i64 12
%95 = load i32, i32* %9
%96 = call i32 @be_wr(i8* %94, i32 %95)
%97 = load i8*, i8** %13
%98 = getelementptr inbounds i8, i8* %97, i64 16
%99 = call i32 @be_wr(i8* %98, i32 32768)
%100 = load i8*, i8** %13
%101 = getelementptr inbounds i8, i8* %100, i64 20
%102 = call i32 @be_wr(i8* %101, i32 32768)
%103 = load i8*, i8** %13
%104 = getelementptr inbounds i8, i8* %103, i64 28
store i8 5, i8* %104
%105 = load i8*, i8** %13
%106 = getelementptr inbounds i8, i8* %105, i64 29
store i8 2, i8* %106
%107 = load i8*, i8** %13
%108 = getelementptr inbounds i8, i8* %107, i64 30
store i8 2, i8* %108
%109 = load i8*, i8** %13
%110 = getelementptr inbounds i8, i8* %109, i64 31
store i8 1, i8* %110
%111 = load i8*, i8** %13
%112 = getelementptr inbounds i8, i8* %111, i64 32
%113 = call i32 @strncpy(i8* %112, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 32)
%114 = load i8*, i8** %13
%115 = getelementptr inbounds i8, i8* %114, i64 63
store i8 0, i8* %115
%116 = load i8*, i8** %13
%117 = getelementptr inbounds i8, i8* %116, i64 64
%118 = call i32 @strncpy(i8* %117, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32 16)
%119 = load i8*, i8** %13
%120 = getelementptr inbounds i8, i8* %119, i64 79
store i8 0, i8* %120
%121 = load i8*, i8** %13
%122 = getelementptr inbounds i8, i8* %121, i64 80
%123 = call i32 @strncpy(i8* %122, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32 16)
%124 = load i8*, i8** %13
%125 = getelementptr inbounds i8, i8* %124, i64 95
store i8 0, i8* %125
%126 = load i8*, i8** %13
%127 = getelementptr inbounds i8, i8* %126, i64 96
store i8 0, i8* %127
%128 = load i8*, i8** %13
%129 = getelementptr inbounds i8, i8* %128, i64 97
store i8 -128, i8* %129
%130 = load i8*, i8** %13
%131 = getelementptr inbounds i8, i8* %130, i64 98
store i8 -56, i8* %131
%132 = load i8*, i8** %13
%133 = getelementptr inbounds i8, i8* %132, i64 99
store i8 22, i8* %133
%134 = load i8*, i8** %13
%135 = getelementptr inbounds i8, i8* %134, i64 100
store i8 -127, i8* %135
%136 = load i8*, i8** %13
%137 = getelementptr inbounds i8, i8* %136, i64 101
store i8 104, i8* %137
%138 = load i8*, i8** %13
%139 = getelementptr inbounds i8, i8* %138, i64 104
%140 = load i32, i32* %9
%141 = call i32 @crc32(i32 0, i8* %139, i32 %140)
store i32 %141, i32* %14
%142 = load i8*, i8** %13
%143 = getelementptr inbounds i8, i8* %142, i64 24
%144 = load i32, i32* %14
%145 = call i32 @be_wr(i8* %143, i32 %144)
%146 = load i32, i32* %14
%147 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i32 %146)
%148 = load i8*, i8** %13
%149 = call i32 @crc32(i32 0, i8* %148, i32 104)
store i32 %149, i32* %14
%150 = load i8*, i8** %13
%151 = getelementptr inbounds i8, i8* %150, i64 4
%152 = load i32, i32* %14
%153 = call i32 @be_wr(i8* %151, i32 %152)
%154 = load i32, i32* %14
%155 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.11, i64 0, i64 0), i32 %154)
%156 = load i8*, i8** %12
%157 = load i64, i64* %15
%158 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.12, i64 0, i64 0), i8* %156, i64 %157)
%159 = load i8*, i8** %12
%160 = load i32, i32* @O_RDWR
%161 = load i32, i32* @O_CREAT
%162 = or i32 %160, %161
%163 = load i32, i32* @O_TRUNC
%164 = or i32 %162, %163
%165 = load i32, i32* @S_IRWXU
%166 = load i32, i32* @S_IRGRP
%167 = or i32 %165, %166
%168 = call i32 (i8*, i32, ...) @open(i8* %159, i32 %164, i32 %167)
store i32 %168, i32* %7
%169 = load i32, i32* %7
%170 = icmp ne i32 %169, 0
br i1 %170, label %173, label %171
171: ; preds = %84
%172 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.13, i64 0, i64 0))
store i32 0, i32* %3
br label %188
173: ; preds = %84
%174 = load i32, i32* %7
%175 = load i8*, i8** %13
%176 = load i64, i64* %15
%177 = call i64 @write(i32 %174, i8* %175, i64 %176)
store i64 %177, i64* %16
%178 = load i64, i64* %16
%179 = load i64, i64* %15
%180 = icmp ult i64 %178, %179
br i1 %180, label %181, label %183
181: ; preds = %173
%182 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.14, i64 0, i64 0))
store i32 0, i32* %3
br label %188
183: ; preds = %173
%184 = load i32, i32* %7
%185 = call i32 @close(i32 %184)
%186 = load i8*, i8** %13
%187 = call i32 @free(i8* %186)
store i32 0, i32* %3
br label %188
188: ; preds = %183, %181, %171, %78, %64, %53, %36
%189 = load i32, i32* %3
ret i32 %189
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [23 x i8] c"%s <infile> <outfile>\0A\00"
@.str.2 = [30 x i8] c"INFILE: %s, size: %08x bytes\0A\00"
@.str.3 = [22 x i8] c"Allocate %08zx bytes\0A\00"
@O_RDONLY = global i32 0
@.str.7 = [11 x i8] c"kernel.img\00"
@.str.8 = [10 x i8] c"dns-313v3\00"
@.str.9 = [8 x i8] c"2.01b04\00"
@.str.10 = [23 x i8] c"data checksum: 0x%08x\0A\00"
@.str.11 = [25 x i8] c"header checksum: 0x%08x\0A\00"
@.str.12 = [32 x i8] c"OUTFILE: %s, size: %08zx bytes\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@S_IRGRP = global i32 0
@str = [31 x i8] c"OOM: could not allocate buffer\00"
@str.15 = [33 x i8] c"ERROR: could not open input file\00"
@str.16 = [34 x i8] c"ERROR: could not open output file\00"
@str.17 = [44 x i8] c"ERROR: could not write complete output file\00"
@str.18 = [34 x i8] c"ERROR: could not read entire file\00"
@str.19 = [19 x i8] c"Too few arguments.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.19, i64 0, i64 0))
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %10
10: ; preds = %6, %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = call i32 @stat(i8* %12, %struct.stat* %3)
%16 = icmp slt i32 %15, 0
br i1 %16, label %104, label %17
17: ; preds = %10
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %12, i32 %19)
%21 = add nsw i32 %19, 104
%22 = sext i32 %21 to i64
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %22)
%24 = tail call i8* @malloc(i64 %22)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %104
28: ; preds = %17
%29 = tail call i32 @memset(i8* %24, i32 0, i64 %22)
%30 = load i32, i32* @O_RDONLY
%31 = tail call i32 (i8*, i32, ...) @open(i8* %12, i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.15, i64 0, i64 0))
%35 = tail call i32 @free(i8* %24)
br label %104
36: ; preds = %28
%37 = getelementptr inbounds i8, i8* %24, i64 104
%38 = tail call i64 @read(i32 %31, i8* %37, i32 %19)
%39 = sext i32 %19 to i64
%40 = icmp ult i64 %38, %39
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.18, i64 0, i64 0))
%43 = tail call i32 @free(i8* %24)
%44 = tail call i32 @close(i32 %31)
br label %104
45: ; preds = %36
%46 = tail call i32 @close(i32 %31)
%47 = tail call i32 @be_wr(i8* %24, i32 654645590)
%48 = getelementptr inbounds i8, i8* %24, i64 8
%49 = tail call i32 @be_wr(i8* %48, i32 1275491212)
%50 = getelementptr inbounds i8, i8* %24, i64 12
%51 = tail call i32 @be_wr(i8* %50, i32 %19)
%52 = getelementptr inbounds i8, i8* %24, i64 16
%53 = tail call i32 @be_wr(i8* %52, i32 32768)
%54 = getelementptr inbounds i8, i8* %24, i64 20
%55 = tail call i32 @be_wr(i8* %54, i32 32768)
%56 = getelementptr inbounds i8, i8* %24, i64 28
store i8 5, i8* %56
%57 = getelementptr inbounds i8, i8* %24, i64 29
store i8 2, i8* %57
%58 = getelementptr inbounds i8, i8* %24, i64 30
store i8 2, i8* %58
%59 = getelementptr inbounds i8, i8* %24, i64 31
store i8 1, i8* %59
%60 = getelementptr inbounds i8, i8* %24, i64 32
%61 = tail call i32 @strncpy(i8* %60, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 32)
%62 = getelementptr inbounds i8, i8* %24, i64 63
store i8 0, i8* %62
%63 = getelementptr inbounds i8, i8* %24, i64 64
%64 = tail call i32 @strncpy(i8* %63, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32 16)
%65 = getelementptr inbounds i8, i8* %24, i64 79
store i8 0, i8* %65
%66 = getelementptr inbounds i8, i8* %24, i64 80
%67 = tail call i32 @strncpy(i8* %66, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32 16)
%68 = getelementptr inbounds i8, i8* %24, i64 95
store i8 0, i8* %68
%69 = getelementptr inbounds i8, i8* %24, i64 96
store i8 0, i8* %69
%70 = getelementptr inbounds i8, i8* %24, i64 97
store i8 -128, i8* %70
%71 = getelementptr inbounds i8, i8* %24, i64 98
store i8 -56, i8* %71
%72 = getelementptr inbounds i8, i8* %24, i64 99
store i8 22, i8* %72
%73 = getelementptr inbounds i8, i8* %24, i64 100
store i8 -127, i8* %73
%74 = getelementptr inbounds i8, i8* %24, i64 101
store i8 104, i8* %74
%75 = tail call i32 @crc32(i32 0, i8* %37, i32 %19)
%76 = getelementptr inbounds i8, i8* %24, i64 24
%77 = tail call i32 @be_wr(i8* %76, i32 %75)
%78 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i32 %75)
%79 = tail call i32 @crc32(i32 0, i8* %24, i32 104)
%80 = getelementptr inbounds i8, i8* %24, i64 4
%81 = tail call i32 @be_wr(i8* %80, i32 %79)
%82 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.11, i64 0, i64 0), i32 %79)
%83 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.12, i64 0, i64 0), i8* %14, i64 %22)
%84 = load i32, i32* @O_RDWR
%85 = load i32, i32* @O_CREAT
%86 = or i32 %85, %84
%87 = load i32, i32* @O_TRUNC
%88 = or i32 %86, %87
%89 = load i32, i32* @S_IRWXU
%90 = load i32, i32* @S_IRGRP
%91 = or i32 %90, %89
%92 = tail call i32 (i8*, i32, ...) @open(i8* %14, i32 %88, i32 %91)
%93 = icmp eq i32 %92, 0
br i1 %93, label %94, label %96
94: ; preds = %45
%95 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.16, i64 0, i64 0))
br label %104
96: ; preds = %45
%97 = tail call i64 @write(i32 %92, i8* %24, i64 %22)
%98 = icmp ult i64 %97, %22
br i1 %98, label %99, label %101
99: ; preds = %96
%100 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.17, i64 0, i64 0))
br label %104
101: ; preds = %96
%102 = tail call i32 @close(i32 %92)
%103 = tail call i32 @free(i8* %24)
br label %104
104: ; preds = %10, %101, %99, %94, %41, %33, %26
%105 = phi i32 [ 0, %41 ], [ 0, %99 ], [ 0, %101 ], [ 0, %94 ], [ 0, %33 ], [ 0, %26 ], [ %15, %10 ]
ret i32 %105
}