// File: /workspace/github_c_makefile_star2_O2/jtuu[P]quesler/src/debug.c
// Function: disassemble_instruction
size_t disassemble_instruction(Chunk* chunk, size_t offset, bool line_info) {
    for (size_t i = 0; i < chunk->labels_count; i++) {
        if (chunk->labels[i] == (int32_t) offset) {
            printf("%ld:\n", i);
            break;
        }
    }

    if (line_info) {
        printf("%04ld ", offset);

        if (offset > 0 && chunk->lines[offset] == chunk->lines[offset - 1]) {
            printf("   | ");
        } else {
            printf("%4ld ", chunk->lines[offset]);
        }
    } else {
        printf("    ");
    }

    size_t opcode_size = 1;
    size_t args_size = 0;

    uint16_t opcode = chunk->code[offset];
    
    if (opcode == EXTENDED_OPCODE_TABLE1 ||
            opcode == EXTENDED_OPCODE_TABLE2) {
        opcode_size = 2;
        opcode = (uint16_t) READ2(&chunk->code[offset]);
    }

    const OpcodeDefinition* opcode_def = get_opcode_definition(opcode);

    if (opcode_def == NULL) {
        fprintf(stderr, "Disassembler: Unknown opcode 0x%hx at offset 0x%08lx\n", opcode, offset);
        printf("unknown%hx", opcode);
        goto done;
    }

    printf("%s", opcode_def->name);

    uint8_t* arg = &chunk->code[offset + opcode_size];

    for (size_t i = 0; i < OPCODE_MAX_ARITY; i++) {
        ParameterType param_type = opcode_def->parameters[i];

        if (param_type == T_NONE || param_type == T_ARGS) {
            break;
        } else if (param_type == T_IMED || param_type == T_PUSH) {
            continue;
        } else {
            if (i < 2) {
                printf(" ");
            } else {
                printf(", ");
            }

            switch (param_type) {
                case T_REG:
                case T_BREG:
                    printf("R%hhu", *arg);
                    break;
                case T_DWORD:
                    printf("%d", READ4(arg));
                    break;
                case T_STR: {
                    uint16_t* str_start = (uint16_t*) arg;
                    uint16_t* str_end = (uint16_t*) arg;
                    
                    printf("\"");
                    while (*str_end != 0) {
                        printf("%lc", (wchar_t) *str_end);
                        str_end++;
                    }
                    printf("\"");

                    size_t str_size = (size_t) (str_end - str_start + 1) * 2;
                    args_size += str_size;
                    arg += str_size;
                    break;
                }
                case T_FUNC: {
                    size_t label = (size_t) READ2(arg);
                    int32_t label_offset = chunk->labels[label];
                    printf("%lu (%d)", label, label_offset);
                    break;
                }
                default:
                    fprintf(stderr, "Disassembler: Unknown parameter type %u\n", param_type);
                    goto done;
            }
        }

        if (!is_variable_sized(param_type)) {
            size_t param_size = get_parameter_size(param_type);
            args_size += param_size;
            arg += param_size;
        }
    }

done:
    printf("\n");
    return offset + opcode_size + args_size;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x40(%rdi), %rax
xorl %esi, %esi
cmpq %rsi, %rax
je 0x5f4c
movq 0x38(%rbx), %rcx
cmpl %r14d, (%rcx,%rsi,4)
je 0x5f3e
incq %rsi
jmp 0x5f2a
leaq 0x26ef(%rip), %rdi      # 0x8634
xorl %eax, %eax
callq 0x40a0
testb %bpl, %bpl
je 0x5f7f
leaq 0x26e2(%rip), %rdi      # 0x863a
movq %r14, %rsi
xorl %eax, %eax
callq 0x40a0
movq 0x18(%rbx), %rax
testq %r14, %r14
je 0x5f8f
movq (%rax,%r14,8), %rsi
cmpq -0x8(%rax,%r14,8), %rsi
jne 0x5f92
leaq 0x26c4(%rip), %rdi      # 0x8641
jmp 0x5f86
leaq 0x26c7(%rip), %rdi      # 0x864d
xorl %eax, %eax
callq 0x40a0
jmp 0x5fa0
movq (%rax), %rsi
leaq 0x26ae(%rip), %rdi      # 0x8647
xorl %eax, %eax
callq 0x40a0
movq 0x10(%rbx), %rax
movzbl (%rax,%r14), %ebp
movl %ebp, %ecx
andb $-0x2, %cl
cmpb $-0x8, %cl
jne 0x5fc2
shll $0x8, %ebp
movzbl 0x1(%rax,%r14), %eax
orl %eax, %ebp
pushq $0x2
jmp 0x5fc4
pushq $0x1
popq %r13
movl %ebp, %edi
callq 0x639c
testq %rax, %rax
je 0x6105
movq %rax, %r12
movq 0x8(%rax), %rsi
leaq 0x26af(%rip), %rdi      # 0x8693
xorl %eax, %eax
callq 0x40a0
movq %rbx, 0x10(%rsp)
movq 0x10(%rbx), %rbx
movq %r13, (%rsp)
addq %r13, %rbx
movq %r14, 0x8(%rsp)
addq %r14, %rbx
leaq 0x2695(%rip), %r13      # 0x869f
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpq $0xb, %r14
je 0x6151
movl 0x10(%r12,%r14,4), %ebp
cmpl $0x2, %ebp
jb 0x6151
cmpl $0x9, %ebp
je 0x60fd
cmpl $0xc, %ebp
je 0x60fd
cmpq $0x1, %r14
ja 0x604a
pushq $0x20
popq %rdi
callq 0x4040
jmp 0x6058
leaq 0x2645(%rip), %rdi      # 0x8696
xorl %eax, %eax
callq 0x40a0
leal -0x6(%rbp), %eax
cmpl $0x2, %eax
jb 0x6097
cmpl $0x2, %ebp
je 0x60ad
cmpl $0xa, %ebp
je 0x60c1
cmpl $0x13, %ebp
jne 0x6137
pushq $0x22
popq %rdi
callq 0x4040
addq $0x2, %r15
addq $0x2, %rbx
movzwl -0x2(%rbx), %esi
testl %esi, %esi
je 0x60a3
movq %r13, %rdi
xorl %eax, %eax
callq 0x40a0
jmp 0x607b
movzbl (%rbx), %esi
leaq 0x25f8(%rip), %rdi      # 0x8699
jmp 0x60b8
pushq $0x22
popq %rdi
callq 0x4040
jmp 0x60e5
movl (%rbx), %esi
bswapl %esi
leaq 0x21f2(%rip), %rdi      # 0x82aa
xorl %eax, %eax
callq 0x40a0
jmp 0x60e5
movzwl (%rbx), %eax
rolw $0x8, %ax
movzwl %ax, %esi
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rax
movl (%rax,%rsi,4), %edx
leaq 0x25c5(%rip), %rdi      # 0x86a3
xorl %eax, %eax
callq 0x40a0
movl %ebp, %edi
callq 0x6405
testb %al, %al
jne 0x60fd
movl %ebp, %edi
callq 0x6412
addq %rax, %r15
addq %rax, %rbx
incq %r14
jmp 0x6010
movq 0xeee4(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x253c(%rip), %rsi      # 0x8652
xorl %r15d, %r15d
movl %ebp, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0x40e0
leaq 0x255c(%rip), %rdi      # 0x8688
movl %ebp, %esi
xorl %eax, %eax
callq 0x40a0
jmp 0x615a
movq 0xeeb2(%rip), %rax      # 0x14ff0
movq (%rax), %rdi
leaq 0x2564(%rip), %rsi      # 0x86ac
movl %ebp, %edx
xorl %eax, %eax
callq 0x40e0
movq 0x8(%rsp), %r14
movq (%rsp), %r13
pushq $0xa
popq %rdi
callq 0x4040
addq %r14, %r13
addq %r15, %r13
movq %r13, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop