// File: /workspace/github_cpp_makefile_star2_O1/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 $0xf8, %rsp
movq %r9, %rbp
movq %rdi, 0x8(%rsp)
imulq %rdx, %r8
movq 0x8(%rsi), %rax
testq %rax, %rax
je 0x1b2664
movq (%rsi), %rdi
addq $0x8, %rdi
xorl %r9d, %r9d
movl (%rdi), %r10d
subl -0x4(%rdi), %r10d
addq %r10, %r9
addq $0x1c, %rdi
decq %rax
jne 0x1b264f
jmp 0x1b2667
xorl %r9d, %r9d
cmpq %r8, %r9
jbe 0x1b2af0
movq %rsi, 0x40(%rsp)
movq %r8, 0x38(%rsp)
xorl %edi, %edi
movq %rdx, 0x30(%rsp)
movq %rdx, %rsi
movq %rcx, 0x20(%rsp)
movq %rcx, %rdx
callq 0x49869
movq %rax, %rbx
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
addq %rbx, %rax
cmpq %rax, 0x50(%rbp)
jae 0x1b26f7
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %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(%rbp)
movq 0x58(%rbp), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x58(%rbp)
movq 0x58(%rbp), %r13
movq %rbx, 0x28(%rsp)
leaq (,%rbx,8), %rdx
movq %r13, %rdi
movl $0xff, %esi
callq 0x20e0
movq 0x58(%rbp), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%rsp), %r15
cmpq $0x0, 0x10(%r15)
movq 0x30(%rsp), %rbp
movq 0x20(%rsp), %r12
je 0x1b2779
xorl %ebx, %ebx
xorl %r14d, %r14d
movq (%r15), %rax
leaq (%rax,%rbx), %rdi
movl $0x0, 0x28(%rax,%rbx)
movb $0x0, 0x2c(%rax,%rbx)
movq %r12, %rsi
callq 0x4a082
movq (%r15), %rdi
addq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x1b13c8
incq %r14
addq $0xa8, %rbx
cmpq 0x10(%r15), %r14
jb 0x1b2739
movq %r13, 0x10(%rsp)
movq 0x40(%rsp), %r12
movq 0x8(%r12), %rax
testq %rax, %rax
movq %rbp, %r14
je 0x1b2849
movl $0x1, %ecx
xorl %edx, %edx
cmpq %rax, %rcx
je 0x1b27c8
movq (%r12), %rsi
leaq (%rcx,%rcx,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rcx, %rdi
movl (%rsi,%rdi), %edi
leaq (%rdx,%rdx,8), %r8
leaq (%r8,%r8,2), %r8
addq %rdx, %r8
cmpl (%rsi,%r8), %edi
jne 0x1b27c8
movq %rdx, %rdi
jmp 0x1b283a
movq (%r15), %rsi
movq (%r12), %r8
leaq (%rdx,%rdx,8), %rdi
leaq (%rdi,%rdi,2), %r9
addq %rdx, %r9
movl (%r8,%r9), %edi
imulq $0xa8, %rdi, %r10
movl $0x0, 0x28(%rsi,%r10)
movq %rcx, %rdi
cmpq %rcx, %rdx
jae 0x1b283a
addq %r10, %rsi
addq $0x28, %rsi
movl (%rsi), %edi
addq %r9, %r8
addq $0x8, %r8
movq %rdx, %r9
movl %edi, %r10d
movl (%r8), %edi
subl -0x4(%r8), %edi
addl %r10d, %edi
cmpq %rdx, %r9
jbe 0x1b2829
movl -0x4(%r8), %r10d
cmpl -0x1c(%r8), %r10d
jb 0x1b2b02
incq %r9
addq $0x1c, %r8
cmpq %r9, %rcx
jne 0x1b2809
movl %edi, (%rsi)
movq %rcx, %rdi
incq %rcx
movq %rdi, %rdx
cmpq %rax, %rcx
jbe 0x1b279b
movq 0x28(%rsp), %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax,8), %rbp
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x1b28c4
movq (%r15), %r15
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x1b1436
testq %rax, %rax
js 0x1b2898
shlq $0x20, %rax
orq %rbx, %rax
btsq $0x3f, %rax
movq %rax, (%rbp,%rbx,8)
jmp 0x1b28aa
shlq $0x20, %rax
negq %rax
orq %rbx, %rax
movq %rax, (%rbp,%rbx,8)
incq %r13
incq %rbx
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
addq $0xa8, %r15
cmpq %rax, %rbx
jb 0x1b2868
jmp 0x1b28c7
xorl %r13d, %r13d
leaq (,%rax,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x194fef
cmpq $0x2, %r13
movq 0x28(%rsp), %r8
jl 0x1b2930
movq %r13, %rdx
shrq %rdx
xorl %eax, %eax
cmpq $0x1, %rdx
movl $0x0, %ecx
sbbq %rdx, %rcx
shlq $0x3, %r13
leaq (,%r8,8), %rdx
addq %r13, %rdx
movq 0x18(%rsp), %rsi
addq %rsi, %rdx
addq $-0x8, %rdx
movq (%rbp), %rsi
movq (%rdx,%rax,8), %rdi
movq %rdi, (%rbp)
movq %rsi, (%rdx,%rax,8)
decq %rax
addq $0x8, %rbp
cmpq %rax, %rcx
jne 0x1b2914
movq 0x8(%rsp), %rbp
movq 0x10(%rbp), %rax
testq %rax, %rax
movq 0x38(%rsp), %r10
jle 0x1b2980
movq (%rbp), %rcx
movq 0x18(%rsp), %rdx
leaq (%rdx,%r8,8), %rdx
addq $-0x8, %rdx
xorl %esi, %esi
movq %rax, %rdi
movl (%rdx,%rdi,8), %r8d
imulq $0xa8, %r8, %r9
movl 0x28(%rcx,%r9), %r8d
movb $0x1, 0x2c(%rcx,%r9)
cmpq $0x2, %rdi
jl 0x1b2980
decq %rdi
addq %r8, %rsi
cmpq %r10, %rsi
jbe 0x1b2959
testq %rax, %rax
je 0x1b29ac
movq (%rbp), %rcx
addq $0x28, %rcx
xorl %edx, %edx
movl $0xffffffff, (%rcx)     # imm = 0xFFFFFFFF
cmpb $0x0, 0x4(%rcx)
je 0x1b29a0
movl %edx, (%rcx)
incq %rdx
addq $0xa8, %rcx
decq %rax
jne 0x1b298f
cmpq $0x0, 0x8(%r12)
je 0x1b2a25
xorl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
movq (%rbp), %rdi
movq (%r12), %rsi
movl (%rsi,%rcx), %r8d
imulq $0xa8, %r8, %r8
cmpb $0x0, 0x2c(%rdi,%r8)
je 0x1b2a15
leaq (%rsi,%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(%rsi,%r8)
movups %xmm0, (%rsi,%r8)
movq (%rbp), %rsi
movq (%r12), %rdi
movl (%rdi,%r8), %r9d
imulq $0xa8, %r9, %r9
movl 0x28(%rsi,%r9), %esi
movl %esi, (%rdi,%r8)
incq %rax
incq %rdx
addq $0x1c, %rcx
cmpq 0x8(%r12), %rdx
jb 0x1b29ba
jmp 0x1b2a27
xorl %eax, %eax
movq %rax, 0x8(%r12)
cmpq $0x0, 0x10(%rbp)
je 0x1b2ae9
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x10(%rsp), %rsi
movq (%rbp), %r15
leaq (%r15,%r13), %r12
cmpb $0x0, 0x2c(%r15,%r13)
je 0x1b2aae
cmpq %r14, %rbx
je 0x1b2aa9
movl $0xa8, %edx
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x2160
imulq $0xa8, %r14, %rbp
addq %rbp, %r15
movl $0xa8, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
addq (%rax), %rbp
movl $0xa8, %edx
movq %rbp, %rdi
movq 0x8(%rsp), %rbp
leaq 0x50(%rsp), %rsi
callq 0x2160
movq 0x10(%rsp), %rsi
incq %r14
jmp 0x1b2ad3
movq %r12, %rdi
movq 0x20(%rsp), %rdx
movq 0x30(%rsp), %rcx
leaq 0x48(%rsp), %r8
callq 0x1b1436
movl 0x48(%rsp), %eax
movl %eax, 0x30(%r15,%r13)
movq 0x10(%rsp), %rsi
incq %rbx
addq $0xa8, %r13
cmpq 0x10(%rbp), %rbx
jb 0x1b2a44
jmp 0x1b2aec
xorl %r14d, %r14d
movq %r14, 0x10(%rbp)
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edi, (%rsi)
leaq 0x829d1(%rip), %rdi     # 0x2354dc
leaq 0x80e79(%rip), %rsi     # 0x23398b
leaq 0x829ec(%rip), %rcx     # 0x235505
movl $0x1c4a, %edx           # imm = 0x1C4A
callq 0x2110