// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rsp)
movq (%rdi), %rdi
imulq $0x1c, 0x8(%r15), %rsi
addq %rdi, %rsi
callq 0x1574e1
pushq $0x1
popq %rbx
xorl %r13d, %r13d
xorl %r14d, %r14d
cmpq 0x8(%r15), %rbx
ja 0x18e44f
je 0x18e350
movq (%r15), %rax
imulq $0x1c, %rbx, %rcx
movl 0xc(%rax,%rcx), %ecx
imulq $0x1c, %r13, %rdx
cmpl 0xc(%rax,%rdx), %ecx
je 0x18e447
imulq $0x1c, %r13, %rbp
leaq 0xc(%rbp), %rax
movq %r13, %rcx
cmpq %rbx, %rcx
jae 0x18e36f
movq (%r15), %rdx
shrl (%rdx,%rax)
incq %rcx
addq $0x1c, %rax
jmp 0x18e35b
movq (%r12), %rax
movq 0x8(%rsp), %rcx
subq %r13, %rcx
addq %rbx, %rcx
cmpq %rcx, 0x10(%r12)
jae 0x18e3d1
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
incq %rdx
movq %rdx, 0x10(%r12)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r12)
imulq $0x1c, %r13, %rdi
addq (%r15), %rdi
movq %rbx, %rsi
subq %r13, %rsi
imulq $0x1c, 0x8(%rsp), %rdx
addq %rax, %rdx
subq $0x8, %rsp
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 0x18dcfa
addq $0x30, %rsp
addq %r13, %rax
imulq $0x1c, %r14, %rcx
cmpq %rax, %r13
jae 0x18e444
movq (%r15), %rdx
incq %r14
movups (%rdx,%rbp), %xmm0
movups 0xc(%rdx,%rbp), %xmm1
movups %xmm1, 0xc(%rdx,%rcx)
movups %xmm0, (%rdx,%rcx)
incq %r13
addq $0x1c, %rbp
addq $0x1c, %rcx
jmp 0x18e41a
movq %rbx, %r13
incq %rbx
jmp 0x18e32b
movq %r14, 0x8(%r15)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq