// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: dump_all_chain_simple(kv_ul_ov_t*, kv_ul_ov_t*, long, long, float, float, float, float, float, unsigned long)
int64_t dump_all_chain_simple(kv_ul_ov_t *idx, kv_ul_ov_t *ax, int64_t ax_new_occ, int64_t qlen, 
float primary_cov_rate, float fragement_cov_rate, float primary_fragment_cov_rate, 
float primary_fragment_second_score_rate, float trans_thres, uint64_t mini_primary_fragment_len) 
{
	if(idx->n <= 0) return 0;
	ul_ov_t *m = &(idx->a[idx->n-1]); //largest chain
	ul_ov_t *a = ax->a + ax->n; int64_t k, z, l, idx_n = idx->n, ovlp, om, ok, ff = 0;
	// fprintf(stderr, "[M::%s] m->score:%u, m->qs:%u, m->qe:%u, chain_n:%u\n", __func__, m->qn, m->qs, m->qe, m->te-m->ts);
	if(((m->qe-m->qs) > (qlen*primary_cov_rate)) && 
						(check_trans_rate(a+m->ts, m->te-m->ts, trans_thres))) { ///found a primary chain
		for (k = m->ts, l = 0; k < m->te; k++) {
			a[l] = a[k]; a[l].tn |= ((uint32_t)(0x80000000)); a[l].el = 1;
			l++;
		}
		ax->n += l; ff = 1;
	} else {
		if((((m->qe-m->qs) > (qlen*primary_fragment_cov_rate)) || ((m->qe - m->qs) > mini_primary_fragment_len)) 
		&& (check_trans_rate(a+m->ts, m->te-m->ts, trans_thres))) {
			om = m->qe - m->qs;
			for (k = 0; k < idx_n-1; k++) {
				ovlp = ((MIN((m->qe), (idx->a[k].qe)) > MAX((m->qs), (idx->a[k].qs)))? 
										(MIN((m->qe), (idx->a[k].qe)) - MAX((m->qs), (idx->a[k].qs))):0);
				if(ovlp == 0) continue;
				ok = idx->a[k].qe - idx->a[k].qs;
				if(ok > om ) ok = om;
				if((ovlp > ok*0.1/**0.25**/) && idx->a[k].qn > (m->qn*primary_fragment_second_score_rate)) break;
			}

			if(k >= idx_n-1) {
				for (k = m->ts; k < m->te; k++) {
					// if(a[k].el) a[k].tn |= ((uint32_t)(0x80000000));
					a[k].tn |= ((uint32_t)(0x80000000));
				}
				ff = 1;
			}
		}

		radix_sort_ul_ov_srt_qe(idx->a, idx->a + idx->n);
		for (k = 0; k < idx_n; k++) {
			if(k < idx_n-1 && idx->a[k].qe > idx->a[k+1].qs) break;//not one chain
			if((idx->a[k].qe - idx->a[k].qs) > (qlen*fragement_cov_rate)) {///large enough fragements
				if(!check_trans_rate(a+idx->a[k].ts, idx->a[k].te-idx->a[k].ts, trans_thres)) break;
			}
		}

		if(k == idx_n) {///only if there is a clear chain (with holes)
			for (k = 0; k < idx_n; k++) {
				if((idx->a[k].qe - idx->a[k].qs) <= (qlen*fragement_cov_rate)) continue;
				for (z = idx->a[k].ts; z < idx->a[k].te; z++) {
					// if(a[z].el) a[z].tn |= ((uint32_t)(0x80000000));
					a[z].tn |= ((uint32_t)(0x80000000));
				}
				ff = 1;
			}
		} 
		/**
		for (k = 0, l = 0; k < ax_new_occ; k++) {
			if(!(a[k].el)) continue;
			a[l] = a[k]; l++;
		}

		radix_sort_ul_ov_srt_qe(a, a + l);
		ax->n += l;
		**/
		for (k = 0, l = 0; k < ax_new_occ; k++) {
			if(a[k].el || (a[k].tn&((uint32_t)(0x80000000)))) {
				a[l] = a[k]; l++;
			}
		}
		ax_new_occ = l;

		radix_sort_ul_ov_srt_qe(a, a + ax_new_occ);
		for (k = 1, l = 0; k <= ax_new_occ; k++) {
			if (k == ax_new_occ || a[k].qe != a[l].qe) {
				if(k - l > 1) radix_sort_ul_ov_srt_qs(a+l, a+k);
				l = k;
			}
		}
		ax->n += ax_new_occ;
	}
	return ff;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movss %xmm4, 0x14(%rsp)
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x164f8a
movq %rdx, %r12
movss %xmm1, 0x54(%rsp)
movq (%rdi), %r9
movq %rax, 0x18(%rsp)
decq %rax
movq %rax, 0x68(%rsp)
imulq $0x1c, %rax, %rax
imulq $0x1c, 0x8(%rsi), %rdx
leaq (%r9,%rax), %r11
movl 0x4(%r9,%rax), %r10d
movl 0x8(%r9,%rax), %ebx
movl %ebx, %eax
movl %r10d, 0x50(%rsp)
subl %r10d, %eax
movq %rax, 0x58(%rsp)
cvtsi2ss %rax, %xmm4
movq %rsi, 0x70(%rsp)
movq (%rsi), %rsi
cvtsi2ss %rcx, %xmm1
movq %rdx, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
leaq (%rsi,%rdx), %r14
mulss %xmm1, %xmm0
ucomiss %xmm0, %xmm4
movq %rdi, 0x60(%rsp)
movss %xmm1, 0xc(%rsp)
jbe 0x164e93
movl 0x10(%r11), %r15d
movl 0x14(%r11), %ebp
imulq $0x1c, %r15, %r13
leaq (%r14,%r13), %rdi
movl %ebp, %esi
subl %r15d, %esi
movss 0x14(%rsp), %xmm0
movq %r9, (%rsp)
movq %r11, 0x30(%rsp)
movss %xmm3, 0x10(%rsp)
movq %r8, 0x28(%rsp)
movss %xmm2, 0x20(%rsp)
movss %xmm4, 0x4c(%rsp)
callq 0x1645d6
movss 0x4c(%rsp), %xmm4
movss 0x20(%rsp), %xmm2
movq 0x28(%rsp), %r8
movss 0x10(%rsp), %xmm3
movq 0x30(%rsp), %r11
movq (%rsp), %r9
movss 0xc(%rsp), %xmm1
testl %eax, %eax
je 0x164e93
xorl %ebx, %ebx
pushq $0x1
popq %rax
movq %rax, (%rsp)
leaq (%r15,%rbx), %rax
movl %ebp, %ecx
cmpq %rcx, %rax
jae 0x1651b8
movups (%r14,%r13), %xmm0
movups 0xc(%r14,%r13), %xmm1
movups %xmm1, 0xc(%r14)
movups %xmm0, (%r14)
orb $-0x80, 0xf(%r14)
orb $0x40, 0x1b(%r14)
incq %rbx
movl 0x14(%r11), %ebp
addq $0x1c, %r14
jmp 0x164e59
mulss %xmm1, %xmm2
ucomiss %xmm2, %xmm4
setbe %al
movl 0x58(%rsp), %ecx
cmpq %r8, %rcx
setbe %cl
testb %cl, %al
jne 0x164f91
movss %xmm3, 0x10(%rsp)
movq %r9, %rbp
movl 0x10(%r11), %r15d
movq %r11, 0x30(%rsp)
movl 0x14(%r11), %esi
imulq $0x1c, %r15, %r13
leaq (%r14,%r13), %rdi
movq %rsi, 0x28(%rsp)
subl %r15d, %esi
movss 0x14(%rsp), %xmm0
callq 0x1645d6
testl %eax, %eax
je 0x164fd6
movq %r13, 0x20(%rsp)
movq $0x0, (%rsp)
movq 0x68(%rsp), %rcx
testq %rcx, %rcx
movl $0x0, %eax
cmovgq %rcx, %rax
movq %rbp, %r9
leaq 0x8(%rbp), %rcx
movsd 0x7dcdb(%rip), %xmm0    # 0x1e2bf0
movq 0x18(%rsp), %r13
movq 0x30(%rsp), %r10
movss 0x10(%rsp), %xmm3
movl 0x50(%rsp), %r11d
movq 0x58(%rsp), %rbp
subq $0x1, %rax
jb 0x164fa0
movl -0x4(%rcx), %edi
movl (%rcx), %edx
cmpl %edx, %ebx
movl %edx, %esi
cmovbl %ebx, %esi
cmpl %edi, %r11d
movl %edi, %r8d
cmoval %r11d, %r8d
subl %r8d, %esi
jbe 0x164f84
subl %edi, %edx
cmpl %ebp, %edx
cmovael %ebp, %edx
cvtsi2sd %rsi, %xmm1
cvtsi2sd %rdx, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
jbe 0x164f84
movl -0x8(%rcx), %edx
cvtsi2ss %rdx, %xmm1
movl (%r10), %edx
cvtsi2ss %rdx, %xmm2
mulss %xmm3, %xmm2
ucomiss %xmm2, %xmm1
ja 0x164fe6
addq $0x1c, %rcx
jmp 0x164f2f
xorl %eax, %eax
jmp 0x1651c5
movq $0x0, (%rsp)
movq 0x18(%rsp), %r13
jmp 0x164fe6
movq 0x20(%rsp), %rcx
addq 0x40(%rsp), %rcx
movq 0x38(%rsp), %rax
addq %rcx, %rax
addq $0xc, %rax
pushq $0x1
popq %rcx
movq 0x28(%rsp), %rdx
cmpq %rdx, %r15
jae 0x164fd0
orb $-0x80, 0x3(%rax)
incq %r15
addq $0x1c, %rax
jmp 0x164fbe
movq %rcx, (%rsp)
jmp 0x164fe6
movq $0x0, (%rsp)
movq 0x18(%rsp), %r13
movq %rbp, %r9
pushq $0x1
popq %r15
imulq $0x1c, %r13, %rsi
addq %r9, %rsi
movq %r9, %rdi
callq 0x156f5c
movss 0xc(%rsp), %xmm1
mulss 0x54(%rsp), %xmm1
xorl %ebx, %ebx
testq %r13, %r13
movl $0x0, %ebp
cmovgq %r13, %rbp
pushq $0x4
popq %r13
movq 0x60(%rsp), %r10
movss %xmm1, 0xc(%rsp)
cmpq %rbx, %rbp
je 0x165081
movq (%r10), %rax
movl 0x4(%rax,%r13), %ecx
cmpq 0x68(%rsp), %rbx
jge 0x16503d
cmpl 0x1c(%rax,%r13), %ecx
ja 0x165084
subl (%rax,%r13), %ecx
cvtsi2ss %rcx, %xmm0
ucomiss %xmm1, %xmm0
jbe 0x165078
movl 0xc(%rax,%r13), %ecx
movl 0x10(%rax,%r13), %esi
imulq $0x1c, %rcx, %rdi
addq %r14, %rdi
subl %ecx, %esi
movss 0x14(%rsp), %xmm0
callq 0x1645d6
movss 0xc(%rsp), %xmm1
movq 0x60(%rsp), %r10
testl %eax, %eax
je 0x165084
incq %rbx
addq $0x1c, %r13
jmp 0x165022
movq %rbp, %rbx
cmpq 0x18(%rsp), %rbx
jne 0x1650f7
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rcx
addq %rcx, %rax
addq $0xc, %rax
xorl %ecx, %ecx
pushq $0x1
popq %rdx
movq (%rsp), %r9
cmpq %rbp, %rcx
je 0x1650f3
movq (%r10), %rdi
imulq $0x1c, %rcx, %r8
movl 0x8(%rdi,%r8), %esi
subl 0x4(%rdi,%r8), %esi
cvtsi2ss %rsi, %xmm0
ucomiss %xmm0, %xmm1
jae 0x1650ee
leaq (%rdi,%r8), %rsi
movl 0x10(%rsi), %esi
movl 0x14(%rdi,%r8), %edi
imulq $0x1c, %rsi, %r8
addq %rax, %r8
cmpq %rdi, %rsi
jae 0x1650eb
orb $-0x80, 0x3(%r8)
incq %rsi
addq $0x1c, %r8
jmp 0x1650d8
movq %rdx, %r9
incq %rcx
jmp 0x1650a5
movq %r9, (%rsp)
xorl %ebx, %ebx
testq %r12, %r12
cmovleq %rbx, %r12
movq %r14, %rax
subq $0x1, %r12
jb 0x165134
testb $0x40, 0x1b(%rax)
jne 0x165115
cmpl $0x0, 0xc(%rax)
jns 0x16512e
imulq $0x1c, %rbx, %rcx
movups (%rax), %xmm0
movups 0xc(%rax), %xmm1
movups %xmm1, 0xc(%r14,%rcx)
movups %xmm0, (%r14,%rcx)
incq %rbx
addq $0x1c, %rax
jmp 0x165103
imulq $0x1c, %rbx, %rsi
addq %r14, %rsi
movq %r14, %rdi
callq 0x156f5c
xorl %eax, %eax
testq %rbx, %rbx
movl $0x0, %r13d
cmovgq %rbx, %r13
negq %r13
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %rdx
leaq (%rcx,%rdx), %r12
addq $0x1c, %r12
leaq (%r15,%r13), %rcx
cmpq $0x1, %rcx
je 0x1651b8
cmpq %r15, %rbx
je 0x16518b
movl 0x8(%r12), %ecx
imulq $0x1c, %rax, %rdx
cmpl 0x8(%r14,%rdx), %ecx
jne 0x16518b
movq %rax, %rcx
jmp 0x1651ac
movq %r15, %rdx
subq %rax, %rdx
movq %r15, %rcx
cmpq $0x2, %rdx
jl 0x1651ac
imulq $0x1c, %rax, %rdi
addq %r14, %rdi
movq %r12, %rsi
callq 0x1571fa
movq %r15, %rcx
incq %r15
addq $0x1c, %r12
movq %rcx, %rax
jmp 0x165167
movq 0x70(%rsp), %rax
addq %rbx, 0x8(%rax)
movq (%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq