// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movd %xmm2, 0x30(%rsp)
movss %xmm1, 0xc(%rsp)
movss %xmm0, 0x10(%rsp)
movq 0xd0(%rsp), %r15
andq $0x0, 0x8(%r15)
movq %rsi, 0x48(%rsp)
movq %rdi, 0x50(%rsp)
movq (%rcx), %r10
testq %r10, %r10
je 0x16a887
movq %rcx, %r14
movq 0xc0(%rsp), %rbx
movq (%r8), %r12
xorl %r13d, %r13d
testq %r10, %r10
cmovleq %r13, %r10
movq 0x8(%r9), %rax
movq %rax, 0x40(%rsp)
movq %rax, %rdx
xorl %ebp, %ebp
movq %r9, 0x18(%rsp)
movq %r10, (%rsp)
cmpq %r13, %r10
je 0x16a34c
movq (%r9), %rax
cmpq 0x10(%r9), %rdx
jne 0x16a2ef
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rdx
cmoveq %rdx, %rsi
movq %rsi, 0x10(%r9)
imulq $0x1c, %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %r10
movq 0x18(%rsp), %r9
movq %rax, (%r9)
movq 0x8(%r9), %rdx
imulq $0x1c, %rdx, %r8
incq %rdx
movq %rdx, 0x8(%r9)
movq 0x10(%r14), %rsi
movq (%rsi,%r13,8), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
movl %edi, (%rax,%r8)
movl %ebp, 0x10(%rax,%r8)
addq %rbp, %rsi
movl %esi, 0x14(%rax,%r8)
movl %ebp, %edi
imulq $0x28, %rdi, %rdi
movl 0x14(%r12,%rdi), %edi
movl %edi, 0x4(%rax,%r8)
leal -0x1(%rsi), %edi
imulq $0x28, %rdi, %rdi
movl 0x18(%r12,%rdi), %edi
movl %edi, 0x8(%rax,%r8)
andl $0x0, 0xc(%rax,%r8)
incq %r13
movq %rsi, %rbp
jmp 0x16a2af
movq (%r9), %rcx
movq 0x40(%rsp), %rax
imulq $0x1c, %rax, %rbp
addq %rcx, %rbp
imulq $0x1c, %rdx, %rsi
subq %rax, %rdx
movq %rcx, (%rsp)
movq %rsi, 0x28(%rsp)
addq %rcx, %rsi
movq %rbp, %rdi
movq %rdx, %r13
callq 0x15775d
movq %r13, 0x38(%rsp)
decq %r13
imulq $0x1c, %r13, %rax
movl 0x4(%rax,%rbp), %ecx
movq %rbp, 0x20(%rsp)
movl 0x8(%rax,%rbp), %edx
movl %edx, %esi
subl %ecx, %esi
cvtsi2ss %rsi, %xmm0
cvtsi2ssq 0x98(%rsp), %xmm6
movss 0x10(%rsp), %xmm1
mulss %xmm6, %xmm1
ucomiss %xmm1, %xmm0
ja 0x16a60f
movl %esi, %r14d
cvtsi2sd %r14, %xmm1
mulsd 0x87660(%rip), %xmm1    # 0x1f1a28
movq (%rsp), %rsi
movq 0x28(%rsp), %rdi
leaq (%rsi,%rdi), %r9
addq $-0x30, %r9
xorl %edi, %edi
movq 0x78e8d(%rip), %xmm2    # 0x1e3270
movapd 0x78e95(%rip), %xmm3    # 0x1e3280
movq %r13, %r8
decq %r8
js 0x16a60f
movl -0x4(%r9), %r11d
movl (%r9), %ebp
cmpl %ebp, %edx
movl %ebp, %esi
cmovbl %edx, %esi
cmpl %r11d, %ecx
movl %r11d, %r10d
cmoval %ecx, %r10d
subl %r10d, %esi
cmovbl %edi, %esi
leaq -0x1c(%r9), %r10
subl %r11d, %ebp
subq %rsi, %rbp
movq %rbp, %xmm4
punpckldq %xmm2, %xmm4    # xmm4 = xmm4[0],xmm2[0],xmm4[1],xmm2[1]
subpd %xmm3, %xmm4
movq %r10, %r9
cmpq $0x9, %rbp
jb 0x16a3ee
movapd %xmm4, %xmm5
unpckhpd %xmm4, %xmm5            # xmm5 = xmm5[1],xmm4[1]
addsd %xmm4, %xmm5
ucomisd %xmm1, %xmm5
movq %r10, %r9
jbe 0x16a3ee
mulss 0xc(%rsp), %xmm6
ucomiss %xmm6, %xmm0
setbe %al
cmpq 0xa0(%rsp), %r14
setbe %cl
testb %cl, %al
jne 0x16a89a
movq 0x90(%rsp), %r9
testq %r9, %r9
je 0x16a4b4
subq $0x10, %rsp
movq 0x58(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %r12, %rcx
movq 0x28(%rsp), %r8
pushq 0xa8(%rsp)
pushq 0xe0(%rsp)
movss %xmm6, 0x30(%rsp)
callq 0x18667a
movss 0x30(%rsp), %xmm6
addq $0x20, %rsp
movq (%rsp), %rax
movq 0x28(%rsp), %rcx
addq %rcx, %rax
addq $-0x10, %rax
movq %rax, 0x28(%rsp)
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorl %esi, %esi
movsd 0x78717(%rip), %xmm0    # 0x1e2bf0
movq %r13, %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
js 0x16a8a3
imulq $0x1c, 0x10(%rsp), %rcx
movq 0x20(%rsp), %rax
movl 0x4(%rax,%rcx), %edi
movq %rcx, (%rsp)
movl 0x8(%rax,%rcx), %r8d
movl %r8d, %r9d
subl %edi, %r9d
cvtsi2ss %r9, %xmm1
ucomiss %xmm1, %xmm6
setae %al
cmpq 0xa0(%rsp), %r9
setbe %cl
testb %cl, %al
jne 0x16a8a3
movl %r9d, %eax
cvtsi2sd %rax, %xmm1
movq (%rsp), %rax
addq 0x20(%rsp), %rax
movq %rax, (%rsp)
mulsd %xmm0, %xmm1
movq 0x28(%rsp), %r10
xorl %r11d, %r11d
movq %r13, %rax
addq %r11, %rax
js 0x16a5ec
movq %rsi, %rax
addq %r11, %rax
je 0x16a5e0
movl -0x8(%r10), %edx
movl -0x4(%r10), %ecx
cmpl %ecx, %r8d
movl %ecx, %ebp
cmovbl %r8d, %ebp
cmpl %edx, %edi
movl %edx, %eax
cmoval %edi, %eax
subl %eax, %ebp
jbe 0x16a5e0
subl %edx, %ecx
cmpl $0x1, (%r10)
cvtsi2sd %rbp, %xmm2
jne 0x16a5a7
movl %ecx, %eax
cvtsi2sd %rax, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
ja 0x16a8a3
ucomisd %xmm1, %xmm2
ja 0x16a8a3
cmpl %r9d, %ecx
cmovael %r9d, %ecx
cvtsi2sd %rcx, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x16a5e0
movl -0xc(%r10), %eax
cvtsi2ss %rax, %xmm2
movq (%rsp), %rax
movl (%rax), %eax
cvtsi2ss %rax, %xmm3
mulss 0x30(%rsp), %xmm3
ucomiss %xmm3, %xmm2
ja 0x16a8a3
decq %r11
addq $-0x1c, %r10
jmp 0x16a547
movq (%rsp), %rax
movl $0x1, 0xc(%rax)
incq %rsi
movq 0x10(%rsp), %rax
decq %rax
movl $0x0, 0xc(%rsp)
jmp 0x16a4dc
addq 0x20(%rsp), %rax
movl $0x1, 0xc(%rax)
pushq $0x1
popq %rsi
movq 0x18(%rsp), %rax
andq $0x0, 0x8(%r15)
movq 0x38(%rsp), %rdx
movq %rdx, %rdi
movq %rsi, 0x30(%rsp)
subq %rsi, %rdi
xorl %ecx, %ecx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
cmpq %rdx, %rdi
jge 0x16a877
movq (%r15), %rax
cmpq 0x10(%r15), %rcx
jne 0x16a688
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r15)
imulq $0x28, %rdx, %rsi
movq %r15, %r14
movq %rdi, %r15
movq %rax, %rdi
callq 0x2370
movq %r15, %rdi
movq %r14, %r15
movq %rax, (%r14)
movq 0x8(%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r15)
imulq $0x28, %rcx, %rcx
orl $-0x1, 0x8(%rax,%rcx)
movq %rdi, (%rsp)
imulq $0x1c, %rdi, %rdx
movq 0x20(%rsp), %rsi
movq 0x4(%rsi,%rdx), %xmm0
movq 0x10(%rsi,%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)
andl $0x0, (%rax,%rcx)
andl $0x80000000, 0x4(%rax,%rcx) # imm = 0x80000000
movslq %edx, %rax
imulq $0x28, %rax, %rsi
addq %r12, %rsi
movd %xmm2, %eax
movslq %eax, %rdx
subq $0x8, %rsp
movq 0x58(%rsp), %rdi
movq 0xb0(%rsp), %rcx
movq 0xb8(%rsp), %r8
movq 0xc0(%rsp), %r9
pushq %rbx
callq 0x16efef
popq %rcx
popq %rdx
movq (%r15), %rax
movq 0x8(%r15), %rdx
movq 0x8(%rbx), %rcx
addq %rdx, %rcx
cmpq %rcx, 0x10(%r15)
jae 0x16a771
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
incq %rdx
movq %rdx, 0x10(%r15)
imulq $0x28, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r15)
movq 0x8(%r15), %rdx
imulq $0x28, %rdx, %rcx
addq %rax, %rcx
pushq $0x4
popq %rsi
pushq $0x20
popq %rdi
xorl %r8d, %r8d
movq 0x8(%rbx), %r9
cmpq %r9, %r8
jge 0x16a83e
movq (%rbx), %r10
movl -0x4(%r10,%rsi), %r9d
cmpq %rbp, %r9
je 0x16a7dc
imulq $0x28, %rdx, %r11
addq %rax, %r11
movl %r9d, -0x18(%rdi,%r11)
orl $-0x1, -0x20(%rdi,%r11)
movl (%r10,%rsi), %r10d
movl %r10d, (%rdi,%r11)
andl $0x0, -0x14(%rdi,%r11)
movq 0x48(%rsp), %r10
movq (%r10), %r10
movq 0x18(%r10), %r10
movq 0x20(%r10), %r10
shrl %r9d
movl (%r10,%r9,8), %r9d
andl %r13d, %r9d
movl %r9d, -0x10(%rdi,%r11)
jmp 0x16a82e
movslq 0x4(%r10,%rsi), %r9
movslq -0x1c(%rcx), %r10
addq %r9, %r10
imulq $0x28, %r10, %r9
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq 0x20(%r12,%r9), %rdx
movq %rdx, (%rdi,%rax)
movdqu (%r12,%r9), %xmm0
movdqu 0x10(%r12,%r9), %xmm1
movdqu %xmm1, -0x10(%rdi,%rax)
movdqu %xmm0, -0x20(%rdi,%rax)
movq (%rbx), %rax
movl (%rax,%rsi), %r9d
movq (%r15), %rax
movq 0x8(%r15), %rdx
imulq $0x28, %rdx, %r10
addq %rax, %r10
movl %r9d, (%rdi,%r10)
incq %r8
addq $0x28, %rdi
addq $0xc, %rsi
jmp 0x16a781
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl -0x24(%rcx), %eax
movl $0x80000000, %edx       # imm = 0x80000000
andl %edx, %eax
orl %r9d, %eax
movl %eax, -0x24(%rcx)
movq 0x8(%r15), %rcx
addq 0x8(%rbx), %rcx
movq %rcx, 0x8(%r15)
movq (%rsp), %rdi
incq %rdi
movq 0x18(%rsp), %rax
movq 0x38(%rsp), %rdx
jmp 0x16a645
movq 0x30(%rsp), %rsi
movq 0x40(%rsp), %rcx
movq %rcx, 0x8(%rax)
jmp 0x16a889
xorl %esi, %esi
movl %esi, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %esi, %esi
movq 0x18(%rsp), %rax
jmp 0x16a87c
testb $0x1, 0xc(%rsp)
movq 0x18(%rsp), %rax
je 0x16a623
jmp 0x16a87c