// File: /workspace/github_cpp_makefile_star2_O2/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), %rdx
testq %rdx, %rdx
je 0x186448
movq %rcx, %r13
movq %rsi, 0x50(%rsp)
movq %r9, 0x10(%rsp)
movq %rdi, 0x60(%rsp)
xorl %r9d, %r9d
testq %rdx, %rdx
movl $0x0, %ebp
cmovgq %rdx, %rbp
movq %r8, 0x58(%rsp)
movq (%r8), %r14
pushq $-0x1
popq %rbx
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %edi, %edi
xorl %r15d, %r15d
movl $0x0, 0xc(%rsp)
xorl %r11d, %r11d
cmpq %r9, %rbp
je 0x186159
movq 0x10(%r13), %rax
movq (%rax,%r9,8), %r12
movq %r12, %r10
shrq $0x20, %r10
cmpq %rbx, %r10
jle 0x186151
movl %r15d, %eax
imulq $0x28, %rax, %rax
movl 0x14(%r14,%rax), %ecx
leal (%r12,%r15), %r11d
leal (%r12,%r15), %eax
decl %eax
imulq $0x28, %rax, %rax
movl 0x18(%r14,%rax), %edi
movq %r10, %rbx
movl %r9d, %r8d
movl %r15d, 0xc(%rsp)
addq %r12, %r15
incq %r9
jmp 0x18610e
movl 0xc(%rsp), %r12d
imulq $0x28, %r12, %r15
movl 0x14(%r14,%r15), %eax
leal -0x1(%r11), %r9d
imulq $0x28, %r9, %r9
cmpl 0x14(%r14,%r9), %eax
jg 0x18652c
addq %r14, %r15
addq %r14, %r9
movl 0x18(%r9), %eax
cmpl 0x18(%r15), %eax
jl 0x18652c
movl %edi, %r9d
subl %ecx, %r9d
cvtsi2ss %r9, %xmm9
cvtsi2ssq 0xa8(%rsp), %xmm4
mulss %xmm4, %xmm0
ucomiss %xmm0, %xmm9
movl %r11d, %eax
movq %rax, 0x20(%rsp)
ja 0x1863bb
leaq -0x10(%r14), %rax
movq %rax, 0x40(%rsp)
movl %r8d, %eax
movq %rax, 0x48(%rsp)
movl %edi, %eax
movq %rax, 0x38(%rsp)
movl %ecx, %eax
movq %rax, 0x30(%rsp)
movl %r9d, %eax
movq %rax, 0x18(%rsp)
cvtsi2sd %rax, %xmm0
mulsd 0x6b83b(%rip), %xmm0    # 0x1f1a28
xorl %edi, %edi
movq 0x5d079(%rip), %xmm5    # 0x1e3270
movapd 0x5d081(%rip), %xmm6    # 0x1e3280
xorl %r10d, %r10d
movq %r11, 0x28(%rsp)
cmpq %rdi, %rbp
je 0x1863bb
movq 0x10(%r13), %rcx
movl (%rcx,%rdi,8), %r8d
addq %r10, %r8
cmpq %rdi, 0x48(%rsp)
je 0x1862a0
movq %rbp, %rsi
movq %r13, %rbp
imulq $0x28, %r8, %rax
movq 0x40(%rsp), %r9
movslq (%r9,%rax), %r11
movq 0x38(%rsp), %rax
movq %rax, %r9
cmpq %r11, %rax
jb 0x186245
movq %r11, %r9
imulq $0x28, %r10, %rax
movslq 0x14(%r14,%rax), %r13
movq 0x30(%rsp), %r10
movq %r10, %rax
cmpq %r13, %r10
ja 0x18625e
movq %r13, %rax
subq %rax, %r9
jae 0x186266
xorl %r9d, %r9d
addq %r13, %r9
subq %r9, %r11
movq %r11, %xmm7
punpckldq %xmm5, %xmm7    # xmm7 = xmm7[0],xmm5[0],xmm7[1],xmm5[1]
subpd %xmm6, %xmm7
cmpq $0x9, %r11
movq 0x28(%rsp), %r11
movq %rbp, %r13
movq %rsi, %rbp
jb 0x1862a0
movapd %xmm7, %xmm8
unpckhpd %xmm7, %xmm8            # xmm8 = xmm8[1],xmm7[1]
addsd %xmm7, %xmm8
ucomisd %xmm0, %xmm8
ja 0x1862ab
incq %rdi
movq %r8, %r10
jmp 0x186207
mulss %xmm1, %xmm4
ucomiss %xmm4, %xmm9
jbe 0x186448
subq $0x8, %rsp
movq 0x58(%rsp), %rdi
movq %rcx, %rsi
movq %r12, %rcx
movq 0x28(%rsp), %r8
movq %r14, %r9
movapd %xmm3, %xmm0
pushq 0xb0(%rsp)
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
pushq 0xc0(%rsp)
pushq 0x38(%rsp)
movss %xmm2, 0x40(%rsp)
callq 0x185b7d
movq 0x58(%rsp), %r11
addq $0x30, %rsp
cvtsi2ss %rbx, %xmm0
mulss 0x10(%rsp), %xmm0
xorl %eax, %eax
movsd 0x5c8d3(%rip), %xmm1    # 0x1e2bf0
xorl %ecx, %ecx
movq 0x30(%rsp), %r10
cmpq %rax, %rbp
je 0x1863bb
movq %rcx, %r8
movq 0x10(%r13), %rcx
movq (%rcx,%rax,8), %rdx
movl %edx, %ecx
addq %r8, %rcx
cmpq %rax, 0x48(%rsp)
je 0x1863b3
imulq $0x28, %rcx, %rsi
movq 0x40(%rsp), %rdi
movslq (%rdi,%rsi), %rsi
movq 0x38(%rsp), %r9
movq %r9, %rdi
cmpq %rsi, %r9
jb 0x186361
movq %rsi, %rdi
imulq $0x28, %r8, %r8
movslq 0x14(%r14,%r8), %r8
movq %r10, %r9
cmpq %r8, %r10
ja 0x186375
movq %r8, %r9
subq %r9, %rdi
jbe 0x1863b3
jae 0x18637e
xorl %edi, %edi
subq %r8, %rsi
cmpq 0x18(%rsp), %rsi
jb 0x18638d
movq 0x18(%rsp), %rsi
cvtsi2sd %rdi, %xmm2
cvtsi2sd %rsi, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x1863b3
shrq $0x20, %rdx
cvtsi2ss %rdx, %xmm2
ucomiss %xmm0, %xmm2
ja 0x186448
incq %rax
jmp 0x186324
subl 0xc(%rsp), %r11d
subq $0x8, %rsp
movq 0x68(%rsp), %rdi
movq %r15, %rsi
movq %r11, %rdx
movq 0xb8(%rsp), %rcx
movq 0xc0(%rsp), %r8
movq 0xc8(%rsp), %r9
movq 0xd0(%rsp), %r13
pushq %r13
callq 0x16efef
popq %rcx
popq %rdx
testl %eax, %eax
je 0x186448
movq 0x58(%rsp), %rbx
andq $0x0, 0x8(%rbx)
movq 0x20(%rsp), %rdx
cmpq %rdx, %r12
jae 0x18644f
movq 0x8(%rbx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rbx)
imulq $0x28, %rax, %rax
movq 0x20(%r15), %rcx
movq %rcx, 0x20(%r14,%rax)
movups (%r15), %xmm0
movupd 0x10(%r15), %xmm1
movupd %xmm1, 0x10(%r14,%rax)
movups %xmm0, (%r14,%rax)
incq %r12
addq $0x28, %r15
jmp 0x18640b
xorl %eax, %eax
jmp 0x18651d
movq (%rbx), %rax
movq 0x8(%r13), %rcx
cmpq %rcx, 0x10(%rbx)
jae 0x1864aa
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(%rbx)
imulq $0x28, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%r13), %rcx
imulq $0x28, %rcx, %rdx
addq %rax, %rdx
addq $-0x28, %rdx
imulq $0xc, %rcx, %rsi
testq %rcx, %rcx
jle 0x186512
movq (%r13), %rdi
movl -0xc(%rdi,%rsi), %r8d
cmpl $-0x1, %r8d
je 0x1864d6
movl %r8d, 0x8(%rdx)
orl $-0x1, (%rdx)
jmp 0x1864fe
movslq -0x4(%rdi,%rsi), %rdi
imulq $0x28, %rdi, %rdi
movq 0x20(%rax,%rdi), %r8
movq %r8, 0x20(%rdx)
movups (%rax,%rdi), %xmm0
movupd 0x10(%rax,%rdi), %xmm1
movupd %xmm1, 0x10(%rdx)
movups %xmm0, (%rdx)
movq (%r13), %rdi
movl -0x8(%rdi,%rsi), %edi
movl %edi, 0x20(%rdx)
addq $-0x28, %rdx
addq $-0xc, %rsi
decq %rcx
jmp 0x1864b9
movq 0x8(%r13), %rax
movq %rax, 0x8(%rbx)
pushq $0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6cbb7(%rip), %rdi     # 0x1f30ea
leaq 0x6b5e9(%rip), %rsi     # 0x1f1b23
leaq 0x6e4a5(%rip), %rcx     # 0x1f49e6
movl $0x3a77, %edx           # imm = 0x3A77
callq 0x2110