// File: /workspace/github_c_makefile_star2_O1/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, 0x60(%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 0x1db09
movq %rax, %r13
movslq (%r12), %rsi
shlq $0x4, %rsi
movq %r15, %rdi
callq 0x126e9
movq %rax, %r14
movq (%rbx), %rsi
shlq $0x4, %rsi
movq %r15, 0x68(%rsp)
movq %r15, %rdi
callq 0x126e9
movq %rax, 0x40(%rsp)
movslq (%r12), %rax
testq %rax, %rax
jle 0xc258
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
movabsq $0x100000000, %rdx      # imm = 0x100000000
xorl %esi, %esi
xorl %r15d, %r15d
cmpl $0x0, (%r13,%rcx)
je 0xc24a
movq 0x10(%r13,%rcx), %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 %rdx, %rsi
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0xc224
jmp 0xc25b
xorl %r15d, %r15d
movq %r13, 0x38(%rsp)
cmpl $0x2, %r15d
jb 0xc286
movslq %r15d, %r12
movq %r12, %r13
shrq %r13
decq %r13
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0xae27
addq $-0x1, %r13
jb 0xc272
testl %r15d, %r15d
jle 0xc533
movq %r14, %rax
addq $-0x10, %rax
movq %rax, 0x70(%rsp)
movl 0xe0(%rsp), %eax
decl %eax
movl %eax, 0x5c(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x48(%rsp)
movq 0x40(%rsp), %r12
movq 0x38(%rsp), %r13
cmpq $0x0, 0x60(%rsp)
sete %al
movl 0xc(%r14), %ecx
leaq (%rcx,%rcx,2), %rcx
leaq (%r13,%rcx,8), %r11
movq (%r14), %rbp
movq 0x80(%rsp), %rcx
movq (%rcx), %rbx
testb $0x3, %bl
sete %cl
orb %al, %cl
movl $0x0, %ecx
je 0xc326
testl $0x300000, %ebx         # imm = 0x300000
je 0xc3d4
movl %ebp, %eax
andl $0x1, %eax
movl 0x4(%r11), %edx
andl $0x1, %edx
cmpl %edx, %eax
jne 0xc3ce
btl $0x15, %ebx
jae 0xc3d4
jmp 0xc3d8
movq %r15, 0x50(%rsp)
movq %r11, %r15
movq 0x78(%rsp), %rax
movq 0x20(%rax), %r13
movq %rbp, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %r12
movq (%r13,%r12,8), %rsi
movq 0x60(%rsp), %rdi
callq 0x2240
xorl %ecx, %ecx
testb $0x1, %bl
je 0xc3aa
testl %eax, %eax
jne 0xc3aa
leaq (,%r12,8), %rdx
addq %r13, %rdx
movl 0xe0(%rsp), %esi
cmpl %esi, 0x10(%rdx)
movq 0x40(%rsp), %r12
movq 0x38(%rsp), %r13
movq %r15, %r11
movq 0x50(%rsp), %r15
jne 0xc3bc
movl 0x4(%r11), %esi
movl %esi, %edi
xorl %ebp, %edi
xorl %ecx, %ecx
movl $0x0, %edx
cmpl $0x2, %edi
jb 0xc3da
movl %ebp, %edx
andl $0x1, %edx
andl $0x1, %esi
xorl %ecx, %ecx
cmpl %esi, %edx
sete %cl
jmp 0xc3bc
movq 0x40(%rsp), %r12
movq 0x38(%rsp), %r13
movq %r15, %r11
movq 0x50(%rsp), %r15
testb $0x2, %bl
je 0xc2f7
testl %eax, %eax
jg 0xc3d8
jmp 0xc2f7
btl $0x14, %ebx
jb 0xc3d8
movb $0x1, %dl
jmp 0xc3da
xorl %edx, %edx
testb %dl, %dl
je 0xc4d5
movl %ebp, %edx
shrl %edx
movl %ebp, %edi
andl $0x1, %edi
movl 0x4(%r11), %eax
movl %eax, %r8d
andl $0x1, %r8d
movl %edx, %esi
cmpl %r8d, %edi
movabsq $0x7fffffff00000000, %r9 # imm = 0x7FFFFFFF00000000
jne 0xc433
movq 0x30(%rsp), %r8
leaq 0x1(%r8), %rdx
movq %r8, %rdi
shlq $0x4, %rdi
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
andq %r10, %rbp
orq %rbp, %rsi
movq %rsi, (%r12,%rdi)
movq 0x8(%r11), %rsi
shrl %eax
jmp 0xc47f
movq 0xe8(%rsp), %rdx
movq (%rdx), %rdi
movq 0x48(%rsp), %r8
incq %r8
movq %r8, 0x48(%rsp)
subq %r8, %rdi
movq 0x30(%rsp), %rdx
movq %rdi, %r8
shlq $0x4, %rdi
andq %r9, %rbp
orq %rbp, %rsi
btsq $0x3f, %rsi
movq %rsi, (%r12,%rdi)
movq 0x8(%r11), %rsi
movl %esi, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addl 0x5c(%rsp), %edi
shrl %eax
notl %eax
addl %edi, %eax
shlq $0x20, %rsi
andq %r9, %rsi
movl %eax, %edi
orq %rsi, %rdi
shlq $0x4, %r8
leaq (%r12,%r8), %rax
addq $0x8, %rax
movq %rdi, 0x8(%r12,%r8)
movq 0x8(%r11), %rsi
shlq $0x10, %rsi
movabsq $-0x1000000000000, %r9  # imm = 0xFFFF000000000000
andq %r9, %rsi
orq %rdi, %rsi
movq %rsi, 0x8(%r12,%r8)
cmpq $0x0, 0x8(%r11)
jns 0xc4c8
btsq $0x2a, %rsi
movq %rsi, (%rax)
testl %ecx, %ecx
je 0xc4d0
orb $0x8, 0x5(%rax)
movq %rdx, 0x30(%rsp)
movq 0x8(%r14), %rax
movl (%r11), %ecx
decl %ecx
cmpl %eax, %ecx
jbe 0xc504
incq %rax
movq %rax, 0x8(%r14)
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%r13,%rcx,8), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %rax
movq %rax, (%r14)
jmp 0xc51b
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 0xae27
testl %r15d, %r15d
jg 0xc2c4
jmp 0xc54f
movq $0x0, 0x48(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x40(%rsp), %r12
movq 0x38(%rsp), %r13
movq 0x68(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1261e
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1261e
movq 0x48(%rsp), %rdx
movq %rdx, %rax
sarq %rax
testq %rax, %rax
movq 0xe8(%rsp), %r14
jle 0xc5d8
movq %rdx, %rcx
negq %rcx
leaq -0x10(%r12), %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 (%r12,%rsi), %xmm0
movups %xmm0, (%r8,%rdi)
movq (%r14), %rsi
addq %rcx, %rsi
shlq $0x4, %rsi
movaps 0x90(%rsp), %xmm0
movups %xmm0, (%r12,%rsi)
incq %rcx
addq $-0x10, %r8
decq %rax
jne 0xc58d
movq (%r14), %rsi
movq 0x30(%rsp), %rdi
leaq (%rdi,%rdx), %rbx
cmpq %rbx, %rsi
jle 0xc606
shlq $0x4, %rdi
addq %r12, %rdi
shlq $0x4, %rsi
addq %r12, %rsi
shlq $0x4, %rdx
subq %rdx, %rsi
callq 0x2340
movq %rbx, (%r14)
movq %r12, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq