// File: /workspace/github_c_makefile_star2_O3/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 $0xa8, %rsp
movq %r8, 0x50(%rsp)
movq %rcx, %r10
movl %edx, %ecx
movq %rdi, %r15
movq 0xe8(%rsp), %rbx
movl 0xe0(%rsp), %edx
movaps 0xf0(%rsp), %xmm0
movq 0x100(%rsp), %rdi
movl 0xdc(%rsi), %r8d
movq %rsi, 0x80(%rsp)
movl 0xe0(%rsi), %eax
movq %rdi, 0x28(%rsp)
movups %xmm0, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
movq %r9, 0x8(%rsp)
movq %r10, 0x78(%rsp)
movq %r10, (%rsp)
leaq 0x8c(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
movl %eax, %r9d
callq 0x1db42
movq %rax, %rbp
movslq (%r12), %rsi
shlq $0x4, %rsi
movq %r15, %rdi
callq 0x1264d
movq %rax, %r14
movq (%rbx), %rsi
shlq $0x4, %rsi
movq %r15, 0x68(%rsp)
movq %r15, %rdi
callq 0x1264d
movq %rax, %r13
movslq (%r12), %rax
xorl %r15d, %r15d
testq %rax, %rax
jle 0xc3fd
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
movabsq $0x100000000, %rcx      # imm = 0x100000000
xorl %edx, %edx
xorl %esi, %esi
cmpl $0x0, (%rbp,%rdx)
je 0xc0cf
movq 0x10(%rbp,%rdx), %rdi
movq (%rdi), %rdi
movslq %r15d, %r15
movq %r15, %r8
shlq $0x4, %r8
movq %rdi, (%r14,%r8)
movq %rsi, 0x8(%r14,%r8)
incl %r15d
addq %rcx, %rsi
addq $0x18, %rdx
cmpq %rdx, %rax
jne 0xc0aa
movq %r13, 0x38(%rsp)
cmpl $0x2, %r15d
jb 0xc106
movslq %r15d, %r12
movq %r12, %r13
shrq %r13
decq %r13
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0xad1f
addq $-0x1, %r13
jb 0xc0f2
testl %r15d, %r15d
jle 0xc411
movq %r14, %rax
addq $-0x10, %rax
movq %rax, 0x70(%rsp)
movl 0xe0(%rsp), %eax
decl %eax
movl %eax, 0x64(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x38(%rsp), %r13
movq 0x50(%rsp), %r12
movq %rbp, 0x48(%rsp)
testq %r12, %r12
sete %al
movl 0xc(%r14), %ecx
leaq (%rcx,%rcx,2), %rcx
leaq (%rbp,%rcx,8), %r10
movq (%r14), %rbp
movq 0x80(%rsp), %rcx
movq (%rcx), %rbx
testb $0x3, %bl
sete %cl
orb %al, %cl
movb $0x1, %r11b
je 0xc1ae
movq 0x30(%rsp), %r8
testl $0x300000, %ebx         # imm = 0x300000
je 0xc23b
movl %ebp, %ecx
andl $0x1, %ecx
movl 0x4(%r10), %eax
movl %eax, %esi
andl $0x1, %esi
cmpq %rsi, %rcx
jne 0xc24b
btl $0x15, %ebx
jae 0xc255
jmp 0xc379
movq %r10, 0x58(%rsp)
movq 0x78(%rsp), %rax
movq 0x20(%rax), %r13
movq %rbp, %rax
shrq $0x20, %rax
movq %r12, %rdi
leaq (%rax,%rax,2), %r12
movq (%r13,%r12,8), %rsi
callq 0x2240
testb $0x1, %bl
je 0xc351
testl %eax, %eax
jne 0xc351
leaq (,%r12,8), %rax
addq %r13, %rax
movl 0xe0(%rsp), %ecx
cmpl %ecx, 0x10(%rax)
movq 0x38(%rsp), %r13
movq 0x50(%rsp), %r12
movq 0x30(%rsp), %r8
movq 0x58(%rsp), %r10
movb $0x1, %r11b
jne 0xc17c
movl 0x4(%r10), %eax
movl %eax, %ecx
xorl %ebp, %ecx
cmpl $0x2, %ecx
jb 0xc379
movl %ebp, %ecx
andl $0x1, %ecx
andl $0x1, %eax
cmpl %eax, %ecx
setne %r11b
jmp 0xc17c
movl 0x4(%r10), %eax
movl %ebp, %ecx
andl $0x1, %ecx
movl %eax, %esi
andl $0x1, %esi
jmp 0xc255
btl $0x14, %ebx
jb 0xc379
movl %ebp, %edx
shrl %edx
movl %edx, %edx
cmpl %esi, %ecx
movabsq $0x7fffffff00000000, %r9 # imm = 0x7FFFFFFF00000000
jne 0xc291
leaq 0x1(%r8), %rcx
movq %r8, %rsi
shlq $0x4, %rsi
movabsq $-0x100000000, %rdi     # imm = 0xFFFFFFFF00000000
andq %rdi, %rbp
orq %rbp, %rdx
movq %rdx, (%r13,%rsi)
movq 0x8(%r10), %rdx
shrl %eax
jmp 0xc2dc
movq 0xe8(%rsp), %rcx
movq (%rcx), %rsi
movq 0x40(%rsp), %rdi
incq %rdi
movq %rdi, 0x40(%rsp)
subq %rdi, %rsi
movq %r8, %rcx
movq %rsi, %r8
shlq $0x4, %rsi
andq %r9, %rbp
orq %rbp, %rdx
btsq $0x3f, %rdx
movq %rdx, (%r13,%rsi)
movq 0x8(%r10), %rdx
movl %edx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addl 0x64(%rsp), %esi
shrl %eax
notl %eax
addl %esi, %eax
shlq $0x20, %rdx
andq %r9, %rdx
movl %eax, %esi
orq %rdx, %rsi
shlq $0x4, %r8
movq %rsi, 0x8(%r13,%r8)
movq 0x8(%r10), %rax
shlq $0x10, %rax
movabsq $-0x1000000000000, %rdx # imm = 0xFFFF000000000000
andq %rdx, %rax
orq %rsi, %rax
movq %rax, 0x8(%r13,%r8)
movq 0x8(%r10), %rdx
testq %rdx, %rdx
setns %sil
testb %r11b, %sil
movq 0x48(%rsp), %rbp
jne 0xc34c
movq %rax, %rsi
btsq $0x2a, %rsi
testq %rdx, %rdx
cmovnsq %rax, %rsi
leaq (%r8,%r13), %rax
addq $0x8, %rax
movq %rsi, %rdx
btsq $0x2b, %rdx
testb %r11b, %r11b
cmovneq %rsi, %rdx
movq %rdx, (%rax)
movq %rcx, %r8
jmp 0xc37e
testb $0x2, %bl
je 0xc3e1
testl %eax, %eax
movq 0x38(%rsp), %r13
movq 0x50(%rsp), %r12
movq 0x30(%rsp), %r8
movq 0x58(%rsp), %r10
movb $0x1, %r11b
jle 0xc17c
movq 0x48(%rsp), %rbp
movq 0x8(%r14), %rax
movl (%r10), %ecx
decl %ecx
cmpl %eax, %ecx
movq %r8, 0x30(%rsp)
jbe 0xc3b2
incq %rax
movq %rax, 0x8(%r14)
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rbp,%rcx,8), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %rax
movq %rax, (%r14)
jmp 0xc3c9
movl %r15d, %eax
shlq $0x4, %rax
movq 0x70(%rsp), %rcx
movups (%rcx,%rax), %xmm0
movups %xmm0, (%r14)
decl %r15d
movl %r15d, %esi
xorl %edi, %edi
movq %r14, %rdx
callq 0xad1f
testl %r15d, %r15d
jg 0xc149
jmp 0xc428
movq 0x38(%rsp), %r13
movq 0x50(%rsp), %r12
movq 0x30(%rsp), %r8
movq 0x58(%rsp), %r10
movb $0x1, %r11b
jmp 0xc17c
movq $0x0, 0x40(%rsp)
movq $0x0, 0x30(%rsp)
jmp 0xc428
movq $0x0, 0x40(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x38(%rsp), %r13
movq 0x68(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x12587
movq %rbx, %rdi
movq %r14, %rsi
callq 0x12587
movq 0x40(%rsp), %rdx
movq %rdx, %rax
sarq %rax
testq %rax, %rax
movq 0xe8(%rsp), %r14
jle 0xc4b2
movq %rdx, %rcx
negq %rcx
leaq -0x10(%r13), %r8
movq (%r14), %rsi
movq %rsi, %rdi
shlq $0x4, %rdi
movups (%r8,%rdi), %xmm0
movaps %xmm0, 0x90(%rsp)
addq %rcx, %rsi
shlq $0x4, %rsi
movups (%r13,%rsi), %xmm0
movups %xmm0, (%r8,%rdi)
movq (%r14), %rsi
addq %rcx, %rsi
shlq $0x4, %rsi
movaps 0x90(%rsp), %xmm0
movups %xmm0, (%r13,%rsi)
incq %rcx
addq $-0x10, %r8
decq %rax
jne 0xc465
movq (%r14), %rsi
movq 0x30(%rsp), %rdi
leaq (%rdi,%rdx), %rbx
cmpq %rbx, %rsi
jle 0xc4e0
shlq $0x4, %rdi
addq %r13, %rdi
shlq $0x4, %rsi
addq %r13, %rsi
shlq $0x4, %rdx
subq %rdx, %rsi
callq 0x2340
movq %rbx, (%r14)
movq %r13, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq