// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: direct_gchain(mg_tbuf_s*, ul_vec_t*, glchain_t*, gdpchain_t*, st_mt_t*, haplotype_evdience_alloc*, ul_idx_t const*, ug_opt_t const*, long, double, long, long, Chain_Data*, asg_t const*, long)
uint32_t direct_gchain(mg_tbuf_t *b, ul_vec_t *rch, glchain_t *ll, gdpchain_t *gdp, st_mt_t *sps, haplotype_evdience_alloc *hap, const ul_idx_t *uref, const ug_opt_t *uopt,
int64_t bw, double diff_ec_ul, int64_t max_skip, int64_t ulid, Chain_Data* dp, const asg_t *rg, int64_t linear_only)
{
	// if(ulid != 86660) return 0;
	kv_ul_ov_t *idx = &(ll->lo), *init = &(ll->tk); int64_t max_idx;
	idx->n = init->n = 0;
	gl_rg2ug_gen(rch, idx, uref, 1, 2, ulid);
	if(idx->n == 0) return 0;
	///generate linear chains
	gen_linear_chains(idx, init, uref, uopt, bw, diff_ec_ul, rch->rlen, dp);
	// gen_linear_chains_backup(idx, init, uref, uopt, bw, diff_ec_ul, rch->rlen, max_skip, ll, sps);
// 	gen_linear_chains_backup(kv_ul_ov_t *res, kv_ul_ov_t *buf, const ul_idx_t *uref, const ug_opt_t *uopt, int64_t bw, 
// double diff_ec_ul, int64_t qlen, int64_t max_skip, glchain_t *bufg, st_mt_t *bufs)
	assert(idx->n);
	if(idx->n == 0) return 0;
	///idx includes unitig alignments, while init keeps read alignments 
	// fprintf(stderr, "\n++[M::%s::%.*s(id:%ld), len:%u] idx->n:%lu\n", __func__, UL_INF.nid.a[ulid].n, UL_INF.nid.a[ulid].a,
	// ulid, rch->rlen, (uint64_t)idx->n);

	dump_linear_chain(uref->ug, idx, &(gdp->l), rch->rlen, ulid);
	if(gdp->l.n == 0) return 0;
	// fprintf(stderr, "\n+++[M::%s::id->%ld, len->%u] idx->n:%lu\n", __func__, ulid, rch->rlen, (uint64_t)idx->n);
	// kv_resize(uint64_t, ll->srt.a, idx->n); kv_resize(uint64_t, hap->snp_srt, idx->n); kv_resize(uint64_t, gdp->v, idx->n);
	// occ = gl_chain_advance(&(gdp->l), &(gdp->swap), uref, uopt, G_CHAIN_BW, diff_ec_ul, qlen, UG_SKIP, dumy->overlapID, ll->srt.a.a, hap->snp_srt.a, G_CHAIN_TRANS_WEIGHT, 0, NULL, uref->ug, debug_i, km);
	// print_ru_raw_chains(idx, init,  &(gdp->l), rch, uref->ug);

	kv_resize(uint64_t, ll->srt.a, gdp->l.n);
	max_idx = gl_chain_graph(b->km, uref, uref->ug, &(gdp->l), &(gdp->swap), &(gdp->dst), &(gdp->out),
        &(gdp->path), rch->rlen, uopt, G_CHAIN_BW, linear_only?-1:diff_ec_ul, N_GCHAIN_RATE, ll->srt.a.a, sps, dp, UG_SKIP_GRAPH_N, UG_ITER_N, UG_DIS_N, 1);
	//sps -> idx; (gdp->l) -> alignments
	if(max_idx >= 0) {
		max_idx = select_max_gchain(b->km, uref, ulid, sps, &(gdp->l), &(ll->tk), uref->ug->g, &(gdp->dst_done), &(gdp->out), &(gdp->path), &(gdp->swap));
		if(max_idx) {
			if((!linear_only) || ((linear_only) && (gdp->swap.a[0].qe-gdp->swap.a[0].qs) 
																		>= (rch->rlen*P_CHAIN_COV))) {
				update_ul_vec_t(uref, idx, init, rch, &(gdp->swap), &(gdp->l), ulid, rg, dp, UG_SKIP_GRAPH_N, UG_ITER_N, UG_DIS_N);
			} 
			return (rch->dd == 1?1:0);
		}
	}
	// if(max_idx >= 0 && select_max_gchain(b->km, uref, ulid, sps, &(gdp->l), &(ll->tk), uref->ug->g, &(gdp->dst_done), &(gdp->out), &(gdp->path), &(gdp->swap))){
	// 	update_ul_vec_t(uref, idx, init, rch, &(gdp->swap), &(gdp->l), ulid, rg, dp, UG_SKIP_GRAPH_N, UG_ITER_N, UG_DIS_N);
	// 	return (rch->dd == 1?1: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, bw, diff_ec_ul, N_GCHAIN_RATE, ll->srt.a.a, sps, gdp->f.a, hap->snp_srt.a, gdp->v.a);
	// fprintf(stderr, "++++[M::%s::id->%ld, len->%u] gdp->l.n:%lu\n", __func__, ulid, rch->rlen, (uint64_t)gdp->l.n);
	// fprintf(stderr, "+[M::%s::] gdp->l.n:%lu\n", __func__, (uint64_t)gdp->l.n);
	//sps has the chain idx; gdp->l has the chain
	if(max_idx >= 0 && gen_max_gchain_adv(b->km, uref, ulid, sps, &(gdp->l), idx, init, rch->rlen, P_CHAIN_COV, 0.3, 
			0.1, PRIMARY_UL_CHAIN_MIN, uref->ug->g, &(gdp->dst_done), &(gdp->out), &(gdp->path), ll->srt.a.a, &(gdp->swap))) {
		// update_ul_vec_t(rch, &(gdp->l), uref);
		// fprintf(stderr, "\n++[M::%s::(id:%ld), len:%u]\n", __func__, ulid, rch->rlen);
		update_ul_vec_t(uref, idx, init, rch, &(gdp->swap), &(gdp->l), ulid);
		// __ac_X31_hash_string("hehe");
		// if(rch->dd == 1) {
		// 	fprintf(stderr, "[M::%s::%.*s(id:%ld)] ulen->%u\n", __func__, UL_INF.nid.a[ulid].n, UL_INF.nid.a[ulid].a, ulid, rch->rlen);
		// }
		return (rch->dd == 1?1:0);
	// } else {
	// 	// uint64_t i;
	// 	fprintf(stderr, "unsuccess->[M::%s::id->%ld, len->%u] gdp->l.n:%lu\n", __func__, ulid, rch->rlen, (uint64_t)gdp->l.n);
	// 	for (i = 0; i < gdp->l.n; ++i) {
	// 		fprintf(stderr, "(%lu)\t%u\t%u\t%c\tutg%.6d%c(%u)\t%u\t%u\tsrc:%u\tscore:%d\n", 
	// 			i, gdp->l.a[i].qs, gdp->l.a[i].qe, "+-"[gdp->l.a[i].v&1], (int32_t)(gdp->l.a[i].v>>1)+1, "lc"[uref->ug->u.a[gdp->l.a[i].v>>1].circ], uref->ug->u.a[gdp->l.a[i].v>>1].len,
	// 			gdp->l.a[i].rs, gdp->l.a[i].re, gdp->l.a[i].v^1, gdp->l.a[i].score);
	// 	}
	}
	**/

	

	// occ = gl_chain_advance(idx, ll->tk.a+ll->tk.n, uref, uopt, G_CHAIN_BW, diff_ec_ul, qlen, UG_SKIP, dumy->overlapID, ll->srt.a.a, hap->snp_srt.a, G_CHAIN_TRANS_WEIGHT, 0, NULL, uref->ug, debug_i, km);

	// simple_g_chain_dp(idx, buf->a, uref, uopt, bw, diff_ec_ul, rch->rlen, max_skip, ll->srt.a.a, hap->snp_srt.a, sps->a);
	// if(check_extension_end(idx, rch->rlen, buf->a)) {
	// 	// ug2rg_gen(idx->a[idx->n-1].qs, idx->a[idx->n-1].qe, buf->a + idx->a[idx->n-1].ts, idx->a[idx->n-1].te - idx->a[idx->n-1].ts, rch);
	// } else {///need graph chaining

	// }

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movsd %xmm0, 0x68(%rsp)
movq %r8, %r12
movq %rcx, %r14
movq %rdx, %r15
movq %rdi, 0x70(%rsp)
movq 0xc0(%rsp), %rbx
xorl %r13d, %r13d
movq %r13, 0x20(%rdx)
movq %r13, 0x8(%rdx)
movl $0x1, %ecx
movl $0x2, %r8d
movq %rsi, 0x78(%rsp)
movq %rsi, %rdi
movq %rdx, %rsi
movq %rbx, %rdx
callq 0x1c4557
cmpq %r13, 0x8(%r15)
je 0x1cbda3
movq 0xe8(%rsp), %rax
movq 0xd0(%rsp), %r8
leaq 0x18(%r15), %rbp
movq %rax, (%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movsd 0x68(%rsp), %xmm0
callq 0x1c548f
cmpq $0x0, 0x8(%r15)
je 0x1cbdb8
movq (%rbx), %rdi
movq 0x78(%rsp), %rbx
movl 0x18(%rbx), %ecx
movq %r15, %rsi
movq %r14, %rdx
callq 0x1c6397
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x1cbda3
movq %rbp, 0x80(%rsp)
cmpq %rax, 0x50(%r15)
jae 0x1cbbf7
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(%r15)
movq 0x58(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x58(%r15)
movq 0xc8(%rsp), %rax
movq 0x70(%rsp), %rcx
movq (%rcx), %rdi
movq 0xc0(%rsp), %rsi
movq (%rsi), %rdx
leaq 0x18(%r14), %r11
leaq 0x30(%r14), %r9
leaq 0x48(%r14), %rbp
movl 0x18(%rbx), %ecx
cmpq $0x0, 0xf8(%rsp)
movsd 0x68(%rsp), %xmm0
je 0x1cbc3a
movsd 0x598fe(%rip), %xmm0    # 0x225538
movq 0x58(%r15), %r10
movq 0xe8(%rsp), %r8
movq %r8, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rbp, (%rsp)
movq $0x1, 0x58(%rsp)
movq $0xc350, 0x50(%rsp)     # imm = 0xC350
movq $0x1388, 0x48(%rsp)     # imm = 0x1388
movq $0x48, 0x40(%rsp)
movq $0x10, 0x20(%rsp)
movsd 0x59850(%rip), %xmm1    # 0x2254e8
movq %r14, %rcx
movq %r11, %r8
movq %r11, %rbx
callq 0x1ad145
testq %rax, %rax
js 0x1cbda3
leaq 0x60(%r14), %rax
movq 0x70(%rsp), %rcx
movq (%rcx), %rdi
movq 0xc0(%rsp), %rsi
movq (%rsi), %rcx
movq 0x18(%rcx), %rcx
leaq 0xa8(%r14), %rdx
movq %rbx, 0x68(%rsp)
movq %rbx, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movq %rbp, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %rcx, (%rsp)
movq %r12, %rcx
movq %r14, %r8
movq 0x80(%rsp), %rbp
movq %rbp, %r9
callq 0x1af191
testl %eax, %eax
je 0x1cbda3
cmpq $0x0, 0xf8(%rsp)
movq 0x78(%rsp), %rbx
movq 0x68(%rsp), %r8
je 0x1cbd4a
movq (%r8), %rax
movl 0x18(%rax), %ecx
subl 0x14(%rax), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movl 0x18(%rbx), %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x67b6c(%rip), %xmm1    # 0x2338b0
ucomisd %xmm1, %xmm0
jb 0x1cbd98
movq 0xf0(%rsp), %rax
movq 0xe8(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x8(%rsp)
movq $0xc350, 0x28(%rsp)     # imm = 0xC350
movq $0x1388, 0x20(%rsp)     # imm = 0x1388
movq $0x48, 0x18(%rsp)
movq 0xc0(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movq %r14, %r9
callq 0x1cb4e1
xorl %r13d, %r13d
cmpb $0x1, 0x50(%rbx)
sete %r13b
movl %r13d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6b2e8(%rip), %rdi     # 0x2370a7
leaq 0x67bc5(%rip), %rsi     # 0x23398b
leaq 0x6b2e1(%rip), %rcx     # 0x2370ae
movl $0x3f24, %edx           # imm = 0x3F24
callq 0x2110