// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: filter_topN(overlap_region_alloc*, kv_ul_ov_t*, unsigned long, unsigned long, unsigned long, glchain_t*)
void filter_topN(overlap_region_alloc* ol, kv_ul_ov_t *aln, uint64_t ql, uint64_t wl, uint64_t max_cov, glchain_t *ll)
{
    uint64_t i, j, k, cc, nw, cc_max = (ql*max_cov), *w_idx, *srt; overlap_region *z;
    for (i = cc = 0; i < aln->n; i++) cc += aln->a[i].qe-aln->a[i].qs;
    if(cc <= cc_max) return;

	nw = get_num_wins(0, ql, wl); kv_resize(uint64_t, ll->srt.a, nw+ol->length); 
	w_idx = ll->srt.a.a; memset(w_idx, -1, nw*sizeof((*w_idx))); srt = ll->srt.a.a + nw;
	for (i = 0; i < ol->length; i++) {
		ol->list[i].is_match = ol->list[i].align_length = 0; 
		append_unmatched_wins(&(ol->list[i]), wl);
		set_sec_e_min(&(ol->list[i]), w_idx, wl, ql);
	}

	for (i = 1, j = 0; i <= aln->n; i++) {
        if (i == aln->n || aln->a[i].qn != aln->a[j].qn) {
			z = &(ol->list[aln->a[j].qn]); z->align_length = 0; 
			for (k = j; k < i; k++) {
				z->align_length += aln->a[k].qe-aln->a[k].qs;
				assert(k <= j || aln->a[k].qs >= aln->a[k-1].qe);
			}
            j = i;
        }
    }

	int64_t sc, m, sec_err, h0, h1;
	for (i = m = h0 = h1 = 0; i < ol->length; i++) {
		sc = cal_sec_e_min(&(ol->list[i]), w_idx, wl, ql, NULL);
		if(sc >= 0) {
			srt[m] = sc; srt[m] <<= 32; srt[m] |= i; srt[m] |= ((uint64_t)0x8000000000000000); h0++;
		} else {
			srt[m] = -sc; srt[m] <<= 32; srt[m] |= i; h1++;
		}
		m++;
	}

	radix_sort_gfa64(srt, srt + ol->length);
	if(h1 > 0) {///have chains with negative weight 
		uint64_t zn = h1>>1, zt;
		for (i = 0; i < zn; i++) {
			zt = srt[i]; srt[i] = srt[h1-i-1]; srt[h1-i-1] = zt;
		}
	}

	for (m = ((int64_t)ol->length)-1, cc = 0; m >= 0 && cc <= cc_max; m--) {
		cc += ol->list[(uint32_t)srt[m]].align_length; 
		ol->list[(uint32_t)srt[m]].is_match = 1;
	}

	for (k = m = 0; k < ol->length; k++) {
		ol->list[k].align_length = (uint32_t)-1;
        if(!ol->list[k].is_match) continue;
        ol->list[k].align_length = m;
        m++;
    }

	for (i = m = 0; i < aln->n; i++) {
		if(ol->list[aln->a[i].qn].is_match == 0) continue;
		aln->a[m] = aln->a[i];
		aln->a[m].qn = ol->list[aln->a[m].qn].align_length;
		m++;
	}
	aln->n = m;

	overlap_region t;
	for (k = m = 0; k < ol->length; k++) {
        if(!ol->list[k].is_match) {
			cal_sec_e_min(&(ol->list[k]), w_idx, wl, ql, &sec_err);
			ol->list[k].non_homopolymer_errors = sec_err;
			continue;
		}
        if(m != (int64_t)k) {
            t = ol->list[k]; ol->list[k] = ol->list[m]; ol->list[m] = t;
        }
        m++;
    }
    ol->length = m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq 0x8(%rsi), %rax
testq %rax, %rax
je 0x1bc30a
movq %r9, %rbx
movq %rcx, %r15
movq %rdx, %r13
movq %rdi, %r12
imulq %rdx, %r8
movq (%rsi), %rcx
addq $0x8, %rcx
xorl %edx, %edx
movl (%rcx), %edi
subl -0x4(%rcx), %edi
addq %rdi, %rdx
addq $0x1c, %rcx
decq %rax
jne 0x1bbe5e
cmpq %r8, %rdx
jbe 0x1bc30a
movq %r8, 0x28(%rsp)
movq %rsi, 0x18(%rsp)
xorl %edi, %edi
movq %r13, %rsi
movq %r15, %rdx
callq 0x48ea5
movq 0x10(%r12), %rdx
movq %rax, (%rsp)
addq %rax, %rdx
cmpq %rdx, 0x50(%rbx)
jae 0x1bbef4
decq %rdx
movq %rdx, %rcx
shrq %rcx
orq %rdx, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x50(%rbx)
movq 0x58(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x58(%rbx)
jmp 0x1bbef8
movq 0x58(%rbx), %rax
movq (%rsp), %rcx
leaq (,%rcx,8), %rdx
movq %rax, 0x20(%rsp)
movq %rax, %rdi
movl $0xff, %esi
callq 0x20e0
movq 0x58(%rbx), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%r12)
je 0x1bbf7f
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x20(%rsp), %rbp
movq (%r12), %rax
leaq (%rax,%rbx), %rdi
movl $0x0, 0x28(%rax,%rbx)
movb $0x0, 0x2c(%rax,%rbx)
movq %r15, %rsi
callq 0x49760
movq (%r12), %rdi
addq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x1ba65f
incq %r14
movq 0x10(%r12), %rax
addq $0xa8, %rbx
cmpq %rax, %r14
jb 0x1bbf31
testq %rax, %rax
sete %al
jmp 0x1bbf81
movb $0x1, %al
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
je 0x1bc055
movq 0x18(%rsp), %rdx
movq (%rdx), %rdx
leaq 0x8(%rdx), %rsi
movl $0x1, %edi
xorl %r8d, %r8d
leaq (%r8,%r8,8), %r10
cmpq %rcx, %rdi
jne 0x1bbfbd
leaq (%r10,%r10,2), %r9
addq %r8, %r9
movl (%rdx,%r9), %r9d
jmp 0x1bbfde
leaq (%rdi,%rdi,8), %r9
leaq (%r9,%r9,2), %r11
addq %rdi, %r11
leaq (%r10,%r10,2), %r9
addq %r8, %r9
movl (%rdx,%r9), %r9d
cmpl %r9d, (%rdx,%r11)
jne 0x1bbfde
movq %r8, %r9
jmp 0x1bc046
movq (%r12), %r11
movl %r9d, %r9d
imulq $0xa8, %r9, %rbx
movl $0x0, 0x28(%r11,%rbx)
movq %rdi, %r9
cmpq %rdi, %r8
jae 0x1bc046
leaq (%r11,%rbx), %r9
addq $0x28, %r9
leaq (%r10,%r10,2), %r10
addq %rsi, %r10
addq %r8, %r10
xorl %r11d, %r11d
movq %r8, %rbx
movl %r11d, %r14d
movl -0x4(%r10), %ebp
movl (%r10), %r11d
subl %ebp, %r11d
addl %r14d, %r11d
cmpq %r8, %rbx
jbe 0x1bc034
cmpl -0x1c(%r10), %ebp
jb 0x1bc31c
incq %rbx
addq $0x1c, %r10
cmpq %rbx, %rdi
jne 0x1bc015
movl %r11d, (%r9)
movq %rdi, %r9
incq %rdi
movq %r9, %r8
cmpq %rcx, %rdi
jbe 0x1bbfa7
movq (%rsp), %rcx
movq 0x10(%rsp), %rdx
leaq (%rdx,%rcx,8), %rbp
testb %al, %al
movq %r15, 0x30(%rsp)
je 0x1bc07b
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x19dd99
jmp 0x1bc13c
movq %r13, %rbx
movq (%r12), %r13
xorl %r14d, %r14d
movq $0x0, 0x8(%rsp)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x1ba6cf
testq %rax, %rax
js 0x1bc0b7
shlq $0x20, %rax
orq %r14, %rax
btsq $0x3f, %rax
jmp 0x1bc0c6
shlq $0x20, %rax
negq %rax
orq %r14, %rax
incq 0x8(%rsp)
movq %rax, (%rbp,%r14,8)
incq %r14
movq 0x10(%r12), %rax
addq $0xa8, %r13
cmpq %rax, %r14
jb 0x1bc08e
leaq (,%rax,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x19dd99
movq 0x8(%rsp), %rsi
cmpq $0x2, %rsi
movq %rbx, %r13
jl 0x1bc13c
movq %rsi, %rax
shrq %rax
negq %rax
movq (%rsp), %rcx
movq 0x10(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
leaq (%rcx,%rsi,8), %rcx
addq $-0x8, %rcx
xorl %edx, %edx
movq (%rbp), %rsi
movq (%rcx,%rdx,8), %rdi
movq %rdi, (%rbp)
movq %rsi, (%rcx,%rdx,8)
decq %rdx
addq $0x8, %rbp
cmpq %rdx, %rax
jne 0x1bc120
movq 0x10(%r12), %r15
testq %r15, %r15
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %r9
jle 0x1bc190
movq (%r12), %rax
movq (%rsp), %rcx
movq 0x10(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
addq $-0x8, %rcx
xorl %edx, %edx
movq %r15, %rsi
movl (%rcx,%rsi,8), %edi
imulq $0xa8, %rdi, %r8
movl 0x28(%rax,%r8), %edi
movb $0x1, 0x2c(%rax,%r8)
cmpq $0x2, %rsi
jb 0x1bc190
decq %rsi
addq %rdi, %rdx
cmpq %r9, %rdx
jbe 0x1bc16a
testq %r15, %r15
je 0x1bc1bd
movq (%r12), %rax
addq $0x28, %rax
xorl %ecx, %ecx
movq %r15, %rdx
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
cmpb $0x0, 0x4(%rax)
je 0x1bc1b2
movl %ecx, (%rax)
incl %ecx
addq $0xa8, %rax
decq %rdx
jne 0x1bc1a2
movq 0x8(%r10), %r8
testq %r8, %r8
je 0x1bc23c
movq (%r12), %rdi
movq (%r10), %rdx
xorl %ecx, %ecx
xorl %esi, %esi
xorl %eax, %eax
movl (%rdx,%rcx), %r9d
imulq $0xa8, %r9, %r9
cmpb $0x0, 0x2c(%rdi,%r9)
je 0x1bc229
leaq (%rdx,%rcx), %rdi
leaq (%rax,%rax,8), %r8
leaq (%r8,%r8,2), %r8
addq %rax, %r8
movups (%rdi), %xmm0
movups 0xc(%rdi), %xmm1
movups %xmm1, 0xc(%rdx,%r8)
movups %xmm0, (%rdx,%r8)
movq (%r12), %rdi
movq (%r10), %rdx
movl (%rdx,%r8), %r9d
imulq $0xa8, %r9, %r9
movl 0x28(%rdi,%r9), %r9d
movl %r9d, (%rdx,%r8)
incq %rax
movq 0x8(%r10), %r8
incq %rsi
addq $0x1c, %rcx
cmpq %r8, %rsi
jb 0x1bc1d3
movq 0x10(%r12), %r15
jmp 0x1bc23e
xorl %eax, %eax
movq %r12, 0x8(%rsp)
movq %rax, 0x8(%r10)
testq %r15, %r15
je 0x1bc2fe
movq %r13, (%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
movq (%rax), %r13
leaq (%r12,%r13), %rbp
cmpb $0x0, 0x2c(%r13,%r12)
je 0x1bc2c5
cmpq %r14, %rbx
je 0x1bc2c0
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
imulq $0xa8, %r14, %r15
addq %r15, %r13
movl $0xa8, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2160
movq 0x8(%rsp), %r13
addq (%r13), %r15
movl $0xa8, %edx
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0x2160
movq 0x10(%r13), %r15
incq %r14
jmp 0x1bc2e9
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq (%rsp), %rcx
leaq 0x38(%rsp), %r8
callq 0x1ba6cf
movl 0x38(%rsp), %eax
movl %eax, 0x30(%r13,%r12)
incq %rbx
addq $0xa8, %r12
cmpq %r15, %rbx
jb 0x1bc25c
jmp 0x1bc301
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movq %r14, 0x10(%rax)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r11d, (%r9)
leaq 0x85486(%rip), %rdi     # 0x2417ac
leaq 0x8392e(%rip), %rsi     # 0x23fc5b
leaq 0x854a1(%rip), %rcx     # 0x2417d5
movl $0x1c4a, %edx           # imm = 0x1C4A
callq 0x2110