// File: /workspace/github_c_makefile_star2_O2/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 0x1b65(%rip), %rsi      # 0x3004
callq 0x1160
testq %rax, %rax
je 0x15ed
movq %rax, %r14
leaq -0x48(%rbp), %r15
andq $0x0, (%r15)
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %rax, %rcx
callq 0x11c0
movq (%r15), %r13
leaq 0x1b4b(%rip), %rdi      # 0x301e
movq %r13, %rsi
xorl %eax, %eax
callq 0x1040
leaq (,%r13,2), %rdi
callq 0x1190
movq %rax, %r12
movq %rax, %rdi
pushq $0x2
popq %rsi
movq %r13, %rdx
movq %r14, -0x38(%rbp)
movq %r14, %rcx
callq 0x11c0
cmpl $0x0, -0x2c(%rbp)
je 0x1537
leaq 0x1b95(%rip), %rdi      # 0x30a4
callq 0x11d0
leaq 0x1b1f(%rip), %r15      # 0x303a
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x1537
movzwl (%r12,%r14,2), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x1040
incq %r14
jmp 0x151e
leaq -0x40(%rbp), %rdi
andq $0x0, (%rdi)
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq -0x38(%rbp), %rcx
callq 0x11c0
cmpq $0x1, %rax
jne 0x15d2
movq -0x40(%rbp), %r15
movl $0x8000, %eax           # imm = 0x8000
cmpq %rax, %r15
cmovaeq %rax, %r15
pushq $0x1
popq %r13
movl $0x8000, %esi           # imm = 0x8000
movq %r13, %rdi
callq 0x10c0
movq %rax, (%rbx)
movq %rax, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq -0x38(%rbp), %rcx
callq 0x11c0
movq %rax, %r13
leaq 0x1aab(%rip), %rdi      # 0x303f
movq %rax, %rsi
xorl %eax, %eax
callq 0x1040
cmpl $0x0, -0x2c(%rbp)
je 0x15d2
leaq 0x1b09(%rip), %rdi      # 0x30b4
callq 0x11d0
leaq 0x1aab(%rip), %r15      # 0x3062
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x15d2
movq (%rbx,%r14,8), %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x1040
incq %r14
jmp 0x15ba
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 0x1a13(%rip), %rdi      # 0x3007
movq %r15, %rsi
xorl %eax, %eax
callq 0x1040
pushq $0x1
popq %rdi
callq 0x1180