// File: /workspace/github_c_makefile_star2_O3/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 0x18f4d
movl 0x40(%r12), %ecx
incl %ecx
movl %ecx, 0x5c(%rax)
testb $0x1, 0x6c(%r12)
je 0x1dec0
movq 0x60(%r12), %rbx
testq %rbx, %rbx
je 0x1dab1
movl -0x4(%rbx), %ecx
jmp 0x1dab3
xorl %ecx, %ecx
movl %ecx, 0x74(%rax)
movslq %ecx, %r14
movq %rax, %r15
leaq (,%r14,4), %rdi
callq 0x12760
movq %r15, -0x30(%rbp)
movq %rax, 0x10(%r15)
testl %r14d, %r14d
jle 0x1dae9
xorl %ecx, %ecx
movl (%rbx), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
addq $0x10, %rbx
cmpq %r14, %rcx
jl 0x1dad8
movq 0x18(%r12), %rdi
xorl %ebx, %ebx
movl $0x0, %eax
testq %rdi, %rdi
je 0x1dafd
movl -0x4(%rdi), %eax
movq -0x30(%rbp), %r14
movl %eax, 0x6c(%r14)
movl $0x8, %esi
callq 0x4b547
movq %rax, 0x18(%r14)
movq 0x28(%r12), %rdi
testq %rdi, %rdi
je 0x1db20
movl -0x4(%rdi), %ebx
movq -0x30(%rbp), %r14
movl %ebx, 0x78(%r14)
movl $0x8, %esi
callq 0x4b547
movq %r14, %rcx
movq %rax, 0x20(%r14)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rbx
testq %rbx, %rbx
je 0x1db4b
movl -0x4(%rbx), %eax
jmp 0x1db4d
xorl %eax, %eax
movslq 0x68(%r12), %r14
subl %r14d, %eax
movl %eax, 0x70(%rcx)
movq %r12, -0x40(%rbp)
je 0x1dc05
movslq %eax, %r15
shlq $0x2, %r15
movq %rcx, %r12
movq %r15, %rdi
callq 0x12760
movq %rax, 0x28(%r12)
testq %rax, %rax
je 0x1deca
leaq (%rbx,%r14,4), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x48567
movq -0x40(%rbp), %r12
movslq 0x68(%r12), %rbx
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rax
movl %ebx, -0x4(%rax)
movq 0x10(%rcx), %r14
testq %r14, %r14
movq -0x30(%rbp), %rcx
je 0x1dc05
movq -0x38(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x1dc05
movslq 0x70(%rcx), %r15
shlq $0x3, %r15
movq %rcx, %r12
movq %r15, %rdi
callq 0x12760
movq %rax, 0x38(%r12)
testq %rax, %rax
je 0x1decf
leaq (%r14,%rbx,8), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x48567
movq -0x40(%rbp), %r12
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 0x6c(%r12), %eax
andl $0x2, %eax
shll $0x10, %eax
movl %eax, 0x58(%rcx)
movl 0x50(%r12), %ebx
testl %ebx, %ebx
je 0x1dc86
movl 0x5c(%rcx), %r14d
addl $0x1f, %r14d
sarl $0x5, %r14d
movslq %r14d, %rdi
movl $0x4, %esi
callq 0x125a0
testq %rax, %rax
je 0x1dec5
movq %rax, %r15
cmpl %ebx, %r14d
cmovgel %ebx, %r14d
movq 0x48(%r12), %rsi
movslq %r14d, %rdx
shlq $0x2, %rdx
movq %rax, %rdi
callq 0x12670
cmpl $0x8, %ebx
jl 0x1dc7e
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 0x1dc8d
xorl %edi, %edi
xorl %esi, %esi
movq %rdx, -0x50(%rbp)
movq 0x60(%r12), %rax
testq %rax, %rax
je 0x1dcb1
movslq -0x4(%rax), %rcx
jmp 0x1dcb3
xorl %ecx, %ecx
cmpq %rcx, %rsi
jge 0x1dd64
movq %rsi, %rcx
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %r13
cmpq %rdx, %r13
jne 0x1dd5c
movl $0x20, %ebx
xorl %r14d, %r14d
movq %rsi, -0x48(%rbp)
movq 0x20(%r13), %rax
testq %rax, %rax
je 0x1dcec
movslq -0x4(%rax), %rcx
jmp 0x1dcee
xorl %ecx, %ecx
cmpq %rcx, %r14
jge 0x1dd54
movq (%rax,%rbx), %r15
testq %r15, %r15
je 0x1dd4b
movl -0x18(%rax,%rbx), %r12d
testq %rdi, %rdi
je 0x1dd11
movl -0x4(%rdi), %eax
leal 0x1(%rax), %ecx
cmpl -0x8(%rdi), %ecx
jl 0x1dd2d
movl $0x1, %esi
movl $0x18, %edx
callq 0x4b4e8
movq -0x48(%rbp), %rsi
movq %rax, %rdi
movl -0x4(%rax), %eax
leal 0x1(%rax), %ecx
movl %ecx, -0x4(%rdi)
cltq
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 %r15, 0x10(%rdi,%rax,8)
incq %r14
addq $0x38, %rbx
jmp 0x1dcdd
movq -0x40(%rbp), %r12
movq -0x50(%rbp), %rdx
incq %rsi
jmp 0x1dca1
movq 0x10(%rdx), %rdx
testq %rdx, %rdx
jne 0x1dc9b
movl $0x30, %r15d
xorl %esi, %esi
movq 0x20(%r12), %rax
testq %rax, %rax
je 0x1dd89
movslq -0x4(%rax), %rcx
jmp 0x1dd8b
xorl %ecx, %ecx
cmpq %rcx, %rsi
jge 0x1de5b
movq -0x10(%rax,%r15), %rbx
testq %rbx, %rbx
je 0x1de4f
movl (%rax,%r15), %r12d
cmpl $-0x1, %r12d
je 0x1ddb8
movq -0x40(%rbp), %rcx
movl 0x68(%rcx), %ecx
subl %ecx, %r12d
jmp 0x1ddc7
movq -0x30(%rbp), %rcx
movl 0x70(%rcx), %r12d
movq -0x40(%rbp), %rcx
movl 0x68(%rcx), %ecx
movl -0x4(%rax,%r15), %r13d
subl %ecx, %r13d
movl $0x0, %ecx
cmovbl %ecx, %r13d
cmpl %r12d, %r13d
ja 0x1deb1
movq -0x30(%rbp), %rcx
movl 0x70(%rcx), %ecx
cmpl %ecx, %r13d
jae 0x1deb6
cmpl %ecx, %r12d
ja 0x1debb
movl -0x28(%rax,%r15), %r14d
testq %rdi, %rdi
je 0x1de0f
movl -0x4(%rdi), %eax
leal 0x1(%rax), %ecx
cmpl -0x8(%rdi), %ecx
jl 0x1de2f
movq %rsi, -0x48(%rbp)
movl $0x1, %esi
movl $0x18, %edx
callq 0x4b4e8
movq -0x48(%rbp), %rsi
movq %rax, %rdi
movl -0x4(%rax), %eax
leal 0x1(%rax), %ecx
movl %ecx, -0x4(%rdi)
cltq
leaq (%rax,%rax,2), %rax
movl %r13d, (%rdi,%rax,8)
movl %r12d, 0x4(%rdi,%rax,8)
movl %r14d, 0x8(%rdi,%rax,8)
movq %rbx, 0x10(%rdi,%rax,8)
movq -0x40(%rbp), %r12
incq %rsi
addq $0x38, %r15
jmp 0x1dd79
testq %rdi, %rdi
je 0x1de65
movl -0x4(%rdi), %eax
jmp 0x1de67
xorl %eax, %eax
movq -0x30(%rbp), %rbx
movl %eax, 0x7c(%rbx)
movl $0x18, %esi
callq 0x4b547
movq %rax, 0x50(%rbx)
cmpl $0x0, 0x7c(%rbx)
je 0x1de86
orb $0x4, 0x5a(%rbx)
movq -0x38(%rbp), %rdi
callq 0x1beec
movq %rbx, %rdi
callq 0x18baa
movq %rbx, %rdi
callq 0x18a6c
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x13128
callq 0x130c0
callq 0x130f4
callq 0x1315c
callq 0x13090
callq 0x13060
callq 0x13030