// File: /workspace/github_c_makefile_star2_O1/janet-lang[P]janet/src/core/compile.c
// Function: janetc_pop_funcdef
JanetFuncDef *janetc_pop_funcdef(JanetCompiler *c) {
    JanetScope *scope = c->scope;
    JanetFuncDef *def = janet_funcdef_alloc();
    def->slotcount = scope->ra.max + 1;

    janet_assert(scope->flags & JANET_SCOPE_FUNCTION, "expected function scope");

    /* Copy envs */
    def->environments_length = janet_v_count(scope->envs);
    def->environments = janet_malloc(sizeof(int32_t) * def->environments_length);
    for (int32_t i = 0; i < def->environments_length; i++) {
        def->environments[i] = scope->envs[i].envindex;
    }

    def->constants_length = janet_v_count(scope->consts);
    def->constants = janet_v_flatten(scope->consts);

    def->defs_length = janet_v_count(scope->defs);
    def->defs = janet_v_flatten(scope->defs);

    /* Copy bytecode (only last chunk) */
    def->bytecode_length = janet_v_count(c->buffer) - scope->bytecode_start;
    if (def->bytecode_length) {
        size_t s = sizeof(int32_t) * (size_t) def->bytecode_length;
        def->bytecode = janet_malloc(s);
        if (NULL == def->bytecode) {
            JANET_OUT_OF_MEMORY;
        }
        safe_memcpy(def->bytecode, c->buffer + scope->bytecode_start, s);
        janet_v__cnt(c->buffer) = scope->bytecode_start;
        if (NULL != c->mapbuffer && c->source) {
            size_t s = sizeof(JanetSourceMapping) * (size_t) def->bytecode_length;
            def->sourcemap = janet_malloc(s);
            if (NULL == def->sourcemap) {
                JANET_OUT_OF_MEMORY;
            }
            safe_memcpy(def->sourcemap, c->mapbuffer + scope->bytecode_start, s);
            janet_v__cnt(c->mapbuffer) = scope->bytecode_start;
        }
    }

    /* Get source from parser */
    def->source = c->source;

    def->arity = 0;
    def->min_arity = 0;
    def->flags = 0;
    if (scope->flags & JANET_SCOPE_ENV) {
        def->flags |= JANET_FUNCDEF_FLAG_NEEDSENV;
    }

    /* Copy upvalue bitset */
    if (scope->ua.count) {
        /* Number of u32s we need to create a bitmask for all slots */
        int32_t slotchunks = (def->slotcount + 31) >> 5;
        /* numchunks is min of slotchunks and scope->ua.count */
        int32_t numchunks = slotchunks > scope->ua.count ? scope->ua.count : slotchunks;
        uint32_t *chunks = janet_calloc(slotchunks, sizeof(uint32_t));
        if (NULL == chunks) {
            JANET_OUT_OF_MEMORY;
        }
        memcpy(chunks, scope->ua.chunks, sizeof(uint32_t) * numchunks);
        /* Register allocator preallocates some registers [240-255, high 16 bits of chunk index 7], we can ignore those. */
        if (scope->ua.count > 7) chunks[7] &= 0xFFFFU;
        def->closure_bitset = chunks;
    }

    /* Capture symbol to local mapping */
    JanetSymbolMap *locals = NULL;

    /* Symbol -> upvalue mapping */
    JanetScope *top = c->scope;
    while (top->parent) top = top->parent;
    for (JanetScope *s = top; s != NULL; s = s->child) {
        for (int32_t j = 0; j < janet_v_count(scope->envs); j++) {
            JanetEnvRef ref = scope->envs[j];
            JanetScope *upscope = ref.scope;
            if (upscope != s) continue;
            for (int32_t i = 0; i < janet_v_count(upscope->syms); i++) {
                SymPair pair = upscope->syms[i];
                if (pair.sym2) {
                    JanetSymbolMap jsm;
                    jsm.birth_pc = UINT32_MAX;
                    jsm.death_pc = j;
                    jsm.slot_index = pair.slot.index;
                    jsm.symbol = pair.sym2;
                    janet_v_push(locals, jsm);
                }
            }
        }
    }

    /* Symbol -> slot mapping */
    for (int32_t i = 0; i < janet_v_count(scope->syms); i++) {
        SymPair pair = scope->syms[i];
        if (pair.sym2) {
            JanetSymbolMap jsm;
            if (pair.death_pc == UINT32_MAX) {
                jsm.death_pc = def->bytecode_length;
            } else {
                jsm.death_pc = pair.death_pc - scope->bytecode_start;
            }
            /* Handle birth_pc == 0 correctly */
            if ((uint32_t) scope->bytecode_start > pair.birth_pc) {
                jsm.birth_pc = 0;
            } else {
                jsm.birth_pc = pair.birth_pc - scope->bytecode_start;
            }
            janet_assert(jsm.birth_pc <= jsm.death_pc, "birth pc after death pc");
            janet_assert(jsm.birth_pc < (uint32_t) def->bytecode_length, "bad birth pc");
            janet_assert(jsm.death_pc <= (uint32_t) def->bytecode_length, "bad death pc");
            jsm.slot_index = pair.slot.index;
            jsm.symbol = pair.sym2;
            janet_v_push(locals, jsm);
        }
    }
    def->symbolmap_length = janet_v_count(locals);
    def->symbolmap = janet_v_flatten(locals);
    if (def->symbolmap_length) def->flags |= JANET_FUNCDEF_FLAG_HASSYMBOLMAP;

    /* Pop the scope */
    janetc_popscope(c);

    /* Do basic optimization */
    janet_bytecode_movopt(def);
    janet_bytecode_remove_noops(def);

    return def;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, -0x38(%rbp)
movq (%rdi), %r12
callq 0x1900b
movl 0x40(%r12), %ecx
incl %ecx
movl %ecx, 0x5c(%rax)
testb $0x1, 0x6c(%r12)
je 0x1e221
movq 0x60(%r12), %rbx
testq %rbx, %rbx
je 0x1de0a
movl -0x4(%rbx), %ecx
jmp 0x1de0c
xorl %ecx, %ecx
movl %ecx, 0x74(%rax)
movslq %ecx, %r14
movq %rax, -0x30(%rbp)
leaq (,%r14,4), %rdi
callq 0x12770
movq -0x30(%rbp), %rsi
movq %rax, 0x10(%rsi)
testl %r14d, %r14d
jle 0x1de47
xorl %ecx, %ecx
movl (%rbx), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
movslq 0x74(%rsi), %rdx
addq $0x10, %rbx
cmpq %rdx, %rcx
jl 0x1de32
movq 0x18(%r12), %rdi
xorl %ebx, %ebx
movl $0x0, %eax
testq %rdi, %rdi
je 0x1de5b
movl -0x4(%rdi), %eax
movq -0x30(%rbp), %r14
movl %eax, 0x6c(%r14)
movl $0x8, %esi
callq 0x4c119
movq %rax, 0x18(%r14)
movq 0x28(%r12), %rdi
testq %rdi, %rdi
je 0x1de7e
movl -0x4(%rdi), %ebx
movq -0x30(%rbp), %r14
movl %ebx, 0x78(%r14)
movl $0x8, %esi
callq 0x4c119
movq %r14, %rcx
movq %rax, 0x20(%r14)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rbx
testq %rbx, %rbx
je 0x1dea9
movl -0x4(%rbx), %eax
jmp 0x1deab
xorl %eax, %eax
movslq 0x68(%r12), %r14
subl %r14d, %eax
movl %eax, 0x70(%rcx)
movq %r12, -0x40(%rbp)
je 0x1df63
movslq %eax, %r15
shlq $0x2, %r15
movq %rcx, %r12
movq %r15, %rdi
callq 0x12770
movq %rax, 0x28(%r12)
testq %rax, %rax
je 0x1e22b
leaq (%rbx,%r14,4), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x49017
movq -0x40(%rbp), %r12
movl 0x68(%r12), %eax
movq -0x38(%rbp), %rdx
movq 0x8(%rdx), %rcx
movl %eax, -0x4(%rcx)
movq 0x10(%rdx), %rbx
testq %rbx, %rbx
movq -0x30(%rbp), %rcx
je 0x1df63
movq -0x38(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x1df63
movslq 0x70(%rcx), %r15
shlq $0x3, %r15
movq %rcx, %r14
movq %r15, %rdi
callq 0x12770
movq %rax, 0x38(%r14)
testq %rax, %rax
je 0x1e230
movslq 0x68(%r12), %rcx
leaq (%rbx,%rcx,8), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x49017
movl 0x68(%r12), %eax
movq -0x38(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl %eax, -0x4(%rcx)
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x40(%rcx)
movq $0x0, 0x60(%rcx)
movl $0x0, 0x58(%rcx)
movl 0x6c(%r12), %eax
andl $0x2, %eax
shll $0x10, %eax
movl %eax, 0x58(%rcx)
movl 0x50(%r12), %ebx
testl %ebx, %ebx
je 0x1dfee
movl 0x5c(%rcx), %r14d
addl $0x1f, %r14d
sarl $0x5, %r14d
movslq %r14d, %rdi
movl $0x4, %esi
callq 0x125a0
testq %rax, %rax
je 0x1e226
movq %rax, %r15
cmpl %ebx, %r14d
cmovll %r14d, %ebx
movq 0x48(%r12), %rsi
movslq %ebx, %rdx
shlq $0x2, %rdx
movq %rax, %rdi
callq 0x12680
cmpl $0x8, 0x50(%r12)
jl 0x1dfe6
movzwl 0x1c(%r15), %eax
movl %eax, 0x1c(%r15)
movq -0x30(%rbp), %rax
movq %r15, 0x30(%rax)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, %rdx
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x1dff5
xorl %edi, %edi
testq %rdx, %rdx
je 0x1e0dc
xorl %esi, %esi
movq %rdx, -0x50(%rbp)
movq 0x60(%r12), %rax
testq %rax, %rax
je 0x1e022
movslq -0x4(%rax), %rcx
jmp 0x1e024
xorl %ecx, %ecx
cmpq %rcx, %rsi
jge 0x1e0d3
movq %rsi, %rcx
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %r14
cmpq %rdx, %r14
jne 0x1e0cb
movl $0x20, %ebx
xorl %r15d, %r15d
movq %rsi, -0x48(%rbp)
movq 0x20(%r14), %rax
testq %rax, %rax
je 0x1e05d
movslq -0x4(%rax), %rcx
jmp 0x1e05f
xorl %ecx, %ecx
cmpq %rcx, %r15
jge 0x1e0c3
movq (%rax,%rbx), %r13
testq %r13, %r13
je 0x1e0ba
movl -0x18(%rax,%rbx), %r12d
testq %rdi, %rdi
je 0x1e081
movl -0x4(%rdi), %eax
incl %eax
cmpl -0x8(%rdi), %eax
jl 0x1e097
movl $0x1, %esi
movl $0x18, %edx
callq 0x4c0b4
movq -0x48(%rbp), %rsi
movq %rax, %rdi
movslq -0x4(%rdi), %rax
leal 0x1(%rax), %ecx
movl %ecx, -0x4(%rdi)
leaq (%rax,%rax,2), %rax
movl $0xffffffff, (%rdi,%rax,8) # imm = 0xFFFFFFFF
movl %esi, 0x4(%rdi,%rax,8)
movl %r12d, 0x8(%rdi,%rax,8)
movq %r13, 0x10(%rdi,%rax,8)
incq %r15
addq $0x38, %rbx
jmp 0x1e04e
movq -0x40(%rbp), %r12
movq -0x50(%rbp), %rdx
incq %rsi
jmp 0x1e012
movq 0x10(%rdx), %rdx
jmp 0x1e003
movl $0x30, %r14d
xorl %esi, %esi
movq 0x20(%r12), %rax
testq %rax, %rax
je 0x1e0f4
movslq -0x4(%rax), %rcx
jmp 0x1e0f6
xorl %ecx, %ecx
cmpq %rcx, %rsi
jge 0x1e1bc
movq -0x10(%rax,%r14), %rbx
testq %rbx, %rbx
je 0x1e1b0
movl (%rax,%r14), %r15d
cmpl $-0x1, %r15d
je 0x1e11e
subl 0x68(%r12), %r15d
jmp 0x1e126
movq -0x30(%rbp), %rcx
movl 0x70(%rcx), %r15d
movq %r12, %rcx
movl -0x4(%rax,%r14), %r12d
subl 0x68(%rcx), %r12d
movl $0x0, %ecx
cmovbl %ecx, %r12d
cmpl %r15d, %r12d
ja 0x1e212
movq -0x30(%rbp), %rcx
movl 0x70(%rcx), %ecx
cmpl %ecx, %r12d
jae 0x1e217
cmpl %ecx, %r15d
ja 0x1e21c
movl -0x28(%rax,%r14), %r13d
testq %rdi, %rdi
je 0x1e171
movl -0x4(%rdi), %eax
incl %eax
cmpl -0x8(%rdi), %eax
jl 0x1e18b
movq %rsi, -0x48(%rbp)
movl $0x1, %esi
movl $0x18, %edx
callq 0x4c0b4
movq -0x48(%rbp), %rsi
movq %rax, %rdi
movslq -0x4(%rdi), %rax
leal 0x1(%rax), %ecx
movl %ecx, -0x4(%rdi)
leaq (%rax,%rax,2), %rax
movl %r12d, (%rdi,%rax,8)
movl %r15d, 0x4(%rdi,%rax,8)
movl %r13d, 0x8(%rdi,%rax,8)
movq %rbx, 0x10(%rdi,%rax,8)
movq -0x40(%rbp), %r12
incq %rsi
addq $0x38, %r14
jmp 0x1e0e4
testq %rdi, %rdi
je 0x1e1c6
movl -0x4(%rdi), %eax
jmp 0x1e1c8
xorl %eax, %eax
movq -0x30(%rbp), %rbx
movl %eax, 0x7c(%rbx)
movl $0x18, %esi
callq 0x4c119
movq %rax, 0x50(%rbx)
cmpl $0x0, 0x7c(%rbx)
je 0x1e1e7
orb $0x4, 0x5a(%rbx)
movq -0x38(%rbp), %rdi
callq 0x1c045
movq %rbx, %rdi
callq 0x18c0c
movq %rbx, %rdi
callq 0x18acc
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x13138
callq 0x130d0
callq 0x13104
callq 0x1316c
callq 0x130a0
callq 0x13070
callq 0x13040