// File: /workspace/github_c_makefile_star2_O2/lh3[P]minimap2/map.c
// Function: collect_seed_hits_heap
static mm128_t *collect_seed_hits_heap(void *km, const mm_mapopt_t *opt, int max_occ, const mm_idx_t *mi, const char *qname, const mm128_v *mv, int qlen, int64_t *n_a, int *rep_len,
								  int *n_mini_pos, uint64_t **mini_pos)
{
	int i, n_m, heap_size = 0;
	int64_t j, n_for = 0, n_rev = 0;
	mm_seed_t *m;
	mm128_t *a, *heap;

	m = mm_collect_matches(km, &n_m, qlen, max_occ, opt->max_max_occ, opt->occ_dist, mi, mv, n_a, rep_len, n_mini_pos, mini_pos);

	heap = (mm128_t*)kmalloc(km, n_m * sizeof(mm128_t));
	a = (mm128_t*)kmalloc(km, *n_a * sizeof(mm128_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) {
		mm_seed_t *q = &m[heap->y>>32];
		mm128_t *p;
		uint64_t r = heap->x;
		int32_t is_self, rpos = (uint32_t)r >> 1;
		if (!skip_seed(opt->flag, r, q, qname, qlen, mi, &is_self)) {
			if ((r&1) == (q->q_pos&1)) { // forward strand
				p = &a[n_for++];
				p->x = (r&0xffffffff00000000ULL) | rpos;
				p->y = (uint64_t)q->q_span << 32 | q->q_pos >> 1;
			} else { // reverse strand
				p = &a[(*n_a) - (++n_rev)];
				p->x = 1ULL<<63 | (r&0xffffffff00000000ULL) | rpos;
				p->y = (uint64_t)q->q_span << 32 | (qlen - ((q->q_pos>>1) + 1 - q->q_span) - 1);
			}
			p->y |= (uint64_t)q->seg_id << MM_SEED_SEG_SHIFT;
			if (q->is_tandem) p->y |= MM_SEED_TANDEM;
			if (is_self) p->y |= MM_SEED_SELF;
		}
		// 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
	for (j = 0; j < n_rev>>1; ++j) {
		mm128_t t = a[(*n_a) - 1 - j];
		a[(*n_a) - 1 - j] = a[(*n_a) - (n_rev - j)];
		a[(*n_a) - (n_rev - j)] = t;
	}
	if (*n_a > n_for + n_rev) {
		memmove(a + n_for, a + (*n_a) - n_rev, n_rev * sizeof(mm128_t));
		*n_a = n_for + n_rev;
	}
	return a;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, 0x78(%rsp)
movq %rcx, %r10
movl %edx, %ecx
movq %rdi, %r14
movq 0xc8(%rsp), %rbx
movl 0xc0(%rsp), %edx
movaps 0xd0(%rsp), %xmm0
movq 0xe0(%rsp), %rdi
movl 0xdc(%rsi), %r8d
movq %rsi, 0x68(%rsp)
movl 0xe0(%rsi), %eax
movq %rdi, 0x28(%rsp)
movups %xmm0, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
movq %r9, 0x8(%rsp)
movq %r10, 0x70(%rsp)
movq %r10, (%rsp)
leaq 0x84(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl %eax, %r9d
callq 0x1aef7
movq %rax, %r12
movslq (%r15), %rsi
shlq $0x4, %rsi
movq %r14, %rdi
callq 0x10afc
movq %rax, %rbp
movq %rax, 0x48(%rsp)
movq (%rbx), %rsi
shlq $0x4, %rsi
movq %r14, 0x60(%rsp)
movq %r14, %rdi
callq 0x10afc
movq %rax, %rbx
movl (%r15), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
shlq $0x20, %rax
movq %r12, 0x38(%rsp)
leaq 0x10(%r12), %rdx
movabsq $0x100000000, %rsi      # imm = 0x100000000
xorl %r14d, %r14d
movslq %r14d, %rdi
cmpq %rcx, %rax
je 0xadb9
cmpl $0x0, -0x10(%rdx)
je 0xadb0
movq (%rdx), %r8
movq (%r8), %r8
movq %rdi, %r9
shlq $0x4, %r9
movq %r8, (%rbp,%r9)
movq %rcx, 0x8(%rbp,%r9)
incl %edi
movl %edi, %r14d
addq %rsi, %rcx
addq $0x18, %rdx
jmp 0xad86
movq %rbp, %rsi
callq 0x9b00
addq $-0x10, 0x48(%rsp)
movl 0xc0(%rsp), %eax
decl %eax
movl %eax, 0x44(%rsp)
movq $0x0, 0x30(%rsp)
xorl %r15d, %r15d
testl %r14d, %r14d
jle 0xaf8c
movl 0xc(%rbp), %eax
imulq $0x18, %rax, %r12
addq 0x38(%rsp), %r12
movq (%rbp), %r13
movq 0x68(%rsp), %rax
movl (%rax), %edi
leaq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rsi
movq %r12, %rdx
movq 0x78(%rsp), %rcx
movl 0xc0(%rsp), %r8d
movq 0x70(%rsp), %r9
callq 0xc60a
testl %eax, %eax
jne 0xaf2d
movl %r13d, %ecx
shrl %ecx
movl %r13d, %esi
andl $0x1, %esi
movl 0x4(%r12), %eax
movl %eax, %edi
andl $0x1, %edi
movl %ecx, %edx
cmpl %edi, %esi
jne 0xae7b
leaq 0x1(%r15), %rcx
movq %r15, %rsi
shlq $0x4, %rsi
movabsq $-0x100000000, %rdi     # imm = 0xFFFFFFFF00000000
andq %rdi, %r13
orq %r13, %rdx
movq %rdx, (%rbx,%rsi)
movq 0x8(%r12), %rdx
shrl %eax
movabsq $0x7fffffff00000000, %rdi # imm = 0x7FFFFFFF00000000
jmp 0xaed0
movq 0xc8(%rsp), %rcx
movq (%rcx), %rsi
movq 0x30(%rsp), %rdi
incq %rdi
movq %rdi, 0x30(%rsp)
subq %rdi, %rsi
movq %r15, %rcx
movq %rsi, %r15
shlq $0x4, %rsi
movabsq $0x7fffffff00000000, %rdi # imm = 0x7FFFFFFF00000000
andq %rdi, %r13
orq %r13, %rdx
btsq $0x3f, %rdx
movq %rdx, (%rbx,%rsi)
movq 0x8(%r12), %rdx
movl %edx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addl 0x44(%rsp), %esi
shrl %eax
notl %eax
addl %esi, %eax
shlq $0x20, %rdx
andq %rdi, %rdx
movl %eax, %esi
orq %rdx, %rsi
shlq $0x4, %r15
leaq (%rbx,%r15), %rax
addq $0x8, %rax
movq %rsi, 0x8(%rbx,%r15)
movq 0x8(%r12), %rdx
shlq $0x10, %rdx
movabsq $-0x1000000000000, %rdi # imm = 0xFFFF000000000000
andq %rdi, %rdx
orq %rsi, %rdx
movq %rdx, 0x8(%rbx,%r15)
cmpq $0x0, 0x8(%r12)
jns 0xaf1b
btsq $0x2a, %rdx
movq %rdx, (%rax)
cmpl $0x0, 0x50(%rsp)
je 0xaf2a
btsq $0x2b, %rdx
movq %rdx, (%rax)
movq %rcx, %r15
movq 0x8(%rbp), %rax
movl (%r12), %ecx
decl %ecx
cmpl %eax, %ecx
jbe 0xaf63
incq %rax
movq %rax, 0x8(%rbp)
movq %rax, %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
movq 0x38(%rsp), %rdx
movq 0x10(%rdx,%rcx), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %rax
movq %rax, (%rbp)
jmp 0xaf7a
movl %r14d, %eax
shlq $0x4, %rax
movq 0x48(%rsp), %rcx
movups (%rcx,%rax), %xmm0
movups %xmm0, (%rbp)
decl %r14d
movl %r14d, %esi
xorl %edi, %edi
movq %rbp, %rdx
callq 0x9a8b
jmp 0xade0
movq 0x60(%rsp), %r14
movq %r14, %rdi
movq 0x38(%rsp), %rsi
callq 0x10a07
movq %r14, %rdi
movq %rbp, %rsi
callq 0x10a07
movq 0x30(%rsp), %rdx
movq %rdx, %rcx
sarq %rcx
xorl %eax, %eax
testq %rcx, %rcx
cmovgq %rcx, %rax
leaq -0x10(%rbx), %rcx
movq %rdx, %r8
negq %r8
movq 0xc8(%rsp), %r12
subq $0x1, %rax
jb 0xb015
movq (%r12), %rsi
movq %rsi, %rdi
shlq $0x4, %rdi
movups (%rcx,%rdi), %xmm0
movaps %xmm0, 0x50(%rsp)
addq %r8, %rsi
shlq $0x4, %rsi
movups (%rbx,%rsi), %xmm0
movups %xmm0, (%rcx,%rdi)
movq (%r12), %rsi
addq %r8, %rsi
shlq $0x4, %rsi
movaps 0x50(%rsp), %xmm0
movups %xmm0, (%rbx,%rsi)
addq $-0x10, %rcx
incq %r8
jmp 0xafcf
movq (%r12), %rsi
leaq (%r15,%rdx), %r14
cmpq %r14, %rsi
jle 0xb043
shlq $0x4, %r15
addq %rbx, %r15
shlq $0x4, %rsi
addq %rbx, %rsi
shlq $0x4, %rdx
subq %rdx, %rsi
movq %r15, %rdi
callq 0x2340
movq %r14, (%r12)
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq