#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");
    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);
  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    15b0 <main+0x3f0>
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    15b0 <main+0x3f0>
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    1313 <main+0x153>
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    15b0 <main+0x3f0>
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    1366 <main+0x1a6>
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    15b0 <main+0x3f0>
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    0xcca(%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    0xcb4(%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    0xc9d(%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    0xc34(%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    0xc17(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x40(%rbp),%rsi
mov    -0x60(%rbp),%rdx
lea    0xc1a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x40(%rbp),%rdi
mov    0x2ac8(%rip),%rax
mov    (%rax),%esi
mov    0x2ae7(%rip),%rax
or     (%rax),%esi
mov    0x2ad6(%rip),%rax
or     (%rax),%esi
mov    0x2ab5(%rip),%rax
mov    (%rax),%edx
mov    0x2a8c(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   10e0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    155c <main+0x39c>
lea    0xbe6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    15b0 <main+0x3f0>
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    1598 <main+0x3d8>
lea    0xbcd(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    15b0 <main+0x3f0>
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     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    0xe95(%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    0xddf(%rip),%rdi
call   1050 <puts@plt>
jmp    1499 <main+0x2c9>
lea    0xded(%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    0xcf4(%rip),%rsi
mov    $0x20,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x3f(%r13)
lea    0x40(%r13),%rdi
lea    0xce5(%rip),%rsi
mov    $0x10,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x4f(%r13)
lea    0x50(%r13),%rdi
lea    0xcd5(%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    0xc9c(%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    0xc84(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xc8d(%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    0xca6(%rip),%rdi
call   1050 <puts@plt>
jmp    1496 <main+0x2c6>
lea    0xc76(%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 = [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 %194
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 %194
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 %73, 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))
%71 = load i8*, i8** %14
%72 = call i32 @free(i8* %71)
store i32 0, i32* %3
br label %194
73: ; preds = %60
%74 = load i32, i32* %6
%75 = load i8*, i8** %14
%76 = getelementptr inbounds i8, i8* %75, i64 104
%77 = load i32, i32* %9
%78 = call i64 @read(i32 %74, i8* %76, i32 %77)
store i64 %78, i64* %18
%79 = load i64, i64* %18
%80 = load i32, i32* %9
%81 = sext i32 %80 to i64
%82 = icmp ult i64 %79, %81
br i1 %82, label %83, label %89
83: ; preds = %73
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%85 = load i8*, i8** %14
%86 = call i32 @free(i8* %85)
%87 = load i32, i32* %6
%88 = call i32 @close(i32 %87)
store i32 0, i32* %3
br label %194
89: ; preds = %73
%90 = load i32, i32* %6
%91 = call i32 @close(i32 %90)
%92 = load i8*, i8** %14
store i8* %92, i8** %15
%93 = load i8*, i8** %14
%94 = getelementptr inbounds i8, i8* %93, i64 0
%95 = call i32 @be_wr(i8* %94, i32 654645590)
%96 = load i8*, i8** %14
%97 = getelementptr inbounds i8, i8* %96, i64 8
%98 = call i32 @be_wr(i8* %97, i32 1275491212)
%99 = load i8*, i8** %14
%100 = getelementptr inbounds i8, i8* %99, i64 12
%101 = load i32, i32* %9
%102 = call i32 @be_wr(i8* %100, i32 %101)
%103 = load i8*, i8** %14
%104 = getelementptr inbounds i8, i8* %103, i64 16
%105 = call i32 @be_wr(i8* %104, i32 32768)
%106 = load i8*, i8** %14
%107 = getelementptr inbounds i8, i8* %106, i64 20
%108 = call i32 @be_wr(i8* %107, i32 32768)
%109 = load i8*, i8** %14
%110 = getelementptr inbounds i8, i8* %109, i64 28
store i8 5, i8* %110
%111 = load i8*, i8** %14
%112 = getelementptr inbounds i8, i8* %111, i64 29
store i8 2, i8* %112
%113 = load i8*, i8** %14
%114 = getelementptr inbounds i8, i8* %113, i64 30
store i8 2, i8* %114
%115 = load i8*, i8** %14
%116 = getelementptr inbounds i8, i8* %115, i64 31
store i8 1, i8* %116
%117 = load i8*, i8** %14
%118 = getelementptr inbounds i8, i8* %117, i64 32
%119 = call i32 @strncpy(i8* %118, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 32)
%120 = load i8*, i8** %14
%121 = getelementptr inbounds i8, i8* %120, i64 63
store i8 0, i8* %121
%122 = load i8*, i8** %14
%123 = getelementptr inbounds i8, i8* %122, i64 64
%124 = call i32 @strncpy(i8* %123, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32 16)
%125 = load i8*, i8** %14
%126 = getelementptr inbounds i8, i8* %125, i64 79
store i8 0, i8* %126
%127 = load i8*, i8** %14
%128 = getelementptr inbounds i8, i8* %127, i64 80
%129 = call i32 @strncpy(i8* %128, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32 16)
%130 = load i8*, i8** %14
%131 = getelementptr inbounds i8, i8* %130, i64 95
store i8 0, i8* %131
%132 = load i8*, i8** %14
%133 = getelementptr inbounds i8, i8* %132, i64 96
store i8 0, i8* %133
%134 = load i8*, i8** %14
%135 = getelementptr inbounds i8, i8* %134, i64 97
store i8 -128, i8* %135
%136 = load i8*, i8** %14
%137 = getelementptr inbounds i8, i8* %136, i64 98
store i8 -56, i8* %137
%138 = load i8*, i8** %14
%139 = getelementptr inbounds i8, i8* %138, i64 99
store i8 22, i8* %139
%140 = load i8*, i8** %14
%141 = getelementptr inbounds i8, i8* %140, i64 100
store i8 -127, i8* %141
%142 = load i8*, i8** %14
%143 = getelementptr inbounds i8, i8* %142, i64 101
store i8 104, i8* %143
%144 = load i8*, i8** %14
%145 = getelementptr inbounds i8, i8* %144, i64 104
%146 = load i32, i32* %9
%147 = call i32 @crc32(i32 0, i8* %145, i32 %146)
store i32 %147, i32* %16
%148 = load i8*, i8** %14
%149 = getelementptr inbounds i8, i8* %148, i64 24
%150 = load i32, i32* %16
%151 = call i32 @be_wr(i8* %149, i32 %150)
%152 = load i32, i32* %16
%153 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i32 %152)
%154 = load i8*, i8** %14
%155 = call i32 @crc32(i32 0, i8* %154, i32 104)
store i32 %155, i32* %16
%156 = load i8*, i8** %14
%157 = getelementptr inbounds i8, i8* %156, i64 4
%158 = load i32, i32* %16
%159 = call i32 @be_wr(i8* %157, i32 %158)
%160 = load i32, i32* %16
%161 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.11, i64 0, i64 0), i32 %160)
%162 = load i8*, i8** %13
%163 = load i64, i64* %17
%164 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.12, i64 0, i64 0), i8* %162, i64 %163)
%165 = load i8*, i8** %13
%166 = load i32, i32* @O_RDWR
%167 = load i32, i32* @O_CREAT
%168 = or i32 %166, %167
%169 = load i32, i32* @O_TRUNC
%170 = or i32 %168, %169
%171 = load i32, i32* @S_IRWXU
%172 = load i32, i32* @S_IRGRP
%173 = or i32 %171, %172
%174 = call i32 (i8*, i32, ...) @open(i8* %165, i32 %170, i32 %173)
store i32 %174, i32* %7
%175 = load i32, i32* %7
%176 = icmp ne i32 %175, 0
br i1 %176, label %179, label %177
177: ; preds = %89
%178 = 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 %194
179: ; preds = %89
%180 = load i32, i32* %7
%181 = load i8*, i8** %14
%182 = load i64, i64* %17
%183 = call i64 @write(i32 %180, i8* %181, i64 %182)
store i64 %183, i64* %18
%184 = load i64, i64* %18
%185 = load i64, i64* %17
%186 = icmp ult i64 %184, %185
br i1 %186, label %187, label %189
187: ; preds = %179
%188 = 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 %194
189: ; preds = %179
%190 = load i32, i32* %7
%191 = call i32 @close(i32 %190)
%192 = load i8*, i8** %14
%193 = call i32 @free(i8* %192)
store i32 0, i32* %3
br label %194
194: ; preds = %189, %187, %177, %83, %69, %58, %39
%195 = load i32, i32* %3
ret i32 %195
}

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 %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 ([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 %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 ([31 x i8], [31 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
}