// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
movq 0x40(%rdi), %rax
testq %rax, %rax
je 0x71d2
movq 0x38(%rbx), %rcx
xorl %esi, %esi
cmpl %r15d, (%rcx,%rsi,4)
je 0x71c4
incq %rsi
cmpq %rsi, %rax
jne 0x71b4
jmp 0x71d2
leaq 0x348d(%rip), %rdi      # 0xa658
xorl %eax, %eax
callq 0x4090
testb %bpl, %bpl
je 0x7205
leaq 0x3480(%rip), %rdi      # 0xa65e
movq %r15, %rsi
xorl %eax, %eax
callq 0x4090
movq 0x18(%rbx), %rax
testq %r15, %r15
je 0x7215
movq (%rax,%r15,8), %rsi
cmpq -0x8(%rax,%r15,8), %rsi
jne 0x7218
leaq 0x3462(%rip), %rdi      # 0xa665
jmp 0x720c
leaq 0x3465(%rip), %rdi      # 0xa671
xorl %eax, %eax
callq 0x4090
jmp 0x7226
movq (%rax), %rsi
leaq 0x344c(%rip), %rdi      # 0xa66b
xorl %eax, %eax
callq 0x4090
movq 0x10(%rbx), %rax
movzbl (%rax,%r15), %ebp
movl %ebp, %ecx
andb $-0x2, %cl
movl $0x1, %r14d
cmpb $-0x8, %cl
jne 0x7250
shll $0x8, %ebp
movzbl 0x1(%rax,%r15), %eax
orl %eax, %ebp
movl $0x2, %r14d
movl %ebp, %edi
callq 0x7698
testq %rax, %rax
movq %r14, 0x20(%rsp)
je 0x740d
movq %rax, %r12
movq 0x8(%rax), %rsi
leaq 0x3444(%rip), %rdi      # 0xa6b7
xorl %eax, %eax
callq 0x4090
movq %rbx, 0x10(%rsp)
movq 0x10(%rbx), %rbx
addq %r14, %rbx
addq %r15, %rbx
leaq 0x3433(%rip), %rbp      # 0xa6c3
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movq %r15, 0x18(%rsp)
movl 0x10(%r12,%r14,4), %r13d
cmpl $0x2, %r13d
jb 0x73e7
cmpl $0x9, %r13d
je 0x73da
cmpl $0xc, %r13d
je 0x73da
cmpq $0x1, %r14
ja 0x72d6
movl $0x20, %edi
callq 0x4040
jmp 0x72e4
leaq 0x33dd(%rip), %rdi      # 0xa6ba
xorl %eax, %eax
callq 0x4090
cmpl $0x9, %r13d
jg 0x7306
leal -0x6(%r13), %eax
cmpl $0x2, %eax
jae 0x737c
movzbl (%rbx), %esi
leaq 0x33bc(%rip), %rdi      # 0xa6bd
jmp 0x7391
cmpl $0xa, %r13d
je 0x739a
cmpl $0x13, %r13d
jne 0x7445
movl $0x22, %edi
callq 0x4040
movzwl (%rbx), %eax
movq %rbx, %r15
testw %ax, %ax
je 0x734d
movq %rbx, %r15
movzwl %ax, %esi
movq %rbp, %rdi
xorl %eax, %eax
callq 0x4090
movzwl 0x2(%r15), %eax
addq $0x2, %r15
testw %ax, %ax
jne 0x7332
movl $0x22, %edi
callq 0x4040
movq %r15, %rax
subq %rbx, %rax
movq 0x8(%rsp), %rcx
addq %rax, %rcx
addq $0x2, %rcx
movq %rcx, 0x8(%rsp)
addq $0x2, %r15
movq %r15, %rbx
movq 0x18(%rsp), %r15
jmp 0x73be
cmpl $0x2, %r13d
jne 0x7445
movl (%rbx), %esi
bswapl %esi
leaq 0x2f3d(%rip), %rdi      # 0xa2ce
xorl %eax, %eax
callq 0x4090
jmp 0x73be
movzwl (%rbx), %eax
rolw $0x8, %ax
movzwl %ax, %esi
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rax
movl (%rax,%rsi,4), %edx
leaq 0x3310(%rip), %rdi      # 0xa6c7
xorl %eax, %eax
callq 0x4090
movl %r13d, %edi
callq 0x770c
testb %al, %al
jne 0x73da
movl %r13d, %edi
callq 0x7719
addq %rax, 0x8(%rsp)
addq %rax, %rbx
incq %r14
cmpq $0xb, %r14
jne 0x72a1
movl $0xa, %edi
callq 0x4040
movq 0x20(%rsp), %rax
addq %r15, %rax
addq 0x8(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xfbdc(%rip), %rax      # 0x16ff0
movq (%rax), %rdi
leaq 0x3258(%rip), %rsi      # 0xa676
movq $0x0, 0x8(%rsp)
movl %ebp, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x40e0
leaq 0x3272(%rip), %rdi      # 0xa6ac
movl %ebp, %esi
xorl %eax, %eax
callq 0x4090
jmp 0x73e7
movq 0xfba4(%rip), %rax      # 0x16ff0
movq (%rax), %rdi
leaq 0x327a(%rip), %rsi      # 0xa6d0
movl %r13d, %edx
xorl %eax, %eax
callq 0x40e0
jmp 0x73e7
nop