// File: /workspace/github_c_makefile_star2_O1/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 0x18e91
movabsq $0xffffffffffff, %rbp   # imm = 0xFFFFFFFFFFFF
andq %rbp, %r15
movl (%r15), %eax
cmpq $0x8, %rax
ja 0x19200
movq %rdx, %r12
movabsq $-0x4000000000001, %r13 # imm = 0xFFFBFFFFFFFFFFFF
leaq 0x5a75(%rip), %rcx      # 0x1e8ac
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0x7ffc000000000000, %r14 # imm = 0x7FFC000000000000
movq %r12, %rax
notq %rax
testq %r14, %rax
jne 0x18fa1
movq %r12, %rax
shrq %rax
movabsq $0x3ffe800000000001, %rcx # imm = 0x3FFE800000000001
cmpq %rcx, %rax
jne 0x18fbe
movabsq $0x7ffd000000000002, %rax # imm = 0x7FFD000000000002
incq %rax
cmpq %rax, %r12
je 0x191dc
xorpd %xmm0, %xmm0
jmp 0x18fa9
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x191fb
movabsq $0x7ffc000000000000, %r14 # imm = 0x7FFC000000000000
movq %r15, %rdi
callq 0xaa1a
leaq 0x605e(%rip), %rsi      # 0x1ef1a
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x95d0
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 %r15, %rdi
movq %r12, %rsi
callq 0x9f27
movq %rax, %r14
movabsq $0x7ffd000000000002, %rax # imm = 0x7FFD000000000002
addq $-0x2, %rax
cmpq %rax, %r14
jne 0x18ed4
movq %rbx, %rdi
movq %r12, %rsi
callq 0xab87
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
callq 0xb8d6
cmpq %r13, %r12
jbe 0x18f39
andq %rbp, %r12
movl (%r12), %edi
callq 0xab7d
testb %al, %al
je 0x19194
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x19219
leaq 0x6092(%rip), %rsi      # 0x1efe4
jmp 0x191ad
movabsq $0x7ffc000000000000, %r14 # imm = 0x7FFC000000000000
movq %r12, %rax
notq %rax
testq %r14, %rax
jne 0x18fae
movq %r12, %rax
shrq %rax
movabsq $0x3ffe800000000001, %rcx # imm = 0x3FFE800000000001
cmpq %rcx, %rax
jne 0x190b4
movabsq $0x7ffd000000000002, %rax # imm = 0x7FFD000000000002
incq %rax
cmpq %rax, %r12
je 0x191e9
xorpd %xmm0, %xmm0
jmp 0x18fb6
movq %r12, %rdi
callq 0x9060
movb $0x1, %r12b
jmp 0x18fc1
movq %r12, %rdi
callq 0x9060
movb $0x1, %r12b
jmp 0x190b7
xorl %r12d, %r12d
movapd %xmm0, (%rsp)
callq 0x2330
movapd (%rsp), %xmm2
ucomisd %xmm2, %xmm0
setnp %al
sete %bpl
andb %al, %bpl
andb %r12b, %bpl
cmpb $0x1, %bpl
jne 0x1900c
movapd 0x5841(%rip), %xmm0     # 0x1e830
andpd %xmm2, %xmm0
movsd 0x581d(%rip), %xmm1     # 0x1e818
ucomisd %xmm0, %xmm1
jb 0x19205
cvttsd2si %xmm2, %r12
jmp 0x19041
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x1920a
leaq 0x73e2(%rip), %rsi      # 0x20407
leaq 0x5f9c(%rip), %rdx      # 0x1efc8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x95d0
movq 0x5f0(%rbx), %rcx
movq %rax, 0x58(%rcx)
testb %bpl, %bpl
je 0x18ed4
movl 0x14(%r15), %ebp
cmpq %rbp, %r12
jb 0x19091
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x1921e
leaq 0x73c3(%rip), %rsi      # 0x2042f
leaq 0x668f(%rip), %rdx      # 0x1f702
movq %rbx, %rdi
xorl %eax, %eax
callq 0x95d0
movq 0x5f0(%rbx), %rcx
movq %rax, 0x58(%rcx)
cmpq %rbp, %r12
jae 0x18ed4
leaq (%r15,%r12), %rsi
addq $0x1c, %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x7fc8
incq %r13
orq %rax, %r13
movq %r13, %r14
jmp 0x18ed4
xorl %r12d, %r12d
movapd %xmm0, (%rsp)
callq 0x2330
movapd (%rsp), %xmm2
ucomisd %xmm2, %xmm0
setnp %al
sete %bpl
andb %al, %bpl
andb %r12b, %bpl
cmpb $0x1, %bpl
jne 0x19102
movapd 0x574b(%rip), %xmm0     # 0x1e830
andpd %xmm2, %xmm0
movsd 0x5727(%rip), %xmm1     # 0x1e818
ucomisd %xmm0, %xmm1
jb 0x1920f
cvttsd2si %xmm2, %r12
jmp 0x19137
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x19214
leaq 0x72ec(%rip), %rsi      # 0x20407
leaq 0x5ea6(%rip), %rdx      # 0x1efc8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x95d0
movq 0x5f0(%rbx), %rcx
movq %rax, 0x58(%rcx)
testb %bpl, %bpl
je 0x18ed4
movl 0x18(%r15), %r13d
cmpq %r13, %r12
jb 0x19187
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x19223
leaq 0x72cd(%rip), %rsi      # 0x2042f
leaq 0x67b4(%rip), %rdx      # 0x1f91d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x95d0
movq 0x5f0(%rbx), %rcx
movq %rax, 0x58(%rcx)
cmpq %r13, %r12
jae 0x18ed4
movq 0x10(%r15), %rax
movq (%rax,%r12,8), %r14
jmp 0x18ed4
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x19228
leaq 0x5e26(%rip), %rsi      # 0x1efd3
movabsq $0x7ffc000000000000, %r14 # imm = 0x7FFC000000000000
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x95d0
movq 0x5f0(%rbx), %rcx
movq %rax, 0x58(%rcx)
movq %rbx, %rdi
callq 0xb949
jmp 0x18ed4
movsd 0x3edc(%rip), %xmm0     # 0x1d0c0
jmp 0x18fa9
movsd 0x3ecf(%rip), %xmm0     # 0x1d0c0
jmp 0x18fb6
callq 0x7704
callq 0x76ce
callq 0x78ea
callq 0x78b4
callq 0x7848
callq 0x7812
callq 0x77a6
callq 0x7770
callq 0x787e
callq 0x77dc
callq 0x773a