// File: /workspace/github_c_makefile_star2_O3/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 0x30(%rdi), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3809
leaq 0x42050(%rbx), %rsi
movq 0x30(%rbx), %rdi
movq %r12, %rdx
callq 0x3809
leaq 0x42058(%rbx), %rsi
movq 0x30(%rbx), %rdi
movq %r12, %rdx
callq 0x3809
leaq 0x42060(%rbx), %rsi
movq 0x30(%rbx), %rdi
movq %r12, %rdx
callq 0x3809
cmpl $0x0, 0x2040(%rbx)
jle 0x35bd
xorl %r13d, %r13d
movq %rsp, %r12
movq 0x40(%rbx,%r13,8), %rsi
movq 0x30(%rbx), %rdi
movq %r12, %rdx
callq 0x3809
incq %r13
movslq 0x2040(%rbx), %rax
cmpq %rax, %r13
jl 0x359d
movq 0x30(%rbx), %rdi
movq %rsp, %rdx
movq %r15, %rsi
callq 0x3809
leaq 0x42090(%rbx), %r13
movq 0x42070(%rbx), %rax
subq %r13, %rax
testq %rax, %rax
jle 0x3613
xorl %ebp, %ebp
movq %rsp, %r15
movq %r13, %r12
movq 0x30(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3809
incq %rbp
movq 0x42070(%rbx), %rax
subq %r13, %rax
sarq $0x3, %rax
addq $0x8, %r12
cmpq %rbp, %rax
jg 0x35ea
cmpl $0x2, 0x42078(%rbx)
jl 0x364e
leaq 0x4a0a0(%rbx), %r15
movl $0x1, %r13d
movq %rsp, %r12
movq 0x30(%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3809
incq %r13
movslq 0x42078(%rbx), %rax
addq $0x10, %r15
cmpq %rax, %r13
jl 0x362c
movq (%rsp), %rax
cmpq %rax, %r14
jae 0x37d4
leaq 0x2c96(%rip), %r13      # 0x62f8
movq %rsp, %r15
movl %r14d, %eax
andl $0x7, %eax
cmpq $0x3, %rax
je 0x367f
testq %rax, %rax
jne 0x370b
movl (%r14), %eax
jmp 0x3685
movl %r14d, %eax
sarl $0x3, %eax
addl $-0x6, %eax
cmpl $0xe, %eax
ja 0x370b
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
leaq 0x10(%r14), %rsi
movq 0x30(%rbx), %rdi
movq %r15, %rdx
callq 0x3809
leaq 0x18(%r14), %rsi
jmp 0x36f5
cmpl $0x0, 0x8014(%r14)
jle 0x370b
leaq 0x10(%r14), %r12
xorl %ebp, %ebp
movq 0x30(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3809
incq %rbp
movslq 0x8014(%r14), %rax
addq $0x8, %r12
cmpq %rax, %rbp
jl 0x36bd
jmp 0x370b
leaq 0x18(%r14), %rsi
movq 0x30(%rbx), %rdi
movq %r15, %rdx
callq 0x3809
leaq 0x10(%r14), %rsi
movq 0x30(%rbx), %rdi
jmp 0x3703
movq 0x30(%rbx), %rdi
leaq 0x10(%r14), %rsi
movq %r15, %rdx
callq 0x3809
movq %r14, %rdi
callq 0x3491
addq %rax, %r14
addq $0xf, %r14
andq $-0x10, %r14
movq (%rsp), %rax
cmpq %r14, %rax
ja 0x3665
jmp 0x37d4
cmpl $0x0, 0x10(%r14)
jle 0x370b
leaq 0x18(%r14), %r12
xorl %ebp, %ebp
movq 0x30(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3809
incq %rbp
movslq 0x10(%r14), %rax
addq $0x8, %r12
cmpq %rax, %rbp
jl 0x373d
jmp 0x370b
cmpl $0x0, 0x10(%r14)
jle 0x370b
leaq 0x18(%r14), %r12
xorl %ebp, %ebp
movq 0x30(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3809
incq %rbp
movslq 0x10(%r14), %rax
addq $0x10, %r12
cmpq %rax, %rbp
jl 0x376b
jmp 0x370b
leaq 0x10(%r14), %rsi
movq 0x30(%rbx), %rdi
movq %r15, %rdx
callq 0x3809
cmpl $0x0, 0x18(%r14)
jle 0x370b
leaq 0x20(%r14), %r12
xorl %ebp, %ebp
movq 0x30(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3809
incq %rbp
movslq 0x18(%r14), %rax
addq $0x8, %r12
cmpq %rax, %rbp
jl 0x37b0
jmp 0x370b
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