#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*/ DUMP_REGISTERS();
int /*<<< orphan*/ Disassemble(unsigned char *, int);
int /*<<< orphan*/ MEM_Handler(unsigned char *);
int PinGetPIN_A();
int PinGetPIN_D();
scalar_t__ PinGetPIN_SYNC();
int /*<<< orphan*/ PinSetO1(int);
int /*<<< orphan*/ PinSetO2(int);
int /*<<< orphan*/ PinSetRESET(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
void TEST_VIA_BINARY(const char *filename) {
  unsigned char someMemory[0x10000];
  FILE *infile = fopen(filename, "rb");
  if (infile == NULL) {
    exit(3);
  }
  fread(someMemory, 1, 1262, infile);
  fclose(infile);
  PinSetRESET(1);
  PinSetO1(1);
  PinSetO1(0);
  PinSetO2(1);
  PinSetO2(0);
  PinSetO1(1);
  PinSetO1(0);
  PinSetO2(1);
  PinSetO2(0);
  PinSetRESET(0);
  printf("\n\n\n\n\n\n PERFORMING EXECUTION OF 8080TEST ROM \n\n\n\n\n\n\n");
  int safeRegDump = -1;
  int PIN_A_LATCH = 0;
  while (1 == 1) {
    PinSetO1(1);
    PinSetO1(0);
    PinSetO2(1);
    PinSetO2(0);
    if (PinGetPIN_SYNC() && PinGetPIN_D() == (0xA2)) {
      PIN_A_LATCH = PinGetPIN_A();
      safeRegDump = 2;
    }
    if (safeRegDump > -1) {
      if (safeRegDump == 0) {
        DUMP_REGISTERS();
        Disassemble(someMemory, PIN_A_LATCH);
      }
      safeRegDump--;
    }
    MEM_Handler(someMemory);
  }
}


x86 assembly (clang, -O0):
<TEST_VIA_BINARY>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10020,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe06(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x10018(%rbp)
cmpq   $0x0,-0x10018(%rbp)
jne    121e <TEST_VIA_BINARY+0x3e>
mov    $0x3,%edi
call   10f0 <exit@plt>
lea    -0x10010(%rbp),%rdi
mov    -0x10018(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4ee,%edx
call   1050 <fread@plt>
mov    -0x10018(%rbp),%rdi
call   1060 <fclose@plt>
mov    $0x1,%edi
call   10b0 <PinSetRESET@plt>
mov    $0x1,%edi
call   10a0 <PinSetO1@plt>
xor    %edi,%edi
call   10a0 <PinSetO1@plt>
mov    $0x1,%edi
call   1040 <PinSetO2@plt>
xor    %edi,%edi
call   1040 <PinSetO2@plt>
mov    $0x1,%edi
call   10a0 <PinSetO1@plt>
xor    %edi,%edi
call   10a0 <PinSetO1@plt>
mov    $0x1,%edi
call   1040 <PinSetO2@plt>
xor    %edi,%edi
call   1040 <PinSetO2@plt>
xor    %edi,%edi
call   10b0 <PinSetRESET@plt>
lea    0xd60(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x1001c(%rbp)
ff ff ff
movl   $0x0,-0x10020(%rbp)
mov    $0x1,%edi
call   10a0 <PinSetO1@plt>
xor    %edi,%edi
call   10a0 <PinSetO1@plt>
mov    $0x1,%edi
call   1040 <PinSetO2@plt>
xor    %edi,%edi
call   1040 <PinSetO2@plt>
mov    $0x0,%al
call   10d0 <PinGetPIN_SYNC@plt>
cmp    $0x0,%rax
je     1318 <TEST_VIA_BINARY+0x138>
mov    $0x0,%al
call   1100 <PinGetPIN_D@plt>
cmp    $0xa2,%eax
jne    1318 <TEST_VIA_BINARY+0x138>
mov    $0x0,%al
call   1080 <PinGetPIN_A@plt>
mov    %eax,-0x10020(%rbp)
movl   $0x2,-0x1001c(%rbp)
cmpl   $0xffffffff,-0x1001c(%rbp)
jle    135a <TEST_VIA_BINARY+0x17a>
cmpl   $0x0,-0x1001c(%rbp)
jne    134b <TEST_VIA_BINARY+0x16b>
mov    $0x0,%al
call   1090 <DUMP_REGISTERS@plt>
lea    -0x10010(%rbp),%rdi
mov    -0x10020(%rbp),%esi
call   10c0 <Disassemble@plt>
mov    -0x1001c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1001c(%rbp)
lea    -0x10010(%rbp),%rdi
call   1030 <MEM_Handler@plt>
jmp    12bc <TEST_VIA_BINARY+0xdc>

x86 assembly (clang, -O2):
<TEST_VIA_BINARY>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10000,%rsp
lea    0xe0e(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1307 <TEST_VIA_BINARY+0x127>
mov    %rax,%rbp
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x4ee,%edx
mov    %rax,%rcx
call   1060 <fread@plt>
mov    %rbp,%rdi
call   1070 <fclose@plt>
mov    $0x1,%edi
call   10b0 <PinSetRESET@plt>
mov    $0x1,%edi
call   10a0 <PinSetO1@plt>
xor    %edi,%edi
call   10a0 <PinSetO1@plt>
mov    $0x1,%edi
call   1040 <PinSetO2@plt>
xor    %edi,%edi
call   1040 <PinSetO2@plt>
mov    $0x1,%edi
call   10a0 <PinSetO1@plt>
xor    %edi,%edi
call   10a0 <PinSetO1@plt>
mov    $0x1,%edi
call   1040 <PinSetO2@plt>
xor    %edi,%edi
call   1040 <PinSetO2@plt>
xor    %edi,%edi
call   10b0 <PinSetRESET@plt>
lea    0xd84(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%ebx
xor    %ebp,%ebp
jmp    12a9 <TEST_VIA_BINARY+0xc9>
nopl   (%rax)
xor    %eax,%eax
call   1080 <PinGetPIN_A@plt>
mov    %eax,%ebp
mov    $0x2,%ebx
add    $0xffffffff,%ebx
mov    %r14,%rdi
call   1030 <MEM_Handler@plt>
mov    $0x1,%edi
call   10a0 <PinSetO1@plt>
xor    %edi,%edi
call   10a0 <PinSetO1@plt>
mov    $0x1,%edi
call   1040 <PinSetO2@plt>
xor    %edi,%edi
call   1040 <PinSetO2@plt>
xor    %eax,%eax
call   10d0 <PinGetPIN_SYNC@plt>
test   %rax,%rax
je     12e5 <TEST_VIA_BINARY+0x105>
xor    %eax,%eax
call   1100 <PinGetPIN_D@plt>
cmp    $0xa2,%eax
je     1290 <TEST_VIA_BINARY+0xb0>
test   %ebx,%ebx
js     1300 <TEST_VIA_BINARY+0x120>
jne    129e <TEST_VIA_BINARY+0xbe>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <DUMP_REGISTERS@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   10c0 <Disassemble@plt>
jmp    129e <TEST_VIA_BINARY+0xbe>
mov    $0xffffffff,%ebx
jmp    12a1 <TEST_VIA_BINARY+0xc1>
mov    $0x3,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [52 x i8] c"\0A\0A\0A\0A\0A\0A PERFORMING EXECUTION OF 8080TEST ROM \0A\0A\0A\0A\0A\0A\0A\00"
define void @TEST_VIA_BINARY(i8* %0) {
%2 = alloca i8*
%3 = alloca [65536 x i8]
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @exit(i32 3)
unreachable
13: ; preds = %1
%14 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%15 = load i32*, i32** %4
%16 = call i32 @fread(i8* %14, i32 1, i32 1262, i32* %15)
%17 = load i32*, i32** %4
%18 = call i32 @fclose(i32* %17)
%19 = call i32 @PinSetRESET(i32 1)
%20 = call i32 @PinSetO1(i32 1)
%21 = call i32 @PinSetO1(i32 0)
%22 = call i32 @PinSetO2(i32 1)
%23 = call i32 @PinSetO2(i32 0)
%24 = call i32 @PinSetO1(i32 1)
%25 = call i32 @PinSetO1(i32 0)
%26 = call i32 @PinSetO2(i32 1)
%27 = call i32 @PinSetO2(i32 0)
%28 = call i32 @PinSetRESET(i32 0)
%29 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
store i32 0, i32* %6
br label %30
30: ; preds = %13, %56
%31 = call i32 @PinSetO1(i32 1)
%32 = call i32 @PinSetO1(i32 0)
%33 = call i32 @PinSetO2(i32 1)
%34 = call i32 @PinSetO2(i32 0)
%35 = call i64 (...) @PinGetPIN_SYNC()
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = call i32 (...) @PinGetPIN_D()
%39 = icmp eq i32 %38, 162
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 (...) @PinGetPIN_A()
store i32 %41, i32* %6
store i32 2, i32* %5
br label %42
42: ; preds = %40, %37, %30
%43 = load i32, i32* %5
%44 = icmp sgt i32 %43, -1
br i1 %44, label %45, label %56
45: ; preds = %42
%46 = load i32, i32* %5
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %45
%49 = call i32 (...) @DUMP_REGISTERS()
%50 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%51 = load i32, i32* %6
%52 = call i32 @Disassemble(i8* %50, i32 %51)
br label %53
53: ; preds = %48, %45
%54 = load i32, i32* %5
%55 = add nsw i32 %54, -1
store i32 %55, i32* %5
br label %56
56: ; preds = %53, %42
%57 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%58 = call i32 @MEM_Handler(i8* %57)
br label %30
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@str = [51 x i8] c"\0A\0A\0A\0A\0A\0A PERFORMING EXECUTION OF 8080TEST ROM \0A\0A\0A\0A\0A\0A\00"
define void @TEST_VIA_BINARY(i8* nocapture %0) {
%2 = alloca [65536 x i8]
%3 = getelementptr inbounds [65536 x i8], [65536 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @exit(i32 3)
unreachable
8: ; preds = %1
%9 = call i32 @fread(i8* %3, i32 1, i32 1262, i32* %4)
%10 = tail call i32 @fclose(i32* %4)
%11 = tail call i32 @PinSetRESET(i32 1)
%12 = tail call i32 @PinSetO1(i32 1)
%13 = tail call i32 @PinSetO1(i32 0)
%14 = tail call i32 @PinSetO2(i32 1)
%15 = tail call i32 @PinSetO2(i32 0)
%16 = tail call i32 @PinSetO1(i32 1)
%17 = tail call i32 @PinSetO1(i32 0)
%18 = tail call i32 @PinSetO2(i32 1)
%19 = tail call i32 @PinSetO2(i32 0)
%20 = tail call i32 @PinSetRESET(i32 0)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %47, %8
%23 = phi i32 [ -1, %8 ], [ %49, %47 ]
%24 = phi i32 [ 0, %8 ], [ %48, %47 ]
%25 = call i32 @PinSetO1(i32 1)
%26 = call i32 @PinSetO1(i32 0)
%27 = call i32 @PinSetO2(i32 1)
%28 = call i32 @PinSetO2(i32 0)
%29 = call i64 (...) @PinGetPIN_SYNC()
%30 = icmp eq i64 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %22
%32 = call i32 (...) @PinGetPIN_D()
%33 = icmp eq i32 %32, 162
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 (...) @PinGetPIN_A()
br label %43
36: ; preds = %31, %22
%37 = icmp sgt i32 %23, -1
br i1 %37, label %38, label %47
38: ; preds = %36
%39 = icmp eq i32 %23, 0
br i1 %39, label %40, label %43
40: ; preds = %38
%41 = call i32 (...) @DUMP_REGISTERS()
%42 = call i32 @Disassemble(i8* %3, i32 %24)
br label %43
43: ; preds = %34, %40, %38
%44 = phi i32 [ 0, %40 ], [ %23, %38 ], [ 2, %34 ]
%45 = phi i32 [ %24, %40 ], [ %24, %38 ], [ %35, %34 ]
%46 = add nsw i32 %44, -1
br label %47
47: ; preds = %43, %36
%48 = phi i32 [ %45, %43 ], [ %24, %36 ]
%49 = phi i32 [ %46, %43 ], [ -1, %36 ]
%50 = call i32 @MEM_Handler(i8* %3)
br label %22
}