// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x98, %rsp
movq %rdi, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq %rdx, 0x78(%rsp)
movq %rcx, 0x70(%rsp)
movq 0x80(%rsp), %rax
testb $0x1, 0x18(%rax)
je 0x71a2
movq 0x88(%rsp), %rax
cmpq $0x0, 0x100(%rax)
je 0x7195
movq 0x88(%rsp), %rax
movq 0x5f0(%rax), %rax
movq 0x38(%rax), %rax
movq %rax, 0x68(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x88(%rsp), %rax
movq 0x5f0(%rax), %rax
leaq 0x60(%rsp), %rcx
movq %rcx, 0x38(%rax)
movq 0x78(%rsp), %rax
addq $0x1c, %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x50(%rsp)
movq 0x78(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x58(%rsp)
movq 0x78(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x5c(%rsp)
movq 0x88(%rsp), %rax
movq 0x100(%rax), %rax
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rcx
movq 0x20(%rcx), %rsi
movq 0x80(%rsp), %rcx
movl 0x1c(%rcx), %edx
leaq 0x40(%rsp), %rcx
movq (%rcx), %r8
movq %r8, (%rsp)
movq 0x8(%rcx), %r8
movq %r8, 0x8(%rsp)
movq 0x10(%rcx), %r8
movq %r8, 0x10(%rsp)
movq 0x18(%rcx), %rcx
movq %rcx, 0x18(%rsp)
callq *%rax
andb $0x1, %al
movb %al, 0x3f(%rsp)
movq 0x68(%rsp), %rcx
movq 0x88(%rsp), %rax
movq 0x5f0(%rax), %rax
movq %rcx, 0x38(%rax)
movq 0x88(%rsp), %rax
movq 0x5f0(%rax), %rax
cmpq $0x0, 0x58(%rax)
je 0x7183
movb $0x0, 0x97(%rsp)
jmp 0x7343
movb 0x3f(%rsp), %al
andb $0x1, %al
movb %al, 0x97(%rsp)
jmp 0x7343
movb $0x0, 0x97(%rsp)
jmp 0x7343
movq 0x80(%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x30(%rsp), %rcx
cmpl 0x30(%rcx), %eax
jae 0x733b
jmp 0x71d2
cmpl $0x0, 0x2c(%rsp)
jb 0x71e7
movl 0x2c(%rsp), %eax
movq 0x30(%rsp), %rcx
cmpl 0x30(%rcx), %eax
jb 0x7221
movq 0x2be0a(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x22f58(%rip), %rsi     # 0x2a150
leaq 0x2310e(%rip), %rdx     # 0x2a30d
leaq 0x2311c(%rip), %rcx     # 0x2a322
leaq 0x22f8f(%rip), %r8      # 0x2a19c
movl $0x4f5, %r9d            # imm = 0x4F5
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x7223
jmp 0x7225
movq 0x30(%rsp), %rax
movq 0x28(%rax), %rax
movl 0x2c(%rsp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jb 0x7256
movq 0x30(%rsp), %rax
movq 0x28(%rax), %rax
movl 0x2c(%rsp), %ecx
movl (%rax,%rcx,4), %eax
movq 0x30(%rsp), %rcx
movq 0x10(%rcx), %rcx
cmpl 0x68(%rcx), %eax
jb 0x7290
movq 0x2bd9b(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x22ee9(%rip), %rsi     # 0x2a150
leaq 0x2309f(%rip), %rdx     # 0x2a30d
leaq 0x230ad(%rip), %rcx     # 0x2a322
leaq 0x22f20(%rip), %r8      # 0x2a19c
movl $0x4f6, %r9d            # imm = 0x4F6
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x7292
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x60(%rax), %rax
movq 0x30(%rsp), %rcx
movq 0x28(%rcx), %rcx
movl 0x2c(%rsp), %edx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movl 0x10(%rax), %eax
movq 0x78(%rsp), %rcx
cmpl 0x10(%rcx), %eax
jne 0x7329
movq 0x20(%rsp), %rax
movl 0x14(%rax), %eax
movq 0x78(%rsp), %rcx
cmpl 0x14(%rcx), %eax
jne 0x7329
movq 0x20(%rsp), %rdi
addq $0x1c, %rdi
movq 0x78(%rsp), %rsi
addq $0x1c, %rsi
movq 0x78(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, %edx
callq 0x2200
cmpl $0x0, %eax
jne 0x7329
movq 0x70(%rsp), %rdx
movq 0x80(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rax
movl 0x2c(%rsp), %ecx
movq %rdx, (%rax,%rcx,8)
movb $0x1, 0x97(%rsp)
jmp 0x7343
jmp 0x732b
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x71be
movb $0x0, 0x97(%rsp)
movb 0x97(%rsp), %al
andb $0x1, %al
addq $0x98, %rsp
retq
nopw %cs:(%rax,%rax)