// File: /workspace/github_c_makefile_star2_O1/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 0x2bfe0
movq %rax, %r14
movl 0x70(%rbx), %esi
testl %esi, %esi
jle 0x18b17
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 0x18af9
jmp 0x18b19
xorl %eax, %eax
movslq %esi, %rcx
movl %eax, (%r14,%rcx,4)
cmpl $0x0, 0x70(%rbx)
jle 0x18ba5
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), %ecx
cmpl $0x4, %ecx
jae 0x18b93
movl $0x10, %ecx
movl %r9d, %r10d
sarl %cl, %r10d
leal (%rdx,%r10), %r11d
movslq %r10d, %r10
addq %rdx, %r10
addl %edx, %edi
subl %r11d, %edi
addl (%r14,%r10,4), %edi
shll %cl, %edi
addl %edi, %r9d
movslq %esi, %rcx
movl %r9d, (%r8,%rcx,4)
movq 0x38(%rbx), %rdi
testq %rdi, %rdi
je 0x18b7f
movq (%rdi,%rdx,8), %r8
movq %r8, (%rdi,%rcx,8)
incl %esi
incq %rdx
movl %esi, %edi
negl %edi
movslq 0x70(%rbx), %rcx
cmpq %rcx, %rdx
jl 0x18b2c
jmp 0x18ba5
testl %r10d, %r10d
je 0x18b81
movl $0x8, %ecx
cmpl $0x1c, %r10d
je 0x18b49
jmp 0x18b67
movslq 0x7c(%rbx), %rdx
testq %rdx, %rdx
jle 0x18be9
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 0x18be0
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 0x18bc1
movl %eax, 0x70(%rbx)
movq 0x28(%rbx), %rdi
movslq %eax, %rsi
shlq $0x2, %rsi
callq 0x12850
movq %rax, 0x28(%rbx)
movq %r14, %rdi
popq %rbx
popq %r14
popq %rbp
jmp 0x2c187