// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %r9, 0x50(%rsp)
movq %r8, %r13
movq %rcx, %r15
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %r14
movq 0xa0(%rsp), %rax
movl $0x1, (%rax)
xorl %ebx, %ebx
movq %rbx, 0x8(%rcx)
movq %rbx, 0x20(%rcx)
movq (%rdi), %rsi
movq 0x10(%rdi), %rdi
callq 0x19d4ef
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1bb195
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x1bb69d
cmpq %rax, 0x50(%r15)
jae 0x1bb3df
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 0x1bb43a
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(%r15), %rax
movq 0x98(%rsp), %rbx
movq 0x18(%r15), %rdx
cmpq %rax, 0x28(%r15)
jae 0x1bb4a6
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)
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax), %rsi
incq %rsi
movq %rdx, %rdi
callq 0x2370
movq %rax, %rdx
movq %rax, 0x18(%r15)
movq 0x28(%r12), %rax
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 0x90(%rsp), %rbp
movq %rbp, (%rsp)
movl $0x0, 0x40(%rsp)
movq $0x4b, 0x8(%rsp)
movsd 0x760f0(%rip), %xmm0    # 0x2315e8
movsd 0x76070(%rip), %xmm1    # 0x231570
movl $0x10, %r9d
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rcx
movq %rbx, %r8
callq 0x1b9d75
testq %rax, %rax
je 0x1bb69b
movq 0x8(%r15), %rcx
testq %rcx, %rcx
je 0x1bb69b
movq (%r15), %rdx
leaq -0x1(%rcx), %rax
leaq (%rax,%rax,8), %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rcx,%rsi), %rdi
decq %rdi
addq %rdx, %rdi
cmpq $0x1, %rcx
jg 0x1bb562
movl 0x14(%rdi), %eax
subl 0x10(%rdi), %eax
xorl %ebx, %ebx
cmpl $0x2, %eax
jae 0x1bb69d
jmp 0x1bb78a
movl 0x8(%rdi), %esi
subl 0x4(%rdi), %esi
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rbp, %xmm1
mulsd 0x760d8(%rip), %xmm1    # 0x231658
ucomisd %xmm0, %xmm1
jae 0x1bb69b
movl %esi, %esi
cmpq %rbp, %rsi
jge 0x1bb6af
leaq (%rcx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rcx, %rsi
leaq (%rdx,%rsi), %r9
addq $0x8, %r9
movsd 0x7fcb8(%rip), %xmm0    # 0x23b268
movsd 0x81f50(%rip), %xmm1    # 0x23d508
xorl %esi, %esi
leaq (%rax,%rax,8), %r8
leaq (%r8,%r8,2), %r8
addq %rax, %r8
addq %rdx, %r8
movq %r9, 0x48(%rsp)
movq %rax, %r10
incq %r10
cmpq %rcx, %r10
jae 0x1bb667
movl -0x4(%r9), %r12d
movl (%r9), %ebp
movl 0x4(%r8), %edi
movl 0x8(%r8), %ebx
cmpl %ebx, %ebp
movl %ebx, %r13d
cmovbl %ebp, %r13d
cmpl %edi, %r12d
movl %edi, %r11d
cmoval %r12d, %r11d
subl %r11d, %r13d
jbe 0x1bb62b
xorps %xmm2, %xmm2
cvtsi2sd %r13, %xmm2
subl %r12d, %ebp
xorps %xmm3, %xmm3
cvtsi2sd %rbp, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x1bb647
cmpl $0x20, %r13d
jbe 0x1bb631
jmp 0x1bb6ba
subl %r12d, %ebp
xorl %r13d, %r13d
cmpl %ebp, %r13d
je 0x1bb6ba
addq $0x1c, %r9
subl %edi, %ebx
cmpl %ebx, %r13d
jne 0x1bb5d0
jmp 0x1bb6ba
movl %ebx, %r11d
subl %edi, %r11d
xorps %xmm3, %xmm3
cvtsi2sd %r11, %xmm3
mulsd %xmm1, %xmm3
cmpl $0x21, %r13d
jb 0x1bb631
ucomisd %xmm3, %xmm2
jbe 0x1bb631
jmp 0x1bb6ba
movl 0x14(%r8), %edi
subl 0x10(%r8), %edi
addq %rdi, %rsi
leaq -0x1(%rax), %rdi
movq 0x48(%rsp), %r9
addq $-0x1c, %r9
testq %rax, %rax
movq %rdi, %rax
movq 0x90(%rsp), %rbp
movl $0x0, %r13d
jg 0x1bb5ba
jmp 0x1bb6c8
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x14(%rdi), %esi
subl 0x10(%rdi), %esi
movq %rax, %r13
jmp 0x1bb6c8
incq %rax
movq %rax, %r13
movq 0x90(%rsp), %rbp
movl $0x1, %ebx
cmpq 0x10(%r14), %rsi
je 0x1bb69d
xorl %edi, %edi
movq %rbp, %rsi
movq 0x50(%rsp), %r12
movq %r12, %rdx
callq 0x48ea5
movq %rax, %rbx
movq 0x58(%r15), %r8
cmpq %rax, 0x50(%r15)
jae 0x1bb748
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)
leaq 0x8(,%rcx,8), %rsi
movq %r8, %rdi
callq 0x2370
movq %rax, %r8
movq %rax, 0x58(%r15)
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rsi
addq %r13, %rsi
addq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0x18(%r15), %rcx
subq %r13, %rdx
movq %rbp, 0x8(%rsp)
movq %r12, (%rsp)
movq %r14, %rdi
movq %rbx, %r9
callq 0x1ba8eb
movl $0x1, %ebx
testq %rax, %rax
je 0x1bb69d
movq 0xa0(%rsp), %rax
movl $0x0, (%rax)
jmp 0x1bb69d