// File: /workspace/github_cpp_makefile_star2_O1/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), %r11
testq %r11, %r11
je 0x1ca9c3
movq %r9, %r15
movq 0x78(%rsp), %r9
movq 0x8(%r9), %rax
movq %rax, 0x20(%rsp)
movq (%rdx), %r12
movq %rsi, %r14
jle 0x1ca895
movq %rdi, %rbp
movq 0x18(%r12), %rax
movq 0x20(%rax), %rax
movl 0x8(%r15), %edx
shrl %edx
movl (%rax,%rdx,8), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movq %r8, 0x18(%rsp)
movq %r12, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq 0x18(%r12), %rax
movq 0x20(%rax), %rax
movl 0x8(%r15), %edx
shrl %edx
movl (%rax,%rdx,8), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andq %rdx, %rax
subq %rax, %rbx
movq %r8, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %r9, %r13
movq %r9, %r8
movq %rbx, %r9
movq %r11, %r12
callq 0x1c9368
movq %r12, %r11
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r12
movq 0x18(%rsp), %r8
movq %r13, %r9
movq 0x18(%r12), %rax
movq 0x20(%rax), %rax
movl 0x8(%r15), %edx
shrl %edx
movl (%rax,%rdx,8), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andq %rdx, %rax
movslq 0x20(%r15), %rdx
addq %rax, %rdx
addq %rdx, %rbx
addq $0x28, %r15
decq %r11
jne 0x1ca80b
movq %r14, 0x18(%rsp)
movq (%r9), %rbx
movq 0x8(%r9), %r15
movq 0x20(%rsp), %r13
leaq (,%r13,4), %r14
addq %r13, %r14
leaq (%rbx,%r14,8), %rsi
subq %r13, %r15
movq 0x98(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %rsi, 0x28(%rsp)
movq %r15, %rdx
movq 0x80(%rsp), %rcx
movq %r9, %rbp
movq 0x88(%rsp), %r8
movq 0x90(%rsp), %r9
callq 0x1c9fd9
testq %rax, %rax
je 0x1ca9bf
testq %r15, %r15
movq 0x18(%rsp), %rdx
js 0x1ca9a0
leaq (%rbx,%r14,8), %rbx
addq $0x14, %rbx
leaq 0x1(%r15), %rbp
movq $-0x1, %rsi
xorl %r13d, %r13d
xorl %r14d, %r14d
movq 0x20(%rsp), %rdi
cmpq %r15, %r13
jge 0x1ca94a
cmpq $0x1, %r13
movl $0x0, %eax
sbbl %eax, %eax
leal (%rdi,%r13), %ecx
decl %ecx
orl %ecx, %eax
movl %eax, 0x10(%rbx)
xorl %eax, %eax
cmpl $0x0, (%rbx)
setns %al
addq %rax, %r14
cmpq %r13, %r15
je 0x1ca954
cmpl $0x0, (%rbx)
js 0x1ca989
movq %r13, %rcx
subq %rsi, %rcx
movq %r13, %rax
cmpq $0x2, %rcx
jl 0x1ca98c
movl 0x18(%rdx), %r9d
movq %r12, %rdi
movq %r13, %rdx
movq 0x28(%rsp), %rcx
movq %r15, %r8
callq 0x1c9a04
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %r13, %rax
jmp 0x1ca98c
movq %rsi, %rax
incq %r13
addq $0x28, %rbx
movq %rax, %rsi
cmpq %r13, %rbp
jne 0x1ca924
testq %r14, %r14
jg 0x1ca9c3
leaq 0x6c2d9(%rip), %rdi     # 0x236c80
leaq 0x68fdd(%rip), %rsi     # 0x23398b
leaq 0x6c2d2(%rip), %rcx     # 0x236c87
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