// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x3b13(%rip), %rsi      # 0x5031
callq 0x1150
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x154a
movq -0x8(%rbp), %rsi
leaq 0x3afb(%rip), %rdi      # 0x5034
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x1170
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x30(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x11b0
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rsi
leaq 0x3ac7(%rip), %rdi      # 0x504b
movb $0x0, %al
callq 0x1040
movq -0x28(%rbp), %rdi
shlq %rdi
callq 0x1180
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl $0x2, %esi
callq 0x11b0
cmpl $0x0, -0xc(%rbp)
je 0x15fd
leaq 0x3aa9(%rip), %rdi      # 0x5067
movb $0x0, %al
callq 0x1040
movl $0x0, -0x3c(%rbp)
movslq -0x3c(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x15fb
movq -0x38(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movzwl (%rax,%rcx,2), %esi
leaq 0x3a8f(%rip), %rdi      # 0x5078
movb $0x0, %al
callq 0x1040
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x15cc
jmp 0x15fd
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x50(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x11b0
cmpq $0x1, %rax
jne 0x16ec
movq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x8000, -0x48(%rbp)    # imm = 0x8000
jbe 0x1648
movq $0x8000, -0x48(%rbp)    # imm = 0x8000
movl $0x8000, %edi           # imm = 0x8000
callq 0x1180
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
xorl %esi, %esi
movl $0x8000, %edx           # imm = 0x8000
callq 0x10a0
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x11b0
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rsi
leaq 0x39e6(%rip), %rdi      # 0x507d
movb $0x0, %al
callq 0x1040
cmpl $0x0, -0xc(%rbp)
je 0x16ea
leaq 0x39f5(%rip), %rdi      # 0x50a0
movb $0x0, %al
callq 0x1040
movl $0x0, -0x5c(%rbp)
movslq -0x5c(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0x16e8
movq -0x18(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x39d0(%rip), %rdi      # 0x50a6
movb $0x0, %al
callq 0x1040
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x16b9
jmp 0x16ea
jmp 0x16ec
movq -0x20(%rbp), %rdi
callq 0x1120
movq -0x38(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nop