// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movl 0x70(%rax), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x3a870
movq %rax, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x70(%rcx), %eax
jge 0x19ff5
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
andl $0x7f, %eax
movl %eax, -0x20(%rbp)
movl -0x14(%rbp), %edx
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
cmpl $0x0, -0x20(%rbp)
je 0x19fe8
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x19fea
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x19fa4
movl -0x14(%rbp), %edx
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rcx
movslq 0x70(%rcx), %rcx
movl %edx, (%rax,%rcx,4)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x70(%rcx), %eax
jge 0x1a136
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
andl $0x7f, %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x4c(%rbp)
testl %eax, %eax
je 0x1a071
jmp 0x1a05a
movl -0x4c(%rbp), %eax
subl $0x1c, %eax
je 0x1a076
jmp 0x1a064
movl -0x4c(%rbp), %eax
addl $-0x1d, %eax
subl $0x4, %eax
jb 0x1a0ab
jmp 0x1a0e0
jmp 0x1a128
movl -0x28(%rbp), %eax
movl -0x2c(%rbp), %ecx
sarl $0x8, %ecx
addl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
subl -0x34(%rbp), %eax
movl -0x28(%rbp), %ecx
subl -0x24(%rbp), %ecx
addl %ecx, %eax
shll $0x8, %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x1a0e2
movl -0x28(%rbp), %eax
movl -0x2c(%rbp), %ecx
sarl $0x10, %ecx
addl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
subl -0x34(%rbp), %eax
movl -0x28(%rbp), %ecx
subl -0x24(%rbp), %ecx
addl %ecx, %eax
shll $0x10, %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x1a0e2
jmp 0x1a0e2
movl -0x2c(%rbp), %edx
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x1a11f
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x24(%rbp), %rcx
movq -0x8(%rbp), %rdx
movq 0x38(%rdx), %rdx
movslq -0x28(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1a015
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x7c(%rcx), %eax
jge 0x1a19e
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movslq -0x3c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpl $-0x1, (%rax)
jae 0x1a191
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movl (%rcx), %ecx
movl (%rax,%rcx,4), %ecx
movq -0x48(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movl 0x4(%rcx), %ecx
movl (%rax,%rcx,4), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x1a193
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1a13d
movl -0x14(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x70(%rax)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x8(%rbp), %rax
movslq 0x70(%rax), %rsi
shlq $0x2, %rsi
callq 0x12840
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rdi
callq 0x3acb0
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)