// File: /workspace/github_c_makefile_star2_O0/janet-lang[P]janet/src/core/bytecode.c
// Function: janet_verify
int janet_verify(JanetFuncDef *def) {
    int vargs = !!(def->flags & JANET_FUNCDEF_FLAG_VARARG);
    int32_t i;
    int32_t maxslot = def->arity + vargs;
    int32_t sc = def->slotcount;

    if (def->bytecode_length == 0) return 1;

    if (maxslot > sc) return 2;

    /* Verify each instruction */
    for (i = 0; i < def->bytecode_length; i++) {
        uint32_t instr = def->bytecode[i];
        /* Check for invalid instructions */
        if ((instr & 0x7F) >= JOP_INSTRUCTION_COUNT) {
            return 3;
        }
        enum JanetInstructionType type = janet_instructions[instr & 0x7F];
        switch (type) {
            case JINT_0:
                continue;
            case JINT_S: {
                if ((int32_t)(instr >> 8) >= sc) return 4;
                continue;
            }
            case JINT_SI:
            case JINT_SU:
            case JINT_ST: {
                if ((int32_t)((instr >> 8) & 0xFF) >= sc) return 4;
                continue;
            }
            case JINT_L: {
                int32_t jumpdest = i + (((int32_t)instr) >> 8);
                if (jumpdest < 0 || jumpdest >= def->bytecode_length) return 5;
                continue;
            }
            case JINT_SS: {
                if ((int32_t)((instr >> 8) & 0xFF) >= sc ||
                        (int32_t)(instr >> 16) >= sc) return 4;
                continue;
            }
            case JINT_SSI:
            case JINT_SSU: {
                if ((int32_t)((instr >> 8) & 0xFF) >= sc ||
                        (int32_t)((instr >> 16) & 0xFF) >= sc) return 4;
                continue;
            }
            case JINT_SL: {
                int32_t jumpdest = i + (((int32_t)instr) >> 16);
                if ((int32_t)((instr >> 8) & 0xFF) >= sc) return 4;
                if (jumpdest < 0 || jumpdest >= def->bytecode_length) return 5;
                continue;
            }
            case JINT_SSS: {
                if (((int32_t)(instr >> 8) & 0xFF) >= sc ||
                        ((int32_t)(instr >> 16) & 0xFF) >= sc ||
                        ((int32_t)(instr >> 24) & 0xFF) >= sc) return 4;
                continue;
            }
            case JINT_SD: {
                if ((int32_t)((instr >> 8) & 0xFF) >= sc) return 4;
                if ((int32_t)(instr >> 16) >= def->defs_length) return 6;
                continue;
            }
            case JINT_SC: {
                if ((int32_t)((instr >> 8) & 0xFF) >= sc) return 4;
                if ((int32_t)(instr >> 16) >= def->constants_length) return 7;
                continue;
            }
            case JINT_SES: {
                /* How can we check the last slot index? We need info parent funcdefs. Resort
                 * to runtime checks for now. Maybe invalid upvalue references could be defaulted
                 * to nil? (don't commit to this in the long term, though) */
                if ((int32_t)((instr >> 8) & 0xFF) >= sc) return 4;
                if ((int32_t)((instr >> 16) & 0xFF) >= def->environments_length) return 8;
                continue;
            }
        }
    }

    /* Verify last instruction is either a jump, return, return-nil, or tailcall. Eventually,
     * some real flow analysis would be ideal, but this should be very effective. Will completely
     * prevent running over the end of bytecode. However, valid functions with dead code will
     * be rejected. */
    {
        uint32_t lastop = def->bytecode[def->bytecode_length - 1] & 0xFF;
        switch (lastop) {
            default:
                return 9;
            case JOP_RETURN:
            case JOP_RETURN_NIL:
            case JOP_JUMP:
            case JOP_ERROR:
            case JOP_TAILCALL:
                break;
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl 0x58(%rax), %eax
andl $0x10000, %eax          # imm = 0x10000
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movl 0x60(%rax), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0x5c(%rax), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x70(%rax)
jne 0x1a5f3
movl $0x1, -0x4(%rbp)
jmp 0x1a8f3
movl -0x1c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jle 0x1a607
movl $0x2, -0x4(%rbp)
jmp 0x1a8f3
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x70(%rcx), %eax
jge 0x1a89c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
andl $0x7f, %eax
cmpl $0x4d, %eax
jb 0x1a647
movl $0x3, -0x4(%rbp)
jmp 0x1a8f3
movl -0x24(%rbp), %eax
andl $0x7f, %eax
movl %eax, %ecx
leaq 0x95ffa(%rip), %rax     # 0xb0650
movl (%rax,%rcx,4), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq %rax, -0x40(%rbp)
subq $0xd, %rax
ja 0x1a88c
movq -0x40(%rbp), %rax
leaq 0x66964(%rip), %rcx     # 0x80fdc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x1a88e
movl -0x24(%rbp), %eax
shrl $0x8, %eax
cmpl -0x20(%rbp), %eax
jl 0x1a69d
movl $0x4, -0x4(%rbp)
jmp 0x1a8f3
jmp 0x1a88e
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
cmpl -0x20(%rbp), %eax
jl 0x1a6be
movl $0x4, -0x4(%rbp)
jmp 0x1a8f3
jmp 0x1a88e
movl -0x18(%rbp), %eax
movl -0x24(%rbp), %ecx
sarl $0x8, %ecx
addl %ecx, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jl 0x1a6e3
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x70(%rcx), %eax
jl 0x1a6ef
movl $0x5, -0x4(%rbp)
jmp 0x1a8f3
jmp 0x1a88e
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
cmpl -0x20(%rbp), %eax
jge 0x1a70f
movl -0x24(%rbp), %eax
shrl $0x10, %eax
cmpl -0x20(%rbp), %eax
jl 0x1a71b
movl $0x4, -0x4(%rbp)
jmp 0x1a8f3
jmp 0x1a88e
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
cmpl -0x20(%rbp), %eax
jge 0x1a740
movl -0x24(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
cmpl -0x20(%rbp), %eax
jl 0x1a74c
movl $0x4, -0x4(%rbp)
jmp 0x1a8f3
jmp 0x1a88e
movl -0x18(%rbp), %eax
movl -0x24(%rbp), %ecx
sarl $0x10, %ecx
addl %ecx, %eax
movl %eax, -0x30(%rbp)
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
cmpl -0x20(%rbp), %eax
jl 0x1a77b
movl $0x4, -0x4(%rbp)
jmp 0x1a8f3
cmpl $0x0, -0x30(%rbp)
jl 0x1a78d
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x70(%rcx), %eax
jl 0x1a799
movl $0x5, -0x4(%rbp)
jmp 0x1a8f3
jmp 0x1a88e
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
cmpl -0x20(%rbp), %eax
jge 0x1a7ce
movl -0x24(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
cmpl -0x20(%rbp), %eax
jge 0x1a7ce
movl -0x24(%rbp), %eax
shrl $0x18, %eax
andl $0xff, %eax
cmpl -0x20(%rbp), %eax
jl 0x1a7da
movl $0x4, -0x4(%rbp)
jmp 0x1a8f3
jmp 0x1a88e
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
cmpl -0x20(%rbp), %eax
jl 0x1a7fb
movl $0x4, -0x4(%rbp)
jmp 0x1a8f3
movl -0x24(%rbp), %eax
shrl $0x10, %eax
movq -0x10(%rbp), %rcx
cmpl 0x78(%rcx), %eax
jl 0x1a816
movl $0x6, -0x4(%rbp)
jmp 0x1a8f3
jmp 0x1a88e
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
cmpl -0x20(%rbp), %eax
jl 0x1a834
movl $0x4, -0x4(%rbp)
jmp 0x1a8f3
movl -0x24(%rbp), %eax
shrl $0x10, %eax
movq -0x10(%rbp), %rcx
cmpl 0x6c(%rcx), %eax
jl 0x1a84f
movl $0x7, -0x4(%rbp)
jmp 0x1a8f3
jmp 0x1a88e
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
cmpl -0x20(%rbp), %eax
jl 0x1a86d
movl $0x4, -0x4(%rbp)
jmp 0x1a8f3
movl -0x24(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movq -0x10(%rbp), %rcx
cmpl 0x74(%rcx), %eax
jl 0x1a88a
movl $0x8, -0x4(%rbp)
jmp 0x1a8f3
jmp 0x1a88e
jmp 0x1a88e
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1a60e
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rax
movl 0x70(%rcx), %ecx
decl %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx,4), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x44(%rbp)
subl $0x1, %eax
je 0x1a8ea
jmp 0x1a8c0
movl -0x44(%rbp), %eax
addl $-0x3, %eax
subl $0x2, %eax
jb 0x1a8ea
jmp 0x1a8cd
movl -0x44(%rbp), %eax
subl $0x1c, %eax
je 0x1a8ea
jmp 0x1a8d7
movl -0x44(%rbp), %eax
subl $0x36, %eax
je 0x1a8ea
jmp 0x1a8e1
movl $0x9, -0x4(%rbp)
jmp 0x1a8f3
jmp 0x1a8ec
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopl (%rax,%rax)