// File: /workspace/github_c_makefile_star2_O2/janet-lang[P]janet/src/core/compile.c
// Function: janetc_resolve
JanetSlot janetc_resolve(
    JanetCompiler *c,
    const uint8_t *sym) {

    JanetSlot ret = janetc_cslot(janet_wrap_nil());
    JanetScope *scope = c->scope;
    SymPair *pair;
    int foundlocal = 1;
    int unused = 0;

    /* Search scopes for symbol, starting from top */
    while (scope) {
        int32_t i, len;
        if (scope->flags & JANET_SCOPE_UNUSED)
            unused = 1;
        len = janet_v_count(scope->syms);
        /* Search in reverse order */
        for (i = len - 1; i >= 0; i--) {
            pair = scope->syms + i;
            if (pair->sym == sym) {
                ret = pair->slot;
                goto found;
            }
        }
        if (scope->flags & JANET_SCOPE_FUNCTION)
            foundlocal = 0;
        scope = scope->parent;
    }

    /* Symbol not found - check for global */
    {
        JanetBinding binding = janet_resolve_ext(c->env, sym);
        if (binding.type == JANET_BINDING_NONE) {
            Janet handler = janet_table_get(c->env, janet_ckeywordv("missing-symbol"));
            switch (janet_type(handler)) {
                case JANET_NIL:
                    break;
                case JANET_FUNCTION:
                    if (!lookup_missing(c, sym, janet_unwrap_function(handler), &binding))
                        return janetc_cslot(janet_wrap_nil());
                    break;
                default:
                    janetc_error(c, janet_formatc("invalid lookup handler %V", handler));
                    return janetc_cslot(janet_wrap_nil());
            }
        }

        switch (binding.type) {
            default:
            case JANET_BINDING_NONE:
                janetc_error(c, janet_formatc("unknown symbol %q", janet_wrap_symbol(sym)));
                return janetc_cslot(janet_wrap_nil());
            case JANET_BINDING_DEF:
            case JANET_BINDING_MACRO: /* Macro should function like defs when not in calling pos */
                ret = janetc_cslot(binding.value);
                break;
            case JANET_BINDING_DYNAMIC_DEF:
            case JANET_BINDING_DYNAMIC_MACRO:
                ret = janetc_cslot(binding.value);
                ret.flags |= JANET_SLOT_REF | JANET_SLOT_NAMED | JANET_SLOTTYPE_ANY;
                ret.flags &= ~JANET_SLOT_CONSTANT;
                break;
            case JANET_BINDING_VAR: {
                ret = janetc_cslot(binding.value);
                ret.flags |= JANET_SLOT_REF | JANET_SLOT_NAMED | JANET_SLOT_MUTABLE | JANET_SLOTTYPE_ANY;
                ret.flags &= ~JANET_SLOT_CONSTANT;
                break;
            }
        }
        JanetCompileLintLevel depLevel = JANET_C_LINT_RELAXED;
        switch (binding.deprecation) {
            case JANET_BINDING_DEP_NONE:
                break;
            case JANET_BINDING_DEP_RELAXED:
                depLevel = JANET_C_LINT_RELAXED;
                break;
            case JANET_BINDING_DEP_NORMAL:
                depLevel = JANET_C_LINT_NORMAL;
                break;
            case JANET_BINDING_DEP_STRICT:
                depLevel = JANET_C_LINT_STRICT;
                break;
        }
        if (binding.deprecation != JANET_BINDING_DEP_NONE) {
            janetc_lintf(c, depLevel, "%q is deprecated", janet_wrap_symbol(sym));
        }
        return ret;
    }

    /* Symbol was found */
found:

    /* Constants can be returned immediately (they are stateless) */
    if (ret.flags & (JANET_SLOT_CONSTANT | JANET_SLOT_REF))
        return ret;

    /* Unused references and locals shouldn't add captured envs. */
    if (unused || foundlocal) {
        ret.envindex = -1;
        return ret;
    }

    /* non-local scope needs to expose its environment */
    JanetScope *original_scope = scope;
    pair->keep = 1;
    while (scope && !(scope->flags & JANET_SCOPE_FUNCTION))
        scope = scope->parent;
    janet_assert(scope, "invalid scopes");
    scope->flags |= JANET_SCOPE_ENV;

    /* In the function scope, allocate the slot as an upvalue */
    janetc_regalloc_touch(&scope->ua, ret.index);

    /* Iterate through child scopes and make sure environment is propagated */
    scope = scope->child;

    /* Propagate env up to current scope */
    int32_t envindex = -1;
    while (scope) {
        if (scope->flags & JANET_SCOPE_FUNCTION) {
            int32_t j, len;
            int scopefound = 0;
            /* Check if scope already has env. If so, break */
            len = janet_v_count(scope->envs);
            for (j = 0; j < len; j++) {
                if (scope->envs[j].envindex == envindex) {
                    scopefound = 1;
                    envindex = j;
                    break;
                }
            }
            /* Add the environment if it is not already referenced */
            if (!scopefound) {
                len = janet_v_count(scope->envs);
                JanetEnvRef ref;
                ref.envindex = envindex;
                ref.scope = original_scope;
                janet_v_push(scope->envs, ref);
                envindex = len;
            }
        }
        scope = scope->child;
    }

    ret.envindex = envindex;
    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
movabsq $-0x77fffffffffff, %r15 # imm = 0xFFF8800000000001
movq %r15, %rdi
callq 0x4b313
pushq $0x1
popq %rdx
xorl %esi, %esi
pushq $-0x1
popq %rdi
xorl %eax, %eax
movl %edx, %ecx
movq %r13, %r14
movq (%r14), %r14
testq %r14, %r14
je 0x1a779
movl 0x6c(%r14), %r9d
testb $0x8, %r9b
cmovnel %edx, %eax
movq 0x20(%r14), %r8
movl %edi, %r10d
testq %r8, %r8
je 0x1a5f5
movl -0x4(%r8), %r10d
decl %r10d
testl %r10d, %r10d
js 0x1a60d
movl %r10d, %r11d
imulq $0x38, %r11, %r11
decl %r10d
cmpq %r12, 0x18(%r8,%r11)
jne 0x1a5f5
jmp 0x1a61a
testb $0x1, %r9b
cmovnel %esi, %ecx
addq $0x8, %r14
jmp 0x1a5cb
addq %r11, %r8
movq (%r8), %rsi
movl 0x8(%r8), %r15d
movl 0x10(%r8), %r12d
movl 0x14(%r8), %edx
testl $0x90000, %r12d         # imm = 0x90000
je 0x1a645
movl 0xc(%r8), %eax
movq %rsi, (%rbx)
movl %r15d, 0x8(%rbx)
movl %eax, 0xc(%rbx)
jmp 0x1a654
orl %ecx, %eax
je 0x1a66d
movq %rsi, (%rbx)
movl %r15d, 0x8(%rbx)
orl $-0x1, 0xc(%rbx)
movl %r12d, 0x10(%rbx)
movl %edx, 0x14(%rbx)
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, -0x68(%rbp)
movl %edx, -0x38(%rbp)
movl $0x1, 0x28(%r8)
movq %r14, %r13
testq %r13, %r13
je 0x1a9df
movl 0x6c(%r13), %eax
testb $0x1, %al
jne 0x1a696
movq 0x8(%r13), %r13
jmp 0x1a67f
movq %rbx, -0x30(%rbp)
orl $0x2, %eax
movl %eax, 0x6c(%r13)
leaq 0x48(%r13), %rdi
movl %r15d, %esi
callq 0x3d36c
pushq $-0x1
popq %rbx
xorl %edi, %edi
pushq $0x1
popq %rsi
pushq $0x10
popq %rdx
movq 0x10(%r13), %r13
testq %r13, %r13
je 0x1a755
testb $0x1, 0x6c(%r13)
je 0x1a6b8
movq 0x60(%r13), %rax
testq %rax, %rax
je 0x1a6da
movl -0x4(%rax), %ecx
jmp 0x1a6dc
xorl %ecx, %ecx
testl %ecx, %ecx
cmovlel %edi, %ecx
movq %rax, %r8
xorl %r9d, %r9d
cmpq %r9, %rcx
je 0x1a6fa
cmpl %ebx, (%r8)
je 0x1a711
incq %r9
addq $0x10, %r8
jmp 0x1a6e7
testq %rax, %rax
je 0x1a716
movl -0x4(%rax), %ecx
leal 0x1(%rcx), %r8d
cmpl -0x8(%rax), %r8d
jge 0x1a718
movl %ecx, %r9d
jmp 0x1a73a
movl %r9d, %ebx
jmp 0x1a6b8
xorl %ecx, %ecx
movl %ecx, -0x34(%rbp)
movq %rax, %rdi
callq 0x4799c
movl -0x34(%rbp), %r9d
pushq $0x10
popq %rdx
pushq $0x1
popq %rsi
xorl %edi, %edi
movq %rax, 0x60(%r13)
movl -0x4(%rax), %ecx
leal 0x1(%rcx), %r8d
movl %r8d, -0x4(%rax)
movslq %ecx, %rcx
shlq $0x4, %rcx
movl %ebx, (%rax,%rcx)
movq %r14, 0x8(%rax,%rcx)
movl %r9d, %ebx
jmp 0x1a6b8
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, (%rax)
movl %r15d, 0x8(%rax)
movl %ebx, 0xc(%rax)
movq %rax, %rbx
movl %r12d, 0x10(%rax)
movl -0x38(%rbp), %eax
movl %eax, 0x14(%rbx)
jmp 0x1a65b
movq 0x18(%r13), %rsi
leaq -0x60(%rbp), %r14
movq %r14, %rdi
movq %r12, %rdx
callq 0x45b75
movl (%r14), %eax
testl %eax, %eax
je 0x1a7df
decl %eax
cmpl $0x4, %eax
ja 0x1a944
movq %rbx, -0x30(%rbp)
leaq 0x35b87(%rip), %rcx     # 0x50330
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x58(%rbp), %rbx
movq %rbx, %xmm0
movq %rbx, %rax
shrq $0x2f, %rax
andb $0xf, %al
movl $0x10000, %ecx          # imm = 0x10000
btsl %eax, %ecx
ucomisd %xmm0, %xmm0
movl $0x10001, %r14d         # imm = 0x10001
cmovpl %ecx, %r14d
jmp 0x1a8e9
movq 0x18(%r13), %r14
leaq 0x35d40(%rip), %rdi     # 0x5052a
callq 0x439c6
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
movq %rax, %rdi
callq 0x4b366
movq %r14, %rdi
movq %rax, %rsi
callq 0x43c35
movq %rax, %xmm0
movq %rax, %rdx
shrq $0x2f, %rdx
andl $0xf, %edx
xorl %ecx, %ecx
ucomisd %xmm0, %xmm0
cmovpl %edx, %ecx
cmpl $0x1, %ecx
je 0x1a9d7
cmpl $0xc, %ecx
jne 0x1a95f
movq %rax, %rdi
callq 0x4b1f1
movq %rax, %r14
movq 0x10(%rax), %rax
cmpl $0x1, 0x64(%rax)
jg 0x1a972
cmpl $0x0, 0x68(%rax)
jle 0x1a972
movabsq $-0x5800000000000, %rsi # imm = 0xFFFA800000000000
movq %r12, %rdi
callq 0x4b366
leaq -0x70(%rbp), %rcx
movq %rax, (%rcx)
pushq $0x40
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0x271c9
testq %rax, %rax
je 0x1a97b
movq %rax, %r14
movq 0x18(%r13), %rax
movq %rax, 0x28(%r14)
callq 0x29627
movl %eax, -0x30(%rbp)
movq %r15, %rdi
callq 0x4b313
leaq -0x40(%rbp), %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x47bb8
movl %eax, %r14d
movl -0x30(%rbp), %edi
callq 0x2964e
testl %r14d, %r14d
je 0x1a9b2
movq -0x40(%rbp), %rsi
leaq 0x35ddd(%rip), %rdi     # 0x506af
jmp 0x1a969
movl $0xaffff, %r14d         # imm = 0xAFFFF
jmp 0x1a8e5
movl $0xeffff, %r14d         # imm = 0xEFFFF
movq -0x58(%rbp), %rbx
movl -0x50(%rbp), %eax
testl %eax, %eax
je 0x1a92c
xorl %ecx, %ecx
cmpl $0x2, %eax
sete %cl
cmpl $0x3, %eax
pushq $0x2
popq %r15
cmovnel %ecx, %r15d
movabsq $-0x5800000000000, %rsi # imm = 0xFFFA800000000000
movq %r12, %rdi
callq 0x4b366
leaq 0x35c49(%rip), %rdx     # 0x50565
movq %r13, %rdi
movl %r15d, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x19fd2
movq -0x30(%rbp), %rax
movq %rbx, (%rax)
movq %rax, %rbx
orq $-0x1, 0x8(%rax)
movl %r14d, 0x10(%rax)
jmp 0x1a65b
movabsq $-0x5800000000000, %rsi # imm = 0xFFFA800000000000
movq %r12, %rdi
callq 0x4b366
leaq 0x35bf6(%rip), %rdi     # 0x50553
jmp 0x1a966
leaq 0x35bd3(%rip), %rdi     # 0x50539
movq %rax, %rsi
xorl %eax, %eax
callq 0x3c0ed
jmp 0x1a987
leaq 0x35cd6(%rip), %rdi     # 0x5064f
jmp 0x1a982
leaq 0x35d00(%rip), %rdi     # 0x50682
callq 0x411ea
cmpl $0x1, 0x48(%r13)
je 0x1a99a
movl $0x1, 0x48(%r13)
movq %rax, 0x30(%r13)
movq %r15, %rdi
callq 0x4b313
movq %rbx, %rdi
movq %rax, %rsi
callq 0x19f5c
jmp 0x1a65b
movq -0x40(%rbp), %rsi
leaq -0x88(%rbp), %r14
movq %r14, %rdi
callq 0x456dc
movq 0x10(%r14), %rax
movq %rax, -0x50(%rbp)
movupd (%r14), %xmm0
movapd %xmm0, -0x60(%rbp)
movl -0x60(%rbp), %eax
jmp 0x1a793
movq 0x6560a(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x35620(%rip), %rsi     # 0x50010
leaq 0x35b7f(%rip), %rcx     # 0x50576
leaq 0x35b8b(%rip), %r8      # 0x50589
movl $0x15f, %edx            # imm = 0x15F
xorl %eax, %eax
callq 0x125e0
callq 0x12100