// File: 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
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x18(%rbp), %rax
imulq -0x28(%rbp), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x2aee7a
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x38(%rbp), %rdx
addq %rdx, %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x2aee30
movq -0x50(%rbp), %rax
cmpq -0x60(%rbp), %rax
ja 0x2aee89
jmp 0x2af75e
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %edi
callq 0x340c0
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x58(%rbp), %rcx
movq -0x8(%rbp), %rdx
addq 0x10(%rdx), %rcx
cmpq %rcx, %rax
jae 0x2aef8a
movq -0x58(%rbp), %rcx
movq -0x8(%rbp), %rax
addq 0x10(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x50(%rax)
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rcx
shrq %rcx
movq -0x30(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rcx
shrq $0x2, %rcx
movq -0x30(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rcx
shrq $0x4, %rcx
movq -0x30(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rcx
shrq $0x8, %rcx
movq -0x30(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rcx
shrq $0x10, %rcx
movq -0x30(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x50(%rax)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x58(%rax)
jmp 0x2aef8c
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
movq -0x58(%rbp), %rdx
shlq $0x3, %rdx
movl $0xff, %esi
callq 0x2100
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x2af059
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x38(%rbp), %rcx
addq %rcx, %rax
movl $0x0, 0x28(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x38(%rbp), %rcx
addq %rcx, %rax
movb $0x0, 0x2c(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x38(%rbp), %rax
addq %rax, %rdi
movq -0x20(%rbp), %rsi
callq 0x34ab0
movq -0x8(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x38(%rbp), %rax
addq %rax, %rdi
movq -0x68(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x2aca20
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x2aefcd
movq $0x1, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
ja 0x2af1c7
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
je 0x2af0b1
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x40(%rbp), %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
je 0x2af1b4
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x40(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x2af1ac
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x48(%rbp), %rdx
addq %rdx, %rax
subl 0x4(%rax), %ecx
movq -0x78(%rbp), %rax
addl 0x28(%rax), %ecx
movl %ecx, 0x28(%rax)
movq -0x48(%rbp), %rcx
movb $0x1, %al
cmpq -0x40(%rbp), %rcx
movb %al, -0x159(%rbp)
jbe 0x2af16c
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movq -0x48(%rbp), %rdx
subq $0x1, %rdx
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
setae %al
movb %al, -0x159(%rbp)
movb -0x159(%rbp), %al
testb $0x1, %al
jne 0x2af178
jmp 0x2af17a
jmp 0x2af199
leaq 0xe31ff(%rip), %rdi     # 0x392380
leaq 0xe16a7(%rip), %rsi     # 0x39082f
movl $0x1c4a, %edx           # imm = 0x1C4A
leaq 0xe3215(%rip), %rcx     # 0x3923a9
callq 0x2130
jmp 0x2af19b
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x2af0ea
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x2af1b6
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x2af069
movq $0x0, -0xa0(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x2af321
movq -0x8(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x38(%rbp), %rax
addq %rax, %rdi
movq -0x68(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x2acb80
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jl 0x2af2a7
movq -0x80(%rbp), %rdx
movq -0x70(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rdx
movq -0x70(%rbp), %rax
movq -0x88(%rbp), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
movq -0x88(%rbp), %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0x2af2fe
xorl %eax, %eax
movl %eax, %edx
subq -0x80(%rbp), %rdx
movq -0x70(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rdx
movq -0x70(%rbp), %rax
movq -0x88(%rbp), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x2af1f0
movq -0x70(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x27a9b0
cmpq $0x0, -0xa0(%rbp)
jle 0x2af3d5
movq -0xa0(%rbp), %rax
sarq %rax
movq %rax, -0xa8(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jae 0x2af3d3
movq -0x70(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
movq -0x70(%rbp), %rax
movq -0xa0(%rbp), %rcx
subq -0x38(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rdx
movq -0x70(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xb0(%rbp), %rdx
movq -0x70(%rbp), %rax
movq -0xa0(%rbp), %rcx
subq -0x38(%rbp), %rcx
subq $0x1, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x2af364
jmp 0x2af3d5
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
subq $0x1, %rax
movq %rax, -0x88(%rbp)
movq $0x0, -0x50(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x88(%rbp)
movb %al, -0x15a(%rbp)
jl 0x2af413
movq -0x50(%rbp), %rax
cmpq -0x60(%rbp), %rax
setbe %al
movb %al, -0x15a(%rbp)
movb -0x15a(%rbp), %al
testb $0x1, %al
jne 0x2af41f
jmp 0x2af489
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x70(%rbp), %rcx
movq -0x88(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x70(%rbp), %rcx
movq -0x88(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movb $0x1, 0x2c(%rax)
movq -0x88(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x2af3f0
movq $0x0, -0x88(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x2af51b
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x48(%rbp), %rcx
addq %rcx, %rax
movl $0xffffffff, 0x28(%rax) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x48(%rbp), %rcx
addq %rcx, %rax
cmpb $0x0, 0x2c(%rax)
jne 0x2af4dd
jmp 0x2af50d
movq -0x88(%rbp), %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x48(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x28(%rax)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x2af49c
movq $0x0, -0x88(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x2af60c
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movzbl 0x2c(%rax), %eax
cmpl $0x0, %eax
jne 0x2af570
jmp 0x2af5fb
movq -0x10(%rbp), %rax
movq (%rax), %rcx
imulq $0x1c, -0x38(%rbp), %rax
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x88(%rbp), %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rax)
movl 0x18(%rcx), %ecx
movl %ecx, 0x18(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x88(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x88(%rbp), %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x2af52e
movq -0x88(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x2af74f
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x48(%rbp), %rcx
addq %rcx, %rax
cmpb $0x0, 0x2c(%rax)
jne 0x2af6a5
movq -0x8(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x48(%rbp), %rax
addq %rax, %rdi
movq -0x68(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rcx
leaq -0x90(%rbp), %r8
callq 0x2acb80
movq -0x90(%rbp), %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x48(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x30(%rax)
jmp 0x2af73e
movq -0x88(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x2af72c
movq -0x8(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x48(%rbp), %rax
addq %rax, %rsi
leaq -0x158(%rbp), %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x8(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x88(%rbp), %rax
addq %rax, %rsi
movq -0x8(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x48(%rbp), %rax
addq %rax, %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x8(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x88(%rbp), %rax
addq %rax, %rdi
leaq -0x158(%rbp), %rsi
movl $0xa8, %edx
callq 0x2180
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x2af62e
movq -0x88(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nopw (%rax,%rax)