// File: /workspace/github_c_makefile_star2_O1/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 0x391e
leaq 0x42050(%rbx), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x391e
leaq 0x42058(%rbx), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x391e
leaq 0x42060(%rbx), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x391e
cmpl $0x0, 0x2040(%rbx)
jle 0x36be
xorl %r13d, %r13d
movq %rsp, %r12
movq 0x40(%rbx,%r13,8), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x391e
incq %r13
movslq 0x2040(%rbx), %rax
cmpq %rax, %r13
jl 0x369f
movq %rsp, %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x391e
leaq 0x42090(%rbx), %r13
movq 0x42070(%rbx), %rax
subq %r13, %rax
testq %rax, %rax
jle 0x3712
xorl %ebp, %ebp
movq %rsp, %r15
movq %r13, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x391e
incq %rbp
movq 0x42070(%rbx), %rax
subq %r13, %rax
sarq $0x3, %rax
addq $0x8, %r12
cmpq %rbp, %rax
jg 0x36ea
cmpl $0x2, 0x42078(%rbx)
jl 0x374c
leaq 0x4a0a0(%rbx), %r15
movl $0x1, %r13d
movq %rsp, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x391e
incq %r13
movslq 0x42078(%rbx), %rax
addq $0x10, %r15
cmpq %rax, %r13
jl 0x372b
movq (%rsp), %rax
cmpq %rax, %r14
jae 0x38e9
leaq 0x3c28(%rip), %r13      # 0x7388
movq %rsp, %r15
movl %r14d, %eax
andl $0x7, %eax
cmpq $0x5, %rax
ja 0x37a8
leaq 0x3bfa(%rip), %rcx      # 0x7370
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl (%r14), %eax
jmp 0x37ad
movl $0x1, %eax
jmp 0x37ad
movl $0x3, %eax
jmp 0x37ad
movl %r14d, %eax
sarl $0x3, %eax
jmp 0x37ad
movl $0x2, %eax
jmp 0x37ad
movl $0x4, %eax
jmp 0x37ad
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addl $-0x6, %eax
cmpl $0xe, %eax
ja 0x386d
movl $0x10, %esi
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movl $0x18, %r12d
jmp 0x384e
leaq 0x10(%r14), %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x391e
cmpl $0x0, 0x18(%r14)
jle 0x386d
leaq 0x20(%r14), %r12
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x391e
incq %rbp
movslq 0x18(%r14), %rax
addq $0x8, %r12
cmpq %rax, %rbp
jl 0x37f0
jmp 0x386d
cmpl $0x0, 0x8014(%r14)
jle 0x386d
leaq 0x10(%r14), %r12
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x391e
incq %rbp
movslq 0x8014(%r14), %rax
addq $0x8, %r12
cmpq %rax, %rbp
jl 0x3820
jmp 0x386d
movl $0x10, %r12d
movl $0x18, %esi
addq %r14, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x391e
movq %r12, %rsi
addq %r14, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x391e
movq %r14, %rdi
callq 0x3577
addq %rax, %r14
addq $0xf, %r14
andq $-0x10, %r14
movq (%rsp), %rax
cmpq %r14, %rax
ja 0x3763
jmp 0x38e9
cmpl $0x0, 0x10(%r14)
jle 0x386d
leaq 0x18(%r14), %r12
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x391e
incq %rbp
movslq 0x10(%r14), %rax
addq $0x8, %r12
cmpq %rax, %rbp
jl 0x389c
jmp 0x386d
cmpl $0x0, 0x10(%r14)
jle 0x386d
leaq 0x18(%r14), %r12
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x391e
incq %rbp
movslq 0x10(%r14), %rax
addq $0x10, %r12
cmpq %rax, %rbp
jl 0x38c9
jmp 0x386d
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