// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq %rdi, %r14
movq 0x40(%rdi), %rax
testq %rax, %rax
je 0x72ad
movq 0x38(%r14), %rcx
xorl %esi, %esi
cmpl %ebx, (%rcx,%rsi,4)
je 0x729f
incq %rsi
cmpq %rsi, %rax
jne 0x7290
jmp 0x72ad
leaq 0x338e(%rip), %rdi      # 0xa634
xorl %eax, %eax
callq 0x4090
testb %bpl, %bpl
je 0x72e0
leaq 0x3381(%rip), %rdi      # 0xa63a
movq %rbx, %rsi
xorl %eax, %eax
callq 0x4090
testq %rbx, %rbx
je 0x72f0
movq 0x18(%r14), %rax
movq (%rax,%rbx,8), %rcx
cmpq -0x8(%rax,%rbx,8), %rcx
jne 0x72f0
leaq 0x3363(%rip), %rdi      # 0xa641
jmp 0x72e7
leaq 0x3366(%rip), %rdi      # 0xa64d
xorl %eax, %eax
callq 0x4090
jmp 0x7306
movq 0x18(%r14), %rax
movq (%rax,%rbx,8), %rsi
leaq 0x3348(%rip), %rdi      # 0xa647
xorl %eax, %eax
callq 0x4090
movq 0x10(%r14), %rax
movzbl (%rax,%rbx), %ebp
movl %ebp, %ecx
andb $-0x2, %cl
movl $0x1, %r15d
cmpb $-0x8, %cl
jne 0x732e
shll $0x8, %ebp
movzbl 0x1(%rax,%rbx), %eax
orl %eax, %ebp
movl $0x2, %r15d
movl %ebp, %edi
callq 0x77b8
testq %rax, %rax
je 0x7533
movq %rax, %r12
movq 0x8(%rax), %rsi
leaq 0x3347(%rip), %rdi      # 0xa693
xorl %eax, %eax
callq 0x4090
movq %r14, 0x18(%rsp)
movq %r15, %rax
movq 0x10(%r14), %r15
movq %rax, 0x20(%rsp)
addq %rax, %r15
addq %rbx, %r15
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movl 0x10(%r12,%r14,4), %r13d
movl $0x6, %ebp
cmpl $0x2, %r13d
jb 0x74c6
movl $0x8, %ebp
cmpl $0x9, %r13d
je 0x74c6
cmpl $0xc, %r13d
je 0x74c6
cmpq $0x1, %r14
ja 0x73b5
movl $0x20, %edi
callq 0x4040
jmp 0x73c3
leaq 0x32da(%rip), %rdi      # 0xa696
xorl %eax, %eax
callq 0x4090
cmpl $0x9, %r13d
jg 0x73e6
leal -0x6(%r13), %eax
cmpl $0x2, %eax
jae 0x7468
movzbl (%r15), %esi
leaq 0x32b8(%rip), %rdi      # 0xa699
jmp 0x747a
cmpl $0xa, %r13d
je 0x7483
cmpl $0x13, %r13d
jne 0x74db
movq %rbx, 0x10(%rsp)
movl $0x22, %edi
callq 0x4040
movzwl (%r15), %eax
movq %r15, %rbp
testw %ax, %ax
leaq 0x3285(%rip), %rbx      # 0xa69f
je 0x7439
movq %r15, %rbp
movzwl %ax, %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x4090
movzwl 0x2(%rbp), %eax
addq $0x2, %rbp
testw %ax, %ax
jne 0x741f
movl $0x22, %edi
callq 0x4040
movq %rbp, %rax
subq %r15, %rax
movq 0x8(%rsp), %rcx
addq %rax, %rcx
addq $0x2, %rcx
movq %rcx, 0x8(%rsp)
addq $0x2, %rbp
movq %rbp, %r15
movq 0x10(%rsp), %rbx
jmp 0x74a8
cmpl $0x2, %r13d
jne 0x74db
movl (%r15), %esi
bswapl %esi
leaq 0x2e30(%rip), %rdi      # 0xa2aa
xorl %eax, %eax
callq 0x4090
jmp 0x74a8
movzwl (%r15), %eax
rolw $0x8, %ax
movzwl %ax, %esi
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rax
movl (%rax,%rsi,4), %edx
leaq 0x3202(%rip), %rdi      # 0xa6a3
xorl %eax, %eax
callq 0x4090
movl %r13d, %edi
callq 0x782c
xorl %ebp, %ebp
testb %al, %al
jne 0x74c6
movl %r13d, %edi
callq 0x7839
addq %rax, 0x8(%rsp)
addq %rax, %r15
testb $0x7, %bpl
jne 0x7502
incq %r14
cmpq $0xb, %r14
jne 0x7376
jmp 0x74fd
movq 0xfb0e(%rip), %rax      # 0x16ff0
movq (%rax), %rdi
leaq 0x31c0(%rip), %rsi      # 0xa6ac
movl %r13d, %edx
xorl %eax, %eax
callq 0x40e0
movl $0x5, %ebp
jmp 0x74c6
movl $0x6, %ebp
addl $-0x5, %ebp
cmpl $0x1, %ebp
movq 0x20(%rsp), %r15
ja 0x7521
movl $0xa, %edi
callq 0x4040
addq %rbx, %r15
addq 0x8(%rsp), %r15
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xfab6(%rip), %rax      # 0x16ff0
movq (%rax), %rdi
leaq 0x310e(%rip), %rsi      # 0xa652
movq $0x0, 0x8(%rsp)
movl %ebp, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x40e0
leaq 0x3128(%rip), %rdi      # 0xa688
movl %ebp, %esi
xorl %eax, %eax
callq 0x4090
jmp 0x750f
nop