// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movq %r9, -0x60(%rbp)
movsd %xmm0, -0x68(%rbp)
movq 0x48(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x48(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x48(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x70(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x40(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq 0x10(%rbp), %rdx
movq 0x30(%rbp), %rcx
callq 0x2ddac0
movq -0x70(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x2dee35
movl $0x0, -0x2c(%rbp)
jmp 0x2df108
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq 0x10(%rbp), %rdx
movq 0x18(%rbp), %rcx
movq 0x20(%rbp), %r8
movsd -0x68(%rbp), %xmm0
movq -0x40(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r9d
movq 0x38(%rbp), %rax
movq %rax, (%rsp)
callq 0x2de9e0
movq -0x70(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x2dee82
movl $0x0, -0x2c(%rbp)
jmp 0x2df108
movq 0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x70(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x40(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %ecx
movq 0x30(%rbp), %r8
callq 0x2d2df0
movq -0x50(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x2deec0
movl $0x0, -0x2c(%rbp)
jmp 0x2df108
jmp 0x2deec2
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x50(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x2defa3
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x50(%rax)
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rcx
shrq %rcx
movq -0x48(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rcx
shrq $0x2, %rcx
movq -0x48(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rcx
shrq $0x4, %rcx
movq -0x48(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rcx
shrq $0x8, %rcx
movq -0x48(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rcx
shrq $0x10, %rcx
movq -0x48(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x50(%rax)
movq -0x48(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x58(%rax)
jmp 0x2defa5
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq 0x10(%rbp), %rsi
movq 0x10(%rbp), %rax
movq (%rax), %rdx
movq -0x50(%rbp), %rcx
movq -0x50(%rbp), %r8
addq $0x18, %r8
movq -0x50(%rbp), %r9
addq $0x30, %r9
movq -0x50(%rbp), %r13
addq $0x48, %r13
movq -0x50(%rbp), %r12
addq $0x60, %r12
movq -0x40(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r15d
movq 0x18(%rbp), %r14
movq -0x48(%rbp), %rax
movq 0x58(%rax), %rbx
movq -0x58(%rbp), %r11
movq 0x38(%rbp), %r10
movq 0x30(%rbp), %rax
movsd 0xa0025(%rip), %xmm0    # 0x37f030
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq $0x10, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq $0x48, 0x40(%rsp)
movq $0x1388, 0x48(%rsp)     # imm = 0x1388
movq $0x4c4b40, 0x50(%rsp)   # imm = 0x4C4B40
movq %rax, 0x58(%rsp)
callq 0x2a66d0
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jl 0x2df101
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq 0x10(%rbp), %rsi
movq 0x30(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x50(%rbp), %r8
movq -0x48(%rbp), %r9
addq $0x18, %r9
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %r14
movq -0x50(%rbp), %rbx
addq $0xa8, %rbx
movq -0x50(%rbp), %r11
addq $0x48, %r11
movq -0x50(%rbp), %r10
addq $0x60, %r10
movq -0x50(%rbp), %rax
addq $0x18, %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x2a9da0
movl %eax, %eax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
je 0x2df0ff
movq 0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
addq $0x18, %rsi
movq 0x30(%rbp), %rdx
callq 0x2dc4b0
movl $0x1, -0x2c(%rbp)
jmp 0x2df108
jmp 0x2df101
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)