// File: /workspace/github_c_makefile_star2_O2/pedro-onate[P]eva/eva.c
// Function: es_gc
void es_gc(es_ctx_t* ctx)
{
/* TODO: Ensure scan and next pointers respect alignment */
  //printf("running gc\n");
  //struct timeval t0, t1, dt;
  char* scan, *next, *tmp;

  //gettimeofday(&t0, NULL);

  es_heap_t* heap = &ctx->heap;
  scan = next = heap->to_space;

  es_mark_copy(heap, &ctx->env, &next);
  es_mark_copy(heap, &ctx->iport, &next);
  es_mark_copy(heap, &ctx->oport, &next);
  es_mark_copy(heap, &ctx->bytecode, &next);

  for(int i = 0; i < ctx->roots.top; i++) {
    es_mark_copy(heap, ctx->roots.stack[i], &next);
  }

  es_mark_copy(heap, &ctx->env, &next);
  for(int i = 0; i < ctx->sp - ctx->stack; i++) {
    es_mark_copy(heap, &ctx->stack[i], &next);
  }
  for(int i = 1; i < ctx->fp; i++) {
    es_mark_copy(heap, &ctx->frames[i].args, &next);
  }

  while(scan < next) {
    es_val_t obj = es_obj_to_val(scan);
    switch(es_type_of(obj)) {
    case ES_PAIR_TYPE:      es_pair_mark_copy(heap, obj, &next);     break;
    case ES_CLOSURE_TYPE:   es_closure_mark_copy(heap, obj, &next);  break;
    case ES_VECTOR_TYPE:    es_vector_mark_copy(heap, obj, &next);   break;
    case ES_ENV_TYPE:       es_env_mark_copy(heap, obj, &next);      break;
    case ES_ARGS_TYPE:      es_args_mark_copy(heap, obj, &next);     break;
    case ES_BYTECODE_TYPE:  es_bytecode_mark_copy(heap, obj, &next); break;
    case ES_MACRO_TYPE:     es_macro_mark_copy(heap, obj, &next);    break;
    default:                                                         break;
    }
    scan = alignp(scan + es_size_of(obj), ES_DEFAULT_ALIGNMENT);
  }

  tmp              = heap->from_space;
  heap->from_space = heap->to_space;
  heap->end        = heap->from_space + heap->size;
  heap->to_space   = tmp;
  heap->to_end     = heap->to_space + heap->size;
  heap->next       = next;

  //gettimeofday(&t1, NULL);
  //timeval_subtract(&dt, &t1, &t0);
  //printf("gc time: %f\n", dt.tv_sec * 1000.0 + dt.tv_usec / 1000.0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x20(%rdi), %r14
movq %rsp, %r12
movq %r14, (%r12)
leaq 0x42080(%rdi), %r15
movq %r15, %rsi
movq %r12, %rdx
callq 0x3006
leaq 0x42050(%rbx), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x3006
leaq 0x42058(%rbx), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x3006
leaq 0x42060(%rbx), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x3006
xorl %r13d, %r13d
movslq 0x2040(%rbx), %rax
cmpq %rax, %r13
jge 0x2e29
movq 0x40(%rbx,%r13,8), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x3006
incq %r13
jmp 0x2e08
movq %rsp, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3006
leaq 0x42090(%rbx), %r13
xorl %ebp, %ebp
movq %r13, %r15
movq 0x42070(%rbx), %rax
subq %r13, %rax
sarq $0x3, %rax
cmpq %rbp, %rax
jle 0x2e70
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3006
incq %rbp
addq $0x8, %r15
jmp 0x2e46
leaq 0x4a0a0(%rbx), %r15
pushq $0x1
popq %r13
movq %rsp, %r12
movslq 0x42078(%rbx), %rax
cmpq %rax, %r13
jge 0x2ea1
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3006
incq %r13
addq $0x10, %r15
jmp 0x2e7e
pushq $0x10
popq %rbp
movq %rsp, %r15
movq (%rsp), %rax
cmpq %rax, %r14
jae 0x2fd1
movq %r14, %rdi
callq 0x182d
addl $-0x6, %eax
cmpl $0xe, %eax
ja 0x2f06
leaq 0x324d(%rip), %rdx      # 0x6118
movslq (%rdx,%rax,4), %rcx
addq %rdx, %rcx
movq %rbp, %rsi
pushq $0x18
popq %r12
movq %rbp, %rax
jmpq *%rcx
pushq $0x18
popq %rsi
movq %rbp, %r12
addq %r14, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x3006
movq %r12, %rax
addq %r14, %rax
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x3006
movq %r14, %rdi
callq 0x2d2a
addq %rax, %r14
addq $0xf, %r14
andq $-0x10, %r14
jmp 0x2ea7
leaq 0x10(%r14), %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x3006
leaq 0x20(%r14), %r12
xorl %r13d, %r13d
movslq 0x18(%r14), %rax
cmpq %rax, %r13
jge 0x2f06
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3006
incq %r13
addq $0x8, %r12
jmp 0x2f31
leaq 0x10(%r14), %r12
xorl %r13d, %r13d
movslq 0x8014(%r14), %rax
cmpq %rax, %r13
jge 0x2f06
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3006
incq %r13
addq $0x8, %r12
jmp 0x2f58
leaq 0x18(%r14), %r12
xorl %r13d, %r13d
movslq 0x10(%r14), %rax
cmpq %rax, %r13
jge 0x2f06
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3006
incq %r13
addq $0x8, %r12
jmp 0x2f82
leaq 0x18(%r14), %r12
xorl %r13d, %r13d
movslq 0x10(%r14), %rax
cmpq %rax, %r13
jge 0x2f06
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3006
incq %r13
addq $0x10, %r12
jmp 0x2fad
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rdx
movq 0x20(%rbx), %rsi
movq %rsi, 0x18(%rbx)
addq %rcx, %rsi
movq %rsi, 0x28(%rbx)
movq %rdx, 0x20(%rbx)
addq %rcx, %rdx
movq %rdx, 0x30(%rbx)
movq %rax, 0x10(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq