// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: gen_max_gchain_adv(void*, ul_idx_t const*, long, st_mt_t*, vec_mg_lchain_t*, kv_ul_ov_t*, kv_ul_ov_t*, long, float, float, float, unsigned long, asg_t const*, st_mt_t*, vec_sp_node_t*, vec_mg_pathv_t*, unsigned long*, vec_mg_lchain_t*)
uint32_t gen_max_gchain_adv(void *km, const ul_idx_t *uref, int64_t ulid, st_mt_t *idx, vec_mg_lchain_t *e, kv_ul_ov_t *raw_idx, kv_ul_ov_t *raw_chn,
int64_t qlen, float primary_cov_rate, float primary_fragment_cov_rate, float primary_fragment_second_score_rate, uint64_t mini_primary_fragment_len, 
const asg_t *g, st_mt_t *dst_done, vec_sp_node_t *out, vec_mg_pathv_t *res, uint64_t *b, vec_mg_lchain_t *gchains)
{
	gchains->n = 0;
	if(idx->n <= 0) return 0;
	int64_t a_n, idx_n = idx->n, i, k, is_done = 0, n_mchain = 0; uint64_t om, ok, ovlp, novlp; 
	ul_ov_t *m = NULL, *p = NULL; mg_lchain_t *a = e->a, *g_item; int64_t raw_idx_n = raw_idx->n;
	ul_ov_t *gb = NULL; int64_t gb_n = 0;
	for (i = a_n = 0; i < idx_n; ++i) {
		kv_pushp(ul_ov_t, *raw_idx, &p);
		p->qn = ((int64_t)(idx->a[i]>>32));//score
		p->ts = a_n; p->te = a_n + ((uint32_t)idx->a[i]);
		p->qs = a[p->ts].qs; p->qe = a[p->te-1].qe; p->tn = 0;//(tn = 1) -> normal; (t = 0) -> duplicated chain
		a_n += ((uint32_t)idx->a[i]);
		// fprintf(stderr, "[M::%s::i->%ld] score->%u, qs->%u, qe->%u, occ->%u\n", __func__, i, p->qn, p->qs, p->qe, p->te-p->ts); 
	}
	gb = raw_idx->a + raw_idx_n; gb_n = raw_idx->n - raw_idx_n;
	radix_sort_ul_ov_srt_qn(gb, gb + gb_n);//sort by scores
	m = &(gb[gb_n-1]);///max chain
	// assert(a[m.ts].qs<=a[m.te-1].qs && a[m.te-1].qe>=a[m.ts].qe);
	// print_chain(a + m.ts, m.te - m.ts);
	///for debug
	// dedup_second_chain(uref, idx->a, idx_n, m.ts, m.te, a, raw_idx, raw_chn, b, bw, diff_ec_ul);
	// debug_ll_chains(uref, idx->a, idx_n, m.ts, m.te, a, raw_idx, raw_chn, b, bw, diff_ec_ul, qlen);

	if((m->qe - m->qs) > (qlen*primary_cov_rate)) {
		is_done = 1; m->tn = 1; n_mchain = 1;
	}

	if(is_done == 0) {
		for (i = gb_n - 2; i >= 0; i--) {///from the second best chain
			p = &(gb[i]);
			ovlp = ((MIN(m->qe, p->qe) > MAX(m->qs, p->qs))? (MIN(m->qe, p->qe) - MAX(m->qs, p->qs)):0);
			novlp = (p->qe - p->qs) - ovlp;
			if(novlp > ((m->qe-m->qs)*GC_OFFSET_RATE) && novlp > GC_OFFSET_POS) break;
		}
		if(i < 0) { ///no alignment that is on the left or the right side of the primary chain
			is_done = 2; m->tn = 1; n_mchain = 1;
		}
	}

	if(is_done == 0) {
		if(((m->qe - m->qs) > (qlen*primary_fragment_cov_rate)) || ((m->qe - m->qs) > mini_primary_fragment_len)) {
			// dedup_second_chain(uref, idx->a, idx_n, m.ts, m.te, a, raw_idx, raw_chn, b, bw, diff_ec_ul, qlen);
			if(raw_chn && raw_idx) dedup_second_chain_adv(uref, gb, gb_n, a, raw_idx, raw_chn, b, qlen, ulid);
			for (k = gb_n-1, n_mchain = 0; k >= 0; k--) {
				m = &(gb[k]);///max chain
				// fprintf(stderr, "++[M::%s::k->%ld] score->%u, qs->%u, qe->%u\n", __func__, k, m->qn, m->qs, m->qe); 
				if(((m->qe - m->qs) <= (qlen*primary_fragment_cov_rate)) && 
															((m->qe - m->qs) <= mini_primary_fragment_len)) break;
				om = m->qe - m->qs;
				for (i = gb_n-1; i >= 0; i--) {
					if(i == k) continue;
					p = &(gb[i]);
					// fprintf(stderr, "--[M::%s::i->%ld] score->%u, qs->%u, qe->%u\n", __func__, i, p->qn, p->qs, p->qe); 
					ovlp = ((MIN(m->qe, p->qe) > MAX(m->qs, p->qs))? (MIN(m->qe, p->qe) - MAX(m->qs, p->qs)):0);
					if(ovlp == 0) continue;
					ok = p->qe - p->qs;
					if(p->tn == 1 && ((ovlp > ok*0.1) || (ovlp > om*0.1))) break;
					if(ok > om) ok = om;
					if((ovlp > ok*0.1) && (p->qn > (m->qn*primary_fragment_second_score_rate))) break;
				}
				if(i < 0) {
					is_done = 3; m->tn = 1; n_mchain++;
				} else {
					 break;
				 }
			}
		}
	}

	if(is_done) {
		gchains->n = 0;
		for (k = gb_n - n_mchain; k < gb_n; k++) {
			kv_pushp(mg_lchain_t, *gchains, &g_item);
			g_item->v = (uint32_t)-1; 
			g_item->qs = gb[k].qs; g_item->qe = gb[k].qe; 
			g_item->rs = gb[k].ts; g_item->re = gb[k].te; 
			g_item->cnt = g_item->off = 0;
			gen_gchain_track(km, a + g_item->rs, g_item->re - g_item->rs, g, dst_done, out, res);
			kv_resize(mg_lchain_t, *gchains, gchains->n + res->n); ///a = gchains->a + gchains->n;
			for (i = 0, g_item = &(gchains->a[gchains->n-1]); i < ((int64_t)res->n); i++) {
				if(res->a[i].v == (uint32_t)-1) {
					gchains->a[i+gchains->n] = a[res->a[i].pre + g_item->rs]; 
					gchains->a[i+gchains->n].dist_pre = res->a[i].d;

					// fprintf(stderr, "+[M::%s::]\tutg%.6dl(%c)\n", __func__, 
					// (int32_t)(gchains->a[i+gchains->n].v>>1)+1, "+-"[gchains->a[i+gchains->n].v&1]);
				} else {
					gchains->a[i+gchains->n].v = res->a[i].v; 
					gchains->a[i+gchains->n].off = -1; 
					gchains->a[i+gchains->n].dist_pre = res->a[i].d;
					///the nodes detected by the graph chaining should be fully covered
					gchains->a[i+gchains->n].rs = 0;
					gchains->a[i+gchains->n].re = uref->ug->g->seq[res->a[i].v>>1].len;
					// fprintf(stderr, "aaaaaaa, ulid->%ld\n", ulid);
					// fprintf(stderr, "-[M::%s::]\tutg%.6dl(%c)\n", __func__, 
					// (int32_t)(gchains->a[i+gchains->n].v>>1)+1, "+-"[gchains->a[i+gchains->n].v&1]);
				}
			}
			g_item->cnt = res->n;
			gchains->n += res->n;
			// fprintf(stderr, "sbsbsbsb, ulid->%ld\n", ulid);
			// debug_gchain(km, g, gchains->a + gchains->n - res->n, res->n, dst_done, out);
		}
	}
	raw_idx->n = raw_idx_n;
	return n_mchain;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movd %xmm2, 0x20(%rsp)
movss %xmm1, 0x2c(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, 0x40(%rsp)
movq 0xc0(%rsp), %rax
movq $0x0, 0x8(%rax)
movq (%rcx), %rsi
testq %rsi, %rsi
je 0x1b366d
movq (%r8), %r12
movq 0x8(%r9), %rax
movq %r9, 0x10(%rsp)
movq %rax, 0x30(%rsp)
movss %xmm0, (%rsp)
jle 0x1b3674
movq %rcx, %rbp
movq (%r9), %r15
xorl %ebx, %ebx
xorl %r13d, %r13d
cmpq 0x10(%r9), %rax
jne 0x1b35fc
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%r9)
leaq (%rcx,%rcx,8), %rax
movq %rsi, %r14
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
movq %r15, %rdi
callq 0x2370
movq %r14, %rsi
movq 0x10(%rsp), %r9
movq %rax, %r15
movq %rax, (%r9)
movq 0x8(%r9), %rax
leaq 0x1(%rax), %r14
movq %r14, 0x8(%r9)
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
movq 0x10(%rbp), %rax
movq (%rax,%r13,8), %rdx
movq %rdx, %rax
shrq $0x20, %rax
movl %eax, (%r15,%rcx)
movl %ebx, 0x10(%r15,%rcx)
addq %rbx, %rdx
movl %edx, 0x14(%r15,%rcx)
movl %ebx, %eax
leaq (%rax,%rax,4), %rax
movl 0x14(%r12,%rax,8), %eax
movl %eax, 0x4(%r15,%rcx)
leal -0x1(%rdx), %eax
leaq (%rax,%rax,4), %rax
movl 0x18(%r12,%rax,8), %eax
movl %eax, 0x8(%r15,%rcx)
movl $0x0, 0xc(%r15,%rcx)
incq %r13
movq %r14, %rax
movq %rdx, %rbx
cmpq %r13, %rsi
jne 0x1b35ba
jmp 0x1b367a
xorl %eax, %eax
jmp 0x1b3a59
movq (%r9), %r15
movq %rax, %r14
movq 0x30(%rsp), %rcx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rbp
addq %rcx, %rbp
addq %r15, %rbp
movq %r14, %rbx
subq %rcx, %rbx
leaq (%r14,%r14,8), %rax
leaq (%rax,%rax,2), %r13
leaq (%r15,%r14), %rsi
addq %r13, %rsi
movq %rbp, %rdi
callq 0x19ead4
leaq -0x1(%rbx), %rdi
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
movq %rbx, 0x18(%rsp)
leaq -0x1(%rbx,%rax), %rcx
movl 0x4(%rbp,%rcx), %edx
movq %rbp, 0x8(%rsp)
movl 0x8(%rbp,%rcx), %esi
movq %rdi, %rbp
movl %esi, %edi
subl %edx, %edi
xorps %xmm0, %xmm0
cvtsi2ss %rdi, %xmm0
movq 0x88(%rsp), %rbx
cvtsi2ss %rbx, %xmm6
movss (%rsp), %xmm1
mulss %xmm6, %xmm1
ucomiss %xmm1, %xmm0
ja 0x1b3798
cmpq $0x2, 0x18(%rsp)
jl 0x1b3798
movl %edi, %eax
movq %rax, (%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x8c43b(%rip), %xmm1    # 0x23fb58
addq %r14, %r13
leaq (%r15,%r13), %r8
addq $-0x30, %r8
xorl %r10d, %r10d
movq 0x7db4d(%rip), %xmm2    # 0x231280
movapd 0x7db55(%rip), %xmm3    # 0x231290
movq %rbp, %r11
movl -0x4(%r8), %r9d
movl (%r8), %edi
cmpl %edi, %esi
movl %edi, %eax
cmovbl %esi, %eax
cmpl %r9d, %edx
movl %r9d, %r14d
cmoval %edx, %r14d
subl %r14d, %eax
cmovbl %r10d, %eax
subl %r9d, %edi
subq %rax, %rdi
movq %rdi, %xmm4
punpckldq %xmm2, %xmm4    # xmm4 = xmm4[0],xmm2[0],xmm4[1],xmm2[1]
subpd %xmm3, %xmm4
cmpq $0x9, %rdi
jb 0x1b378c
movapd %xmm4, %xmm5
unpckhpd %xmm4, %xmm5            # xmm5 = xmm5[1],xmm4[1]
addsd %xmm4, %xmm5
ucomisd %xmm1, %xmm5
ja 0x1b3a68
decq %r11
addq $-0x1c, %r8
testq %r11, %r11
jg 0x1b373e
addq 0x8(%rsp), %rcx
movl $0x1, 0xc(%rcx)
movl $0x1, %eax
movq 0xc0(%rsp), %rsi
movq $0x0, 0x8(%rsi)
movq 0xb0(%rsp), %r15
movq 0x18(%rsp), %r13
movq %rax, 0x20(%rsp)
subq %rax, %r13
xorl %ecx, %ecx
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq (%rsi), %rax
cmpq 0x10(%rsi), %rcx
jne 0x1b3811
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rsi)
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rsi
movq %rax, %rdi
callq 0x2370
movq 0xc0(%rsp), %rsi
movq %rax, (%rsi)
movq 0x8(%rsi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rsi)
leaq (%rcx,%rcx,4), %rcx
movl $0xffffffff, 0x8(%rax,%rcx,8) # imm = 0xFFFFFFFF
leaq (,%r13,8), %rdx
addq %r13, %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r13, %rdx
movq 0x8(%rsp), %rdi
movq 0x4(%rdi,%rdx), %xmm0
movq 0x10(%rdi,%rdx), %rdx
movq %rdx, %xmm1
pshufd $0x55, %xmm1, %xmm2     # xmm2 = xmm1[1,1,1,1]
psubd %xmm1, %xmm2
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqu %xmm1, 0xc(%rax,%rcx,8)
movl $0x0, (%rax,%rcx,8)
andl $0x80000000, 0x4(%rax,%rcx,8) # imm = 0x80000000
movslq %edx, %rax
leaq (%rax,%rax,4), %rax
movq %rsi, %r14
leaq (%r12,%rax,8), %rsi
movd %xmm2, %eax
movslq %eax, %rdx
subq $0x8, %rsp
movq 0x48(%rsp), %rdi
movq 0xa0(%rsp), %rcx
movq 0xa8(%rsp), %r8
movq 0xb0(%rsp), %r9
pushq %r15
callq 0x1b8b8c
addq $0x10, %rsp
movq (%r14), %rax
movq 0x8(%r14), %rsi
movq 0x8(%r15), %rdx
leaq (%rdx,%rsi), %rcx
cmpq %rcx, 0x10(%r14)
jae 0x1b391f
decq %rcx
movq %rcx, %rdx
shrq %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x2, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x4, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x8, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x10, %rdx
orq %rcx, %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x10(%r14)
leaq 0x8(,%rdx,8), %rcx
leaq (%rcx,%rcx,4), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r14)
movq 0x8(%r14), %rsi
movq 0x8(%r15), %rdx
leaq (%rsi,%rsi,4), %rcx
leaq (%rax,%rcx,8), %rcx
testq %rdx, %rdx
jle 0x1b3a0f
movq (%r15), %r10
movl $0x4, %edi
movl $0x20, %r8d
xorl %r9d, %r9d
movl -0x4(%r10,%rdi), %r11d
cmpq %rbx, %r11
je 0x1b3999
leaq (%rsi,%rsi,4), %rbp
leaq (%rax,%rbp,8), %rbp
movl %r11d, -0x18(%r8,%rbp)
movl $0xffffffff, -0x20(%r8,%rbp) # imm = 0xFFFFFFFF
movl (%r10,%rdi), %r14d
movl %r14d, (%r8,%rbp)
movl $0x0, -0x14(%r8,%rbp)
movq 0x38(%rsp), %r14
movq (%r14), %r14
movq 0x18(%r14), %r14
movq 0x20(%r14), %r14
shrl %r11d
movl (%r14,%r11,8), %r11d
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
andl %r14d, %r11d
movl %r11d, -0x10(%r8,%rbp)
jmp 0x1b39fb
movslq 0x4(%r10,%rdi), %rdx
movslq -0x1c(%rcx), %r10
addq %rdx, %r10
leaq (%r10,%r10,4), %rdx
leaq (%rsi,%rsi,4), %rsi
leaq (%rax,%rsi,8), %rax
movq 0x20(%r12,%rdx,8), %rsi
movq %rsi, (%r8,%rax)
movdqu (%r12,%rdx,8), %xmm0
movdqu 0x10(%r12,%rdx,8), %xmm1
movdqu %xmm1, -0x10(%r8,%rax)
movdqu %xmm0, -0x20(%r8,%rax)
movq (%r15), %r10
movl (%r10,%rdi), %edx
movq 0xc0(%rsp), %rsi
movq (%rsi), %rax
movq 0x8(%rsi), %rsi
leaq (%rsi,%rsi,4), %r11
leaq (%rax,%r11,8), %r11
movl %edx, (%r8,%r11)
movq 0x8(%r15), %rdx
incq %r9
addq $0xc, %rdi
addq $0x28, %r8
cmpq %rdx, %r9
jl 0x1b3941
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl -0x24(%rcx), %eax
movl $0x80000000, %esi       # imm = 0x80000000
andl %esi, %eax
orl %edx, %eax
movl %eax, -0x24(%rcx)
movq 0xc0(%rsp), %rsi
movq 0x8(%rsi), %rcx
addq 0x8(%r15), %rcx
movq %rcx, 0x8(%rsi)
incq %r13
cmpq 0x18(%rsp), %r13
movq 0x10(%rsp), %rdx
jl 0x1b37d5
movq 0x20(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0x8(%rdx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
mulss 0x2c(%rsp), %xmm6
ucomiss %xmm6, %xmm0
setbe %al
movq (%rsp), %rcx
cmpq 0x90(%rsp), %rcx
setbe %cl
testb %cl, %al
jne 0x1b3c1f
movq 0x80(%rsp), %r9
testq %r9, %r9
je 0x1b3ad0
subq $0x10, %rsp
movq 0x48(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %r12, %rcx
movq 0x20(%rsp), %r8
pushq %rbx
pushq 0xd0(%rsp)
movss %xmm6, 0x20(%rsp)
callq 0x1d28ca
movss 0x20(%rsp), %xmm6
addq $0x20, %rsp
leaq (%r15,%r13), %rbx
addq $-0x10, %rbx
movb $0x1, %cl
xorl %eax, %eax
movsd 0x7d12c(%rip), %xmm0    # 0x230c10
leaq (,%rbp,8), %rdx
addq %rbp, %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rbp, %rdx
movq 0x8(%rsp), %rdi
movl 0x4(%rdi,%rdx), %esi
movl 0x8(%rdi,%rdx), %edi
movl %edi, %r8d
subl %esi, %r8d
xorps %xmm1, %xmm1
cvtsi2ss %r8, %xmm1
ucomiss %xmm1, %xmm6
setae %r9b
cmpq 0x90(%rsp), %r8
setbe %r10b
testb %r10b, %r9b
jne 0x1b3c2b
movl %r8d, %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9, %xmm1
addq 0x8(%rsp), %rdx
mulsd %xmm0, %xmm1
movq %rbx, %r9
xorl %r10d, %r10d
movq %rax, %r11
addq %r10, %r11
je 0x1b3be3
movl -0x8(%r9), %r14d
movl -0x4(%r9), %r11d
cmpl %r11d, %edi
movl %r11d, %r15d
cmovbl %edi, %r15d
cmpl %r14d, %esi
movl %r14d, %r13d
cmoval %esi, %r13d
subl %r13d, %r15d
jbe 0x1b3be3
subl %r14d, %r11d
cmpl $0x1, (%r9)
xorps %xmm2, %xmm2
cvtsi2sd %r15, %xmm2
jne 0x1b3ba8
movl %r11d, %r14d
xorps %xmm3, %xmm3
cvtsi2sd %r14, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
ja 0x1b3c2b
ucomisd %xmm1, %xmm2
ja 0x1b3c2b
cmpl %r8d, %r11d
cmovael %r8d, %r11d
xorps %xmm3, %xmm3
cvtsi2sd %r11, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x1b3be3
movl -0xc(%r9), %r11d
xorps %xmm2, %xmm2
cvtsi2ss %r11, %xmm2
movl (%rdx), %r11d
xorps %xmm3, %xmm3
cvtsi2ss %r11, %xmm3
mulss 0x20(%rsp), %xmm3
ucomiss %xmm3, %xmm2
ja 0x1b3c2b
movq 0x18(%rsp), %r11
addq %r10, %r11
decq %r11
decq %r10
addq $-0x1c, %r9
testq %r11, %r11
jg 0x1b3b47
movl $0x1, 0xc(%rdx)
incq %rax
leaq -0x1(%rbp), %rdx
xorl %ecx, %ecx
testq %rbp, %rbp
movq %rdx, %rbp
jg 0x1b3ae4
jmp 0x1b37a9
xorl %eax, %eax
movq 0x10(%rsp), %rdx
jmp 0x1b3a50
testb $0x1, %cl
movq 0x10(%rsp), %rdx
jne 0x1b3a50
movq 0xc0(%rsp), %rsi
movq $0x0, 0x8(%rsi)
testq %rax, %rax
jne 0x1b37b9
jmp 0x1b3a50