// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq (%rcx), %r12
testq %r12, %r12
je 0x1c83e2
movq %rsi, 0x48(%rsp)
movq %r9, 0x50(%rsp)
movq (%r8), %r14
movq %rcx, 0x28(%rsp)
jle 0x1c83ea
movq 0x10(%rcx), %rax
movq $-0x1, %rbx
xorl %r15d, %r15d
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r11d, %r11d
xorl %esi, %esi
movq (%rax,%rsi,8), %r9
movq %r9, %r10
shrq $0x20, %r10
cmpq %rbx, %r10
jle 0x1c83c0
movl %ecx, %edx
leaq (%rdx,%rdx,4), %rdx
movl 0x14(%r14,%rdx,8), %ebp
leal (%r9,%rcx), %r15d
leal (%r9,%rcx), %edx
decl %edx
leaq (%rdx,%rdx,4), %rdx
movl 0x18(%r14,%rdx,8), %r13d
movq %r10, %rbx
movl %esi, %r11d
movl %ecx, %edx
addq %r9, %rcx
incq %rsi
cmpq %rsi, %r12
jne 0x1c838a
movl %r13d, (%rsp)
movl %ebp, 0x4(%rsp)
movl %r11d, %eax
movq %rax, 0x20(%rsp)
cvtsi2ss %rbx, %xmm10
jmp 0x1c8410
xorl %r15d, %r15d
jmp 0x1c8876
movq $0x0, 0x20(%rsp)
movss 0x665a4(%rip), %xmm10   # 0x22e9a0
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
xorl %edx, %edx
xorl %r15d, %r15d
movl %edx, %eax
movq %rax, 0x38(%rsp)
leaq (%rax,%rax,4), %rax
movl 0x14(%r14,%rax,8), %edx
leal -0x1(%r15), %ecx
leaq (%rcx,%rcx,4), %rcx
cmpl 0x14(%r14,%rcx,8), %edx
jg 0x1c8888
leaq (%r14,%rax,8), %r13
leaq (%r14,%rcx,8), %rax
movl 0x18(%rax), %eax
cmpl 0x18(%r13), %eax
jl 0x1c8888
movl (%rsp), %eax
movl %eax, %ecx
subl 0x4(%rsp), %ecx
cvtsi2ss %rcx, %xmm9
cvtsi2ssq 0xa8(%rsp), %xmm4
mulss %xmm4, %xmm0
ucomiss %xmm0, %xmm9
movq %r8, 0x58(%rsp)
movq %rdi, 0x60(%rsp)
movq %r15, 0x30(%rsp)
movq %rcx, 0x40(%rsp)
ja 0x1c8596
testq %r12, %r12
setg %dl
jle 0x1c858a
leaq -0x10(%r14), %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rax
movq 0x10(%rax), %r8
movl (%rsp), %eax
movq %rax, 0x10(%rsp)
movl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movl 0x4(%rsp), %ebx
mulsd 0x6b3ca(%rip), %xmm0    # 0x233888
movq %r12, %rax
negq %rax
movq 0x20(%rsp), %r10
negq %r10
movl $0x1, %ecx
movq $0x0, 0x8(%rsp)
movq 0x5cd5e(%rip), %xmm5    # 0x225240
movapd 0x5cd66(%rip), %xmm6    # 0x225250
xorl %r15d, %r15d
movq %r15, %rdi
leaq (%r10,%rcx), %rsi
movl -0x8(%r8,%rcx,8), %r15d
addq %rdi, %r15
cmpq $0x1, %rsi
je 0x1c8570
leaq (%r15,%r15,4), %rsi
movq 0x18(%rsp), %r9
movslq (%r9,%rsi,8), %rsi
movq 0x10(%rsp), %r9
movq %r9, %rbp
cmpq %rsi, %r9
jb 0x1c851f
movq %rsi, %rbp
leaq (%rdi,%rdi,4), %rdi
movslq 0x14(%r14,%rdi,8), %rdi
movq %rbx, %r11
cmpq %rdi, %rbx
ja 0x1c8533
movq %rdi, %r11
movl $0x0, %r9d
subq %r11, %rbp
jb 0x1c8541
movq %rbp, %r9
addq %rdi, %r9
subq %r9, %rsi
movq %rsi, %xmm7
punpckldq %xmm5, %xmm7    # xmm7 = xmm7[0],xmm5[0],xmm7[1],xmm5[1]
subpd %xmm6, %xmm7
cmpq $0x9, %rsi
jb 0x1c8570
movapd %xmm7, %xmm8
unpckhpd %xmm7, %xmm8            # xmm8 = xmm8[1],xmm7[1]
addsd %xmm7, %xmm8
ucomisd %xmm0, %xmm8
ja 0x1c85a2
cmpq %r12, %rcx
setl %dl
leaq (%rax,%rcx), %rsi
incq %rsi
incq %rcx
cmpq $0x1, %rsi
jne 0x1c84ed
movl $0x2, %eax
movq %rax, 0x8(%rsp)
jmp 0x1c85a2
movl $0x1, %eax
movq %rax, 0x8(%rsp)
xorl %edx, %edx
testb $0x1, %dl
movq 0x8(%rsp), %rax
je 0x1c86e4
mulss %xmm1, %xmm4
ucomiss %xmm4, %xmm9
jbe 0x1c86e2
movss %xmm10, 0x10(%rsp)
movss %xmm2, 0x18(%rsp)
movq 0x28(%rsp), %rbx
movq 0x10(%rbx), %rsi
movl 0x30(%rsp), %r8d
subq $0x8, %rsp
movq 0x50(%rsp), %rdi
movq %r12, %rdx
movq 0x40(%rsp), %rcx
movq %r14, %r9
movapd %xmm3, %xmm0
pushq 0xb0(%rsp)
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
pushq 0xc0(%rsp)
pushq 0x78(%rsp)
callq 0x1c7d54
addq $0x30, %rsp
movl $0x3, %eax
testq %r12, %r12
jle 0x1c86e4
movl 0x40(%rsp), %eax
leaq -0x10(%r14), %rdx
movq 0x10(%rbx), %rsi
movl (%rsp), %edi
movl 0x4(%rsp), %r8d
movss 0x10(%rsp), %xmm3
mulss 0x18(%rsp), %xmm3
movsd 0x5c580(%rip), %xmm0    # 0x224bd0
xorl %r9d, %r9d
xorl %ecx, %ecx
movq %r9, %r15
movq (%rsi,%rcx,8), %r10
movl %r10d, %r9d
addq %r15, %r9
cmpq %rcx, 0x20(%rsp)
je 0x1c86cf
leaq (%r9,%r9,4), %r11
movslq (%rdx,%r11,8), %r11
movq %rdi, %rbx
cmpq %r11, %rdi
jb 0x1c867c
movq %r11, %rbx
leaq (%r15,%r15,4), %r15
movslq 0x14(%r14,%r15,8), %r15
movq %r8, %rbp
cmpq %r15, %r8
ja 0x1c8690
movq %r15, %rbp
subq %rbp, %rbx
jbe 0x1c86cf
jae 0x1c8699
xorl %ebx, %ebx
subq %r15, %r11
cmpq %rax, %r11
jb 0x1c86a4
movq %rax, %r11
xorps %xmm1, %xmm1
cvtsi2sd %rbx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r11, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
jbe 0x1c86cf
shrq $0x20, %r10
xorps %xmm1, %xmm1
cvtsi2ss %r10, %xmm1
ucomiss %xmm3, %xmm1
ja 0x1c86e2
incq %rcx
cmpq %rcx, %r12
jne 0x1c8655
movl $0x3, %eax
jmp 0x1c86e4
xorl %eax, %eax
xorl %r15d, %r15d
testq %rax, %rax
je 0x1c8876
movq 0xc8(%rsp), %rbx
movq 0xc0(%rsp), %r9
movq 0xb8(%rsp), %r8
movq 0xb0(%rsp), %rcx
movq 0x30(%rsp), %r12
movl %r12d, %edx
movq 0x38(%rsp), %rbp
subl %ebp, %edx
subq $0x8, %rsp
movq 0x68(%rsp), %rdi
movq %r13, %rsi
pushq %rbx
callq 0x1af92c
addq $0x10, %rsp
testl %eax, %eax
je 0x1c8876
movq 0x58(%rsp), %r15
movq $0x0, 0x8(%r15)
cmpl %r12d, %ebp
jae 0x1c878e
movl %r12d, %eax
subq %rbp, %rax
movq 0x8(%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r15)
leaq (%rcx,%rcx,4), %rcx
movq 0x20(%r13), %rdx
movq %rdx, 0x20(%r14,%rcx,8)
movupd (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups %xmm1, 0x10(%r14,%rcx,8)
movupd %xmm0, (%r14,%rcx,8)
addq $0x28, %r13
decq %rax
jne 0x1c8755
movq 0x8(%rbx), %rax
cmpq %rax, 0x10(%r15)
jae 0x1c87eb
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x10(%r15)
movq (%r15), %rdi
leaq 0x8(,%rcx,8), %rax
leaq (%rax,%rax,4), %rsi
callq 0x2370
movq %rax, (%r15)
movq 0x8(%rbx), %rsi
testq %rsi, %rsi
jle 0x1c8868
movq (%r15), %rax
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
movq (%rbx), %rdi
movl -0xc(%rdi,%rsi), %r8d
cmpl $-0x1, %r8d
je 0x1c8829
movl %r8d, 0x8(%rdx)
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
jmp 0x1c8850
movslq -0x4(%rdi,%rsi), %rdi
leaq (%rdi,%rdi,4), %rdi
movq 0x20(%rax,%rdi,8), %r8
movq %r8, 0x20(%rdx)
movupd (%rax,%rdi,8), %xmm0
movups 0x10(%rax,%rdi,8), %xmm1
movups %xmm1, 0x10(%rdx)
movupd %xmm0, (%rdx)
movq (%rbx), %rdi
movl -0x8(%rdi,%rsi), %edi
movl %edi, 0x20(%rdx)
decq %rcx
addq $-0x28, %rdx
addq $-0xc, %rsi
cmpq $0x1, %rcx
jg 0x1c880f
movq 0x8(%rbx), %rax
movq %rax, 0x8(%r15)
movl $0x1, %r15d
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6c6c3(%rip), %rdi     # 0x234f52
leaq 0x6b0f5(%rip), %rsi     # 0x23398b
leaq 0x6dfb1(%rip), %rcx     # 0x23684e
movl $0x3a77, %edx           # imm = 0x3A77
callq 0x2110