// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
movq %rsi, %rbp
movq 0x88(%rsp), %rax
andq $0x0, 0x20(%rax)
movq %rdi, %r14
andq $0x0, 0x20(%rdx)
movq 0x50(%rsp), %r13
andq $0x0, 0x8(%rdx)
movq %rsi, %rdi
movq %rdx, %rsi
movq %r13, %rdx
callq 0x189874
xorl %ebx, %ebx
cmpq %rbx, 0x8(%r12)
je 0x18a59e
movq %r14, (%rsp)
movq 0x60(%rsp), %r8
leaq 0x18(%r12), %r14
subq $0x8, %rsp
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movsd 0x18(%rsp), %xmm0
pushq 0x80(%rsp)
callq 0x18a1bf
popq %rax
popq %rcx
cmpq $0x0, 0x8(%r12)
je 0x18a59e
movq (%r13), %rdi
movl (%rbp), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %r12, %rsi
movq %r15, %rdx
callq 0x184311
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x18a59e
movq 0x58(%rsp), %rdi
cmpq %rax, 0x50(%r12)
jae 0x18a4c7
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 0x58(%rsp), %rdi
movq %rax, 0x58(%r12)
jmp 0x18a4cc
movq 0x58(%r12), %rax
movq (%r13), %rdx
leaq 0x18(%r15), %r12
movl (%rbp), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subq $0x60, %rsp
movq 0xd8(%rsp), %rsi
movq %rsi, 0x38(%rsp)
movq 0x68(%rsp), %rbp
movq %rbp, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movq %rdi, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq $0x4c4b40, 0x50(%rsp)   # imm = 0x4C4B40
movq $0x1388, 0x48(%rsp)     # imm = 0x1388
movq $0x48, 0x40(%rsp)
movq $0x10, 0x20(%rsp)
movsd 0x59005(%rip), %xmm0    # 0x1e3538
movq %r13, %rsi
movq %r15, %rcx
movq %r12, %r8
callq 0x16d7f7
addq $0x60, %rsp
testq %rax, %rax
js 0x18a59e
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 %rbp, %rcx
movq %r15, %r8
movq %r14, %r9
pushq %r12
pushq %rax
pushq %rdx
pushq %r11
pushq 0x18(%r10)
callq 0x16f298
addq $0x30, %rsp
testl %eax, %eax
je 0x18a59e
movq 0x88(%rsp), %rdi
movq %r12, %rsi
callq 0x188d93
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq