// File: /workspace/github_c_makefile_star2_O3/minkcv[P]vm/main.c
// Function: readBinary
uint16_t* readBinary(const char* filename, int print, uint8_t **rom)
{
    FILE* bin = fopen(filename, "rb");
    if (bin == NULL)
    {
        printf("Error reading file %s\n", filename);
        exit(1);
    }
    size_t numInstructions = 0, numInstructionsLe = 0;
    // binaries are length prefixed
    fread(&numInstructionsLe, sizeof(uint16_t), 1, bin);
    numInstructions = retro_le_to_cpu16(numInstructionsLe);
    printf("Binary is %Id instructions\n", numInstructions);
    uint16_t* code = malloc(sizeof(uint16_t) * numInstructions);
    fread(code, sizeof(uint16_t), numInstructions, bin);
    if (print)
    {
        printf("Binary Program:\n");
        int i;
        for (i = 0; i < numInstructions; i++)
            printf("%4X\n", code[i]);
    }

    size_t romSize = 0, romSizeLe = 0;
    // binaries are length prefixed
    if (fread(&romSizeLe, sizeof(uint16_t), 1, bin) == 1)
    {
	romSize = retro_le_to_cpu16(romSizeLe);
	if (romSize > 128 * MEMORY_SEGMENT_SIZE)
	    romSize = 128 * MEMORY_SEGMENT_SIZE;
	*rom = malloc(128 * MEMORY_SEGMENT_SIZE);
	memset(*rom, 0, 128 * MEMORY_SEGMENT_SIZE);
	size_t bytesRead = fread(*rom, sizeof(uint8_t), romSize, bin);
	printf("Read %Id bytes from the embed rom\n", bytesRead);
	if (print)
	{
	    printf("Rom:\n");
	    int i;
	    for (i = 0; i < bytesRead; i++)
		printf("%2X\n", rom[i]);
	}
    }

    fclose(bin);
    return code;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %rbx
movl %esi, -0x2c(%rbp)
movq %rdi, %r15
leaq 0x1b69(%rip), %rsi      # 0x3004
callq 0x1160
testq %rax, %rax
je 0x1603
movq %rax, %r14
leaq -0x48(%rbp), %r15
movq $0x0, (%r15)
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rax, %rcx
callq 0x11c0
movq (%r15), %r13
leaq 0x1b48(%rip), %rdi      # 0x301e
movq %r13, %rsi
xorl %eax, %eax
callq 0x1040
leaq (,%r13,2), %rdi
callq 0x1190
movq %rax, %r12
movl $0x2, %esi
movq %rax, %rdi
movq %r13, %rdx
movq %r14, -0x38(%rbp)
movq %r14, %rcx
callq 0x11c0
cmpl $0x0, -0x2c(%rbp)
je 0x153f
leaq 0x1b90(%rip), %rdi      # 0x30a4
callq 0x11d0
testq %r13, %r13
je 0x153f
leaq 0x1b15(%rip), %r15      # 0x303a
xorl %r14d, %r14d
movzwl (%r12,%r14,2), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x1040
incq %r14
cmpq %r14, %r13
jne 0x1528
leaq -0x40(%rbp), %rdi
movq $0x0, (%rdi)
movl $0x2, %esi
movl $0x1, %edx
movq -0x38(%rbp), %rcx
callq 0x11c0
cmpq $0x1, %rax
jne 0x15e8
movq -0x40(%rbp), %r15
movl $0x8000, %eax           # imm = 0x8000
cmpq %rax, %r15
cmovaeq %rax, %r15
movl $0x1, %edi
movl $0x8000, %esi           # imm = 0x8000
callq 0x10c0
movq %rax, (%rbx)
movl $0x1, %esi
movq %rax, %rdi
movq %r15, %rdx
movq -0x38(%rbp), %rcx
callq 0x11c0
movq %rax, %r13
leaq 0x1a98(%rip), %rdi      # 0x303f
movq %rax, %rsi
xorl %eax, %eax
callq 0x1040
cmpl $0x0, -0x2c(%rbp)
je 0x15e8
leaq 0x1af6(%rip), %rdi      # 0x30b4
callq 0x11d0
testq %r13, %r13
je 0x15e8
leaq 0x1a93(%rip), %r15      # 0x3062
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x1040
incq %r14
cmpq %r14, %r13
jne 0x15d2
movq -0x38(%rbp), %rdi
callq 0x1130
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19fd(%rip), %rdi      # 0x3007
movq %r15, %rsi
xorl %eax, %eax
callq 0x1040
movl $0x1, %edi
callq 0x1180