#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ DisassembleCH8(unsigned char *, int);
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  FILE *fp = fopen(argv[1], "rb");
  if (fp == NULL) {
    printf("Error could not read file.");
    exit(1);
  }
  fseek(fp, 0, SEEK_END);
  int fileSize = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  unsigned char *buffer = malloc(fileSize + 0x200);
  fread(buffer + 0x200, 1, fileSize, fp);
  fclose(fp);
  int pc = 0x200;
  while (pc < (fileSize + 0x200)) {
    DisassembleCH8(buffer, pc);
    pc += 2;
    printf("\n");
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe4b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11df <main+0x4f>
lea    0xe33(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x1c(%rbp),%edi
add    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x200,%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x200,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x1c(%rbp),%ecx
add    $0x200,%ecx
cmp    %ecx,%eax
jge    128b <main+0xfb>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1060 <DisassembleCH8@plt>
mov    -0x2c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x2c(%rbp)
lea    0xd9d(%rip),%rdi
call   1050 <printf@plt>
jmp    1251 <main+0xc1>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
lea    0xe4e(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1261 <main+0xc1>
mov    %rax,%r14
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebp
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0x200(%rbp),%r15d
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
lea    0x200(%rax),%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
test   %ebp,%ebp
jle    124c <main+0xac>
mov    $0x200,%ebp
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <DisassembleCH8@plt>
add    $0x2,%ebp
mov    $0xa,%edi
call   1030 <putchar@plt>
cmp    %r15d,%ebp
jl     1230 <main+0x90>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9b(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [27 x i8] c"Error could not read file.\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [2 x i8] c"\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 i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32*, i32** %6
%21 = load i32, i32* @SEEK_END
%22 = call i32 @fseek(i32* %20, i32 0, i32 %21)
%23 = load i32*, i32** %6
%24 = call i32 @ftell(i32* %23)
store i32 %24, i32* %7
%25 = load i32*, i32** %6
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %25, i32 0, i32 %26)
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 512
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %8
%31 = load i8*, i8** %8
%32 = getelementptr inbounds i8, i8* %31, i64 512
%33 = load i32, i32* %7
%34 = load i32*, i32** %6
%35 = call i32 @fread(i8* %32, i32 1, i32 %33, i32* %34)
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
store i32 512, i32* %9
br label %38
38: ; preds = %43, %19
%39 = load i32, i32* %9
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 512
%42 = icmp slt i32 %39, %41
br i1 %42, label %43, label %50
43: ; preds = %38
%44 = load i8*, i8** %8
%45 = load i32, i32* %9
%46 = call i32 @DisassembleCH8(i8* %44, i32 %45)
%47 = load i32, i32* %9
%48 = add nsw i32 %47, 2
store i32 %48, i32* %9
%49 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %38
50: ; preds = %38
%51 = load i32*, i32** %6
%52 = call i32 @fclose(i32* %51)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [27 x i8] c"Error could not read file.\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %5, i32 0, i32 %11)
%13 = tail call i32 @ftell(i32* %5)
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %5, i32 0, i32 %14)
%16 = add nsw i32 %13, 512
%17 = tail call i8* @malloc(i32 %16)
%18 = getelementptr inbounds i8, i8* %17, i64 512
%19 = tail call i32 @fread(i8* %18, i32 1, i32 %13, i32* %5)
%20 = tail call i32 @fclose(i32* %5)
%21 = icmp sgt i32 %13, 0
br i1 %21, label %22, label %28
22: ; preds = %10, %22
%23 = phi i32 [ %25, %22 ], [ 512, %10 ]
%24 = tail call i32 @DisassembleCH8(i8* %17, i32 %23)
%25 = add nuw nsw i32 %23, 2
%26 = tail call i32 @putchar(i32 10)
%27 = icmp slt i32 %25, %16
br i1 %27, label %22, label %28
28: ; preds = %22, %10
%29 = tail call i32 @fclose(i32* %5)
ret i32 0
}