// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4f95a
movq %rax, %xmm0
movq %rax, -0x40(%rbp)
shrq $0x2f, %rax
andb $0xf, %al
movl $0x10000, %ecx          # imm = 0x10000
btsl %eax, %ecx
ucomisd %xmm0, %xmm0
movl $0x10001, %ebx          # imm = 0x10001
cmovpl %ecx, %ebx
movq %r14, %r13
movq (%r14), %r14
movl $0x1, %esi
xorl %eax, %eax
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0x1, %edx
testq %r14, %r14
je 0x1c3d1
movl 0x6c(%r14), %r9d
testb $0x8, %r9b
cmovnel %esi, %eax
movq 0x20(%r14), %r8
testq %r8, %r8
je 0x1c335
movl -0x4(%r8), %r10d
decl %r10d
jmp 0x1c33b
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
testl %r10d, %r10d
js 0x1c35e
movl %r10d, %edi
imulq $0x38, %rdi, %r11
leaq (%r8,%r11), %rdi
cmpq %r12, 0x18(%r8,%r11)
je 0x1c36f
leal -0x1(%r10), %r11d
testl %r10d, %r10d
movl %r11d, %r10d
jg 0x1c340
xorl %r8d, %r8d
testb $0x1, %r9b
cmovnel %r8d, %edx
movq 0x8(%r14), %r14
jmp 0x1c390
movq (%rdi), %rcx
movq %rcx, -0x40(%rbp)
movl 0x8(%rdi), %ecx
movl %ecx, -0x30(%rbp)
movl 0xc(%rdi), %ecx
movl 0x10(%rdi), %ebx
movl 0x14(%rdi), %r8d
movl %r8d, -0x2c(%rbp)
movl $0x7, %r8d
testl %r8d, %r8d
je 0x1c30f
cmpl $0x7, %r8d
jne 0x1c7dc
testl $0x90000, %ebx          # imm = 0x90000
je 0x1c55b
movq -0x40(%rbp), %rax
movq %rax, (%r15)
movl -0x30(%rbp), %eax
movl %eax, 0x8(%r15)
movl %ecx, 0xc(%r15)
movl %ebx, 0x10(%r15)
movl -0x2c(%rbp), %eax
movl %eax, 0x14(%r15)
jmp 0x1c7dc
movq %r15, -0x38(%rbp)
movq %r13, %r14
movq 0x18(%r13), %rsi
leaq -0x60(%rbp), %r15
movq %r15, %rdi
movq %r12, %rdx
callq 0x4a15b
cmpl $0x0, (%r15)
je 0x1c43c
movl -0x60(%rbp), %eax
decl %eax
cmpl $0x4, %eax
ja 0x1c71c
leaq 0x38f2a(%rip), %rcx     # 0x55330
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 0x1c6bb
movq 0x18(%r14), %r15
leaq 0x390e3(%rip), %rdi     # 0x5552a
callq 0x47cd3
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
movq %rax, %rdi
callq 0x4f9ad
movq %r15, %rdi
movq %rax, %rsi
callq 0x47f51
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 0x1c3f1
cmpl $0xc, %ecx
jne 0x1c737
movq %rax, %rdi
callq 0x4f838
movq %rax, %r15
movq 0x10(%rax), %rax
cmpl $0x1, 0x64(%rax)
jg 0x1c76f
cmpl $0x0, 0x68(%rax)
jle 0x1c76f
movabsq $-0x5800000000000, %rsi # imm = 0xFFFA800000000000
movq %r12, %rdi
callq 0x4f9ad
leaq -0x68(%rbp), %rcx
movq %rax, (%rcx)
movq %r15, %rdi
movl $0x40, %esi
movl $0x1, %edx
callq 0x29abc
testq %rax, %rax
je 0x1c778
movq %rax, %r15
movq 0x18(%r14), %rax
movq %rax, 0x28(%r15)
callq 0x2bf94
movl %eax, %ebx
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4f95a
leaq -0x48(%rbp), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x4c28d
movl %eax, %r15d
movl %ebx, %edi
callq 0x2bfbb
testl %r15d, %r15d
je 0x1c7ee
movq -0x48(%rbp), %rsi
leaq 0x39177(%rip), %rdi     # 0x556af
xorl %eax, %eax
callq 0x3fd86
cmpl $0x1, 0x48(%r14)
je 0x1c80f
movl $0x1, 0x48(%r14)
movq %rax, 0x30(%r14)
jmp 0x1c80f
orl %edx, %eax
movl -0x2c(%rbp), %ecx
je 0x1c590
movq -0x40(%rbp), %rax
movq %rax, (%r15)
movl -0x30(%rbp), %eax
movl %eax, 0x8(%r15)
movl $0xffffffff, 0xc(%r15)  # imm = 0xFFFFFFFF
movl %ebx, 0x10(%r15)
movl %ecx, 0x14(%r15)
jmp 0x1c7dc
movl $0xaffff, %r14d         # imm = 0xAFFFF
jmp 0x1c6b7
movq %r15, -0x38(%rbp)
movl $0x1, 0x28(%rdi)
testq %r14, %r14
je 0x1c5b4
movq %r14, %r15
movl 0x6c(%r15), %eax
testb $0x1, %al
jne 0x1c5b9
movq 0x8(%r15), %r15
testq %r15, %r15
jne 0x1c5a3
callq 0x1300c
orl $0x2, %eax
movl %eax, 0x6c(%r15)
leaq 0x48(%r15), %rdi
movl -0x30(%rbp), %esi
callq 0x41110
movq 0x10(%r15), %r12
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %r12, %r12
je 0x1c74a
movl -0x2c(%rbp), %r8d
testb $0x1, 0x6c(%r12)
je 0x1c69e
movq 0x60(%r12), %rdi
testq %rdi, %rdi
je 0x1c5fe
movl -0x4(%rdi), %ecx
jmp 0x1c600
xorl %ecx, %ecx
testl %ecx, %ecx
setg %al
jle 0x1c641
xorl %r13d, %r13d
cmpl %r15d, (%rdi)
je 0x1c644
movl %ecx, %eax
leaq 0x10(%rdi), %rdx
leaq -0x1(%rax), %rcx
xorl %r13d, %r13d
cmpq %r13, %rcx
je 0x1c638
leaq 0x10(%rdx), %rsi
incq %r13
cmpl %r15d, (%rdx)
movq %rsi, %rdx
jne 0x1c61c
cmpq %rax, %r13
setb %al
jmp 0x1c644
incq %r13
cmpq %rax, %r13
setb %al
movl %r15d, %r13d
testb %al, %al
je 0x1c64d
movl %r13d, %r15d
jmp 0x1c69e
testq %rdi, %rdi
je 0x1c658
movl -0x4(%rdi), %r15d
jmp 0x1c65b
xorl %r15d, %r15d
testq %rdi, %rdi
je 0x1c66a
movl -0x4(%rdi), %eax
incl %eax
cmpl -0x8(%rdi), %eax
jl 0x1c682
movl $0x1, %esi
movl $0x10, %edx
callq 0x4c0b4
movl -0x2c(%rbp), %r8d
movq %rax, 0x60(%r12)
movq 0x60(%r12), %rax
movslq -0x4(%rax), %rcx
leal 0x1(%rcx), %edx
movl %edx, -0x4(%rax)
shlq $0x4, %rcx
movl %r13d, (%rax,%rcx)
movq %r14, 0x8(%rax,%rcx)
movq 0x10(%r12), %r12
testq %r12, %r12
jne 0x1c5e3
jmp 0x1c74e
movl $0xeffff, %r14d         # imm = 0xEFFFF
movq -0x58(%rbp), %rbx
movq -0x38(%rbp), %r15
movl -0x50(%rbp), %eax
testl %eax, %eax
je 0x1c708
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 0x4f9ad
leaq 0x38e71(%rip), %rdx     # 0x55565
movq %r13, %rdi
movl %r15d, %esi
movq -0x38(%rbp), %r15
movq %rax, %rcx
xorl %eax, %eax
callq 0x1bcc7
movq %rbx, (%r15)
movq $-0x1, 0x8(%r15)
movl %r14d, 0x10(%r15)
jmp 0x1c7dc
movabsq $-0x5800000000000, %rsi # imm = 0xFFFA800000000000
movq %r12, %rdi
callq 0x4f9ad
leaq 0x38e1e(%rip), %rdi     # 0x55553
jmp 0x1c73e
leaq 0x38dfb(%rip), %rdi     # 0x55539
movq %rax, %rsi
xorl %eax, %eax
callq 0x3fd86
jmp 0x1c784
movl -0x2c(%rbp), %r8d
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, (%rax)
movl -0x30(%rbp), %ecx
movl %ecx, 0x8(%rax)
movl %r15d, 0xc(%rax)
movq %rax, %r15
movl %ebx, 0x10(%rax)
movl %r8d, 0x14(%rax)
jmp 0x1c7dc
leaq 0x38ed9(%rip), %rdi     # 0x5564f
jmp 0x1c77f
leaq 0x38f03(%rip), %rdi     # 0x55682
callq 0x4516c
cmpl $0x1, 0x48(%r14)
je 0x1c797
movl $0x1, 0x48(%r14)
movq %rax, 0x30(%r14)
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4f95a
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
movq -0x38(%rbp), %r15
movl %ecx, 0x10(%r15)
movq $-0x1, 0x8(%r15)
movq %rax, (%r15)
movq %r15, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x48(%rbp), %rsi
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x49c82
movq 0x10(%rbx), %rax
movq %rax, -0x50(%rbp)
movupd (%rbx), %xmm0
movapd %xmm0, -0x60(%rbp)
testl %r15d, %r15d
jne 0x1c797
jmp 0x1c3f1