// File: /workspace/github_c_makefile_star2_O3/janet-lang[P]janet/src/core/bytecode.c
// Function: janet_bytecode_remove_noops
void janet_bytecode_remove_noops(JanetFuncDef *def) {

    /* Get an instruction rewrite map so we can rewrite jumps */
    uint32_t *pc_map = janet_smalloc(sizeof(uint32_t) * (1 + def->bytecode_length));
    uint32_t new_bytecode_length = 0;
    for (int32_t i = 0; i < def->bytecode_length; i++) {
        uint32_t instr = def->bytecode[i];
        uint32_t opcode = instr & 0x7F;
        pc_map[i] = new_bytecode_length;
        if (opcode != JOP_NOOP) {
            new_bytecode_length++;
        }
    }
    pc_map[def->bytecode_length] = new_bytecode_length;

    /* Linear scan rewrite bytecode and sourcemap. Also fix jumps. */
    int32_t j = 0;
    for (int32_t i = 0; i < def->bytecode_length; i++) {
        uint32_t instr = def->bytecode[i];
        uint32_t opcode = instr & 0x7F;
        int32_t old_jump_target = 0;
        int32_t new_jump_target = 0;
        switch (opcode) {
            case JOP_NOOP:
                continue;
            case JOP_JUMP:
                /* relative pc is in DS field of instruction */
                old_jump_target = i + (((int32_t)instr) >> 8);
                new_jump_target = pc_map[old_jump_target];
                instr += (uint32_t)(new_jump_target - old_jump_target + (i - j)) << 8;
                break;
            case JOP_JUMP_IF:
            case JOP_JUMP_IF_NIL:
            case JOP_JUMP_IF_NOT:
            case JOP_JUMP_IF_NOT_NIL:
                /* relative pc is in ES field of instruction */
                old_jump_target = i + (((int32_t)instr) >> 16);
                new_jump_target = pc_map[old_jump_target];
                instr += (uint32_t)(new_jump_target - old_jump_target + (i - j)) << 16;
                break;
            default:
                break;
        }
        def->bytecode[j] = instr;
        if (def->sourcemap != NULL) {
            def->sourcemap[j] = def->sourcemap[i];
        }
        j++;
    }

    /* Rewrite symbolmap */
    for (int32_t i = 0; i < def->symbolmap_length; i++) {
        JanetSymbolMap *sm = def->symbolmap + i;
        /* Don't rewrite upvalue mappings */
        if (sm->birth_pc < UINT32_MAX) {
            sm->birth_pc = pc_map[sm->birth_pc];
            sm->death_pc = pc_map[sm->death_pc];
        }
    }

    def->bytecode_length = new_bytecode_length;
    def->bytecode = janet_realloc(def->bytecode, def->bytecode_length * sizeof(uint32_t));
    janet_sfree(pc_map);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
movq %rdi, %rbx
movslq 0x70(%rdi), %rax
leaq 0x4(,%rax,4), %rdi
callq 0x2bb46
movq %rax, %r14
movslq 0x70(%rbx), %rsi
testq %rsi, %rsi
jle 0x18ab9
movq 0x28(%rbx), %rcx
xorl %edx, %edx
xorl %eax, %eax
movl (%rcx,%rdx,4), %esi
andl $0x7f, %esi
movl %eax, (%r14,%rdx,4)
cmpl $0x1, %esi
sbbl $-0x1, %eax
incq %rdx
movslq 0x70(%rbx), %rsi
cmpq %rsi, %rdx
jl 0x18a9b
jmp 0x18abb
xorl %eax, %eax
movl %eax, (%r14,%rsi,4)
movl 0x70(%rbx), %ecx
testl %ecx, %ecx
jle 0x18b43
xorl %edx, %edx
xorl %edi, %edi
xorl %esi, %esi
movq 0x28(%rbx), %r8
movl (%r8,%rdx,4), %r9d
movl %r9d, %r10d
andl $0x7f, %r10d
leal -0x1d(%r10), %r11d
cmpl $0x4, %r11d
jae 0x18b31
movl $0x10, %ecx
movl %r9d, %r10d
sarl %cl, %r10d
movslq %r10d, %r10
leaq (%rdx,%r10), %r11
subl %r10d, %edi
addl (%r14,%r11,4), %edi
shll %cl, %edi
addl %edi, %r9d
movslq %esi, %rcx
movl %r9d, (%r8,%rcx,4)
movq 0x38(%rbx), %rdi
testq %rdi, %rdi
je 0x18b1b
movq (%rdi,%rdx,8), %r8
movq %r8, (%rdi,%rcx,8)
incl %esi
movl 0x70(%rbx), %ecx
incq %rdx
movl %esi, %edi
negl %edi
movslq %ecx, %r8
cmpq %r8, %rdx
jl 0x18acc
jmp 0x18b43
testl %r10d, %r10d
je 0x18b20
movl $0x8, %ecx
cmpl $0x1c, %r10d
je 0x18aea
jmp 0x18b03
movslq 0x7c(%rbx), %rdx
testq %rdx, %rdx
jle 0x18b87
movq 0x50(%rbx), %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl (%rcx,%rsi), %r8d
cmpq %rdi, %r8
je 0x18b7e
movl (%r14,%r8,4), %r8d
movl %r8d, (%rcx,%rsi)
movl 0x4(%rcx,%rsi), %r8d
movl (%r14,%r8,4), %r8d
movl %r8d, 0x4(%rcx,%rsi)
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0x18b5f
movl %eax, 0x70(%rbx)
movq 0x28(%rbx), %rdi
movslq %eax, %rsi
shlq $0x2, %rsi
callq 0x12840
movq %rax, 0x28(%rbx)
movq %r14, %rdi
popq %rbx
popq %r14
popq %rbp
jmp 0x2bcd9