// File: /workspace/github_c_makefile_star2_O0/atomlang[P]atomlang/src/pk_vm.c
// Function: vmCollectGarbage
void vmCollectGarbage(PKVM* vm) {

  // Reset VM's bytes_allocated value and count it again so that we don't
  // required to know the size of each object that'll be freeing.
  vm->bytes_allocated = 0;

  // Mark the core libs and builtin functions.
  markObject(vm, &vm->core_libs->_super);
  for (uint32_t i = 0; i < vm->builtins_count; i++) {
    markObject(vm, &vm->builtins[i].fn->_super);
  }

  // Mark the scripts cache.
  markObject(vm, &vm->scripts->_super);

  // Mark temp references.
  for (int i = 0; i < vm->temp_reference_count; i++) {
    markObject(vm, vm->temp_reference[i]);
  }

  // Mark the handles.
  for (PkHandle* h = vm->handles; h != NULL; h = h->next) {
    markValue(vm, h->value);
  }

  // Garbage collection triggered at the middle of a compilation.
  if (vm->compiler != NULL) {
    compilerMarkObjects(vm, vm->compiler);
  }

  if (vm->fiber != NULL) {
    markObject(vm, &vm->fiber->_super);
  }

  // Pop the marked objects from the working set and push all of it's
  // referenced objects. This will repeat till no more objects left in the
  // working set.
  popMarkedObjects(vm);

  // Now sweep all the un-marked objects in then link list and remove them
  // from the chain.

  // [ptr] is an Object* reference that should be equal to the next
  // non-garbage Object*.
  Object** ptr = &vm->first;
  while (*ptr != NULL) {

    // If the object the pointer points to wasn't marked it's unreachable.
    // Clean it. And update the pointer points to the next object.
    if (!(*ptr)->is_marked) {
      Object* garbage = *ptr;
      *ptr = garbage->next;
      freeObject(vm, garbage);

    } else {
      // Unmark the object for the next garbage collection.
      (*ptr)->is_marked = false;
      ptr = &(*ptr)->next;
    }
  }

  // Next GC heap size will be change depends on the byte we've left with now,
  // and the [heap_fill_percent].
  vm->next_gc = vm->bytes_allocated + (
    (vm->bytes_allocated * vm->heap_fill_percent) / 100);
  if (vm->next_gc < vm->min_heap_size) vm->next_gc = vm->min_heap_size;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x20(%rsp), %rdi
movq 0x20(%rsp), %rax
movq 0x130(%rax), %rsi
callq 0x3d50
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0x5e8(%rcx), %eax
jae 0x117e6
movq 0x20(%rsp), %rdi
movq 0x20(%rsp), %rax
addq $0x138, %rax            # imm = 0x138
movl 0x1c(%rsp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rsi
callq 0x3d50
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x117a4
movq 0x20(%rsp), %rdi
movq 0x20(%rsp), %rax
movq 0x128(%rax), %rsi
callq 0x3d50
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0xb8(%rcx), %eax
jge 0x1183b
movq 0x20(%rsp), %rdi
movq 0x20(%rsp), %rax
movslq 0x18(%rsp), %rcx
movq 0x38(%rax,%rcx,8), %rsi
callq 0x3d50
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x11804
movq 0x20(%rsp), %rax
movq 0xc0(%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x11876
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rsi
callq 0x3e10
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x1184c
movq 0x20(%rsp), %rax
cmpq $0x0, 0x120(%rax)
je 0x1189b
movq 0x20(%rsp), %rdi
movq 0x20(%rsp), %rax
movq 0x120(%rax), %rsi
callq 0x164f0
movq 0x20(%rsp), %rax
cmpq $0x0, 0x5f0(%rax)
je 0x118c0
movq 0x20(%rsp), %rdi
movq 0x20(%rsp), %rax
movq 0x5f0(%rax), %rsi
callq 0x3d50
movq 0x20(%rsp), %rdi
callq 0x3fe0
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
cmpq $0x0, (%rax)
je 0x11938
movq 0x8(%rsp), %rax
movq (%rax), %rax
testb $0x1, 0x4(%rax)
jne 0x11919
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
movq (%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x8(%rsp), %rax
movq %rcx, (%rax)
movq 0x20(%rsp), %rdi
movq (%rsp), %rsi
callq 0x6640
jmp 0x11936
movq 0x8(%rsp), %rax
movq (%rax), %rax
movb $0x0, 0x4(%rax)
movq 0x8(%rsp), %rax
movq (%rax), %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
jmp 0x118d4
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rsp), %rdx
movslq 0x20(%rdx), %rdx
imulq %rdx, %rax
movl $0x64, %esi
xorl %edx, %edx
divq %rsi
addq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x20(%rsp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x11993
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x10(%rax)
addq $0x28, %rsp
retq
nopl (%rax,%rax)