// File: /workspace/github_c_makefile_star2_O2/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, -0x40(%rbp)
movq (%rdi), %r14
callq 0x17536
movl 0x40(%r14), %ecx
incl %ecx
movq %rax, -0x30(%rbp)
movl %ecx, 0x5c(%rax)
testb $0x1, 0x6c(%r14)
je 0x1c15e
movq 0x60(%r14), %rbx
testq %rbx, %rbx
movq %r14, -0x38(%rbp)
je 0x1bcfd
movl -0x4(%rbx), %eax
jmp 0x1bcff
xorl %eax, %eax
movq -0x30(%rbp), %r15
movl %eax, 0x74(%r15)
movslq %eax, %r14
leaq (,%r14,4), %rdi
callq 0x12760
movq %rax, 0x10(%r15)
xorl %ecx, %ecx
cmpq %r14, %rcx
jge 0x1bd30
movl (%rbx), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
addq $0x10, %rbx
jmp 0x1bd1d
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %ebx, %ebx
movl $0x0, %eax
testq %rdi, %rdi
je 0x1bd47
movl -0x4(%rdi), %eax
movq -0x30(%rbp), %r14
movl %eax, 0x6c(%r14)
pushq $0x8
popq %rsi
callq 0x479f8
movq %rax, 0x18(%r14)
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rdi
testq %rdi, %rdi
je 0x1bd6b
movl -0x4(%rdi), %ebx
movq -0x30(%rbp), %r14
movl %ebx, 0x78(%r14)
pushq $0x8
popq %rsi
callq 0x479f8
movq %rax, 0x20(%r14)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rbx
testq %rbx, %rbx
je 0x1bd91
movl -0x4(%rbx), %eax
jmp 0x1bd93
xorl %eax, %eax
movq -0x38(%rbp), %rdx
movslq 0x68(%rdx), %r14
subl %r14d, %eax
movq -0x30(%rbp), %r12
movl %eax, 0x70(%r12)
je 0x1be48
movslq %eax, %r15
shlq $0x2, %r15
movq %r15, %rdi
callq 0x12760
movq %rax, 0x28(%r12)
testq %rax, %rax
je 0x1c1a3
leaq (%rbx,%r14,4), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x44ae2
movq -0x38(%rbp), %rdx
movslq 0x68(%rdx), %rbx
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rax
movl %ebx, -0x4(%rax)
movq 0x10(%rcx), %r14
testq %r14, %r14
movq -0x30(%rbp), %r12
je 0x1be48
movq -0x40(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x1be48
movslq 0x70(%r12), %r15
shlq $0x3, %r15
movq %r15, %rdi
callq 0x12760
movq %rax, 0x38(%r12)
testq %rax, %rax
je 0x1c1c2
leaq (%r14,%rbx,8), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x44ae2
movq -0x38(%rbp), %rdx
movl 0x68(%rdx), %eax
movq -0x40(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl %eax, -0x4(%rcx)
movq -0x30(%rbp), %r12
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x40(%r12)
andq $0x0, 0x60(%r12)
movl 0x6c(%rdx), %eax
andl $0x2, %eax
shll $0x10, %eax
movl %eax, 0x58(%r12)
movl 0x50(%rdx), %ebx
testl %ebx, %ebx
je 0x1becc
movl 0x5c(%r12), %r14d
addl $0x1f, %r14d
sarl $0x5, %r14d
movslq %r14d, %rdi
pushq $0x4
popq %rsi
callq 0x125a0
testq %rax, %rax
je 0x1c184
movq %rax, %r15
cmpl %ebx, %r14d
cmovgel %ebx, %r14d
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rsi
movslq %r14d, %rdx
shlq $0x2, %rdx
movq %r15, %rdi
callq 0x12670
cmpl $0x8, %ebx
jl 0x1bec0
movzwl 0x1c(%r15), %eax
movl %eax, 0x1c(%r15)
movq -0x30(%rbp), %rax
movq %r15, 0x30(%rax)
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, %rsi
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x1bed3
xorl %edi, %edi
testq %rsi, %rsi
je 0x1bfb4
xorl %r13d, %r13d
movq %rsi, -0x50(%rbp)
movq 0x60(%rdx), %rax
testq %rax, %rax
je 0x1bf00
movslq -0x4(%rax), %rcx
jmp 0x1bf02
xorl %ecx, %ecx
cmpq %rcx, %r13
jge 0x1bfab
movq %r13, %rcx
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %rbx
cmpq %rsi, %rbx
jne 0x1bfa3
pushq $0x20
popq %r14
xorl %esi, %esi
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x1bf35
movslq -0x4(%rax), %rcx
jmp 0x1bf37
xorl %ecx, %ecx
cmpq %rcx, %rsi
jge 0x1bf9b
movq (%rax,%r14), %r12
testq %r12, %r12
je 0x1bf92
movl -0x18(%rax,%r14), %r15d
testq %rdi, %rdi
je 0x1bf5a
movl -0x4(%rdi), %eax
leal 0x1(%rax), %ecx
cmpl -0x8(%rdi), %ecx
jl 0x1bf76
movq %rsi, -0x48(%rbp)
pushq $0x1
popq %rsi
pushq $0x18
popq %rdx
callq 0x4799c
movq -0x48(%rbp), %rsi
movq %rax, %rdi
movl -0x4(%rax), %eax
leal 0x1(%rax), %ecx
movl %ecx, -0x4(%rdi)
cltq
imulq $0x18, %rax, %rax
orl $-0x1, (%rdi,%rax)
movl %r13d, 0x4(%rdi,%rax)
movl %r15d, 0x8(%rdi,%rax)
movq %r12, 0x10(%rdi,%rax)
incq %rsi
addq $0x38, %r14
jmp 0x1bf26
movq -0x38(%rbp), %rdx
movq -0x50(%rbp), %rsi
incq %r13
jmp 0x1bef1
movq 0x10(%rsi), %rsi
jmp 0x1bee1
pushq $0x30
popq %rbx
xorl %esi, %esi
movq 0x20(%rdx), %rax
testq %rax, %rax
je 0x1bfc8
movslq -0x4(%rax), %rcx
jmp 0x1bfca
xorl %ecx, %ecx
cmpq %rcx, %rsi
jge 0x1c08e
movq -0x10(%rax,%rbx), %r13
testq %r13, %r13
je 0x1c082
movl (%rax,%rbx), %r12d
cmpl $-0x1, %r12d
je 0x1bff3
movl 0x68(%rdx), %ecx
subl %ecx, %r12d
jmp 0x1bffe
movq -0x30(%rbp), %rcx
movl 0x70(%rcx), %r12d
movl 0x68(%rdx), %ecx
movl -0x4(%rax,%rbx), %r14d
subl %ecx, %r14d
movl $0x0, %ecx
cmovbl %ecx, %r14d
cmpl %r12d, %r14d
ja 0x1c0e2
movq -0x30(%rbp), %rcx
movl 0x70(%rcx), %ecx
cmpl %ecx, %r14d
jae 0x1c108
cmpl %ecx, %r12d
ja 0x1c12e
movl -0x28(%rax,%rbx), %r15d
testq %rdi, %rdi
je 0x1c046
movl -0x4(%rdi), %eax
leal 0x1(%rax), %ecx
cmpl -0x8(%rdi), %ecx
jl 0x1c062
movq %rsi, -0x48(%rbp)
pushq $0x1
popq %rsi
pushq $0x18
popq %rdx
callq 0x4799c
movq -0x48(%rbp), %rsi
movq %rax, %rdi
movl -0x4(%rax), %eax
leal 0x1(%rax), %ecx
movl %ecx, -0x4(%rdi)
cltq
imulq $0x18, %rax, %rax
movl %r14d, (%rdi,%rax)
movl %r12d, 0x4(%rdi,%rax)
movl %r15d, 0x8(%rdi,%rax)
movq %r13, 0x10(%rdi,%rax)
movq -0x38(%rbp), %rdx
incq %rsi
addq $0x38, %rbx
jmp 0x1bfb9
testq %rdi, %rdi
je 0x1c098
movl -0x4(%rdi), %eax
jmp 0x1c09a
xorl %eax, %eax
movq -0x30(%rbp), %rbx
movl %eax, 0x7c(%rbx)
pushq $0x18
popq %rsi
callq 0x479f8
movq %rax, 0x50(%rbx)
cmpl $0x0, 0x7c(%rbx)
je 0x1c0b7
orb $0x4, 0x5a(%rbx)
movq -0x40(%rbp), %rdi
callq 0x1a338
movq %rbx, %rdi
callq 0x171a4
movq %rbx, %rdi
callq 0x17070
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x63f07(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x33f1d(%rip), %rsi     # 0x50010
leaq 0x3447c(%rip), %rcx     # 0x50576
leaq 0x34500(%rip), %r8      # 0x50601
movl $0x3dc, %edx            # imm = 0x3DC
jmp 0x1c152
movq 0x63ee1(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x33ef7(%rip), %rsi     # 0x50010
leaq 0x34456(%rip), %rcx     # 0x50576
leaq 0x344f2(%rip), %r8      # 0x50619
movl $0x3dd, %edx            # imm = 0x3DD
jmp 0x1c152
movq 0x63ebb(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x33ed1(%rip), %rsi     # 0x50010
leaq 0x34430(%rip), %rcx     # 0x50576
leaq 0x344d9(%rip), %r8      # 0x50626
movl $0x3de, %edx            # imm = 0x3DE
xorl %eax, %eax
callq 0x125e0
callq 0x12100
movq 0x63e8b(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x33ea1(%rip), %rsi     # 0x50010
leaq 0x34400(%rip), %rcx     # 0x50576
leaq 0x3446c(%rip), %r8      # 0x505e9
movl $0x375, %edx            # imm = 0x375
jmp 0x1c152
movq 0x63e65(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x30e6f(%rip), %rsi     # 0x4d004
leaq 0x343da(%rip), %rdx     # 0x50576
movl $0x3ab, %ecx            # imm = 0x3AB
jmp 0x1c1df
movq 0x63e46(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x30e50(%rip), %rsi     # 0x4d004
leaq 0x343bb(%rip), %rdx     # 0x50576
movl $0x38a, %ecx            # imm = 0x38A
jmp 0x1c1df
movq 0x63e27(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x30e31(%rip), %rsi     # 0x4d004
leaq 0x3439c(%rip), %rdx     # 0x50576
movl $0x392, %ecx            # imm = 0x392
xorl %eax, %eax
callq 0x125e0
pushq $0x1
popq %rdi
callq 0x12a60