// File: /workspace/github_cpp_makefile_star2_O1/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, 0x60(%rsp)
movq %r8, %r13
movq %rcx, %r15
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %r14
movq 0xb0(%rsp), %rax
movl $0x1, (%rax)
xorl %ebx, %ebx
movq %rbx, 0x8(%rcx)
movq %rbx, 0x20(%rcx)
movq (%rdi), %rsi
movq 0x10(%rdi), %rdi
callq 0x194745
movq %r13, %rdi
movq %r14, 0x50(%rsp)
movq %r14, %rsi
movq %r15, %rdx
callq 0x1b198e
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x1b1eab
cmpq %rax, 0x50(%r15)
jae 0x1b1bd7
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(%r15)
movq 0x58(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x58(%r15)
movq 0x8(%r15), %rax
cmpq %rax, 0x8(%rbp)
jae 0x1b1c32
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 0xa8(%rsp), %rbx
movq 0xa0(%rsp), %r14
movq 0x8(%r15), %rax
cmpq %rax, 0x28(%r15)
jae 0x1b1ca4
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, 0x28(%r15)
movq 0x18(%r15), %rdi
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax), %rsi
incq %rsi
callq 0x2370
movq %rax, 0x18(%r15)
movq 0x28(%r12), %rax
movq 0x18(%r15), %rdx
movq 0x58(%r15), %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 %r14, (%rsp)
movl $0x0, 0x40(%rsp)
movq $0x4b, 0x8(%rsp)
movsd 0x737f6(%rip), %xmm0    # 0x2254e8
movsd 0x73776(%rip), %xmm1    # 0x225470
movl $0x10, %r9d
movq 0x50(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rcx
movq %rbx, %r8
callq 0x1b0b1d
testq %rax, %rax
je 0x1b1ea9
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x1b1ea9
movq (%r15), %rdx
leaq -0x1(%rax), %r12
leaq (%r12,%r12,8), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx), %rsi
decq %rsi
movq %rdx, 0x58(%rsp)
addq %rdx, %rsi
cmpq $0x1, %rax
jg 0x1b1d63
movl 0x14(%rsi), %eax
subl 0x10(%rsi), %eax
xorl %ebx, %ebx
cmpl $0x2, %eax
jae 0x1b1eab
jmp 0x1b1f83
movl 0x8(%rsi), %edx
subl 0x4(%rsi), %edx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r14, %xmm1
mulsd 0x737d7(%rip), %xmm1    # 0x225558
ucomisd %xmm0, %xmm1
jae 0x1b1ea9
movl %edx, %ecx
cmpq %r14, %rcx
jge 0x1b1ebd
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
movq 0x58(%rsp), %rdx
leaq (%rdx,%rcx), %rsi
addq $0x8, %rsi
xorl %edi, %edi
movsd 0x7d260(%rip), %xmm0    # 0x22f018
movsd 0x7f4f8(%rip), %xmm1    # 0x2312b8
xorl %edx, %edx
leaq (%r12,%r12,8), %rcx
leaq (%rcx,%rcx,2), %r8
addq %r12, %r8
addq 0x58(%rsp), %r8
movq %rsi, %r10
movq %r12, %r9
incq %r9
cmpq %rax, %r9
jae 0x1b1e75
movl -0x4(%r10), %ecx
movl (%r10), %ebp
movl 0x4(%r8), %r11d
movl 0x8(%r8), %ebx
cmpl %ebx, %ebp
movl %ebx, %r13d
cmovbl %ebp, %r13d
cmpl %r11d, %ecx
movl %r11d, %r14d
cmoval %ecx, %r14d
subl %r14d, %r13d
cmovbl %edi, %r13d
xorps %xmm2, %xmm2
cvtsi2sd %r13, %xmm2
subl %ecx, %ebp
xorps %xmm3, %xmm3
cvtsi2sd %rbp, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x1b1e36
cmpl $0x20, %r13d
ja 0x1b1e70
cmpl %ebp, %r13d
jne 0x1b1e60
jmp 0x1b1e70
movl %ebx, %ecx
subl %r11d, %ecx
xorps %xmm3, %xmm3
cvtsi2sd %rcx, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
seta %cl
cmpl $0x21, %r13d
setae %r14b
cmpl %ebp, %r13d
je 0x1b1e70
andb %cl, %r14b
jne 0x1b1e70
addq $0x1c, %r10
subl %r11d, %ebx
cmpl %ebx, %r13d
jne 0x1b1dd8
cmpq %rax, %r9
jb 0x1b1e9c
movl 0x14(%r8), %ecx
subl 0x10(%r8), %ecx
addq %rcx, %rdx
leaq -0x1(%r12), %rcx
addq $-0x1c, %rsi
testq %r12, %r12
movq %rcx, %r12
jg 0x1b1dc2
movq $-0x1, %r12
incq %r12
movq 0xa0(%rsp), %r14
jmp 0x1b1ec3
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x14(%rsi), %edx
subl 0x10(%rsi), %edx
movl $0x1, %ebx
movq 0x50(%rsp), %rax
cmpq 0x10(%rax), %rdx
je 0x1b1eab
xorl %edi, %edi
movq %r14, %rsi
movq 0x60(%rsp), %r13
movq %r13, %rdx
callq 0x49869
movq %rax, %rbx
cmpq %rax, 0x50(%r15)
jae 0x1b1f42
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(%r15)
movq 0x58(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x58(%r15)
leaq (%r12,%r12,8), %rax
leaq (%rax,%rax,2), %rsi
addq %r12, %rsi
addq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0x18(%r15), %rcx
subq %r12, %rdx
movq 0x58(%r15), %r8
movq %r14, 0x8(%rsp)
movq %r13, (%rsp)
movq 0x50(%rsp), %rdi
movq %rbx, %r9
callq 0x1b1650
movl $0x1, %ebx
testq %rax, %rax
je 0x1b1eab
movq 0xb0(%rsp), %rax
movl $0x0, (%rax)
jmp 0x1b1eab