// File: /workspace/github_c_makefile_star2_O2/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 %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %rbx
movslq 0x70(%rdi), %rax
leaq 0x4(,%rax,4), %rdi
callq 0x29673
movq %rax, %r14
xorl %ecx, %ecx
xorl %eax, %eax
movslq 0x70(%rbx), %rdx
cmpq %rdx, %rcx
jge 0x170b8
movq 0x28(%rbx), %rdx
movl (%rdx,%rcx,4), %edx
andl $0x7f, %edx
movl %eax, (%r14,%rcx,4)
cmpl $0x1, %edx
sbbl $-0x1, %eax
incq %rcx
jmp 0x17096
movl %eax, (%r14,%rdx,4)
xorl %edx, %edx
pushq $0x8
popq %rsi
pushq $0x10
popq %rdi
xorl %r8d, %r8d
movslq 0x70(%rbx), %rcx
cmpq %rcx, %rdx
jge 0x1713c
movq 0x28(%rbx), %r9
movl (%r9,%rdx,4), %r10d
movl %r10d, %r11d
andl $0x7f, %r11d
leal -0x1d(%r11), %ecx
cmpl $0x4, %ecx
jae 0x1712d
movl %edi, %ecx
movl %r10d, %r11d
sarl %cl, %r11d
movslq %r11d, %r11
leaq (%rdx,%r11), %r15
addl %edx, %r11d
addl %r8d, %r11d
movl %edx, %r12d
subl %r11d, %r12d
addl (%r14,%r15,4), %r12d
shll %cl, %r12d
addl %r12d, %r10d
movslq %r8d, %rcx
movl %r10d, (%r9,%rcx,4)
movq 0x38(%rbx), %r9
testq %r9, %r9
je 0x17125
movq (%r9,%rdx,8), %r10
movq %r10, (%r9,%rcx,8)
incl %r8d
incq %rdx
jmp 0x170c7
testl %r11d, %r11d
je 0x17128
movl %esi, %ecx
cmpl $0x1c, %r11d
je 0x170ea
jmp 0x1710d
movl 0x7c(%rbx), %edx
xorl %ecx, %ecx
testl %edx, %edx
cmovlel %ecx, %edx
imulq $0x18, %rdx, %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpq %rcx, %rdx
je 0x1717d
movq 0x50(%rbx), %rdi
movl (%rdi,%rcx), %r8d
cmpq %rsi, %r8
je 0x17177
movl (%r14,%r8,4), %r8d
movl %r8d, (%rdi,%rcx)
movl 0x4(%rdi,%rcx), %r8d
movl (%r14,%r8,4), %r8d
movl %r8d, 0x4(%rdi,%rcx)
addq $0x18, %rcx
jmp 0x1714f
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 %r12
popq %r14
popq %r15
popq %rbp
jmp 0x298c1