// File: /workspace/github_c_makefile_star2_O3/atomlang[P]atomlang/src/pk_core.c
// Function: varGetSubscript
Var varGetSubscript(PKVM* vm, Var on, Var key) {
  if (!IS_OBJ(on)) {
    VM_SET_ERROR(vm, stringFormat(vm, "$ type is not subscriptable.",
                                  varTypeName(on)));
    return VAR_NULL;
  }

  Object* obj = AS_OBJ(on);
  switch (obj->type) {
    case OBJ_STRING:
    {
      int64_t index;
      String* str = ((String*)obj);
      if (!validateInteger(vm, key, &index, "List index")) {
        return VAR_NULL;
      }
      if (!validateIndex(vm, index, str->length, "String")) {
        return VAR_NULL;
      }
      String* c = newStringLength(vm, str->data + index, 1);
      return VAR_OBJ(c);
    }

    case OBJ_LIST:
    {
      int64_t index;
      pkVarBuffer* elems = &((List*)obj)->elements;
      if (!validateInteger(vm, key, &index, "List index")) {
        return VAR_NULL;
      }
      if (!validateIndex(vm, index, elems->count, "List")) {
        return VAR_NULL;
      }
      return elems->data[index];
    }

    case OBJ_MAP:
    {
      Var value = mapGet((Map*)obj, key);
      if (IS_UNDEF(value)) {

        String* key_str = toString(vm, key);
        vmPushTempRef(vm, &key_str->_super);
        if (IS_OBJ(key) && !isObjectHashable(AS_OBJ(key)->type)) {
          VM_SET_ERROR(vm, stringFormat(vm, "Invalid key '@'.", key_str));
        } else {
          VM_SET_ERROR(vm, stringFormat(vm, "Key '@' not exists", key_str));
        }
        vmPopTempRef(vm);
        return VAR_NULL;
      }
      return value;
    }

    case OBJ_RANGE:
    case OBJ_SCRIPT:
    case OBJ_FUNC:
    case OBJ_FIBER:
    case OBJ_CLASS:
    case OBJ_INST:
      TODO;
      UNREACHABLE();

    default:
      UNREACHABLE();
  }

  UNREACHABLE();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movq %rsi, %rax
shrq $0x32, %rax
cmpl $0x3ffe, %eax           # imm = 0x3FFE
jbe 0x182dc
movabsq $0xffffffffffff, %rbp   # imm = 0xFFFFFFFFFFFF
andq %rbp, %r15
movl (%r15), %eax
cmpq $0x8, %rax
ja 0x185a5
movq %rdx, %r12
movabsq $-0x4000000000001, %r13 # imm = 0xFFFBFFFFFFFFFFFF
leaq 0x566a(%rip), %rcx      # 0x1d8ec
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0x7ffc000000000000, %r14 # imm = 0x7FFC000000000000
movq %r12, %rax
notq %rax
testq %r14, %rax
jne 0x183d8
movq %r12, %rax
shrq %rax
movabsq $0x3ffe800000000001, %rcx # imm = 0x3FFE800000000001
cmpq %rcx, %rax
jne 0x1844a
movabsq $0x7ffd000000000002, %rax # imm = 0x7FFD000000000002
incq %rax
cmpq %rax, %r12
je 0x18581
xorpd %xmm0, %xmm0
jmp 0x183e0
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x185a0
movabsq $0x7ffc000000000000, %r14 # imm = 0x7FFC000000000000
movq %r15, %rdi
callq 0xa89c
leaq 0x5c53(%rip), %rsi      # 0x1df5a
movq %rbx, %rdi
movq %rax, %rdx
jmp 0x184d5
movq %r15, %rdi
movq %r12, %rsi
callq 0x9e0f
movq %rax, %r14
movabsq $0x7ffd000000000002, %rax # imm = 0x7FFD000000000002
addq $-0x2, %rax
cmpq %rax, %r14
jne 0x184e7
movq %rbx, %rdi
movq %r12, %rsi
callq 0xaa0b
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
callq 0xb71b
cmpq %r13, %r12
jbe 0x18369
andq %rbp, %r12
movl (%r12), %edi
callq 0xaa01
testb %al, %al
je 0x1853d
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x185b9
leaq 0x5ca2(%rip), %rsi      # 0x1e024
jmp 0x18552
movabsq $0x7ffc000000000000, %r14 # imm = 0x7FFC000000000000
movq %r12, %rax
notq %rax
testq %r14, %rax
jne 0x1845d
movq %r12, %rax
shrq %rax
movabsq $0x3ffe800000000001, %rcx # imm = 0x3FFE800000000001
cmpq %rcx, %rax
jne 0x184b2
movabsq $0x7ffd000000000002, %rax # imm = 0x7FFD000000000002
incq %rax
cmpq %rax, %r12
je 0x1858e
xorpd %xmm0, %xmm0
jmp 0x18465
movq %r12, %rdi
callq 0x8fa4
movapd %xmm0, (%rsp)
callq 0x2330
movapd (%rsp), %xmm2
ucomisd %xmm2, %xmm0
jne 0x1844a
jp 0x1844a
movapd 0x5471(%rip), %xmm0     # 0x1d870
andpd %xmm2, %xmm0
movsd 0x544d(%rip), %xmm1     # 0x1d858
ucomisd %xmm0, %xmm1
jb 0x185af
cvttsd2si %xmm2, %rax
movl 0x14(%r15), %ecx
cmpq %rax, %rcx
jbe 0x184f9
leaq (%r15,%rax), %rsi
addq $0x1c, %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x7f0c
incq %r13
orq %rax, %r13
movq %r13, %r14
jmp 0x184e7
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x184c4
callq 0x7812
movq %r12, %rdi
callq 0x8fa4
movapd %xmm0, (%rsp)
callq 0x2330
movapd (%rsp), %xmm2
ucomisd %xmm2, %xmm0
jne 0x184b2
jp 0x184b2
movapd 0x53ec(%rip), %xmm0     # 0x1d870
andpd %xmm2, %xmm0
movsd 0x53c8(%rip), %xmm1     # 0x1d858
ucomisd %xmm0, %xmm1
jb 0x185b4
cvttsd2si %xmm2, %rax
movl 0x18(%r15), %ecx
cmpq %rax, %rcx
jbe 0x1851b
movq 0x10(%r15), %rcx
movq (%rcx,%rax,8), %r14
jmp 0x184e7
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x185aa
leaq 0x6f7c(%rip), %rsi      # 0x1f447
leaq 0x5b36(%rip), %rdx      # 0x1e008
movq %rbx, %rdi
xorl %eax, %eax
callq 0x94f4
movq 0x5f0(%rbx), %rcx
movq %rax, 0x58(%rcx)
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x185be
leaq 0x6f5d(%rip), %rsi      # 0x1f46f
leaq 0x6229(%rip), %rdx      # 0x1e742
jmp 0x184d2
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x185c3
leaq 0x6f3b(%rip), %rsi      # 0x1f46f
leaq 0x6422(%rip), %rdx      # 0x1e95d
jmp 0x184d2
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x185c8
leaq 0x5ac1(%rip), %rsi      # 0x1e013
movabsq $0x7ffc000000000000, %r14 # imm = 0x7FFC000000000000
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x94f4
movq 0x5f0(%rbx), %rcx
movq %rax, 0x58(%rcx)
movq %rbx, %rdi
callq 0xb78e
jmp 0x184e7
movsd 0x3b37(%rip), %xmm0     # 0x1c0c0
jmp 0x183e0
movsd 0x3b2a(%rip), %xmm0     # 0x1c0c0
jmp 0x18465
callq 0x76ce
callq 0x7698
callq 0x78b4
callq 0x7770
callq 0x787e
callq 0x77dc
callq 0x773a
callq 0x7848
callq 0x77a6
callq 0x7704