// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: direct_ctg_trans_chain(mg_tbuf_s*, unsigned int, glchain_t*, gdpchain_t*, st_mt_t*, haplotype_evdience_alloc*, ul_idx_t const*, ug_opt_t const*, long, double, long, long, Chain_Data*, ma_ug_t*, scaf_res_t*, ma_ug_t*, scaf_res_t*, ma_ug_t*, bubble_type*, kv_u_trans_t*, asg_t const*, unsigned long, unsigned long)
uint32_t direct_ctg_trans_chain(mg_tbuf_t *b, uint32_t id, 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, ma_ug_t *qry, scaf_res_t *qry_sc, ma_ug_t *ref, scaf_res_t *ref_sc, ma_ug_t *gfa, bubble_type *bub, kv_u_trans_t *ta, const asg_t *rg, uint64_t soff, uint64_t is_exact)
{
	// res->bb.n = 0;
	// if(ulid != 86660) return 0;
	kv_ul_ov_t *idx = &(ll->lo), *init = &(ll->tk); ///int64_t max_idx;
	asg64_v b0, b1; uint32_t is_self = (qry?0:1); idx->n = 0;
	
	cl_trans_gen(id, qry?qry->u.a[id].len:ref->u.a[id].len, qry_sc?&(qry_sc->a[id]):&(ref_sc->a[id]), idx, uref, ref, ref_sc, gfa, bub, ta, is_self, is_exact);
	if(idx->n == 0) return 0;

	copy_asg_arr(b0, (*sps)); 
	ctg_trans_gp_chain(id, idx, uref, uopt, bw, diff_ec_ul, qry?qry->u.a[id].len:ref->u.a[id].len, dp, ref, is_self, &b0, ((is_exact)?(0.5):(0.666666)));
	copy_asg_arr((*sps), b0); 
	if(idx->n == 0) return 0;

	copy_asg_arr(b0, (*sps)); copy_asg_arr(b1, ll->srt.a); 
	filter_ctg_trans(id, qry, ref, idx, &b0, &b1, 0.5, 10000, /**0.333333**/0.85, 10000, 0.3, soff, is_self);
	copy_asg_arr((*sps), b0); copy_asg_arr(ll->srt.a, b1); 
	if(idx->n == 0) return 0;

	push_ctg_trans_res(id, idx, init);

	/**
	// gl_rg2ug_gen(rch, idx, uref, 1, 2, ulid);
	ctg_rg2ug_gen(rch, idx, uref);

	// uint32_t k;
	// fprintf(stderr, "[M::%s] rch->len::%u, rch->n::%u, idx->n::%u\n", __func__, (uint32_t)rch->len, (uint32_t)rch->n, (uint32_t)idx->n);
	// for (k = 0; k < idx->n; k++) {
	// 	fprintf(stderr, "[k->%u::utg%.6u%c(len->%u::n->%u)]\tq::[%u, %u)\t%c\tt::[%u, %u)\n", 
    //         k, (idx->a[k].tn>>1) + 1, "lc"[uref->ug->u.a[(idx->a[k].tn>>1)].circ], uref->ug->u.a[(idx->a[k].tn>>1)].len, idx->a[k].sec,
    //         idx->a[k].qs, idx->a[k].qe, "+-"[idx->a[k].rev], idx->a[k].ts, idx->a[k].te);
	// }

	// for (k = 0; k < idx->n; k++) {
	// 	fprintf(stderr, "utg%.6u%c,", (idx->a[k].tn>>1)+1, "lc"[uref->ug->u.a[(idx->a[k].tn>>1)].circ]);
	// }
	// fprintf(stderr, "\n");
	
	if(idx->n == 0) return 0;
	///generate linear chains
	gen_linear_chains_ctg(idx, init, uref, uopt, bw, diff_ec_ul, rch->len, dp);
	
	if(idx->n == 0) return 0;
	
	dump_linear_chain(uref->ug, idx, &(gdp->l), rch->len, ulid);
	if(gdp->l.n == 0) return 0;

	kv_resize(uint64_t, ll->srt.a, gdp->l.n);
	max_idx = ctg_chain_graph(b->km, uref, uref->ug, &(gdp->l), &(gdp->swap), &(gdp->dst), &(gdp->out),
        &(gdp->path), rch->len, uopt, G_CHAIN_BW, N_GCHAIN_RATE, ll->srt.a.a, sps, dp, UG_SKIP_GRAPH_N, UG_ITER_N, UG_DIS_N*100);

	//sps -> idx; (gdp->l) -> alignments
	if(max_idx >= 0) {
		max_idx = select_max_ctg_chain(b->km, uref, ulid, sps, &(gdp->l), &(ll->tk), uref->ug->g, &(gdp->dst_done), &(gdp->out), &(gdp->path), &(gdp->swap));
		if(max_idx) {
			push_ctg_res(res, &(gdp->swap));
			return 1;
		}
	}
	**/

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm0, 0x10(%rsp)
movq %r8, %r14
movq %rdx, %rbx
movl %esi, %edi
movq 0xf8(%rsp), %r15
movq 0xb0(%rsp), %r13
movq 0x80(%rsp), %r8
movaps 0xd0(%rsp), %xmm0
movq 0xe0(%rsp), %rax
movq 0xc8(%rsp), %rcx
movq 0xb8(%rsp), %rsi
xorl %r12d, %r12d
testq %r13, %r13
sete %r12b
movq $0x0, 0x8(%rdx)
testq %rsi, %rsi
cmoveq %rcx, %rsi
movl %edi, %ebp
imulq $0x58, %rbp, %rdx
addq (%rsi), %rdx
subq $0x30, %rsp
movl %r15d, 0x28(%rsp)
movl %r12d, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movups %xmm0, 0x8(%rsp)
movq %rcx, (%rsp)
movl %edi, 0x3c(%rsp)
movq %rbx, %rcx
callq 0x1cdeb4
addq $0x30, %rsp
cmpq $0x0, 0x8(%rbx)
je 0x1cfd7d
movq 0xc0(%rsp), %rcx
movq 0xa8(%rsp), %rax
movq 0x90(%rsp), %r8
testq %r13, %r13
movq 0x10(%r14), %rdx
movq %r15, %rdi
leaq 0x18(%rsp), %r15
movq %rdx, 0x10(%r15)
movq (%r14), %rdx
movq %rdx, (%r15)
movq 0x8(%r14), %rdx
movq %rdx, 0x8(%r15)
cmoveq %rcx, %r13
movq 0x10(%r13), %rdx
leaq (,%rbp,4), %rsi
addq %rbp, %rsi
movl (%rdx,%rsi,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
xorl %edx, %edx
testq %rdi, %rdi
sete %dl
leaq 0x63bc6(%rip), %rsi     # 0x233860
movsd (%rsi,%rdx,8), %xmm1
movl 0xc(%rsp), %edi
movq %rbx, %rsi
movsd 0x10(%rsp), %xmm0
pushq %r15
pushq %r12
pushq %rcx
pushq %rax
callq 0x1cf135
addq $0x20, %rsp
movq 0x10(%r15), %rbp
movq %rbp, 0x10(%r14)
movq (%r15), %rax
movq %rax, (%r14)
movq 0x8(%r15), %r13
movq %r13, 0x8(%r14)
cmpq $0x0, 0x8(%rbx)
je 0x1cfd7d
movq %rax, 0x10(%rsp)
movq 0xf0(%rsp), %rax
movq 0x58(%rbx), %rcx
leaq 0x30(%rsp), %r15
movq %rcx, 0x10(%r15)
movq 0x48(%rbx), %rcx
movq %rcx, (%r15)
movq 0x50(%rbx), %rcx
movq %rcx, 0x8(%r15)
movl $0x2710, %edx           # imm = 0x2710
leaq 0x18(%rsp), %r8
movsd 0x557d9(%rip), %xmm0    # 0x2254f0
movsd 0x59e69(%rip), %xmm1    # 0x229b88
movsd 0x54eb9(%rip), %xmm2    # 0x224be0
movl 0xc(%rsp), %edi
movq %rbx, %rcx
movq %r15, %r9
pushq %r12
pushq %rax
pushq %rdx
pushq %rdx
callq 0x1cf33a
addq $0x20, %rsp
movq %rbp, 0x10(%r14)
movq 0x10(%rsp), %rax
movq %rax, (%r14)
movq %r13, 0x8(%r14)
movq 0x10(%r15), %rax
movq %rax, 0x58(%rbx)
movq (%r15), %rax
movq %rax, 0x48(%rbx)
movq 0x8(%r15), %rax
movq %rax, 0x50(%rbx)
cmpq $0x0, 0x8(%rbx)
je 0x1cfd7d
leaq 0x18(%rbx), %rdx
movl 0xc(%rsp), %edi
movq %rbx, %rsi
callq 0x1cfa3b
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq