// File: /workspace/github_cpp_makefile_star2_O3/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, 0xc(%rsp)
movq 0x8(%rdi), %rbx
testq %rbx, %rbx
je 0x1ad976
movq %rdx, %r12
movss %xmm1, 0x64(%rsp)
movq %rdi, 0x68(%rsp)
movq (%rdi), %r9
leaq -0x1(%rbx), %rax
movq %rax, 0x48(%rsp)
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
leaq -0x1(%rbx,%rax), %rax
leaq (%r9,%rax), %r11
movq (%rsi), %rdi
movq %rsi, 0x40(%rsp)
movq 0x8(%rsi), %rsi
leaq (%rsi,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
movl 0x4(%r9,%rax), %r10d
movl 0x8(%r9,%rax), %r13d
movl %r13d, %eax
movl %r10d, 0x60(%rsp)
subl %r10d, %eax
movq %rax, 0x70(%rsp)
xorps %xmm4, %xmm4
cvtsi2ss %rax, %xmm4
movq %rsi, 0x10(%rsp)
movq %rdi, 0x38(%rsp)
leaq (%rdi,%rsi), %r14
xorps %xmm1, %xmm1
cvtsi2ss %rcx, %xmm1
movq %rdx, 0x30(%rsp)
addq %rdx, %r14
mulss %xmm1, %xmm0
ucomiss %xmm0, %xmm4
movss %xmm1, 0x4(%rsp)
jbe 0x1ad839
movl 0x10(%r11), %r15d
movl 0x14(%r11), %ecx
leaq (%r15,%r15,8), %rax
leaq (%rax,%rax,2), %rbp
addq %r15, %rbp
leaq (%r14,%rbp), %rdi
movl %ecx, 0x54(%rsp)
movl %ecx, %esi
subl %r15d, %esi
movss 0xc(%rsp), %xmm0
movq %r9, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movss %xmm3, 0x8(%rsp)
movq %r8, 0x18(%rsp)
movss %xmm2, 0x5c(%rsp)
movss %xmm4, 0x58(%rsp)
callq 0x1ace38
movss 0x58(%rsp), %xmm4
movss 0x5c(%rsp), %xmm2
movq 0x18(%rsp), %r8
movss 0x8(%rsp), %xmm3
movq 0x28(%rsp), %r11
movq 0x20(%rsp), %r9
movss 0x4(%rsp), %xmm1
testl %eax, %eax
je 0x1ad839
cmpl 0x54(%rsp), %r15d
jae 0x1ad983
xorl %eax, %eax
movups (%r14,%rbp), %xmm0
movups 0xc(%r14,%rbp), %xmm1
movups %xmm1, 0xc(%r14)
movups %xmm0, (%r14)
orb $-0x80, 0xf(%r14)
orb $0x40, 0x1b(%r14)
movl 0x14(%r11), %ecx
leaq (%r15,%rax), %rdx
incq %rdx
incq %rax
addq $0x1c, %r14
cmpq %rcx, %rdx
jb 0x1ad7f6
movq 0x40(%rsp), %rcx
movq 0x8(%rcx), %rbx
jmp 0x1ad98f
mulss %xmm1, %xmm2
ucomiss %xmm2, %xmm4
setbe %al
movl 0x70(%rsp), %ecx
cmpq %r8, %rcx
setbe %cl
testb %cl, %al
jne 0x1ad97e
movss %xmm3, 0x8(%rsp)
movq %r9, %rbp
movl 0x10(%r11), %ecx
movq %r11, 0x28(%rsp)
movl 0x14(%r11), %r15d
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %rax
movq %rax, 0x18(%rsp)
leaq (%r14,%rax), %rdi
movl %r15d, %esi
movq %rcx, 0x20(%rsp)
subl %ecx, %esi
movss 0xc(%rsp), %xmm0
callq 0x1ace38
testl %eax, %eax
je 0x1ad99d
cmpq $0x0, 0x48(%rsp)
movq %rbp, %r9
movq 0x28(%rsp), %r10
movss 0x8(%rsp), %xmm3
movl 0x60(%rsp), %r11d
movq 0x70(%rsp), %rbp
jle 0x1ad933
leaq 0x8(%r9), %rax
movsd 0x83348(%rip), %xmm0    # 0x230c10
movq 0x48(%rsp), %rcx
movl -0x4(%rax), %edi
movl (%rax), %edx
cmpl %edx, %r13d
movl %edx, %esi
cmovbl %r13d, %esi
cmpl %edi, %r11d
movl %edi, %r8d
cmoval %r11d, %r8d
subl %r8d, %esi
jbe 0x1ad92a
subl %edi, %edx
cmpl %ebp, %edx
cmovael %ebp, %edx
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %rdx, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
jbe 0x1ad92a
movl -0x8(%rax), %edx
xorps %xmm1, %xmm1
cvtsi2ss %rdx, %xmm1
movl (%r10), %edx
xorps %xmm2, %xmm2
cvtsi2ss %rdx, %xmm2
mulss %xmm3, %xmm2
ucomiss %xmm2, %xmm1
ja 0x1ad97e
addq $0x1c, %rax
decq %rcx
jne 0x1ad8cd
movl $0x1, %r13d
movq 0x20(%rsp), %rax
cmpl %r15d, %eax
jae 0x1ad9a3
subq %rax, %r15
movq 0x30(%rsp), %rax
movq 0x10(%rsp), %rcx
addq %rcx, %rax
movq 0x18(%rsp), %rcx
addq %rax, %rcx
movq 0x38(%rsp), %rax
addq %rcx, %rax
addq $0xc, %rax
orb $-0x80, 0x3(%rax)
addq $0x1c, %rax
decq %r15
jne 0x1ad967
jmp 0x1ad9a3
xorl %r13d, %r13d
jmp 0x1adbe1
xorl %r13d, %r13d
jmp 0x1ad9a3
xorl %eax, %eax
movq 0x40(%rsp), %rcx
movq 0x10(%rsp), %rbx
addq %rax, %rbx
movl $0x1, %r13d
jmp 0x1adbdd
xorl %r13d, %r13d
movq %rbp, %r9
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rbx, %rsi
addq %r9, %rsi
movq %r9, %rdi
callq 0x19e2b4
testq %rbx, %rbx
jle 0x1adad5
movq %r13, %rbp
movss 0x4(%rsp), %xmm1
mulss 0x64(%rsp), %xmm1
movq 0x68(%rsp), %rax
movq (%rax), %r13
addq $0x4, %r13
xorl %r15d, %r15d
movss %xmm1, 0x4(%rsp)
movl 0x4(%r13), %eax
cmpq 0x48(%rsp), %r15
jge 0x1ad9f7
cmpl 0x1c(%r13), %eax
ja 0x1ada44
subl (%r13), %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
ucomiss %xmm1, %xmm0
jbe 0x1ada35
movl 0xc(%r13), %eax
movl 0x10(%r13), %esi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rdi
addq %rax, %rdi
addq %r14, %rdi
subl %eax, %esi
movss 0xc(%rsp), %xmm0
callq 0x1ace38
movss 0x4(%rsp), %xmm1
testl %eax, %eax
je 0x1ada44
incq %r15
addq $0x1c, %r13
cmpq %r15, %rbx
jne 0x1ad9e6
movq %rbx, %r15
testq %rbx, %rbx
setle %al
cmpq %rbx, %r15
setne %cl
orb %al, %cl
movq %rbp, %r13
jne 0x1adad5
movq 0x68(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rcx
movq 0x10(%rsp), %rdx
addq %rdx, %rcx
movq 0x38(%rsp), %rdx
addq %rdx, %rcx
addq $0xc, %rcx
xorl %edx, %edx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rdi
addq %rdx, %rdi
movl 0x8(%rax,%rdi), %esi
subl 0x4(%rax,%rdi), %esi
xorps %xmm0, %xmm0
cvtsi2ss %rsi, %xmm0
ucomiss %xmm0, %xmm1
jae 0x1adacd
leaq (%rax,%rdi), %rsi
movl 0x10(%rsi), %esi
movl 0x14(%rax,%rdi), %edi
movl $0x1, %r13d
cmpl %edi, %esi
jae 0x1adacd
subq %rsi, %rdi
leaq (%rsi,%rsi,8), %r8
leaq (%r8,%r8,2), %r8
addq %rcx, %rsi
addq %r8, %rsi
orb $-0x80, 0x3(%rsi)
addq $0x1c, %rsi
decq %rdi
jne 0x1adac0
incq %rdx
cmpq %rbx, %rdx
jne 0x1ada7a
testq %r12, %r12
jle 0x1adbc7
xorl %ebx, %ebx
movq %r14, %rax
testb $0x40, 0x1b(%rax)
jne 0x1adaef
cmpl $0x0, 0xc(%rax)
jns 0x1adb0f
leaq (%rbx,%rbx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rbx, %rcx
movups (%rax), %xmm0
movups 0xc(%rax), %xmm1
movups %xmm1, 0xc(%r14,%rcx)
movups %xmm0, (%r14,%rcx)
incq %rbx
addq $0x1c, %rax
decq %r12
jne 0x1adae3
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rbx, %rsi
addq %r14, %rsi
movq %r14, %rdi
callq 0x19e2b4
testq %rbx, %rbx
jle 0x1adbd4
movq %r13, %rbp
movq %rbx, %r15
negq %r15
movq 0x30(%rsp), %rcx
addq 0x10(%rsp), %rcx
movq 0x38(%rsp), %rax
leaq (%rax,%rcx), %r12
addq $0x1c, %r12
movl $0x1, %r13d
xorl %eax, %eax
movq %r15, %rcx
addq %r13, %rcx
je 0x1adb83
movl 0x8(%r12), %ecx
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
cmpl 0x8(%r14,%rdx), %ecx
jne 0x1adb83
movq %rax, %rcx
jmp 0x1adbab
movq %r13, %rdx
subq %rax, %rdx
movq %r13, %rcx
cmpq $0x2, %rdx
jl 0x1adbab
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rdi
addq %rax, %rdi
addq %r14, %rdi
movq %r12, %rsi
callq 0x19e55c
movq %r13, %rcx
leaq (%r15,%r13), %rdx
incq %rdx
incq %r13
addq $0x1c, %r12
movq %rcx, %rax
cmpq $0x1, %rdx
jne 0x1adb5f
movq %rbp, %r13
jmp 0x1adbd4
movq %r14, %rdi
movq %r14, %rsi
callq 0x19e2b4
xorl %ebx, %ebx
movq 0x40(%rsp), %rcx
addq 0x8(%rcx), %rbx
movq %rbx, 0x8(%rcx)
movq %r13, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq