// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movabsq $-0x4000000000000, %rax # imm = 0xFFFC000000000000
andq 0x60(%rsp), %rax
movabsq $-0x4000000000000, %rcx # imm = 0xFFFC000000000000
cmpq %rcx, %rax
je 0x231e9
jmp 0x23147
jmp 0x23149
movq 0x68(%rsp), %rax
movq 0x5f0(%rax), %rax
cmpq $0x0, 0x58(%rax)
je 0x23196
movq 0xfe95(%rip), %rax      # 0x32ff8
movq (%rax), %rdi
leaq 0x6fe3(%rip), %rsi      # 0x2a150
leaq 0x70f0(%rip), %rdx      # 0x2a264
leaq 0x8fcd(%rip), %rcx      # 0x2c148
leaq 0x8914(%rip), %r8       # 0x2ba96
movl $0x701, %r9d            # imm = 0x701
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x23198
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x60(%rsp), %rdi
callq 0x7590
movq 0x10(%rsp), %rdi
movq %rax, %rdx
leaq 0x8eef(%rip), %rsi      # 0x2c0aa
movb $0x0, %al
callq 0x4ed0
movq %rax, %rcx
movq 0x68(%rsp), %rax
movq 0x5f0(%rax), %rax
movq %rcx, 0x58(%rax)
movabsq $0x7ffc000000000000, %rax # imm = 0x7FFC000000000000
movq %rax, 0x70(%rsp)
jmp 0x23595
movq 0x60(%rsp), %rax
movabsq $0xffffffffffff, %rcx   # imm = 0xFFFFFFFFFFFF
andq %rcx, %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movl (%rax), %eax
movq %rax, 0x8(%rsp)
subq $0x8, %rax
ja 0x23560
movq 0x8(%rsp), %rax
leaq 0x881a(%rip), %rcx      # 0x2ba3c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x68(%rsp), %rdi
movq 0x58(%rsp), %rsi
leaq 0x48(%rsp), %rdx
leaq 0x8f0d(%rip), %rcx      # 0x2c158
callq 0x20580
testb $0x1, %al
jne 0x23268
movabsq $0x7ffc000000000000, %rax # imm = 0x7FFC000000000000
movq %rax, 0x70(%rsp)
jmp 0x23595
movq 0x68(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rax
movl 0x14(%rax), %edx
leaq 0x9611(%rip), %rcx      # 0x2c892
callq 0x235a0
testb $0x1, %al
jne 0x2329e
movabsq $0x7ffc000000000000, %rax # imm = 0x7FFC000000000000
movq %rax, 0x70(%rsp)
jmp 0x23595
movq 0x68(%rsp), %rdi
movq 0x40(%rsp), %rsi
addq $0x1c, %rsi
addq 0x48(%rsp), %rsi
movl $0x1, %edx
callq 0x2760
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movabsq $-0x4000000000000, %rcx # imm = 0xFFFC000000000000
orq %rcx, %rax
movq %rax, 0x70(%rsp)
jmp 0x23595
movq 0x50(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x28(%rsp)
movq 0x68(%rsp), %rdi
movq 0x58(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x8e58(%rip), %rcx      # 0x2c158
callq 0x20580
testb $0x1, %al
jne 0x2331d
movabsq $0x7ffc000000000000, %rax # imm = 0x7FFC000000000000
movq %rax, 0x70(%rsp)
jmp 0x23595
movq 0x68(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rax
movl 0x8(%rax), %edx
leaq 0x9777(%rip), %rcx      # 0x2caad
callq 0x235a0
testb $0x1, %al
jne 0x23353
movabsq $0x7ffc000000000000, %rax # imm = 0x7FFC000000000000
movq %rax, 0x70(%rsp)
jmp 0x23595
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x70(%rsp)
jmp 0x23595
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x5f40
movq %rax, 0x20(%rsp)
movabsq $0x7ffd000000000000, %rax # imm = 0x7FFD000000000000
cmpq %rax, 0x20(%rsp)
jne 0x23518
movq 0x68(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x7980
movq %rax, 0x18(%rsp)
movq 0x68(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x9420
movabsq $-0x4000000000000, %rax # imm = 0xFFFC000000000000
andq 0x58(%rsp), %rax
movabsq $-0x4000000000000, %rcx # imm = 0xFFFC000000000000
cmpq %rcx, %rax
jne 0x2347d
movabsq $0xffffffffffff, %rax   # imm = 0xFFFFFFFFFFFF
andq 0x58(%rsp), %rax
movl (%rax), %edi
callq 0x7950
testb $0x1, %al
jne 0x2347d
jmp 0x233fc
jmp 0x233fe
movq 0x68(%rsp), %rax
movq 0x5f0(%rax), %rax
cmpq $0x0, 0x58(%rax)
je 0x2344b
movq 0xfbe0(%rip), %rax      # 0x32ff8
movq (%rax), %rdi
leaq 0x6d2e(%rip), %rsi      # 0x2a150
leaq 0x6e3b(%rip), %rdx      # 0x2a264
leaq 0x8d18(%rip), %rcx      # 0x2c148
leaq 0x865f(%rip), %r8       # 0x2ba96
movl $0x72a, %r9d            # imm = 0x72A
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x2344d
movq 0x68(%rsp), %rdi
movq 0x18(%rsp), %rdx
leaq 0x8d05(%rip), %rsi      # 0x2c163
movb $0x0, %al
callq 0x4ed0
movq %rax, %rcx
movq 0x68(%rsp), %rax
movq 0x5f0(%rax), %rax
movq %rcx, 0x58(%rax)
jmp 0x234fd
jmp 0x2347f
jmp 0x23481
movq 0x68(%rsp), %rax
movq 0x5f0(%rax), %rax
cmpq $0x0, 0x58(%rax)
je 0x234ce
movq 0xfb5d(%rip), %rax      # 0x32ff8
movq (%rax), %rdi
leaq 0x6cab(%rip), %rsi      # 0x2a150
leaq 0x6db8(%rip), %rdx      # 0x2a264
leaq 0x8c95(%rip), %rcx      # 0x2c148
leaq 0x85dc(%rip), %r8       # 0x2ba96
movl $0x72c, %r9d            # imm = 0x72C
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x234d0
movq 0x68(%rsp), %rdi
movq 0x18(%rsp), %rdx
leaq 0x8c93(%rip), %rsi      # 0x2c174
movb $0x0, %al
callq 0x4ed0
movq %rax, %rcx
movq 0x68(%rsp), %rax
movq 0x5f0(%rax), %rax
movq %rcx, 0x58(%rax)
jmp 0x234fd
movq 0x68(%rsp), %rdi
callq 0x95c0
movabsq $0x7ffc000000000000, %rax # imm = 0x7FFC000000000000
movq %rax, 0x70(%rsp)
jmp 0x23595
movq 0x20(%rsp), %rax
movq %rax, 0x70(%rsp)
jmp 0x23595
jmp 0x23526
movq 0xfacb(%rip), %rax      # 0x32ff8
movq (%rax), %rdi
leaq 0x6c19(%rip), %rsi      # 0x2a150
leaq 0x6e71(%rip), %rdx      # 0x2a3af
leaq 0x8c03(%rip), %rcx      # 0x2c148
leaq 0x854a(%rip), %r8       # 0x2ba96
movl $0x73a, %r9d            # imm = 0x73A
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x23562
movq 0xfa8f(%rip), %rax      # 0x32ff8
movq (%rax), %rdi
leaq 0x6c67(%rip), %rsi      # 0x2a1da
leaq 0x8bce(%rip), %rdx      # 0x2c148
leaq 0x8515(%rip), %rcx      # 0x2ba96
movl $0x73e, %r8d            # imm = 0x73E
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
movq 0x70(%rsp), %rax
addq $0x78, %rsp
retq
nop