#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 uint8_t;
typedef int uint16_t;

/* Variables and functions */
int debug_instr_1(int);
int /*<<< orphan*/ debug_instr_3(int, int, int);
void read_code_memory(uint16_t address, uint8_t bank, uint16_t count,
                      uint8_t *outputData) {
  int i;
  if (address >= 0x8000)
    address = (address & 0x7FFF) + (bank * 0x8000);
  debug_instr_3(0x75, 0xC7, (bank * 16) + 1);
  debug_instr_3(0x90, address >> 8, address);
  for (i = 0; i < count; ++i) {
    debug_instr_1(0xE4);
    outputData[i] = debug_instr_1(0x93);
    debug_instr_1(0xA3);
  }
}


x86 assembly (clang, -O0):
<read_code_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
cmpl   $0x8000,-0x4(%rbp)
jl     1155 <read_code_memory+0x35>
mov    -0x4(%rbp),%eax
and    $0x7fff,%eax
mov    -0x8(%rbp),%ecx
shl    $0xf,%ecx
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%edx
shl    $0x4,%edx
add    $0x1,%edx
mov    $0x75,%edi
mov    $0xc7,%esi
call   1030 <debug_instr_3@plt>
mov    -0x4(%rbp),%esi
sar    $0x8,%esi
mov    -0x4(%rbp),%edx
mov    $0x90,%edi
call   1030 <debug_instr_3@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11cc <read_code_memory+0xac>
mov    $0xe4,%edi
call   1040 <debug_instr_1@plt>
mov    $0x93,%edi
call   1040 <debug_instr_1@plt>
mov    %eax,%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    $0xa3,%edi
call   1040 <debug_instr_1@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1187 <read_code_memory+0x67>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_code_memory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %edx,%r14d
mov    %edi,%eax
and    $0x7fff,%eax
mov    %esi,%ebp
shl    $0xf,%ebp
or     %eax,%ebp
cmp    $0x8000,%edi
cmovl  %edi,%ebp
shl    $0x4,%esi
lea    0x1(%rsi),%edx
mov    $0x75,%edi
mov    $0xc7,%esi
call   1030 <debug_instr_3@plt>
mov    %ebp,%esi
sar    $0x8,%esi
mov    $0x90,%edi
mov    %ebp,%edx
call   1030 <debug_instr_3@plt>
test   %r14d,%r14d
jle    11ab <read_code_memory+0x8b>
mov    %r14d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0xe4,%edi
call   1040 <debug_instr_1@plt>
mov    $0x93,%edi
call   1040 <debug_instr_1@plt>
mov    %eax,(%r15,%rbx,4)
mov    $0xa3,%edi
call   1040 <debug_instr_1@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1180 <read_code_memory+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @read_code_memory(i32 %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%10 = load i32, i32* %5
%11 = icmp sge i32 %10, 32768
br i1 %11, label %12, label %18
12: ; preds = %4
%13 = load i32, i32* %5
%14 = and i32 %13, 32767
%15 = load i32, i32* %6
%16 = mul nsw i32 %15, 32768
%17 = add nsw i32 %14, %16
store i32 %17, i32* %5
br label %18
18: ; preds = %12, %4
%19 = load i32, i32* %6
%20 = mul nsw i32 %19, 16
%21 = add nsw i32 %20, 1
%22 = call i32 @debug_instr_3(i32 117, i32 199, i32 %21)
%23 = load i32, i32* %5
%24 = ashr i32 %23, 8
%25 = load i32, i32* %5
%26 = call i32 @debug_instr_3(i32 144, i32 %24, i32 %25)
store i32 0, i32* %9
br label %27
27: ; preds = %39, %18
%28 = load i32, i32* %9
%29 = load i32, i32* %7
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %42
31: ; preds = %27
%32 = call i32 @debug_instr_1(i32 228)
%33 = call i32 @debug_instr_1(i32 147)
%34 = load i32*, i32** %8
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
store i32 %33, i32* %37
%38 = call i32 @debug_instr_1(i32 163)
br label %39
39: ; preds = %31
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
br label %27
42: ; preds = %27
ret void
}

LLVM IR (-O2):
define void @read_code_memory(i32 %0, i32 %1, i32 %2, i32* nocapture writeonly %3) {
%5 = icmp sgt i32 %0, 32767
%6 = and i32 %0, 32767
%7 = shl nsw i32 %1, 15
%8 = or i32 %7, %6
%9 = select i1 %5, i32 %8, i32 %0
%10 = shl nsw i32 %1, 4
%11 = or i32 %10, 1
%12 = tail call i32 @debug_instr_3(i32 117, i32 199, i32 %11)
%13 = ashr i32 %9, 8
%14 = tail call i32 @debug_instr_3(i32 144, i32 %13, i32 %9)
%15 = icmp sgt i32 %2, 0
br i1 %15, label %16, label %26
16: ; preds = %4
%17 = zext i32 %2 to i64
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ 0, %16 ], [ %24, %18 ]
%20 = tail call i32 @debug_instr_1(i32 228)
%21 = tail call i32 @debug_instr_1(i32 147)
%22 = getelementptr inbounds i32, i32* %3, i64 %19
store i32 %21, i32* %22
%23 = tail call i32 @debug_instr_1(i32 163)
%24 = add nuw nsw i64 %19, 1
%25 = icmp eq i64 %24, %17
br i1 %25, label %26, label %18
26: ; preds = %18, %4
ret void
}