// File: /workspace/github_c_makefile_star2_O0/lh3[P]minigraph/map-algo.c
// Function: collect_seed_hits_heap
static mg128_t *collect_seed_hits_heap(void *km, const mg_mapopt_t *opt, int max_occ, const mg_idx_t *gi, const char *qname, const mg128_v *mv, int qlen, int64_t *n_a, int *rep_len,
								  int *n_mini_pos, int32_t **mini_pos)
{
	int i, n_m, heap_size = 0;
	int64_t n_for = 0, n_rev = 0;
	mg_match_t *m;
	mg128_t *a, *heap;

	m = collect_matches(km, &n_m, max_occ, gi, mv, n_a, rep_len, n_mini_pos, mini_pos);

	heap = (mg128_t*)kmalloc(km, n_m * sizeof(mg128_t));
	a = (mg128_t*)kmalloc(km, *n_a * sizeof(mg128_t));

	for (i = 0, heap_size = 0; i < n_m; ++i) {
		if (m[i].n > 0) {
			heap[heap_size].x = m[i].cr[0];
			heap[heap_size].y = (uint64_t)i<<32;
			++heap_size;
		}
	}
	ks_heapmake_heap(heap_size, heap);
	while (heap_size > 0) {
		mg_match_t *q = &m[heap->y>>32];
		mg128_t *p;
		uint64_t r = heap->x;
		int32_t rpos = (uint32_t)r >> 1;
		// TODO: skip anchor if MG_F_NO_DIAL
		if ((r&1) == (q->q_pos&1)) { // forward strand
			p = &a[n_for++];
			p->x = r>>32<<33 | rpos;
		} else { // reverse strand; TODO: more testing needed for this block
			p = &a[(*n_a) - (++n_rev)];
			p->x = r>>32<<33 | 1ULL<<32 | (gi->g->seg[r>>32].len - (rpos + 1 - q->q_span) - 1);
		}
		p->y = (uint64_t)q->q_span << 32 | q->q_pos >> 1;
		p->y |= (uint64_t)q->seg_id << MG_SEED_SEG_SHIFT;
		if (q->is_tandem) p->y |= MG_SEED_TANDEM;
		p->y |= (uint64_t)(q->n < 255? q->n : 255) << MG_SEED_OCC_SHIFT;
		// update the heap
		if ((uint32_t)heap->y < q->n - 1) {
			++heap[0].y;
			heap[0].x = m[heap[0].y>>32].cr[(uint32_t)heap[0].y];
		} else {
			heap[0] = heap[heap_size - 1];
			--heap_size;
		}
		ks_heapdown_heap(0, heap_size, heap);
	}
	kfree(km, m);
	kfree(km, heap);

	// reverse anchors on the reverse strand, as they are in the descending order
	if (*n_a > n_for + n_rev) {
		memmove(a + n_for, a + (*n_a) - n_rev, n_rev * sizeof(mg128_t));
		*n_a = n_for + n_rev;
	}
	return a;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl $0x0, -0x3c(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %edx
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %r8
movq 0x18(%rbp), %r9
movq 0x20(%rbp), %r11
movq 0x28(%rbp), %r10
movq 0x30(%rbp), %rax
leaq -0x38(%rbp), %rsi
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x2f290
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x38(%rbp), %rsi
shlq $0x4, %rsi
callq 0x2890
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq 0x18(%rbp), %rax
movq (%rax), %rsi
shlq $0x4, %rsi
callq 0x2890
movq %rax, -0x60(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x2df76
movq -0x58(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
jbe 0x2df69
movq -0x58(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq (%rax), %rcx
movq -0x68(%rbp), %rax
movslq -0x3c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movslq -0x34(%rbp), %rcx
shlq $0x20, %rcx
movq -0x68(%rbp), %rax
movslq -0x3c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2df6b
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x2df01
movslq -0x3c(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x2c6e0
cmpl $0x0, -0x3c(%rbp)
jle 0x2e1d5
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
shrl %eax
movl %eax, -0x84(%rbp)
movq -0x80(%rbp), %rax
andq $0x1, %rax
movq -0x70(%rbp), %rcx
movl 0x4(%rcx), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jne 0x2e015
movq -0x60(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x48(%rbp)
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x80(%rbp), %rcx
shrq $0x20, %rcx
shlq $0x21, %rcx
movslq -0x84(%rbp), %rax
orq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, (%rax)
jmp 0x2e090
movq -0x60(%rbp), %rax
movq 0x18(%rbp), %rcx
movq (%rcx), %rcx
movq -0x50(%rbp), %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
subq %rdx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x80(%rbp), %rcx
shrq $0x20, %rcx
shlq $0x21, %rcx
movabsq $0x100000000, %rax      # imm = 0x100000000
orq %rax, %rcx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x80(%rbp), %rdx
shrq $0x20, %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movl (%rax), %eax
movl -0x84(%rbp), %edx
addl $0x1, %edx
movq -0x70(%rbp), %rsi
subl 0x8(%rsi), %edx
subl %edx, %eax
subl $0x1, %eax
movl %eax, %eax
orq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, (%rax)
movq -0x70(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %ecx
shlq $0x20, %rcx
movq -0x70(%rbp), %rax
movl 0x4(%rax), %eax
shrl %eax
movl %eax, %eax
orq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %ecx
shlq $0x30, %rcx
movq -0x78(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2e0f8
movq -0x78(%rbp), %rax
movabsq $0x40000000000, %rcx    # imm = 0x40000000000
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rax
cmpl $0xff, (%rax)
jae 0x2e112
movq -0x70(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x88(%rbp)
jmp 0x2e11f
movl $0xff, %eax
movl %eax, -0x88(%rbp)
jmp 0x2e11f
movl -0x88(%rbp), %eax
movl %eax, %eax
movl %eax, %ecx
shlq $0x38, %rcx
movq -0x78(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x70(%rbp), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0x2e190
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rcx
movq -0x68(%rbp), %rax
movq %rcx, (%rax)
jmp 0x2e1bf
movq -0x68(%rbp), %rax
movq -0x68(%rbp), %rcx
movl -0x3c(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movl -0x3c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x3c(%rbp)
movslq -0x3c(%rbp), %rsi
movq -0x68(%rbp), %rdx
xorl %eax, %eax
movl %eax, %edi
callq 0x2c510
jmp 0x2df83
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x2670
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x2670
movq 0x18(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
addq -0x50(%rbp), %rcx
cmpq %rcx, %rax
jle 0x2e24d
movq -0x60(%rbp), %rdi
movq -0x48(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x60(%rbp), %rsi
movq 0x18(%rbp), %rax
movq (%rax), %rax
shlq $0x4, %rax
addq %rax, %rsi
xorl %eax, %eax
subq -0x50(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq -0x50(%rbp), %rdx
shlq $0x4, %rdx
callq 0x22d0
movq -0x48(%rbp), %rcx
addq -0x50(%rbp), %rcx
movq 0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x60(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw (%rax,%rax)