// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: gen_rovlp_chain_by_ul(asg_t const*, ul_vec_t*, ul_idx_t const*, kv_ul_ov_t*, kv_ul_ov_t*, mg_lchain_t*, long, vec_mg_lchain_t*, Chain_Data*, long, long, long, long)
void gen_rovlp_chain_by_ul(const asg_t *rg, ul_vec_t *rch, const ul_idx_t *uref, kv_ul_ov_t *raw_idx, kv_ul_ov_t *raw_chn, mg_lchain_t *a, int64_t a_n, vec_mg_lchain_t *res, Chain_Data* dp, 
int64_t dp_max_skip, int64_t dp_max_iter, int64_t dp_max_dis, int64_t ulid)
{
	if(a_n == 0) return;
	int64_t k, l, res_n0 = res->n, tt = 0; ma_ug_t *ug = uref->ug;
	// fprintf(stderr, "***[M::%s::a_n->%ld]\n", __func__, a_n);
	///a[0, a_n) is a gchain of untigs
	for (k = 0, l = ug->g->seq[a[0].v>>1].len; k < a_n; k++) {
		// fprintf(stderr, ">k->%ld, ls->%ld, le->%ld, rev->%c\n", k, l - ug->g->seq[a[k].v>>1].len, l, "+-"[a[k].v&1]);
		l -= ug->g->seq[a[k].v>>1].len;
		gl_ug2rg_gen(rg, rch, ug, &(a[k]), res, l, ulid, raw_idx, raw_chn);
		l += ug->g->seq[a[k].v>>1].len + a[k].dist_pre;
	}
	mg_lchain_t *x = res->a + res_n0; int64_t x_n = res->n - res_n0, fn;
	fn = flat_rovlp_chain(ug, x, x_n, dp, dp_max_skip, dp_max_iter, dp_max_dis);
	// fprintf(stderr, "***[M::%s::x_n->%ld] fn::%ld\n", __func__, x_n, fn);
	if(fn) {
		for (l = -1, k = 0; k <= x_n; k++) {
			if(k < x_n) {
				if(k > 0) x[k].hash_pre = k-1+res_n0;
				else x[k].hash_pre = (uint32_t)-1;
				if(x[k].qs >= 0) tt++;
			}
			if(k == x_n || x[k].qs >=0) { ///x[k] and x[l] are anchors
				if(k-l>1) {
					update_rovlp_chain_qse(ug, l, k, x, x_n, rch->rlen);
					// update_rovlp_chain_qse_back(ug, l, k, x, x_n);
				}
				l = k;
			}
		}
		assert(tt > 0);
	} else {
		res->n = res_n0;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, 0x30(%rsp)
movq %rsi, 0x20(%rsp)
movq 0x70(%rsp), %rcx
testq %rcx, %rcx
je 0x18815c
movq %r9, %r15
movq %rdi, %r13
movq 0x78(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
movq (%rdx), %r14
movq 0x18(%r14), %rax
movq 0x20(%rax), %rax
movl 0x8(%r9), %edx
shrl %edx
movl (%rax,%rdx,8), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorl %r12d, %r12d
testq %rcx, %rcx
cmovgq %rcx, %r12
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
subq $0x1, %r12
jb 0x188059
movl 0x8(%r15), %ecx
shrl %ecx
movl (%rax,%rcx,8), %eax
andq %rbp, %rax
subq %rax, %rbx
movq 0x18(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
movq %r15, %rcx
movq 0x78(%rsp), %r8
movq %rbx, %r9
callq 0x186ed6
movq 0x18(%r14), %rax
movq 0x20(%rax), %rax
movl 0x8(%r15), %ecx
shrl %ecx
movl (%rax,%rcx,8), %ecx
andq %rbp, %rcx
movslq 0x20(%r15), %rdx
addq %rbx, %rdx
addq %rcx, %rdx
addq $0x28, %r15
movq %rdx, %rbx
jmp 0x187fee
movq 0x78(%rsp), %rbp
movq (%rbp), %rbx
movq 0x8(%rbp), %r15
movq 0x28(%rsp), %r12
imulq $0x28, %r12, %r13
leaq (%rbx,%r13), %rsi
subq %r12, %r15
movq 0x98(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %rsi, 0x18(%rsp)
movq %r15, %rdx
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %r8
movq 0x90(%rsp), %r9
callq 0x187afe
testq %rax, %rax
je 0x188158
addq %r13, %rbx
addq $0x14, %rbx
decl %r12d
pushq $-0x1
popq %rsi
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpq %r15, %r13
jg 0x188134
jge 0x1880eb
cmpq $0x1, %r13
movl $0x0, %eax
sbbl %eax, %eax
leal (%r12,%r13), %ecx
orl %ecx, %eax
movl %eax, 0x10(%rbx)
xorl %eax, %eax
cmpl $0x0, (%rbx)
setns %al
addq %rax, %rbp
cmpq %r13, %r15
je 0x1880f5
cmpl $0x0, (%rbx)
js 0x188125
movq %r13, %rcx
subq %rsi, %rcx
movq %r13, %rax
cmpq $0x2, %rcx
jl 0x188128
movq 0x20(%rsp), %rax
movl 0x18(%rax), %r9d
movq %r14, %rdi
movq %r13, %rdx
movq 0x18(%rsp), %rcx
movq %r15, %r8
callq 0x1874f4
movq %r13, %rax
jmp 0x188128
movq %rsi, %rax
incq %r13
addq $0x28, %rbx
movq %rax, %rsi
jmp 0x1880c5
testq %rbp, %rbp
jg 0x18815c
leaq 0x6ccd8(%rip), %rdi     # 0x1f4e18
leaq 0x699dc(%rip), %rsi     # 0x1f1b23
leaq 0x6ccd1(%rip), %rcx     # 0x1f4e1f
movl $0x3d5a, %edx           # imm = 0x3D5A
callq 0x2110
movq %r12, 0x8(%rbp)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq