// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: gen_max_gchain(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, asg_t const*, st_mt_t*, vec_sp_node_t*, vec_mg_pathv_t*, unsigned long*, long, double)
uint32_t gen_max_gchain(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, const asg_t *g, st_mt_t *dst_done, 
vec_sp_node_t *out, vec_mg_pathv_t *res, uint64_t *b, int64_t bw, double diff_ec_ul)
{
	if(idx->n <= 0) return 0;
	int64_t a_n, idx_n = idx->n, i, m_sc = 0, is_done = 0; uint64_t s_idx, e_idx, om, ok, ovlp, novlp; 
	ul_ov_t m; memset(&m, 0, sizeof(m)); m_sc = -1; mg_lchain_t *a = e->a;
	for (i = a_n = 0; i < idx_n; ++i) {
		if(((int64_t)(idx->a[i]>>32)) > m_sc) {
			m_sc = ((int64_t)(idx->a[i]>>32)); m.qn = i;
			m.ts = a_n; m.te = a_n + ((uint32_t)idx->a[i]);
			m.qs = a[m.ts].qs; m.qe = a[m.te-1].qe;
		}
		// dedup_second_chain(NULL, 0, a_n, a_n + ((uint32_t)idx->a[i]), a, raw_idx, raw_chn);
		a_n += ((uint32_t)idx->a[i]);
	}
	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;
	if(is_done == 0) {
		// for (i = a_n = 0; i < idx_n; ++i) {
		// 	s_idx = a[a_n].qs; a_n += ((uint32_t)idx->a[i]); e_idx = a[a_n-1].qe;
		// 	if(i == m.qn) continue;
		// 	if(s_idx < m.qs || e_idx < m.qs || s_idx > m.qe || e_idx > m.qe) break;
		// }
		// if(i >= idx_n) is_done = 2;///no alignment that is on the left or the right side of the primary chain
		for (i = a_n = 0; i < idx_n; ++i) {
			s_idx = a[a_n].qs; a_n += ((uint32_t)idx->a[i]); e_idx = a[a_n-1].qe;
			if(i == m.qn) continue;
			ovlp = ((MIN(m.qe, e_idx) > MAX(m.qs, s_idx))? (MIN(m.qe, e_idx) - MAX(m.qs, s_idx)):0);
			novlp = (e_idx - s_idx) - ovlp;
			if(novlp > ((m.qe-m.qs)*GC_OFFSET_RATE) && novlp > GC_OFFSET_POS) break;
		}
		if(i >= idx_n) is_done = 2;///no alignment that is on the left or the right side of the primary chain
	}

	if(is_done == 0) {
		if((m.qe - m.qs) > (qlen*primary_fragment_cov_rate)) {
			dedup_second_chain(uref, idx->a, idx_n, m.ts, m.te, a, raw_idx, raw_chn, b, bw, diff_ec_ul, qlen);

			om = m.qe - m.qs;
			for (i = a_n = 0; i < idx_n; ++i) {
				s_idx = a[a_n].qs; a_n += ((uint32_t)idx->a[i]); e_idx = a[a_n-1].qe;
				if(i == m.qn) continue;
				ovlp = ((MIN(m.qe, e_idx) > MAX(m.qs, s_idx))? (MIN(m.qe, e_idx) - MAX(m.qs, s_idx)):0);
				if(ovlp == 0) continue;
				ok = e_idx - s_idx;
				if(ok > om) ok = om;
				if((ovlp > ok*0.1) && ((int64_t)(idx->a[i]>>32)) > (m_sc*primary_fragment_second_score_rate)) break;
			}
			if(i >= idx_n) is_done = 3;
		}
	}

	if(is_done && gen_gchain_track(km, a + m.ts, m.te - m.ts, g, dst_done, out, res)) {///try to find a path
		for (i = m.ts, e->n = 0; i < (int64_t)m.te; i++) a[e->n++] = a[i];
		// fprintf(stderr, "--[M::%s::id->%ld] [%u, %u), res->n:%lu\n", __func__, ulid, m.qs, m.qe, (uint64_t)res->n);
		kv_resize(mg_lchain_t, *e, res->n); a = e->a;
		for (i = ((int64_t)res->n)-1; i >= 0; i--) {
			
			if(res->a[i].v == (uint32_t)-1) {
				a[i] = a[res->a[i].pre]; a[i].dist_pre = res->a[i].d;
				// fprintf(stderr, "ulid:%ld\t%u\t%u\t%c\tutg%.6dl\t%u\t%u\tdist_pre:%d\n", ulid, a[i].qs, a[i].qe, "+-"[a[i].v&1], (int32_t)(a[i].v>>1)+1, a[i].rs, a[i].re, a[i].dist_pre);
			}
			else {
				a[i].v = res->a[i].v; a[i].off = -1; a[i].dist_pre = res->a[i].d;
				// fprintf(stderr, "ulid:%ld\t*\t*\t%c\tutg%.6dl\t*\t*\tdist_pre:%d\n", ulid, "+-"[a[i].v&1], (int32_t)(a[i].v>>1)+1, a[i].dist_pre);
			}
		}
		e->n = res->n;

		


		// debug_gchain(km, g, e->a, e->n, dst_done, out);


		return 1;
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq (%rcx), %r12
testq %r12, %r12
je 0x1d274c
movq %r8, %rbx
movq %r9, (%rsp)
movq %rdi, 0x40(%rsp)
movq (%r8), %r14
movq %rcx, 0x18(%rsp)
jle 0x1d235f
movq 0x10(%rcx), %rcx
movq $-0x1, %r11
xorl %eax, %eax
xorl %r9d, %r9d
xorl %r8d, %r8d
xorl %edi, %edi
xorl %edx, %edx
xorl %ebp, %ebp
xorl %r10d, %r10d
movq (%rcx,%r10,8), %r15
movq %r15, %r13
shrq $0x20, %r13
cmpq %r11, %r13
jle 0x1d234b
movl %r8d, %edx
leaq (%rdx,%rdx,4), %rdx
movl 0x14(%r14,%rdx,8), %edx
leal (%r15,%r8), %eax
leal (%r15,%r8), %edi
decl %edi
leaq (%rdi,%rdi,4), %rdi
movl 0x18(%r14,%rdi,8), %edi
movq %r13, %r11
movl %r10d, %ebp
movl %r8d, %r9d
addq %r15, %r8
incq %r10
cmpq %r10, %r12
jne 0x1d2313
movl %ebp, %ebp
cvtsi2ss %r11, %xmm10
jmp 0x1d2373
xorl %ebp, %ebp
movss 0x68846(%rip), %xmm10   # 0x23abb0
xorl %edx, %edx
xorl %edi, %edi
xorl %r9d, %r9d
xorl %eax, %eax
movl %r9d, %ecx
movq %rcx, 0x20(%rsp)
leaq (%rcx,%rcx,4), %rcx
movl 0x14(%r14,%rcx,8), %r9d
movq %rax, 0x28(%rsp)
leal -0x1(%rax), %r8d
leaq (%r8,%r8,4), %r8
cmpl 0x14(%r14,%r8,8), %r9d
jg 0x1d275d
leaq (%r14,%rcx,8), %r13
leaq (%r14,%r8,8), %rcx
movl 0x18(%rcx), %ecx
cmpl 0x18(%r13), %ecx
jl 0x1d275d
movl %edi, %r8d
subl %edx, %r8d
cvtsi2ss %r8, %xmm9
movq 0x88(%rsp), %r10
cvtsi2ss %r10, %xmm4
mulss %xmm4, %xmm0
ucomiss %xmm0, %xmm9
ja 0x1d24b4
testq %r12, %r12
jle 0x1d24b4
leaq -0x10(%r14), %rax
movq %rax, 0x38(%rsp)
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rcx
movl %edi, %eax
movq %rax, 0x30(%rsp)
movl %edx, %r15d
movl %r8d, %eax
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x6d744(%rip), %xmm0    # 0x23fb58
xorl %edx, %edx
movq 0x5ee62(%rip), %xmm5    # 0x231280
movapd 0x5ee6a(%rip), %xmm6    # 0x231290
xorl %edi, %edi
movq %rdx, %r11
movl (%rcx,%rdi,8), %edx
addq %r11, %rdx
cmpq %rdi, %rbp
je 0x1d24a8
movq %rbp, %rax
leaq (%rdx,%rdx,4), %r8
movq 0x38(%rsp), %r9
movslq (%r9,%r8,8), %r8
movq 0x30(%rsp), %rbp
movq %rbp, %r9
cmpq %r8, %rbp
jb 0x1d2456
movq %r8, %r9
leaq (%r11,%r11,4), %r11
movslq 0x14(%r14,%r11,8), %r11
movq %r15, %rbp
cmpq %r11, %r15
ja 0x1d246a
movq %r11, %rbp
subq %rbp, %r9
jae 0x1d2472
xorl %r9d, %r9d
movq %rax, %rbp
addq %r11, %r9
subq %r9, %r8
movq %r8, %xmm7
punpckldq %xmm5, %xmm7    # xmm7 = xmm7[0],xmm5[0],xmm7[1],xmm5[1]
subpd %xmm6, %xmm7
cmpq $0x9, %r8
jb 0x1d24a8
movapd %xmm7, %xmm8
unpckhpd %xmm7, %xmm8            # xmm8 = xmm8[1],xmm7[1]
addsd %xmm7, %xmm8
ucomisd %xmm0, %xmm8
ja 0x1d2638
incq %rdi
cmpq %rdi, %r12
jne 0x1d2428
movq 0xa8(%rsp), %r15
movq 0xa0(%rsp), %r9
movq 0x98(%rsp), %r8
movq 0x90(%rsp), %rcx
movq 0x28(%rsp), %r12
movl %r12d, %edx
movq 0x20(%rsp), %rbp
subl %ebp, %edx
subq $0x8, %rsp
movq 0x48(%rsp), %rdi
movq %r13, %rsi
pushq %r15
callq 0x1b8b8c
addq $0x10, %rsp
testl %eax, %eax
je 0x1d274c
movq $0x0, 0x8(%rbx)
cmpl %r12d, %ebp
jae 0x1d254c
movl %r12d, %eax
subq %rbp, %rax
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
leaq (%rcx,%rcx,4), %rcx
movq 0x20(%r13), %rdx
movq %rdx, 0x20(%r14,%rcx,8)
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups %xmm1, 0x10(%r14,%rcx,8)
movups %xmm0, (%r14,%rcx,8)
addq $0x28, %r13
decq %rax
jne 0x1d2515
movq (%rbx), %rax
movq 0x8(%r15), %rsi
cmpq %rsi, 0x10(%rbx)
jae 0x1d25b0
decq %rsi
movq %rsi, %rcx
shrq %rcx
orq %rsi, %rcx
movq %rcx, %rdx
shrq $0x2, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x4, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x8, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x10, %rcx
orq %rdx, %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x10(%rbx)
leaq 0x8(,%rcx,8), %rcx
leaq (%rcx,%rcx,4), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%r15), %rsi
testq %rsi, %rsi
jle 0x1d262a
movq (%r15), %rdi
leaq 0x1(%rsi), %rcx
leaq (%rsi,%rsi,4), %rdx
leaq (%rax,%rdx,8), %rdx
addq $-0x28, %rdx
shlq $0x2, %rsi
leaq (%rsi,%rsi,2), %rsi
movl -0xc(%rdi,%rsi), %r8d
cmpl $-0x1, %r8d
je 0x1d25e7
movl %r8d, 0x8(%rdx)
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
jmp 0x1d260c
movslq -0x4(%rdi,%rsi), %rdi
leaq (%rdi,%rdi,4), %rdi
movq 0x20(%rax,%rdi,8), %r8
movq %r8, 0x20(%rdx)
movups (%rax,%rdi,8), %xmm0
movups 0x10(%rax,%rdi,8), %xmm1
movups %xmm1, 0x10(%rdx)
movups %xmm0, (%rdx)
movq (%r15), %rdi
movl -0x8(%rdi,%rsi), %r8d
movl %r8d, 0x20(%rdx)
decq %rcx
addq $-0x28, %rdx
addq $-0xc, %rsi
cmpq $0x1, %rcx
ja 0x1d25d0
movq 0x8(%r15), %rsi
movq %rsi, 0x8(%rbx)
movl $0x1, %eax
jmp 0x1d274e
mulss %xmm1, %xmm4
ucomiss %xmm4, %xmm9
jbe 0x1d274c
movl 0x28(%rsp), %r8d
subq $0x8, %rsp
movq %rsi, %rdi
movq %rcx, %rsi
movq %r12, %rdx
movq 0x28(%rsp), %rcx
movq %r14, %r9
movaps %xmm3, %xmm0
pushq %r10
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
pushq 0xa0(%rsp)
pushq 0x28(%rsp)
movss %xmm2, 0x3c(%rsp)
movss %xmm10, 0x30(%rsp)
callq 0x1d1cf4
movss 0x30(%rsp), %xmm3
addq $0x30, %rsp
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
mulss 0xc(%rsp), %xmm3
xorl %ecx, %ecx
movsd 0x5e55d(%rip), %xmm0    # 0x230c10
xorl %edx, %edx
movq %rcx, %r9
movq (%rax,%rdx,8), %rsi
movl %esi, %ecx
addq %r9, %rcx
cmpq %rdx, %rbp
je 0x1d273b
leaq (%rcx,%rcx,4), %rdi
movq 0x38(%rsp), %r8
movslq (%r8,%rdi,8), %rdi
movq 0x30(%rsp), %r10
movq %r10, %r8
cmpq %rdi, %r10
jb 0x1d26e3
movq %rdi, %r8
leaq (%r9,%r9,4), %r9
movslq 0x14(%r14,%r9,8), %r9
movq %r15, %r10
cmpq %r9, %r15
ja 0x1d26f7
movq %r9, %r10
subq %r10, %r8
jbe 0x1d273b
jae 0x1d2701
xorl %r8d, %r8d
subq %r9, %rdi
cmpq 0x10(%rsp), %rdi
jb 0x1d2710
movq 0x10(%rsp), %rdi
xorps %xmm1, %xmm1
cvtsi2sd %r8, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %rdi, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
jbe 0x1d273b
shrq $0x20, %rsi
xorps %xmm1, %xmm1
cvtsi2ss %rsi, %xmm1
ucomiss %xmm3, %xmm1
ja 0x1d274c
incq %rdx
cmpq %rdx, %r12
jne 0x1d26b5
jmp 0x1d24b4
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6eabe(%rip), %rdi     # 0x241222
leaq 0x6d4f0(%rip), %rsi     # 0x23fc5b
leaq 0x703ac(%rip), %rcx     # 0x242b1e
movl $0x3a77, %edx           # imm = 0x3A77
callq 0x2110