// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movq %rsi, %r15
movq %rdi, %rbx
movq %rsi, %rax
shrq $0x32, %rax
cmpl $0x3ffe, %eax           # imm = 0x3FFE
jbe 0x13b95
movabsq $0xffffffffffff, %rbp   # imm = 0xFFFFFFFFFFFF
andq %rbp, %r15
movl (%r15), %eax
cmpq $0x8, %rax
ja 0x13d51
movq %rdx, %r12
movabsq $-0x4000000000001, %r13 # imm = 0xFFFBFFFFFFFFFFFF
leaq 0x5d9f(%rip), %rcx      # 0x198cc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0x7ffc000000000000, %r14 # imm = 0x7FFC000000000000
leaq 0x64a1(%rip), %rcx      # 0x19fe8
movq %rsp, %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x122a2
testb %al, %al
je 0x13bd8
movq (%rsp), %r12
movl 0x14(%r15), %edx
leaq 0x6bba(%rip), %rcx      # 0x1a722
movq %rbx, %rdi
movq %r12, %rsi
callq 0x13dde
testb %al, %al
je 0x13bd8
leaq (%r15,%r12), %rsi
addq $0x1c, %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x261f
incq %r13
orq %rax, %r13
movq %r13, %r14
jmp 0x13bd8
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x13d1c
movabsq $0x7ffc000000000000, %r14 # imm = 0x7FFC000000000000
movq %r15, %rdi
callq 0x4e61
leaq 0x637a(%rip), %rsi      # 0x19f3a
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3a8d
movq 0x5f0(%rbx), %rcx
movq %rax, 0x58(%rcx)
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
movq %r12, %rsi
callq 0x42f9
movq %rax, %r14
movabsq $0x7ffd000000000000, %rax # imm = 0x7FFD000000000000
cmpq %rax, %r14
jne 0x13bd8
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4ffa
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5cb0
cmpq %r13, %r12
jbe 0x13c35
andq %rbp, %r12
movl (%r12), %edi
callq 0x4ff0
testb %al, %al
je 0x13ca6
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x13d7f
leaq 0x63b6(%rip), %rsi      # 0x1a004
jmp 0x13cbf
movabsq $0x7ffc000000000000, %r14 # imm = 0x7FFC000000000000
leaq 0x6387(%rip), %rcx      # 0x19fe8
movq %rsp, %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x122a2
testb %al, %al
je 0x13bd8
movq (%rsp), %r12
movl 0x18(%r15), %edx
leaq 0x6cb7(%rip), %rcx      # 0x1a93d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x13dde
testb %al, %al
je 0x13bd8
movq 0x10(%r15), %rax
movq (%rax,%r12,8), %r14
jmp 0x13bd8
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x13dad
leaq 0x6334(%rip), %rsi      # 0x19ff3
movabsq $0x7ffc000000000000, %r14 # imm = 0x7FFC000000000000
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3a8d
movq 0x5f0(%rbx), %rcx
movq %rax, 0x58(%rcx)
movq %rbx, %rdi
callq 0x5dac
jmp 0x13bd8
movq 0xf303(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
leaq 0x43f1(%rip), %rsi      # 0x180f0
leaq 0x4649(%rip), %rdx      # 0x1834f
leaq 0x62cb(%rip), %rcx      # 0x19fd8
leaq 0x5c12(%rip), %r8       # 0x19926
movl $0x73a, %r9d            # imm = 0x73A
jmp 0x13d48
movq 0xf2d5(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
leaq 0x43c3(%rip), %rsi      # 0x180f0
leaq 0x44d0(%rip), %rdx      # 0x18204
leaq 0x629d(%rip), %rcx      # 0x19fd8
leaq 0x5be4(%rip), %r8       # 0x19926
movl $0x701, %r9d            # imm = 0x701
xorl %eax, %eax
callq 0x2250
ud2
movq 0xf2a0(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
leaq 0x4418(%rip), %rsi      # 0x1817a
leaq 0x626f(%rip), %rdx      # 0x19fd8
leaq 0x5bb6(%rip), %rcx      # 0x19926
movl $0x73e, %r8d            # imm = 0x73E
xorl %eax, %eax
callq 0x2250
ud2
movq 0xf272(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
leaq 0x4360(%rip), %rsi      # 0x180f0
leaq 0x446d(%rip), %rdx      # 0x18204
leaq 0x623a(%rip), %rcx      # 0x19fd8
leaq 0x5b81(%rip), %r8       # 0x19926
movl $0x72c, %r9d            # imm = 0x72C
jmp 0x13d48
movq 0xf244(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
leaq 0x4332(%rip), %rsi      # 0x180f0
leaq 0x443f(%rip), %rdx      # 0x18204
leaq 0x620c(%rip), %rcx      # 0x19fd8
leaq 0x5b53(%rip), %r8       # 0x19926
movl $0x72a, %r9d            # imm = 0x72A
jmp 0x13d48