// File: /workspace/github_cpp_makefile_star2_O3/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 0x19e855
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1dbbc0
movl $0x1, %r13d
movq $0x0, (%rsp)
xorl %r15d, %r15d
cmpq %rax, %r13
je 0x1dba73
movq (%rbx), %rax
leaq (,%r13,8), %rcx
addq %r13, %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r13, %rcx
movl 0xc(%rax,%rcx), %ecx
leaq (%r15,%r15,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r15, %rdx
cmpl 0xc(%rax,%rdx), %ecx
je 0x1dbbae
movq %r13, %r14
subq %r15, %r14
jbe 0x1dbaa1
movq (%rbx), %rax
leaq (%r15,%r15,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r15, %rcx
addq %rcx, %rax
addq $0xc, %rax
movq %r15, %rcx
shrl (%rax)
incq %rcx
addq $0x1c, %rax
cmpq %rcx, %r13
jne 0x1dba93
movq (%r12), %rax
movq 0x8(%rsp), %rcx
subq %r15, %rcx
addq %r13, %rcx
cmpq %rcx, 0x10(%r12)
jae 0x1dbb0f
decq %rcx
movq %rcx, %rdx
shrq %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x2, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x4, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x8, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x10, %rdx
orq %rcx, %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x10(%r12)
leaq (%rcx,%rcx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx), %rsi
incq %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r12)
leaq (%r15,%r15,8), %rcx
leaq (%rcx,%rcx,2), %rbp
addq %r15, %rbp
movq (%rbx), %rdi
addq %rbp, %rdi
movq 0x8(%rsp), %rsi
leaq (%rsi,%rsi,8), %rcx
leaq (%rcx,%rcx,2), %rdx
addq %rsi, %rdx
addq %rax, %rdx
subq $0x8, %rsp
movq %r14, %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 0x1db1eb
addq $0x30, %rsp
leaq (%rax,%r15), %rcx
cmpq %rcx, %r15
jae 0x1dbbab
movq %rax, %rcx
movq (%rsp), %rsi
addq %rsi, %rax
leaq (%rsi,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rsi, %rdx
movq (%rbx), %rsi
movups (%rsi,%rbp), %xmm0
movups 0xc(%rsi,%rbp), %xmm1
movups %xmm1, 0xc(%rsi,%rdx)
movups %xmm0, (%rsi,%rdx)
addq $0x1c, %rdx
addq $0x1c, %rbp
decq %rcx
jne 0x1dbb80
movq %r13, %r15
movq %rax, (%rsp)
jmp 0x1dbbae
movq %r13, %r15
incq %r13
movq 0x8(%rbx), %rax
cmpq %rax, %r13
jbe 0x1dba40
jmp 0x1dbbc8
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