// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movq %rdx, %rbx
cmpb $0x1, 0x18(%rsi)
jne 0xa7cb
movq %rdi, %r15
movq 0x100(%rdi), %r8
testq %r8, %r8
je 0xa838
movq 0x5f0(%r15), %rax
movq 0x38(%rax), %r14
leaq 0x60(%rsp), %rdx
movq %rcx, (%rdx)
movq %rdx, 0x38(%rax)
leaq 0x1c(%rbx), %rax
movq %rax, 0x40(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x48(%rsp)
movl 0x14(%rbx), %eax
movl %eax, 0x58(%rsp)
movl 0x10(%rbx), %eax
movl %eax, 0x5c(%rsp)
movq 0x20(%rsi), %rax
movl 0x1c(%rsi), %edx
movups 0x40(%rsp), %xmm0
movups 0x50(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r15, %rdi
movq %rax, %rsi
callq *%r8
movl %eax, %ecx
movq 0x5f0(%r15), %rax
movq %r14, 0x38(%rax)
cmpq $0x0, 0x58(%rax)
sete %al
andb %cl, %al
jmp 0xa83a
movq 0x20(%rsi), %rdx
movq (%rdx), %rax
movl 0x30(%rax), %r13d
testq %r13, %r13
je 0xa838
movq %rdx, 0x28(%rsp)
movq %rcx, 0x30(%rsp)
movq 0x10(%rax), %rbp
movq 0x28(%rax), %r12
movl 0x68(%rbp), %r15d
leaq 0x1c(%rbx), %rax
movq %rax, 0x38(%rsp)
xorl %r14d, %r14d
movl (%r12,%r14,4), %eax
cmpl %r15d, %eax
jae 0xa85f
movq 0x60(%rbp), %rcx
movq (%rcx,%rax,8), %rdi
movl 0x10(%rdi), %eax
cmpl 0x10(%rbx), %eax
jne 0xa830
movl 0x14(%rdi), %edx
cmpl 0x14(%rbx), %edx
jne 0xa830
addq $0x1c, %rdi
movq 0x38(%rsp), %rsi
callq 0x22d0
testl %eax, %eax
je 0xa849
incq %r14
cmpq %r14, %r13
jne 0xa7fd
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x30(%rsp), %rcx
movq %rcx, (%rax,%r14,8)
movb $0x1, %al
jmp 0xa83a
callq 0x286e