// File: /workspace/github_cpp_makefile_star2_O3/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 0x70(%rsp), %rbp
testq %rbp, %rbp
je 0x1d48ef
movq %r9, %r12
movq %r8, %r11
movq %rcx, %rbx
movq 0x98(%rsp), %r13
movq 0x90(%rsp), %r9
movq 0x88(%rsp), %r8
movq 0x80(%rsp), %rcx
movq 0x78(%rsp), %r10
movq 0x8(%r10), %rax
movq (%rdx), %r14
movq %rax, %r15
movq %rsi, 0x20(%rsp)
movq %rax, 0x18(%rsp)
jle 0x1d47f0
movq 0x18(%r14), %rax
movq 0x20(%rax), %rax
movl 0x8(%r12), %ecx
shrl %ecx
movl (%rax,%rcx,8), %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq %rbx, 0x30(%rsp)
movq %rbp, 0x28(%rsp)
movl 0x8(%r12), %ecx
shrl %ecx
movl (%rax,%rcx,8), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andq %rcx, %rax
subq %rax, %r15
movq %r11, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movq %r14, %rdx
movq %r12, %rcx
movq %r10, %rbx
movq %r10, %r8
movq %r15, %r9
movq %rdi, %rbp
movq %r14, %r13
movq %r11, %r14
callq 0x1d3212
movq %r14, %r11
movq %r13, %r14
movq %rbp, %rdi
movq 0x28(%rsp), %rbp
movq %rbx, %r10
movq 0x30(%rsp), %rbx
movq 0x20(%rsp), %rsi
movq 0x18(%r13), %rax
movq 0x20(%rax), %rax
movl 0x8(%r12), %ecx
shrl %ecx
movl (%rax,%rcx,8), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andq %rdx, %rcx
movslq 0x20(%r12), %rdx
addq %r15, %rdx
addq %rcx, %rdx
addq $0x28, %r12
movq %rdx, %r15
decq %rbp
jne 0x1d4736
movq 0x8(%r10), %r15
movq 0x18(%rsp), %rax
movq 0x90(%rsp), %r9
movq 0x88(%rsp), %r8
movq 0x80(%rsp), %rcx
movq 0x98(%rsp), %r13
movq (%r10), %rbx
leaq (%rax,%rax,4), %r12
leaq (%rbx,%r12,8), %rsi
subq %rax, %r15
movq %r13, (%rsp)
movq %rax, %r13
movq %r14, %rdi
movq %rsi, 0x28(%rsp)
movq %r15, %rdx
movq %r10, %rbp
callq 0x1d3ebe
testq %rax, %rax
je 0x1d48eb
testq %r15, %r15
movq 0x20(%rsp), %rdx
js 0x1d48cc
leaq (%rbx,%r12,8), %r13
addq $0x14, %r13
leaq 0x1(%r15), %rbp
movq $-0x1, %rsi
xorl %ebx, %ebx
xorl %r12d, %r12d
movq 0x18(%rsp), %rdi
cmpq %r15, %rbx
jge 0x1d4874
cmpq $0x1, %rbx
movl $0x0, %eax
sbbl %eax, %eax
leal (%rdi,%rbx), %ecx
decl %ecx
orl %ecx, %eax
movl %eax, 0x10(%r13)
xorl %eax, %eax
cmpl $0x0, (%r13)
setns %al
addq %rax, %r12
cmpq %rbx, %r15
je 0x1d4880
cmpl $0x0, (%r13)
js 0x1d48b5
movq %rbx, %rcx
subq %rsi, %rcx
movq %rbx, %rax
cmpq $0x2, %rcx
jl 0x1d48b8
movl 0x18(%rdx), %r9d
movq %r14, %rdi
movq %rbx, %rdx
movq 0x28(%rsp), %rcx
movq %r15, %r8
callq 0x1d38c2
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
movq %rbx, %rax
jmp 0x1d48b8
movq %rsi, %rax
incq %rbx
addq $0x28, %r13
movq %rax, %rsi
cmpq %rbx, %rbp
jne 0x1d484c
testq %r12, %r12
jg 0x1d48ef
leaq 0x6e67d(%rip), %rdi     # 0x242f50
leaq 0x6b381(%rip), %rsi     # 0x23fc5b
leaq 0x6e676(%rip), %rcx     # 0x242f57
movl $0x3d5a, %edx           # imm = 0x3D5A
callq 0x2110
movq %r13, 0x8(%rbp)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq