// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: gl_chain(mg_tbuf_s*, ul_vec_t*, overlap_region_alloc*, Chain_Data*, haplotype_evdience_alloc*, st_mt_t*, glchain_t*, gdpchain_t*, ul_idx_t const*, double, long, long, ug_opt_t const*, long, long, void*)
int64_t gl_chain(mg_tbuf_t *b, ul_vec_t *rch, overlap_region_alloc* olist, 
Chain_Data* dp, haplotype_evdience_alloc *hap, st_mt_t *sps, glchain_t *ll, 
gdpchain_t *gdp, const ul_idx_t *uref, double diff_ec_ul, int64_t winLen, 
int64_t qlen, const ug_opt_t *uopt, int64_t debug_i, int64_t tid, void *km)
{
	ll->tk.n = ll->lo.n = 0;
	kv_ul_ov_t *idx = &(ll->lo);
	ks_introsort_or_xe(olist->length, olist->list);
	gen_gl_aln(olist, uref, idx);
	if(idx->n == 0) return 0;
	int64_t max_idx, occ = 0, f = 0;

	kv_resize(ul_ov_t, ll->tk, idx->n);
	occ = gl_chain_lin(idx, olist->list, ll->tk.a, uref, uopt, G_CHAIN_BW, N_GCHAIN_RATE, qlen, UG_SKIP_N, UG_ITER_N, UG_DIS_N, dp, UG_TRANS_W, 0, NULL, uref->ug, 0, debug_i);
	// prt_chains(idx->a, idx->n, ll->tk.a, NULL, 0, NULL, qlen);
	// prt_chains_vlog(idx->a, idx->n, ll->tk.a, NULL, 0, NULL, qlen, debug_i);


	if(occ) {
		if(ff_chain(idx, qlen, 0.99/**P_CHAIN_COV**/, -1/**G_CHAIN_TRANS_RATE**/, ll->tk.a, NULL, NULL, NULL, diff_ec_ul, winLen, km)) {
			f = l2g_res_chain_sc(uref->ug, ll->tk.a+idx->a[idx->n-1].ts, idx->a[idx->n-1].te-idx->a[idx->n-1].ts, &(gdp->swap));
			// f = l2g_res_chain(uref->ug, ll->tk.a+idx->a[idx->n-1].ts, idx->a[idx->n-1].te-idx->a[idx->n-1].ts, &(gdp->swap), -1/**N_GCHAIN_RATE**/);
		}
	}

	// fprintf(stderr, "+[M::%s]\tf::%ld\n", __func__, f);
	// fprintf(stderr, "1-[M::%s] f::%ld\n", __func__, f);
	// fprintf(stderr, "(beg1) [M::%s] debug_i:%ld, qlen:%ld\n", __func__, debug_i, qlen);
	if(!f) {
		// gl_chain_gen(olist, uref, idx, 0, hap, km);///no trans
		// l2g_chain(uref, idx, &(gdp->l)); ll->tk.n = 0;
		gen_gg_aln(olist, uref, UG_TRANS_W, &(gdp->l)); ll->tk.n = 0;
		///buffer
		// kv_resize(uint64_t, ll->srt.a, gdp->l.n); kv_resize(uint64_t, hap->snp_srt, gdp->l.n);
		// kv_resize(uint64_t, gdp->v, gdp->l.n); kv_resize(int64_t, gdp->f, gdp->l.n);
		// max_idx = hc_gchain1_dp(b->km, uref, uref->ug, &(gdp->l), &(gdp->swap), &(gdp->dst), &(gdp->out), &(gdp->path), rch->rlen,
		// uopt, G_CHAIN_BW, diff_ec_ul, -1, ll->srt.a.a, sps, gdp->f.a, hap->snp_srt.a, gdp->v.a);
		kv_resize(uint64_t, ll->srt.a, gdp->l.n);
		max_idx = gl_chain_graph_adv(b->km, olist->list, uref, uref->ug, &(gdp->l), &(gdp->swap), &(gdp->dst), &(gdp->out),
		&(gdp->path), rch->rlen, uopt, G_CHAIN_BW, diff_ec_ul, ll->srt.a.a, sps, dp, UG_SKIP_GRAPH_N, UG_ITER_N, UG_DIS_N, 0, UG_TRANS_W, UG_TRANS_ERR_W);
		// prt_chains(NULL, 0, NULL, sps->a, sps->n, &(gdp->l), qlen);
		// prt_chains_vlog(NULL, 0, NULL, sps->a, sps->n, &(gdp->l), qlen, debug_i);
		// if(max_idx >= 0 && gen_max_gchain_adv(b->km, uref, debug_i, sps, &(gdp->l), &(ll->tk), NULL, rch->rlen, P_CHAIN_COV, 0.3/**P_FRAGEMENT_PRIMARY_CHAIN_COV**/, 
		// 	0.1, PRIMARY_UL_CHAIN_MIN, uref->ug->g, &(gdp->dst_done), &(gdp->out), &(gdp->path), ll->srt.a.a, &(gdp->swap))) {
		if(max_idx >= 0 && select_max_gchain(b->km, uref, debug_i, sps, &(gdp->l), &(ll->tk), uref->ug->g, &(gdp->dst_done), &(gdp->out), &(gdp->path), &(gdp->swap))){
			// print_raw_chains(&(gdp->swap), debug_i);
			// f = check_trans_rate_gap(&(gdp->swap), &(ll->tk), olist, hap, uref, diff_ec_ul, winLen, G_CHAIN_TRANS_RATE);
			f = 1;
		}
	}
	// fprintf(stderr, "[M::%s] f::%ld\n", __func__, f);
	// if(debug_i == 1756) fprintf(stderr, "[M::%s] ulid:%ld, qlen:%ld, f:%ld\n", __func__, debug_i, qlen, f);
	if(f) update_ul_vec_t_ug(uref, rch, &(gdp->swap), debug_i);
	// debug_ul_vec_t_chain(km, uref->ug->g, rch, &(gdp->dst_done), &(gdp->out));
	// fprintf(stderr, "(beg3) [M::%s::tid:%ld] debug_i:%ld, qlen:%ld\n", __func__, tid, debug_i, qlen);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movsd %xmm0, 0x10(%rsp)
movq %r9, %r13
movq %rcx, (%rsp)
movq %rdx, %rbx
movq 0x60(%rsp), %r12
andq $0x0, 0x8(%r12)
movq %rsi, %rbp
andq $0x0, 0x20(%r12)
movq %rdi, %r15
movq 0x70(%rsp), %r14
movq (%rdx), %rsi
movq 0x10(%rdx), %rdi
callq 0x1562de
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x16ae5a
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x16fbad
movq %r13, 0x18(%rsp)
movq 0x80(%rsp), %r13
movq 0x18(%r12), %rdx
cmpq %rax, 0x28(%r12)
movq %r15, 0x20(%rsp)
jae 0x16fa39
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
incq %rcx
movq %rcx, 0x28(%r12)
imulq $0x1c, %rcx, %rsi
movq %rdx, %rdi
callq 0x2370
movq %rax, %rdx
movq %rax, 0x18(%r12)
movq 0x68(%rsp), %r15
movq (%rbx), %rsi
movq (%r14), %rax
subq $0x60, %rsp
movq %rax, 0x40(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r13, (%rsp)
andq $0x0, 0x48(%rsp)
movq $0x2, 0x28(%rsp)
movq $0xc350, 0x18(%rsp)     # imm = 0xC350
movq $0x1388, 0x10(%rsp)     # imm = 0x1388
movq $0x64, 0x8(%rsp)
pushq $0x10
popq %r9
movsd 0x73a9f(%rip), %xmm0    # 0x1e3538
movq %r12, %rdi
movq %r14, %rcx
movq 0xe8(%rsp), %r8
callq 0x16b71d
addq $0x60, %rsp
testq %rax, %rax
je 0x16fb2b
movq 0x18(%r12), %rdx
movss 0x81fbe(%rip), %xmm0    # 0x1f1a80
movss 0x7cfd6(%rip), %xmm1    # 0x1ecaa0
movq %r12, %rdi
movq %r13, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
movsd 0x10(%rsp), %xmm2
pushq 0xa0(%rsp)
pushq 0x80(%rsp)
callq 0x164670
popq %rcx
popq %rdx
testl %eax, %eax
je 0x16fb2b
movq (%r14), %rdi
imulq $0x1c, 0x8(%r12), %rax
movq (%r12), %rcx
movl -0xc(%rcx,%rax), %r8d
movl -0x8(%rcx,%rax), %edx
imulq $0x1c, %r8, %rsi
addq 0x18(%r12), %rsi
subl %r8d, %edx
leaq 0x18(%r15), %rcx
callq 0x166a55
testq %rax, %rax
jne 0x16fcae
movq 0x18(%rsp), %r13
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x16aff5
andq $0x0, 0x20(%r12)
movq 0x8(%r15), %rax
cmpq %rax, 0x50(%r12)
movq %rbp, 0x8(%rsp)
jae 0x16fbb4
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, 0x50(%r12)
movq 0x58(%r12), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x58(%r12)
jmp 0x16fbb9
xorl %eax, %eax
jmp 0x16fcc3
movq 0x58(%r12), %rax
movq 0x20(%rsp), %rbp
movq (%rbp), %rdi
movq (%rbx), %rsi
movq (%r14), %rcx
leaq 0x18(%r15), %rbx
leaq 0x30(%r15), %rdx
leaq 0x48(%r15), %r10
movq 0x8(%rsp), %r8
movl 0x18(%r8), %r8d
subq $0x80, %rsp
movq 0x80(%rsp), %r9
movq %r9, 0x40(%rsp)
movq %r13, 0x38(%rsp)
movq %rax, 0x30(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movq %r10, 0x80(%rsp)
movq %r10, 0x8(%rsp)
movq %rdx, (%rsp)
movq $0x40, 0x70(%rsp)
movq $0x2, 0x68(%rsp)
andq $0x0, 0x60(%rsp)
movq $0x48, 0x48(%rsp)
movq $0x10, 0x28(%rsp)
movq %r14, %rdx
movq %r15, %r8
movq %rbx, %r9
movsd 0x90(%rsp), %xmm0
callq 0x16da55
addq $0x80, %rsp
testq %rax, %rax
js 0x16fcc0
addq $0x18, %r12
leaq 0x60(%r15), %rax
movq (%rbp), %rdi
movq (%r14), %rdx
leaq 0xa8(%r15), %r10
subq $0x8, %rsp
movq %r14, %rsi
movq %r13, %rcx
movq %r15, %r8
movq %r12, %r9
pushq %rbx
pushq %rax
pushq 0x18(%rsp)
pushq %r10
pushq 0x18(%rdx)
callq 0x16e8d1
addq $0x30, %rsp
testl %eax, %eax
movq 0x8(%rsp), %rbp
je 0x16fcc0
addq $0x18, %r15
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x167b9e
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq