#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(unsigned char *, int);
int /*<<< orphan*/ close(int);
int crc32(int /*<<< orphan*/, unsigned char *, int);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(size_t);
int /*<<< orphan*/ memset(unsigned char *, int, size_t);
int open(char const *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, unsigned char *, int);
int stat(char const *, struct stat *);
int /*<<< orphan*/ strncpy(unsigned char *, char *, int);
size_t write(int, unsigned char *, size_t);
int main(int argc, char **argv) {
  int fdin;
  int fdout;
  struct stat sb;
  uint32_t filesize;
  uint32_t padding;
  int ret = 0;
  const char *pathin;
  const char *pathout;
  unsigned char *buffer;
  unsigned char *infop;
  uint32_t sum;
  size_t bufsize;
  size_t bytes;
  int i;
  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;
  padding = filesize % 4;
  printf("INFILE: %s, size: %08x bytes\n", pathin, filesize);
  bufsize = filesize + 0x68;
  printf("Allocate %08x 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");
    return 0;
  }
  bytes = read(fdin, buffer + 0x68, filesize);
  if (bytes < filesize) {
    printf("ERROR: could not read entire file\n");
    return 0;
  }
  close(fdin);
  infop = buffer;
  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: %08x 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    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x2c(%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,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10b0 <stat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1247 <main+0x87>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1596 <main+0x3d6>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x68,%eax
cltq
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rsi
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x60(%rbp),%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12c0 <main+0x100>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1596 <main+0x3d6>
mov    -0x48(%rbp),%rdi
mov    -0x60(%rbp),%rdx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10e0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    130a <main+0x14a>
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1596 <main+0x3d6>
mov    -0x14(%rbp),%edi
mov    -0x48(%rbp),%rsi
add    $0x68,%rsi
mov    -0x24(%rbp),%edx
call   10a0 <read@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmp    %rcx,%rax
jae    134c <main+0x18c>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1596 <main+0x3d6>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
mov    $0x27051956,%esi
call   10d0 <be_wr@plt>
mov    -0x48(%rbp),%rdi
add    $0x8,%rdi
mov    $0x4c06738c,%esi
call   10d0 <be_wr@plt>
mov    -0x48(%rbp),%rdi
add    $0xc,%rdi
mov    -0x24(%rbp),%esi
call   10d0 <be_wr@plt>
mov    -0x48(%rbp),%rdi
add    $0x10,%rdi
mov    $0x8000,%esi
call   10d0 <be_wr@plt>
mov    -0x48(%rbp),%rdi
add    $0x14,%rdi
mov    $0x8000,%esi
call   10d0 <be_wr@plt>
mov    -0x48(%rbp),%rax
movb   $0x5,0x1c(%rax)
mov    -0x48(%rbp),%rax
movb   $0x2,0x1d(%rax)
mov    -0x48(%rbp),%rax
movb   $0x2,0x1e(%rax)
mov    -0x48(%rbp),%rax
movb   $0x1,0x1f(%rax)
mov    -0x48(%rbp),%rdi
add    $0x20,%rdi
lea    0xce4(%rip),%rsi
mov    $0x20,%edx
call   1040 <strncpy@plt>
mov    -0x48(%rbp),%rax
movb   $0x0,0x3f(%rax)
mov    -0x48(%rbp),%rdi
add    $0x40,%rdi
lea    0xcce(%rip),%rsi
mov    $0x10,%edx
call   1040 <strncpy@plt>
mov    -0x48(%rbp),%rax
movb   $0x0,0x4f(%rax)
mov    -0x48(%rbp),%rdi
add    $0x50,%rdi
lea    0xcb7(%rip),%rsi
mov    $0x10,%edx
call   1040 <strncpy@plt>
mov    -0x48(%rbp),%rax
movb   $0x0,0x5f(%rax)
mov    -0x48(%rbp),%rax
movb   $0x0,0x60(%rax)
mov    -0x48(%rbp),%rax
movb   $0x80,0x61(%rax)
mov    -0x48(%rbp),%rax
movb   $0xc8,0x62(%rax)
mov    -0x48(%rbp),%rax
movb   $0x16,0x63(%rax)
mov    -0x48(%rbp),%rax
movb   $0x81,0x64(%rax)
mov    -0x48(%rbp),%rax
movb   $0x68,0x65(%rax)
mov    -0x48(%rbp),%rsi
add    $0x68,%rsi
mov    -0x24(%rbp),%edx
xor    %edi,%edi
call   1050 <crc32@plt>
mov    %eax,-0x54(%rbp)
mov    -0x48(%rbp),%rdi
add    $0x18,%rdi
mov    -0x54(%rbp),%esi
call   10d0 <be_wr@plt>
mov    -0x54(%rbp),%esi
lea    0xc4e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x48(%rbp),%rsi
xor    %edi,%edi
mov    $0x68,%edx
call   1050 <crc32@plt>
mov    %eax,-0x54(%rbp)
mov    -0x48(%rbp),%rdi
add    $0x4,%rdi
mov    -0x54(%rbp),%esi
call   10d0 <be_wr@plt>
mov    -0x54(%rbp),%esi
lea    0xc31(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x40(%rbp),%rsi
mov    -0x60(%rbp),%rdx
lea    0xc34(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x40(%rbp),%rdi
mov    0x2ae2(%rip),%rax
mov    (%rax),%esi
mov    0x2b01(%rip),%rax
or     (%rax),%esi
mov    0x2af0(%rip),%rax
or     (%rax),%esi
mov    0x2acf(%rip),%rax
mov    (%rax),%edx
mov    0x2aa6(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   10e0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1542 <main+0x382>
lea    0xc00(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1596 <main+0x3d6>
mov    -0x18(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x60(%rbp),%rdx
call   1060 <write@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
cmp    -0x60(%rbp),%rax
jae    157e <main+0x3be>
lea    0xbe7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1596 <main+0x3d6>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%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    0xf79(%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     12b9 <main+0xe9>
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     12c1 <main+0xf1>
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     12d2 <main+0x102>
mov    %eax,%ebx
lea    0x68(%r13),%r12
mov    %eax,%edi
mov    %r12,%rsi
mov    %r14d,%edx
call   10b0 <read@plt>
cmp    %r14,%rax
jae    12e3 <main+0x113>
lea    0xe95(%rip),%rdi
call   1050 <puts@plt>
jmp    147f <main+0x2af>
mov    %eax,%r12d
jmp    1482 <main+0x2b2>
lea    0xdee(%rip),%rdi
call   1050 <puts@plt>
jmp    1482 <main+0x2b2>
lea    0xdfc(%rip),%rdi
call   1050 <puts@plt>
jmp    1482 <main+0x2b2>
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    0xd0b(%rip),%rsi
mov    $0x20,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x3f(%r13)
lea    0x40(%r13),%rdi
lea    0xcfc(%rip),%rsi
mov    $0x10,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x4f(%r13)
lea    0x50(%r13),%rdi
lea    0xcec(%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    0xcb3(%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    0xc9b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xca4(%rip),%rdi
mov    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2bc4(%rip),%rax
mov    0x2be5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2bd2(%rip),%rax
or     (%rax),%esi
mov    0x2bb1(%rip),%rax
mov    0x2b8a(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   10f0 <open@plt>
test   %eax,%eax
je     1462 <main+0x292>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r13,%rsi
mov    %r15,%rdx
call   1070 <write@plt>
cmp    %r15,%rax
jae    1470 <main+0x2a0>
lea    0xcbd(%rip),%rdi
call   1050 <puts@plt>
jmp    147f <main+0x2af>
lea    0xc8d(%rip),%rdi
call   1050 <puts@plt>
jmp    1482 <main+0x2b2>
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 = [21 x i8] c"Allocate %08x 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 = [31 x i8] c"OUTFILE: %s, size: %08x 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 i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i64
%18 = alloca i64
%19 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %11
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %28
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %22, %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
store i8* %31, i8** %12
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
store i8* %34, i8** %13
%35 = load i8*, i8** %12
%36 = call i32 @stat(i8* %35, %struct.stat* %8)
store i32 %36, i32* %11
%37 = load i32, i32* %11
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %28
%40 = load i32, i32* %11
store i32 %40, i32* %3
br label %188
41: ; preds = %28
%42 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%43 = load i32, i32* %42
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = srem i32 %44, 4
store i32 %45, i32* %10
%46 = load i8*, i8** %12
%47 = load i32, i32* %9
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %46, i32 %47)
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 104
%51 = sext i32 %50 to i64
store i64 %51, i64* %17
%52 = load i64, i64* %17
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i64 %52)
%54 = load i64, i64* %17
%55 = call i8* @malloc(i64 %54)
store i8* %55, i8** %14
%56 = load i8*, i8** %14
%57 = icmp ne i8* %56, null
br i1 %57, label %60, label %58
58: ; preds = %41
%59 = 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
60: ; preds = %41
%61 = load i8*, i8** %14
%62 = load i64, i64* %17
%63 = call i32 @memset(i8* %61, i32 0, i64 %62)
%64 = load i8*, i8** %12
%65 = load i32, i32* @O_RDONLY
%66 = call i32 (i8*, i32, ...) @open(i8* %64, i32 %65)
store i32 %66, i32* %6
%67 = load i32, i32* %6
%68 = icmp ne i32 %67, 0
br i1 %68, label %71, label %69
69: ; preds = %60
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %188
71: ; preds = %60
%72 = load i32, i32* %6
%73 = load i8*, i8** %14
%74 = getelementptr inbounds i8, i8* %73, i64 104
%75 = load i32, i32* %9
%76 = call i64 @read(i32 %72, i8* %74, i32 %75)
store i64 %76, i64* %18
%77 = load i64, i64* %18
%78 = load i32, i32* %9
%79 = sext i32 %78 to i64
%80 = icmp ult i64 %77, %79
br i1 %80, label %81, label %83
81: ; preds = %71
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %3
br label %188
83: ; preds = %71
%84 = load i32, i32* %6
%85 = call i32 @close(i32 %84)
%86 = load i8*, i8** %14
store i8* %86, i8** %15
%87 = load i8*, i8** %14
%88 = getelementptr inbounds i8, i8* %87, i64 0
%89 = call i32 @be_wr(i8* %88, i32 654645590)
%90 = load i8*, i8** %14
%91 = getelementptr inbounds i8, i8* %90, i64 8
%92 = call i32 @be_wr(i8* %91, i32 1275491212)
%93 = load i8*, i8** %14
%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** %14
%98 = getelementptr inbounds i8, i8* %97, i64 16
%99 = call i32 @be_wr(i8* %98, i32 32768)
%100 = load i8*, i8** %14
%101 = getelementptr inbounds i8, i8* %100, i64 20
%102 = call i32 @be_wr(i8* %101, i32 32768)
%103 = load i8*, i8** %14
%104 = getelementptr inbounds i8, i8* %103, i64 28
store i8 5, i8* %104
%105 = load i8*, i8** %14
%106 = getelementptr inbounds i8, i8* %105, i64 29
store i8 2, i8* %106
%107 = load i8*, i8** %14
%108 = getelementptr inbounds i8, i8* %107, i64 30
store i8 2, i8* %108
%109 = load i8*, i8** %14
%110 = getelementptr inbounds i8, i8* %109, i64 31
store i8 1, i8* %110
%111 = load i8*, i8** %14
%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** %14
%115 = getelementptr inbounds i8, i8* %114, i64 63
store i8 0, i8* %115
%116 = load i8*, i8** %14
%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** %14
%120 = getelementptr inbounds i8, i8* %119, i64 79
store i8 0, i8* %120
%121 = load i8*, i8** %14
%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** %14
%125 = getelementptr inbounds i8, i8* %124, i64 95
store i8 0, i8* %125
%126 = load i8*, i8** %14
%127 = getelementptr inbounds i8, i8* %126, i64 96
store i8 0, i8* %127
%128 = load i8*, i8** %14
%129 = getelementptr inbounds i8, i8* %128, i64 97
store i8 -128, i8* %129
%130 = load i8*, i8** %14
%131 = getelementptr inbounds i8, i8* %130, i64 98
store i8 -56, i8* %131
%132 = load i8*, i8** %14
%133 = getelementptr inbounds i8, i8* %132, i64 99
store i8 22, i8* %133
%134 = load i8*, i8** %14
%135 = getelementptr inbounds i8, i8* %134, i64 100
store i8 -127, i8* %135
%136 = load i8*, i8** %14
%137 = getelementptr inbounds i8, i8* %136, i64 101
store i8 104, i8* %137
%138 = load i8*, i8** %14
%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* %16
%142 = load i8*, i8** %14
%143 = getelementptr inbounds i8, i8* %142, i64 24
%144 = load i32, i32* %16
%145 = call i32 @be_wr(i8* %143, i32 %144)
%146 = load i32, i32* %16
%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** %14
%149 = call i32 @crc32(i32 0, i8* %148, i32 104)
store i32 %149, i32* %16
%150 = load i8*, i8** %14
%151 = getelementptr inbounds i8, i8* %150, i64 4
%152 = load i32, i32* %16
%153 = call i32 @be_wr(i8* %151, i32 %152)
%154 = load i32, i32* %16
%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** %13
%157 = load i64, i64* %17
%158 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.12, i64 0, i64 0), i8* %156, i64 %157)
%159 = load i8*, i8** %13
%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 = %83
%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 = %83
%174 = load i32, i32* %7
%175 = load i8*, i8** %14
%176 = load i64, i64* %17
%177 = call i64 @write(i32 %174, i8* %175, i64 %176)
store i64 %177, i64* %18
%178 = load i64, i64* %18
%179 = load i64, i64* %17
%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** %14
%187 = call i32 @free(i8* %186)
store i32 0, i32* %3
br label %188
188: ; preds = %183, %181, %171, %81, %69, %58, %39
%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 = [21 x i8] c"Allocate %08x 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 = [31 x i8] c"OUTFILE: %s, size: %08x 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 %101, 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 ([21 x i8], [21 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 %101
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 %35
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))
br label %101
35: ; preds = %28
%36 = getelementptr inbounds i8, i8* %24, i64 104
%37 = tail call i64 @read(i32 %31, i8* %36, i32 %19)
%38 = sext i32 %19 to i64
%39 = icmp ult i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.18, i64 0, i64 0))
br label %101
42: ; preds = %35
%43 = tail call i32 @close(i32 %31)
%44 = tail call i32 @be_wr(i8* %24, i32 654645590)
%45 = getelementptr inbounds i8, i8* %24, i64 8
%46 = tail call i32 @be_wr(i8* %45, i32 1275491212)
%47 = getelementptr inbounds i8, i8* %24, i64 12
%48 = tail call i32 @be_wr(i8* %47, i32 %19)
%49 = getelementptr inbounds i8, i8* %24, i64 16
%50 = tail call i32 @be_wr(i8* %49, i32 32768)
%51 = getelementptr inbounds i8, i8* %24, i64 20
%52 = tail call i32 @be_wr(i8* %51, i32 32768)
%53 = getelementptr inbounds i8, i8* %24, i64 28
store i8 5, i8* %53
%54 = getelementptr inbounds i8, i8* %24, i64 29
store i8 2, i8* %54
%55 = getelementptr inbounds i8, i8* %24, i64 30
store i8 2, i8* %55
%56 = getelementptr inbounds i8, i8* %24, i64 31
store i8 1, i8* %56
%57 = getelementptr inbounds i8, i8* %24, i64 32
%58 = tail call i32 @strncpy(i8* %57, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 32)
%59 = getelementptr inbounds i8, i8* %24, i64 63
store i8 0, i8* %59
%60 = getelementptr inbounds i8, i8* %24, i64 64
%61 = tail call i32 @strncpy(i8* %60, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32 16)
%62 = getelementptr inbounds i8, i8* %24, i64 79
store i8 0, i8* %62
%63 = getelementptr inbounds i8, i8* %24, i64 80
%64 = tail call i32 @strncpy(i8* %63, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32 16)
%65 = getelementptr inbounds i8, i8* %24, i64 95
store i8 0, i8* %65
%66 = getelementptr inbounds i8, i8* %24, i64 96
store i8 0, i8* %66
%67 = getelementptr inbounds i8, i8* %24, i64 97
store i8 -128, i8* %67
%68 = getelementptr inbounds i8, i8* %24, i64 98
store i8 -56, i8* %68
%69 = getelementptr inbounds i8, i8* %24, i64 99
store i8 22, i8* %69
%70 = getelementptr inbounds i8, i8* %24, i64 100
store i8 -127, i8* %70
%71 = getelementptr inbounds i8, i8* %24, i64 101
store i8 104, i8* %71
%72 = tail call i32 @crc32(i32 0, i8* %36, i32 %19)
%73 = getelementptr inbounds i8, i8* %24, i64 24
%74 = tail call i32 @be_wr(i8* %73, i32 %72)
%75 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i32 %72)
%76 = tail call i32 @crc32(i32 0, i8* %24, i32 104)
%77 = getelementptr inbounds i8, i8* %24, i64 4
%78 = tail call i32 @be_wr(i8* %77, i32 %76)
%79 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.11, i64 0, i64 0), i32 %76)
%80 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.12, i64 0, i64 0), i8* %14, i64 %22)
%81 = load i32, i32* @O_RDWR
%82 = load i32, i32* @O_CREAT
%83 = or i32 %82, %81
%84 = load i32, i32* @O_TRUNC
%85 = or i32 %83, %84
%86 = load i32, i32* @S_IRWXU
%87 = load i32, i32* @S_IRGRP
%88 = or i32 %87, %86
%89 = tail call i32 (i8*, i32, ...) @open(i8* %14, i32 %85, i32 %88)
%90 = icmp eq i32 %89, 0
br i1 %90, label %91, label %93
91: ; preds = %42
%92 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.16, i64 0, i64 0))
br label %101
93: ; preds = %42
%94 = tail call i64 @write(i32 %89, i8* %24, i64 %22)
%95 = icmp ult i64 %94, %22
br i1 %95, label %96, label %98
96: ; preds = %93
%97 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.17, i64 0, i64 0))
br label %101
98: ; preds = %93
%99 = tail call i32 @close(i32 %89)
%100 = tail call i32 @free(i8* %24)
br label %101
101: ; preds = %10, %98, %96, %91, %40, %33, %26
%102 = phi i32 [ 0, %40 ], [ 0, %96 ], [ 0, %98 ], [ 0, %91 ], [ 0, %33 ], [ 0, %26 ], [ %15, %10 ]
ret i32 %102
}