// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: gl_chain_flter(overlap_region_alloc*, Correct_dumy*, st_mt_t*, glchain_t*, ul_idx_t const*, double, long, long, ug_opt_t const*, unsigned int*)
int64_t gl_chain_flter(overlap_region_alloc* olist, Correct_dumy* dumy, st_mt_t *sps, glchain_t *ll, 
const ul_idx_t *uref, double diff_ec_ul, int64_t wl, int64_t ql, const ug_opt_t *uopt, uint32_t *need_phase)
{
	(*need_phase) = 1;
	uint64_t k, nw; ul_ov_t *m, *p; int64_t occ, i, ovlp, idx_n;
	ll->tk.n = ll->lo.n = 0;
	kv_ul_ov_t *idx = &(ll->lo); 
	ks_introsort_or_xe(olist->length, olist->list);
	regen_ul_ov_t_lst(uref, olist, idx);
	if(idx->n == 0) return 0;
	kv_resize(uint64_t, ll->srt.a, idx->n);
	kv_resize(uint64_t, *sps, idx->n);
	kv_resize(ul_ov_t, ll->tk, idx->n);

	occ = ed_dp_c(olist, idx, ll->tk.a, uref, uopt, G_CHAIN_BW, N_GCHAIN_RATE, ql, 75, dumy->overlapID, ll->srt.a.a, sps->a, ERROR_RATE, 0, NULL, uref->ug, 0);
	if((!occ) || (!idx->n)) return 0;
	idx_n = idx->n; p = &(idx->a[idx_n-1]);
	if(idx_n <= 1) {//one chain; nothing to do
		if(p->te - p->ts <= 1) (*need_phase) = 0;//one alignment; nothing to do
		return 0;
	}
	// fprintf(stderr, "[M::%s] qs::%u, qe::%u, ql::%ld, occ::%u\n", __func__, p->qs, p->qe, ql, p->te - p->ts);
	if(p->qe-p->qs <= (ql*0.333333)) return 0;///primary chain is too short

	i = idx_n-1; occ = p->te - p->ts;
	if(p->qe-p->qs < ql && idx_n > 1) {
		for (occ = 0; i >= 0; i--) {
			p = &(idx->a[i]);
			for (k = i + 1; k < idx->n; k++) {
				m = &(idx->a[k]);
				ovlp = ((MIN(m->qe, p->qe) > MAX(m->qs, p->qs))? (MIN(m->qe, p->qe) - MAX(m->qs, p->qs)):0);
				if(((ovlp > ((m->qe-m->qs)*0.005)) || (ovlp > ((p->qe-p->qs)*0.015))) && ovlp > 32) break;
				if((ovlp == (m->qe-m->qs)) || (ovlp == (p->qe-p->qs))) break;
			}
			if(k < idx->n) break;
			occ += p->te - p->ts;
		}
		i++;
	} 
	//all alignments are primary chains; nothing to do
	if(occ == (int64_t)olist->length) return 1;

	// if(i >= ((int64_t)idx->n)) return 0;
	nw = get_num_wins(0, ql, wl); kv_resize(uint64_t, ll->srt.a, (uint64_t)nw);
	if(filter_sec(olist, idx->a+i, idx->n-i, ll->tk.a, ll->srt.a.a, nw, wl, ql)) {
		(*need_phase) = 0;
	}
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %rbx
movq %r8, %r13
movq %rcx, %r14
movq 0xb0(%rsp), %rax
movl $0x1, (%rax)
andq $0x0, 0x8(%rcx)
movq %rdx, %rbp
andq $0x0, 0x20(%rcx)
movq %rsi, %r12
movq %rdi, %r15
movq (%rdi), %rsi
movq 0x10(%rdi), %rdi
callq 0x1562de
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x170cc7
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x1711a6
cmpq %rax, 0x50(%r14)
jae 0x170ef1
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(%r14)
movq 0x58(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x58(%r14)
movq 0x8(%r14), %rax
cmpq %rax, 0x8(%rbp)
movq %rbx, 0x50(%rsp)
jae 0x170f51
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, 0x8(%rbp)
movq 0x10(%rbp), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbp)
movq 0x8(%r14), %rax
movq 0xa8(%rsp), %rbx
movq 0x18(%r14), %rdx
cmpq %rax, 0x28(%r14)
jae 0x170fb1
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
incq %rcx
movq %rcx, 0x28(%r14)
imulq $0x1c, %rcx, %rsi
movq %rdx, %rdi
callq 0x2370
movq %rax, %rdx
movq %rax, 0x18(%r14)
movq 0x28(%r12), %rax
movq 0x58(%r14), %rcx
movq 0x10(%rbp), %rsi
movq (%r13), %rdi
movq %rdi, 0x38(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq 0xa0(%rsp), %rbp
movq %rbp, (%rsp)
andl $0x0, 0x40(%rsp)
movq $0x4b, 0x8(%rsp)
pushq $0x10
popq %r9
movsd 0x72534(%rip), %xmm0    # 0x1e3538
movsd 0x724b4(%rip), %xmm1    # 0x1e34c0
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rcx
movq %rbx, %r8
callq 0x16ff6c
testq %rax, %rax
je 0x1711a6
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x1711a6
movq (%r14), %rcx
leaq -0x1(%rax), %r12
imulq $0x1c, %r12, %rsi
addq %rcx, %rsi
cmpq $0x1, %rax
jg 0x171065
movl 0x14(%rsi), %ecx
subl 0x10(%rsi), %ecx
xorl %eax, %eax
cmpl $0x2, %ecx
movq 0xb0(%rsp), %r13
jae 0x1711a8
jmp 0x171295
movl 0x8(%rsi), %edx
subl 0x4(%rsi), %edx
cvtsi2sd %rdx, %xmm0
cvtsi2sd %rbp, %xmm1
mulsd 0x7252b(%rip), %xmm1    # 0x1e35a8
ucomisd %xmm0, %xmm1
movq 0xb0(%rsp), %r13
jae 0x1711a6
movl %edx, %edx
cmpq %rbp, %rdx
jge 0x1711b7
imulq $0x1c, %rax, %rdx
leaq (%rdx,%rcx), %r9
addq $0x8, %r9
xorl %edi, %edi
movsd 0x7c0a8(%rip), %xmm0    # 0x1ed158
movsd 0x7e310(%rip), %xmm1    # 0x1ef3c8
xorl %r8d, %r8d
movq 0x50(%rsp), %rdx
testq %r12, %r12
js 0x17129f
movq %r8, 0x58(%rsp)
imulq $0x1c, %r12, %r8
addq %rcx, %r8
movq %r9, 0x60(%rsp)
movq %r12, %r10
incq %r10
cmpq %rax, %r10
jae 0x171170
movl -0x4(%r9), %esi
movl (%r9), %r13d
movl 0x4(%r8), %edx
movl 0x8(%r8), %r11d
cmpl %r11d, %r13d
movl %r11d, %ebx
cmovbl %r13d, %ebx
cmpl %edx, %esi
movl %edx, %ebp
cmoval %esi, %ebp
subl %ebp, %ebx
cmovbl %edi, %ebx
cvtsi2sd %rbx, %xmm2
subl %esi, %r13d
cvtsi2sd %r13, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x17113c
cmpl $0x20, %ebx
ja 0x1711c6
cmpl %r13d, %ebx
je 0x1711c6
subl %edx, %r11d
jmp 0x171161
subl %edx, %r11d
cvtsi2sd %r11, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
seta %bpl
cmpl $0x21, %ebx
setae %sil
cmpl %r13d, %ebx
je 0x1711c6
andb %bpl, %sil
jne 0x1711c6
addq $0x1c, %r9
cmpl %r11d, %ebx
jne 0x1710dd
jmp 0x1711c6
movl 0x14(%r8), %edx
subl 0x10(%r8), %edx
movq 0x58(%rsp), %r8
addq %rdx, %r8
decq %r12
movq 0x60(%rsp), %r9
addq $-0x1c, %r9
movq 0xb0(%rsp), %r13
movq 0x50(%rsp), %rdx
movq 0xa0(%rsp), %rbp
jmp 0x1710c0
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x14(%rsi), %r8d
subl 0x10(%rsi), %r8d
movq 0x50(%rsp), %rdx
jmp 0x1711e3
incq %r12
movq 0xb0(%rsp), %r13
movq 0x50(%rsp), %rdx
movq 0xa0(%rsp), %rbp
movq 0x58(%rsp), %r8
cmpq 0x10(%r15), %r8
jne 0x1711ee
pushq $0x1
popq %rax
jmp 0x1711a8
xorl %edi, %edi
movq %rbp, %rsi
callq 0x1c867
movq %rax, %rbx
movq 0x58(%r14), %r8
cmpq %rax, 0x50(%r14)
jae 0x17125b
movq %rbx, %rax
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(%r14)
leaq 0x8(,%rcx,8), %rsi
movq %r8, %rdi
callq 0x2370
movq %rax, %r8
movq %rax, 0x58(%r14)
imulq $0x1c, %r12, %rsi
addq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x18(%r14), %rcx
subq %r12, %rdx
movq %rbp, 0x8(%rsp)
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
movq %rbx, %r9
callq 0x1709f9
movq %rax, %rcx
pushq $0x1
popq %rax
testq %rcx, %rcx
je 0x1711a8
andl $0x0, (%r13)
jmp 0x1711a8
xorl %r12d, %r12d
jmp 0x1711e3