#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;
long disassemble_8080(unsigned char *, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, long);
long 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;
  long rom_size, pc = 0;
  if (argc != 3) {
    printf("Usage: ./i8080-disassembler <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("fopen (asm)");
    exit(EXIT_FAILURE);
  }
  while (pc < rom_size) {
    fprintf(asm_file, "%0.4lx\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)
movq   $0x0,-0x30(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11c2 <main+0x42>
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e25(%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    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe52(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1214 <main+0x94>
lea    0xe39(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1219 <main+0x99>
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    1255 <main+0xd5>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xe10(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <disassemble_8080@plt>
mov    %rax,-0x30(%rbp)
jmp    1219 <main+0x99>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    120b <main+0x8b>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    %rsp,%rsi
call   1050 <load_rom@plt>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
lea    0xe56(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1219 <main+0x99>
mov    %rax,%r12
test   %r14,%r14
jle    11f5 <main+0x75>
xor    %ebx,%ebx
lea    0xe49(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
mov    (%rsp),%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1060 <disassemble_8080@plt>
mov    %rax,%rbx
cmp    %r14,%rax
jl     11d0 <main+0x50>
mov    %r12,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
jmp    1225 <main+0xa5>
lea    0xde2(%rip),%rdi
call   1090 <perror@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [56 x i8] c"Usage: ./i8080-disassembler <rom file> <assembly file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [12 x i8] c"fopen (asm)\00"
@.str.3 = [8 x i8] c"%0.4lx\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 i64
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %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 ([56 x i8], [56 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 i64 @load_rom(i8* %19, i8** %7)
store i64 %20, i64* %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 ([12 x i8], [12 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 i64, i64* %9
%34 = load i64, i64* %8
%35 = icmp slt i64 %33, %34
br i1 %35, label %36, label %44
36: ; preds = %32
%37 = load i32*, i32** %6
%38 = load i64, i64* %9
%39 = call i32 @fprintf(i32* %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i64 %38)
%40 = load i8*, i8** %7
%41 = load i64, i64* %9
%42 = load i32*, i32** %6
%43 = call i64 @disassemble_8080(i8* %40, i64 %41, i32* %42)
store i64 %43, i64* %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 = [12 x i8] c"fopen (asm)\00"
@.str.3 = [8 x i8] c"%0.4lx\09\00"
@str = [55 x i8] c"Usage: ./i8080-disassembler <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 ([55 x i8], [55 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 i64 @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 i64 %13, 0
br i1 %19, label %24, label %30
20: ; preds = %10
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 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 i64 [ %28, %24 ], [ 0, %18 ]
%26 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i64 %25)
%27 = load i8*, i8** %3
%28 = call i64 @disassemble_8080(i8* %27, i64 %25, i32* %16)
%29 = icmp slt i64 %28, %13
br i1 %29, label %24, label %30
30: ; preds = %24, %18
%31 = call i32 @fclose(i32* %16)
ret i32 0
}