// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: gen_linear_rchains(kv_ul_ov_t*, kv_ul_ov_t*, asg_t const*, ug_opt_t const*, long, double, long, Chain_Data*, long)
void gen_linear_rchains(kv_ul_ov_t *res, kv_ul_ov_t *buf, const asg_t *rg, const ug_opt_t *uopt, int64_t bw, 
double diff_ec_ul, int64_t qlen, Chain_Data* dp, int64_t ulid)
{
	uint64_t k, l, z, an, m, bn = buf->n; 
    radix_sort_ul_ov_srt_tn(res->a, res->a + res->n);
    ///after this function, res keeps unitig alignment, while buf keeps read alignments
    for (k = 1, l = m = 0; k <= res->n; k++) {
        if(k == res->n || res->a[k].tn != res->a[l].tn) {///qn <- (tn|rev)
			for (z = l; z < k; z++) res->a[z].tn>>=1;
			kv_resize(ul_ov_t, *buf, bn+k-l); 
			an = l + linear_rchain_dp_adv(res->a+l, k-l, buf->a+bn, uopt, bw, diff_ec_ul, qlen, 
			UG_SKIP_N, UG_ITER_N, UG_DIS_N, dp, rg, ulid);
			for (z = l; z < an; z++) res->a[m++] = res->a[z];
			l = k;
		}
    }
    res->n = m; 
	// kv_resize(ul_ov_t, *buf, bn+res->n);
	// int64_t iqs, iqe, its, ite;
	// for (k = 0, z = bn; k < res->n; k++) {
	// 	buf->a[z] = res->a[k]; res->a[k].qn = z; 
	// 	extend_end_coord(NULL, &(res->a[k]), qlen, rg->seq[res->a[k].tn].len, &iqs, &iqe, &its, &ite);
	// 	res->a[k].qs = iqs; res->a[k].qe = iqe; res->a[k].ts = its; res->a[k].te = ite;
	// 	z++;
	// }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movsd %xmm0, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, %r12
movq %rdi, %rbx
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rsp)
movq (%rdi), %rdi
movq 0x8(%rbx), %rsi
leaq (%rsi,%rsi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rdi, %rsi
addq %rax, %rsi
callq 0x195abc
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1d1b52
movl $0x1, %r13d
movq $0x0, (%rsp)
xorl %r14d, %r14d
cmpq %rax, %r13
je 0x1d1a07
movq (%rbx), %rax
leaq (,%r13,8), %rcx
addq %r13, %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r13, %rcx
movl 0xc(%rax,%rcx), %ecx
leaq (%r14,%r14,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r14, %rdx
cmpl 0xc(%rax,%rdx), %ecx
je 0x1d1b40
movq %r13, %r15
subq %r14, %r15
jbe 0x1d1a35
movq (%rbx), %rax
leaq (%r14,%r14,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r14, %rcx
addq %rcx, %rax
addq $0xc, %rax
movq %r14, %rcx
shrl (%rax)
incq %rcx
addq $0x1c, %rax
cmpq %rcx, %r13
jne 0x1d1a27
movq 0x8(%rsp), %rax
subq %r14, %rax
addq %r13, %rax
cmpq %rax, 0x10(%r12)
jae 0x1d1aa0
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, 0x10(%r12)
movq (%r12), %rdi
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax), %rsi
incq %rsi
callq 0x2370
movq %rax, (%r12)
leaq (%r14,%r14,8), %rax
leaq (%rax,%rax,2), %rbp
addq %r14, %rbp
movq (%rbx), %rdi
addq %rbp, %rdi
movq 0x8(%rsp), %rcx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rdx
addq %rcx, %rdx
addq (%r12), %rdx
subq $0x8, %rsp
movq %r15, %rsi
movq 0x20(%rsp), %r8
movsd 0x28(%rsp), %xmm0
pushq 0x18(%rsp)
pushq 0x70(%rsp)
pushq $0xc350                 # imm = 0xC350
pushq $0x1388                 # imm = 0x1388
pushq $0x64
callq 0x1d110e
addq $0x30, %rsp
leaq (%rax,%r14), %rcx
cmpq %rcx, %r14
jae 0x1d1b3d
negq %rax
movq (%rsp), %rdi
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
xorl %edx, %edx
movq (%rbx), %rsi
movups (%rsi,%rbp), %xmm0
movups 0xc(%rsi,%rbp), %xmm1
movups %xmm1, 0xc(%rsi,%rcx)
movups %xmm0, (%rsi,%rcx)
decq %rdx
addq $0x1c, %rcx
addq $0x1c, %rbp
cmpq %rdx, %rax
jne 0x1d1b11
subq %rdx, %rdi
movq %rdi, (%rsp)
movq %r13, %r14
incq %r13
movq 0x8(%rbx), %rax
cmpq %rax, %r13
jbe 0x1d19d4
jmp 0x1d1b5a
movq $0x0, (%rsp)
movq (%rsp), %rax
movq %rax, 0x8(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq