// File: /workspace/github_c_makefile_star2_O2/atomlang[P]atomlang/src/pk_var.c
// Function: instSetAttrib
bool instSetAttrib(PKVM* vm, Instance* inst, String* attrib, Var value) {

  if (inst->is_native) {

    if (vm->config.inst_set_attrib_fn) {
      // Temproarly change the fiber's "return address" to points to the
      // below var 'ret' so that the users can use 'pkGetArg...()' function
      // to validate and get the attribute.
      Var* temp = vm->fiber->ret;
      Var attrib_ptr = value;

      vm->fiber->ret = &attrib_ptr;
      PkStringPtr attr = { attrib->data, NULL, NULL,
                           attrib->length, attrib->hash };
      bool exists = vm->config.inst_set_attrib_fn(vm, inst->native,
                                                  inst->native_id, attr);
      vm->fiber->ret = temp;

      // If the type is incompatible there'll be an error by now, return false
      // and the user of this function has to check VM_HAS_ERROR() as well.
      if (VM_HAS_ERROR(vm)) return false;

      // If the attribute exists on the native type, the host application would
      // returned true by now, return it.
      return exists;
    }

    // If the host application doesn't provided a setter we treat it as it
    // doesn't has the attribute.
    return false;

  } else {

    // TODO: Optimize this with binary search.
    Class* ty = inst->ins->type;
    for (uint32_t i = 0; i < ty->field_names.count; i++) {
      ASSERT_INDEX(i, ty->field_names.count);
      ASSERT_INDEX(ty->field_names.data[i], ty->owner->names.count);
      String* f_name = ty->owner->names.data[ty->field_names.data[i]];
      if (f_name->hash == attrib->hash &&
        f_name->length == attrib->length &&
        memcmp(f_name->data, attrib->data, attrib->length) == 0) {
        inst->ins->fields.data[i] = value;
        return true;
      }
    }

    // Couldn't find the attribute in it's type class, return false.
    return false;
  }

  UNREACHABLE();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %r14
movq %rdx, %rbx
cmpb $0x1, 0x18(%rsi)
jne 0x4d26
movq %rdi, %r15
movq 0x100(%rdi), %rcx
testq %rcx, %rcx
je 0x4d80
movq 0x5f0(%r15), %rax
movq 0x38(%rax), %r12
leaq 0x50(%rsp), %rdx
movq %r14, (%rdx)
movq %rdx, 0x38(%rax)
leaq 0x1c(%rbx), %rax
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rsp)
movl 0x14(%rbx), %eax
movl %eax, 0x48(%rsp)
movl 0x10(%rbx), %eax
movl %eax, 0x4c(%rsp)
movq 0x20(%rsi), %rax
movl 0x1c(%rsi), %edx
movups 0x30(%rsp), %xmm0
movups 0x40(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r15, %rdi
movq %rax, %rsi
callq *%rcx
movl %eax, %ecx
movq 0x5f0(%r15), %rax
movq %r12, 0x38(%rax)
cmpq $0x0, 0x58(%rax)
sete %al
andb %cl, %al
jmp 0x4d82
movq 0x20(%rsi), %rax
movq %rax, 0x28(%rsp)
movq (%rax), %r13
movl 0x30(%r13), %ebp
leaq 0x1c(%rbx), %r15
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0x4d80
movq 0x10(%r13), %rax
movq 0x28(%r13), %rcx
movl (%rcx,%r12,4), %ecx
cmpl 0x68(%rax), %ecx
jae 0x4da2
movq 0x60(%rax), %rax
movq (%rax,%rcx,8), %rdi
movl 0x10(%rdi), %eax
cmpl 0x10(%rbx), %eax
jne 0x4d7b
movl 0x14(%rdi), %edx
cmpl 0x14(%rbx), %edx
jne 0x4d7b
addq $0x1c, %rdi
movq %r15, %rsi
callq 0x22d0
testl %eax, %eax
je 0x4d91
incq %r12
jmp 0x4d3d
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movq %r14, (%rax,%r12,8)
movb $0x1, %al
jmp 0x4d82
movq 0x1e24f(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x1333d(%rip), %rsi     # 0x180f0
leaq 0x134f3(%rip), %rdx     # 0x182ad
leaq 0x13501(%rip), %rcx     # 0x182c2
leaq 0x13374(%rip), %r8      # 0x1813c
movl $0x4f6, %r9d            # imm = 0x4F6
xorl %eax, %eax
callq 0x2250
ud2