// File: /workspace/github_cpp_makefile_star2_O2/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)
andq $0x0, 0x20(%rdx)
movq 0xc0(%rsp), %rbx
andq $0x0, 0x8(%rdx)
pushq $0x1
popq %rcx
pushq $0x2
popq %r8
movq %rsi, 0x78(%rsp)
movq %rsi, %rdi
movq %rdx, %rsi
movq %rbx, %rdx
callq 0x1827cc
xorl %ebp, %ebp
cmpq %rbp, 0x8(%r15)
je 0x189499
movq 0xe8(%rsp), %rax
movq 0xd0(%rsp), %r8
leaq 0x18(%r15), %r13
movq %rax, (%rsp)
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movsd 0x68(%rsp), %xmm0
callq 0x1835be
cmpq $0x0, 0x8(%r15)
je 0x1894ad
movq (%rbx), %rdi
movq 0x78(%rsp), %rbx
movl 0x18(%rbx), %ecx
movq %r15, %rsi
movq %r14, %rdx
callq 0x184311
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x189499
cmpq %rax, 0x50(%r15)
movq %r13, 0x80(%rsp)
jae 0x1892eb
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)
jmp 0x1892ef
movq 0x58(%r15), %rax
movq 0xc0(%rsp), %rsi
movsd 0x68(%rsp), %xmm0
movq 0xc8(%rsp), %rcx
movq 0x70(%rsp), %rdx
movq (%rdx), %rdi
movq (%rsi), %rdx
leaq 0x18(%r14), %r11
leaq 0x30(%r14), %r9
leaq 0x48(%r14), %r13
movl 0x18(%rbx), %r10d
cmpq $0x0, 0xf8(%rsp)
je 0x189333
movsd 0x5a24d(%rip), %xmm0    # 0x1e3580
movq 0xe8(%rsp), %r8
movq %r8, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movq %rcx, 0x18(%rsp)
movq %r10, 0x10(%rsp)
movq %r13, (%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 0x5a1ab(%rip), %xmm1    # 0x1e3538
movq %r14, %rcx
movq %r11, %r8
movq %rsi, %rbx
movq %r13, 0x68(%rsp)
movq %r11, %r13
callq 0x16caf8
testq %rax, %rax
js 0x189499
leaq 0x60(%r14), %rax
movq 0x70(%rsp), %rcx
movq (%rcx), %rdi
movq (%rbx), %rcx
movq 0x18(%rcx), %rcx
leaq 0xa8(%r14), %rdx
movq %r13, 0x70(%rsp)
movq %r13, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %rcx, (%rsp)
movq %rbx, %rsi
movq %r12, %rcx
movq %r14, %r8
movq 0x80(%rsp), %r13
movq %r13, %r9
callq 0x16e8d1
testl %eax, %eax
je 0x189499
cmpq $0x0, 0xf8(%rsp)
movq 0x78(%rsp), %rbx
movq 0x70(%rsp), %r8
je 0x189441
movq (%r8), %rax
movl 0x18(%rax), %ecx
subl 0x14(%rax), %ecx
cvtsi2sd %ecx, %xmm0
movl 0x18(%rbx), %eax
cvtsi2sd %rax, %xmm1
mulsd 0x68615(%rip), %xmm1    # 0x1f1a50
ucomisd %xmm1, %xmm0
jb 0x18948f
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 %r13, %rdx
movq %rbx, %rcx
movq %r14, %r9
callq 0x188c69
xorl %ebp, %ebp
cmpb $0x1, 0x50(%rbx)
sete %bpl
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6bd8b(%rip), %rdi     # 0x1f523f
leaq 0x68668(%rip), %rsi     # 0x1f1b23
leaq 0x6bd84(%rip), %rcx     # 0x1f5246
movl $0x3f24, %edx           # imm = 0x3F24
callq 0x2110