// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: direct_gchain_scaf(mg_tbuf_s*, ma_utg_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*, ul_vec_t*)
uint32_t direct_gchain_scaf(mg_tbuf_t *b, ma_utg_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, ul_vec_t *res)
{
	res->bb.n = 0;
	// 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);
	ctg_rg2ug_gen(rch, idx, uref, ulid);

	// 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**/UG_DIS_N*100, ulid);

	//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), ulid);
			return 1;
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movsd %xmm0, 0x10(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, %r15
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %r14
movq 0x88(%rsp), %rax
movq 0x50(%rsp), %r13
xorl %ebx, %ebx
movq %rbx, 0x20(%rax)
movq %rbx, 0x20(%rdx)
movq %rbx, 0x8(%rdx)
movq %rsi, %rdi
movq %rdx, %rsi
movq %r13, %rdx
callq 0x1cc1d9
cmpq %rbx, 0x8(%rbp)
je 0x1cd08a
movq %r14, (%rsp)
movq 0x60(%rsp), %r8
leaq 0x18(%rbp), %r14
subq $0x8, %rsp
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
movsd 0x18(%rsp), %xmm0
pushq 0x80(%rsp)
callq 0x1ccbec
addq $0x10, %rsp
cmpq $0x0, 0x8(%rbp)
je 0x1cd08a
movq (%r13), %rdi
movl (%r12), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1c6397
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x1cd08a
movq 0x58(%rsp), %rdi
cmpq %rax, 0x50(%rbp)
jae 0x1ccfb0
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(%rbp)
movq 0x58(%rbp), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x58(%rsp), %rdi
movq %rax, 0x58(%rbp)
movq (%r13), %rdx
leaq 0x18(%r15), %rsi
movl (%r12), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x58(%rbp), %rcx
movq %rsi, %rbp
subq $0x60, %rsp
movq 0xd8(%rsp), %rsi
movq %rsi, 0x38(%rsp)
movq 0x68(%rsp), %r12
movq %r12, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdi, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq $0x4c4b40, 0x50(%rsp)   # imm = 0x4C4B40
movq $0x1388, 0x48(%rsp)     # imm = 0x1388
movq $0x48, 0x40(%rsp)
movq $0x10, 0x20(%rsp)
movsd 0x584ca(%rip), %xmm0    # 0x2254e8
movq %r13, %rsi
movq %r15, %rcx
movq %rbp, %r8
callq 0x1ae088
addq $0x60, %rsp
testq %rax, %rax
js 0x1cd08a
leaq 0x60(%r15), %rax
leaq 0x48(%r15), %rdx
movq (%rsp), %rcx
movq (%rcx), %rdi
movq (%r13), %r10
leaq 0xa8(%r15), %r11
subq $0x8, %rsp
movq %r13, %rsi
movq %r12, %rcx
movq %r15, %r8
movq %r14, %r9
pushq %rbp
pushq %rax
pushq %rdx
pushq %r11
pushq 0x18(%r10)
callq 0x1afc10
addq $0x30, %rsp
testl %eax, %eax
je 0x1cd08a
movq 0x88(%rsp), %rdi
movq %rbp, %rsi
callq 0x1cb61a
movl $0x1, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq