#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*/ EXIT_FAILURE;
int disassemble_8080(unsigned char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int load_rom(char *, unsigned char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  FILE *asm_file;
  unsigned char *rom_buffer;
  int rom_size, pc = 0;
  if (argc != 3) {
    printf("Usage: ./a.out <rom file> <assembly file>\n");
    exit(EXIT_FAILURE);
  }
  rom_size = load_rom(argv[1], &rom_buffer);
  asm_file = fopen(argv[2], "w");
  if (asm_file == NULL) {
    perror("Error opening asssembly file.");
    exit(EXIT_FAILURE);
  }
  while (pc < rom_size) {
    fprintf(asm_file, "%0.4x\t", pc);
    pc = disassemble_8080(rom_buffer, pc, asm_file);
  }
  fclose(asm_file);
}


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)
movl   $0x0,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11c1 <main+0x41>
lea    0xe52(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <load_rom@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe47(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1212 <main+0x92>
lea    0xe2e(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1217 <main+0x97>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    124e <main+0xce>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%edx
lea    0xe1a(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1060 <disassemble_8080@plt>
mov    %eax,-0x28(%rbp)
jmp    1217 <main+0x97>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    1207 <main+0x87>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    %rsp,%rsi
call   1050 <load_rom@plt>
mov    %eax,%r15d
mov    0x10(%rbx),%rdi
lea    0xe57(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1215 <main+0x95>
mov    %rax,%rbx
test   %r15d,%r15d
jle    11f2 <main+0x72>
xor    %ebp,%ebp
lea    0xe5c(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1070 <fprintf@plt>
mov    (%rsp),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1060 <disassemble_8080@plt>
mov    %eax,%ebp
cmp    %r15d,%eax
jl     11d0 <main+0x50>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe19(%rip),%rdi
call   1030 <puts@plt>
jmp    1221 <main+0xa1>
lea    0xde6(%rip),%rdi
call   1090 <perror@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [43 x i8] c"Usage: ./a.out <rom file> <assembly file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [30 x i8] c"Error opening asssembly file.\00"
@.str.3 = [7 x i8] c"%0.4x\09\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @load_rom(i8* %19, i8** %7)
store i32 %20, i32* %8
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %6
%25 = load i32*, i32** %6
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %31
27: ; preds = %16
%28 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %16
br label %32
32: ; preds = %36, %31
%33 = load i32, i32* %9
%34 = load i32, i32* %8
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %44
36: ; preds = %32
%37 = load i32*, i32** %6
%38 = load i32, i32* %9
%39 = call i32 @fprintf(i32* %37, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = load i8*, i8** %7
%41 = load i32, i32* %9
%42 = load i32*, i32** %6
%43 = call i32 @disassemble_8080(i8* %40, i32 %41, i32* %42)
store i32 %43, i32* %9
br label %32
44: ; preds = %32
%45 = load i32*, i32** %6
%46 = call i32 @fclose(i32* %45)
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [30 x i8] c"Error opening asssembly file.\00"
@.str.3 = [7 x i8] c"%0.4x\09\00"
@str = [42 x i8] c"Usage: ./a.out <rom file> <assembly file>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = call i32 @load_rom(i8* %12, i8** %3)
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %10
%19 = icmp sgt i32 %13, 0
br i1 %19, label %24, label %30
20: ; preds = %10
%21 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %18, %24
%25 = phi i32 [ %28, %24 ], [ 0, %18 ]
%26 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = load i8*, i8** %3
%28 = call i32 @disassemble_8080(i8* %27, i32 %25, i32* %16)
%29 = icmp slt i32 %28, %13
br i1 %29, label %24, label %30
30: ; preds = %24, %18
%31 = call i32 @fclose(i32* %16)
ret i32 0
}