// File: /workspace/github_cpp_makefile_star2_O2/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 %r9, %rbp
movq %r8, %rbx
movq %rcx, %r13
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, 0x8(%rsp)
movq 0x8(%rsi), %rcx
pushq $0x8
popq %rdx
xorl %eax, %eax
subq $0x1, %rcx
jb 0x171962
movq (%r12), %rsi
movl (%rsi,%rdx), %edi
subl -0x4(%rsi,%rdx), %edi
addq %rdi, %rax
addq $0x1c, %rdx
jmp 0x171948
imulq %r15, %rbx
cmpq %rbx, %rax
jbe 0x171d5d
xorl %edi, %edi
movq %r15, %rsi
movq %r13, %rdx
callq 0x1c867
movq %rax, %r14
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
addq %r14, %rax
cmpq %rax, 0x50(%rbp)
movq %rbx, 0x30(%rsp)
jae 0x1719ec
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, %rdi
movq %rax, 0x58(%rbp)
jmp 0x1719f0
movq 0x58(%rbp), %rdi
movq %r14, 0x10(%rsp)
leaq (,%r14,8), %rdx
movq %rdi, 0x18(%rsp)
movl $0xff, %esi
callq 0x20e0
movq 0x58(%rbp), %rax
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x8(%rsp), %rbp
movq 0x10(%rbp), %rax
cmpq %rax, %r14
jae 0x171a65
movq (%rbp), %rax
leaq (%rax,%rbx), %rdi
andl $0x0, 0x28(%rax,%rbx)
movb $0x0, 0x2c(%rax,%rbx)
movq %r13, %rsi
callq 0x1cf34
movq (%rbp), %rdi
addq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x17072e
incq %r14
addq $0xa8, %rbx
jmp 0x171a1f
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %rdx
leaq (%rdx,%rcx,8), %rbp
movq 0x8(%r12), %rcx
pushq $0x1
popq %rdx
xorl %esi, %esi
cmpq %rcx, %rdx
ja 0x171b0c
movq (%r12), %r8
jne 0x171a96
imulq $0x1c, %rsi, %rdi
movl (%r8,%rdi), %edi
jmp 0x171aa8
imulq $0x1c, %rdx, %r9
imulq $0x1c, %rsi, %rdi
movl (%r8,%rdi), %edi
cmpl %edi, (%r8,%r9)
je 0x171b04
movq 0x8(%rsp), %r9
movq (%r9), %r9
movl %edi, %edi
imulq $0xa8, %rdi, %rdi
addq %r9, %rdi
addq $0x28, %rdi
andl $0x0, (%rdi)
imulq $0x1c, %rsi, %r9
addq %r9, %r8
addq $0x8, %r8
xorl %r9d, %r9d
movq %rsi, %r10
cmpq %rdx, %r10
jae 0x171b01
movl -0x4(%r8), %r11d
movl (%r8), %ebx
subl %r11d, %ebx
addl %ebx, %r9d
movl %r9d, (%rdi)
cmpq %rsi, %r10
jbe 0x171af8
cmpl -0x1c(%r8), %r11d
jb 0x171d6f
incq %r10
addq $0x1c, %r8
jmp 0x171ad4
movq %rdx, %rsi
incq %rdx
jmp 0x171a7d
movq %r15, 0x28(%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpq %rax, %r15
jae 0x171b78
movq 0x8(%rsp), %rax
movq (%rax), %rdi
addq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movq 0x28(%rsp), %rcx
xorl %r8d, %r8d
callq 0x1707b3
testq %rax, %rax
js 0x171b51
shlq $0x20, %rax
orq %r15, %rax
btsq $0x3f, %rax
jmp 0x171b5e
shlq $0x20, %rax
negq %rax
orq %r15, %rax
incq %r14
movq %rax, (%rbp,%r15,8)
incq %r15
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
addq $0xa8, %rbx
jmp 0x171b19
leaq (,%rax,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x156b0d
testq %r14, %r14
movq 0x30(%rsp), %r11
jle 0x171bc3
leaq -0x8(,%r14,8), %rax
addq %rbp, %rax
shrq %r14
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0x171bc3
movq (%rbp,%rcx,8), %rdx
movq (%rax), %rsi
movq %rsi, (%rbp,%rcx,8)
movq %rdx, (%rax)
incq %rcx
addq $-0x8, %rax
jmp 0x171ba5
movq 0x8(%rsp), %r10
movq 0x10(%r10), %rax
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
addq $-0x8, %rcx
xorl %edx, %edx
movq %rax, %rsi
movq 0x18(%rsp), %r14
testq %rsi, %rsi
jle 0x171c13
cmpq %r11, %rdx
ja 0x171c13
movq (%r10), %rdi
movl (%rcx,%rsi,8), %r8d
decq %rsi
imulq $0xa8, %r8, %r8
movl 0x28(%rdi,%r8), %r9d
addq %r9, %rdx
movb $0x1, 0x2c(%rdi,%r8)
jmp 0x171be8
xorl %ecx, %ecx
pushq $0x28
popq %rdx
subq $0x1, %rax
jb 0x171c3a
movq (%r10), %rsi
orl $-0x1, (%rsi,%rdx)
cmpb $0x0, 0x4(%rsi,%rdx)
je 0x171c31
movl %ecx, (%rsi,%rdx)
incl %ecx
addq $0xa8, %rdx
jmp 0x171c18
xorl %eax, %eax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq 0x8(%r12), %rdx
jae 0x171ca2
movq (%r10), %rdi
movq (%r12), %rsi
movl (%rsi,%rax), %r8d
imulq $0xa8, %r8, %r8
cmpb $0x0, 0x2c(%rdi,%r8)
je 0x171c99
leaq (%rsi,%rax), %rdi
imulq $0x1c, %rcx, %r8
movups (%rdi), %xmm0
movups 0xc(%rdi), %xmm1
movups %xmm1, 0xc(%rsi,%r8)
movups %xmm0, (%rsi,%r8)
movq (%r10), %rsi
movq (%r12), %rdi
movl (%rdi,%r8), %r9d
imulq $0xa8, %r9, %r9
movl 0x28(%rsi,%r9), %esi
movl %esi, (%rdi,%r8)
incq %rcx
incq %rdx
addq $0x1c, %rax
jmp 0x171c40
movq %r13, 0x10(%rsp)
movq %rcx, 0x8(%r12)
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpq 0x10(%r10), %r15
jae 0x171d59
movq (%r10), %r12
leaq (%r12,%r13), %rbp
cmpb $0x0, 0x2c(%r12,%r13)
je 0x171d22
cmpq %rbx, %r15
je 0x171d1d
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
imulq $0xa8, %rbx, %r14
addq %r14, %r12
movl $0xa8, %edx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
addq (%rax), %r14
movl $0xa8, %edx
movq %r14, %rdi
movq 0x18(%rsp), %r14
leaq 0x40(%rsp), %rsi
callq 0x2160
incq %rbx
jmp 0x171d45
movq %rbp, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rcx
leaq 0x38(%rsp), %r8
callq 0x1707b3
movl 0x38(%rsp), %eax
movl %eax, 0x30(%r12,%r13)
incq %r15
addq $0xa8, %r13
movq 0x8(%rsp), %r10
jmp 0x171cb4
movq %rbx, 0x10(%r10)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x818fe(%rip), %rdi     # 0x1f3674
leaq 0x7fda6(%rip), %rsi     # 0x1f1b23
leaq 0x81919(%rip), %rcx     # 0x1f369d
movl $0x1c4a, %edx           # imm = 0x1C4A
callq 0x2110