// File: /workspace/github_c_makefile_star2_O1/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, -0x40(%rbp)
movl %esi, %ebx
movq %rdi, %r15
leaq 0x1b19(%rip), %rsi      # 0x3010
callq 0x1150
testq %rax, %rax
je 0x1671
movq %rax, %r14
leaq -0x50(%rbp), %r15
movq $0x0, (%r15)
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rax, %rcx
callq 0x11c0
movq (%r15), %r13
leaq 0x1af8(%rip), %rdi      # 0x302a
movq %r13, %rsi
xorl %eax, %eax
callq 0x1040
leaq (,%r13,2), %rdi
callq 0x1180
movq %rax, %r12
movl $0x2, %esi
movq %rax, %rdi
movq %r13, %rdx
movq %r14, -0x30(%rbp)
movq %r14, %rcx
callq 0x11c0
movl %ebx, -0x34(%rbp)
testl %ebx, %ebx
je 0x159c
leaq 0x1b3f(%rip), %rdi      # 0x30b0
callq 0x11d0
testq %r13, %r13
je 0x159c
leaq 0x1ac4(%rip), %r15      # 0x3046
xorl %r14d, %r14d
movzwl (%r12,%r14,2), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x1040
incq %r14
cmpq %r14, %r13
jne 0x1585
leaq -0x48(%rbp), %rdi
movq $0x0, (%rdi)
movl $0x2, %esi
movl $0x1, %edx
movq -0x30(%rbp), %rcx
callq 0x11c0
cmpq $0x1, %rax
movq -0x40(%rbp), %rbx
jne 0x1656
movq -0x48(%rbp), %r15
movl $0x8000, %eax           # imm = 0x8000
cmpq %rax, %r15
cmovaeq %rax, %r15
movl $0x8000, %edi           # imm = 0x8000
callq 0x1180
movq %rax, %r13
movq %rax, (%rbx)
movl $0x8000, %edx           # imm = 0x8000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movl $0x1, %esi
movq %r13, %rdi
movq %r15, %rdx
movq -0x30(%rbp), %rcx
callq 0x11c0
movq %rax, %r13
leaq 0x1a36(%rip), %rdi      # 0x304b
movq %rax, %rsi
xorl %eax, %eax
callq 0x1040
cmpl $0x0, -0x34(%rbp)
je 0x1656
leaq 0x1a94(%rip), %rdi      # 0x30c0
callq 0x11d0
testq %r13, %r13
je 0x1656
leaq 0x1a31(%rip), %r15      # 0x306e
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x1040
incq %r14
cmpq %r14, %r13
jne 0x1640
movq -0x30(%rbp), %rdi
callq 0x1120
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x199b(%rip), %rdi      # 0x3013
movq %r15, %rsi
xorl %eax, %eax
callq 0x1040
movl $0x1, %edi
callq 0x1170