// File: /workspace/github_c_makefile_star2_O3/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, %r14
movq %rdi, %rbx
movabsq $-0x77fffffffffff, %r15 # imm = 0xFFF8800000000001
movq %r15, %rdi
callq 0x4ece6
movq (%r14), %r13
testq %r13, %r13
je 0x1c1dc
xorl %edx, %edx
movl $0x1, %esi
movl $0x1, %eax
xorl %ecx, %ecx
movl 0x6c(%r13), %r8d
testb $0x8, %r8b
cmovnel %esi, %ecx
movq 0x20(%r13), %rdi
testq %rdi, %rdi
je 0x1c1cc
movl -0x4(%rdi), %r9d
testl %r9d, %r9d
jle 0x1c1cc
incl %r9d
leal -0x2(%r9), %r10d
imulq $0x38, %r10, %r10
cmpq %r12, 0x18(%rdi,%r10)
je 0x1c247
decl %r9d
cmpl $0x2, %r9d
jge 0x1c1b0
testb $0x1, %r8b
cmovnel %edx, %eax
movq 0x8(%r13), %r13
testq %r13, %r13
jne 0x1c190
movq 0x18(%r14), %rsi
leaq -0x60(%rbp), %r13
movq %r13, %rdi
movq %r12, %rdx
callq 0x49643
movl (%r13), %eax
testl %eax, %eax
je 0x1c28f
decl %eax
cmpl $0x4, %eax
ja 0x1c4f1
movq %r14, -0x30(%rbp)
leaq 0x3811f(%rip), %rcx     # 0x54330
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x58(%rbp), %r13
movq %r13, %xmm0
movq %r13, %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 0x1c497
addq %r10, %rdi
movq (%rdi), %r9
movl 0x8(%rdi), %esi
movl 0x10(%rdi), %r8d
movl 0x14(%rdi), %edx
testl $0x90000, %r8d          # imm = 0x90000
je 0x1c26e
movl 0xc(%rdi), %eax
movq %r9, (%rbx)
movl %esi, 0x8(%rbx)
movl %eax, 0xc(%rbx)
jmp 0x1c283
orl %eax, %ecx
je 0x1c396
movq %r9, (%rbx)
movl %esi, 0x8(%rbx)
movl $0xffffffff, 0xc(%rbx)  # imm = 0xFFFFFFFF
movl %r8d, 0x10(%rbx)
movl %edx, 0x14(%rbx)
jmp 0x1c580
movq 0x18(%r14), %r13
leaq 0x38290(%rip), %rdi     # 0x5452a
callq 0x47261
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
movq %rax, %rdi
callq 0x4ed39
movq %r13, %rdi
movq %rax, %rsi
callq 0x474e0
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 0x1c5b8
cmpl $0xc, %ecx
jne 0x1c50c
movq %rax, %rdi
callq 0x4ebc4
movq %rax, %r13
movq 0x10(%rax), %rax
cmpl $0x1, 0x64(%rax)
jg 0x1c51f
cmpl $0x0, 0x68(%rax)
jle 0x1c51f
movabsq $-0x5800000000000, %rsi # imm = 0xFFFA800000000000
movq %r12, %rdi
callq 0x4ed39
leaq -0x70(%rbp), %rcx
movq %rax, (%rcx)
movq %r13, %rdi
movl $0x40, %esi
movl $0x1, %edx
callq 0x295e4
testq %rax, %rax
je 0x1c528
movq %rax, %r13
movq 0x18(%r14), %rax
movq %rax, 0x28(%r13)
callq 0x2bafa
movl %eax, -0x30(%rbp)
movq %r15, %rdi
callq 0x4ece6
leaq -0x40(%rbp), %rdx
movq %r13, %rdi
movq %rax, %rsi
callq 0x4b6c1
movl %eax, %r13d
movl -0x30(%rbp), %edi
callq 0x2bb21
testl %r13d, %r13d
je 0x1c592
movq -0x40(%rbp), %rsi
leaq 0x38329(%rip), %rdi     # 0x546af
jmp 0x1c516
movl $0xaffff, %r14d         # imm = 0xAFFFF
jmp 0x1c493
movl $0x1, 0x28(%rdi)
movq %r13, %r14
movl 0x6c(%r14), %eax
testb $0x1, %al
jne 0x1c3b6
movq 0x8(%r14), %r14
testq %r14, %r14
jne 0x1c3a0
callq 0x12ffc
movq %r9, -0x68(%rbp)
movl %r8d, -0x38(%rbp)
movl %edx, -0x30(%rbp)
orl $0x2, %eax
movl %eax, 0x6c(%r14)
leaq 0x48(%r14), %rdi
movl %esi, -0x34(%rbp)
callq 0x409ac
movq 0x10(%r14), %r15
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testq %r15, %r15
je 0x1c46b
testb $0x1, 0x6c(%r15)
je 0x1c45d
movq 0x60(%r15), %rax
testq %rax, %rax
je 0x1c426
movslq -0x4(%rax), %rcx
testq %rcx, %rcx
jle 0x1c416
movq %rax, %rsi
xorl %edx, %edx
cmpl %r14d, (%rsi)
je 0x1c466
incq %rdx
addq $0x10, %rsi
cmpq %rdx, %rcx
jne 0x1c405
movl -0x4(%rax), %ecx
leal 0x1(%rcx), %edx
cmpl -0x8(%rax), %edx
jge 0x1c428
movl %ecx, %r12d
jmp 0x1c447
xorl %ecx, %ecx
movl %ecx, %r12d
movq %rax, %rdi
movl $0x1, %esi
movl $0x10, %edx
callq 0x4b4e8
movq %rax, 0x60(%r15)
movl -0x4(%rax), %ecx
leal 0x1(%rcx), %edx
movl %edx, -0x4(%rax)
movslq %ecx, %rcx
shlq $0x4, %rcx
movl %r14d, (%rax,%rcx)
movq %r13, 0x8(%rax,%rcx)
movl %r12d, %r14d
movq 0x10(%r15), %r15
jmp 0x1c3de
movl %edx, %r14d
jmp 0x1c45d
movq -0x68(%rbp), %rax
movq %rax, (%rbx)
movl -0x34(%rbp), %eax
movl %eax, 0x8(%rbx)
movl %r14d, 0xc(%rbx)
movl -0x38(%rbp), %eax
movl %eax, 0x10(%rbx)
movl -0x30(%rbp), %eax
movl %eax, 0x14(%rbx)
jmp 0x1c580
movl $0xeffff, %r14d         # imm = 0xEFFFF
movq -0x58(%rbp), %r13
movl -0x50(%rbp), %eax
testl %eax, %eax
je 0x1c4dd
xorl %ecx, %ecx
cmpl $0x2, %eax
sete %cl
cmpl $0x3, %eax
movl $0x2, %r15d
cmovnel %ecx, %r15d
movabsq $-0x5800000000000, %rsi # imm = 0xFFFA800000000000
movq %r12, %rdi
callq 0x4ed39
leaq 0x38099(%rip), %rdx     # 0x54565
movq -0x30(%rbp), %rdi
movl %r15d, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x1bb6f
movq %r13, (%rbx)
movq $-0x1, 0x8(%rbx)
movl %r14d, 0x10(%rbx)
jmp 0x1c580
movabsq $-0x5800000000000, %rsi # imm = 0xFFFA800000000000
movq %r12, %rdi
callq 0x4ed39
leaq 0x38049(%rip), %rdi     # 0x54553
jmp 0x1c513
leaq 0x38026(%rip), %rdi     # 0x54539
movq %rax, %rsi
xorl %eax, %eax
callq 0x3f652
jmp 0x1c534
leaq 0x38129(%rip), %rdi     # 0x5464f
jmp 0x1c52f
leaq 0x38153(%rip), %rdi     # 0x54682
callq 0x4482c
cmpl $0x1, 0x48(%r14)
je 0x1c547
movl $0x1, 0x48(%r14)
movq %rax, 0x30(%r14)
movq %r15, %rdi
callq 0x4ece6
movq %rax, %xmm0
movq %rax, %rcx
shrq $0x2f, %rcx
andb $0xf, %cl
movl $0x10000, %edx          # imm = 0x10000
btsl %ecx, %edx
ucomisd %xmm0, %xmm0
movl $0x10001, %ecx          # imm = 0x10001
cmovpl %edx, %ecx
movl %ecx, 0x10(%rbx)
movq $-0x1, 0x8(%rbx)
movq %rax, (%rbx)
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %rsi
leaq -0x88(%rbp), %r13
movq %r13, %rdi
callq 0x4913f
movq 0x10(%r13), %rax
movq %rax, -0x50(%rbp)
movupd (%r13), %xmm0
movapd %xmm0, -0x60(%rbp)
movl -0x60(%rbp), %eax
jmp 0x1c1fb