// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: gen_linear_chains(kv_ul_ov_t*, kv_ul_ov_t*, ul_idx_t const*, ug_opt_t const*, long, double, long, Chain_Data*)
void gen_linear_chains(kv_ul_ov_t *res, kv_ul_ov_t *buf, const ul_idx_t *uref, const ug_opt_t *uopt, int64_t bw, 
double diff_ec_ul, int64_t qlen, Chain_Data* dp)
{
	uint64_t k, l, z, an, m;
	radix_sort_ul_ov_srt_tn(res->a, res->a + res->n);
	///after this function, res keeps unitig alignment, while buf keeps read alignments
	kv_resize(ul_ov_t, *buf, res->n); buf->n = res->n;
	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)
			// kv_resize(uint64_t, bufg->srt.a, k-l); 
			// kv_resize(uint64_t, *bufs, k-l); 
			for (z = l; z < k; z++) res->a[z].tn>>=1;
			// fprintf(stderr, "\n*[M::%s::] %c\tutg%.6d%c(%u)\tocc:[%lu, %lu)\n", __func__, "+-"[res->a[l].rev], (int32_t)(res->a[l].tn)+1, 
			// "lc"[uref->ug->u.a[res->a[l].tn].circ], uref->ug->u.a[res->a[l].tn].len, l, k);
			an = l + linear_chain_dp_adv(res->a+l, k-l, buf->a+l, uref, uopt, bw, diff_ec_ul, qlen, 
			UG_SKIP_N, UG_ITER_N, UG_DIS_N, dp, uref->ug, l);
			for (z = l; z < an; z++) res->a[m++] = res->a[z];
			// fprintf(stderr, "#occ:[%lu, %lu)\n", l, an);
			l = k;
		}
	}
	res->n = m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movsd %xmm0, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, %r12
movq %rdi, %r15
movq (%rdi), %rdi
imulq $0x1c, 0x8(%r15), %rsi
addq %rdi, %rsi
callq 0x1574e1
movq 0x8(%r15), %rax
cmpq %rax, 0x10(%r12)
jae 0x18364e
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
incq %rcx
movq %rcx, 0x10(%r12)
movq (%r12), %rdi
imulq $0x1c, %rcx, %rsi
callq 0x2370
movq %rax, (%r12)
movq 0x8(%r15), %rax
movq %rax, 0x8(%r12)
pushq $0x1
popq %r13
xorl %ebp, %ebp
xorl %ebx, %ebx
cmpq 0x8(%r15), %r13
ja 0x18373b
je 0x183680
movq (%r15), %rax
imulq $0x1c, %r13, %rcx
movl 0xc(%rax,%rcx), %ecx
imulq $0x1c, %rbp, %rdx
cmpl 0xc(%rax,%rdx), %ecx
je 0x183733
movq (%r15), %rdi
imulq $0x1c, %rbp, %r14
leaq (%rdi,%r14), %rax
addq $0xc, %rax
movq %rbp, %rcx
cmpq %r13, %rcx
jae 0x1836a2
shrl (%rax)
incq %rcx
addq $0x1c, %rax
jmp 0x183692
imulq $0x1c, %rbp, %rdx
addq %rdx, %rdi
movq %r13, %rsi
subq %rbp, %rsi
addq (%r12), %rdx
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq %rbp, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x20(%rsp)
movq $0xc350, 0x18(%rsp)     # imm = 0xC350
movq $0x1388, 0x10(%rsp)     # imm = 0x1388
movq $0x64, 0x8(%rsp)
movq 0x48(%rsp), %r9
movsd 0x50(%rsp), %xmm0
callq 0x182ff4
addq %rbp, %rax
imulq $0x1c, %rbx, %rcx
cmpq %rax, %rbp
jae 0x183730
movq (%r15), %rdx
incq %rbx
movups (%rdx,%r14), %xmm0
movups 0xc(%rdx,%r14), %xmm1
movups %xmm1, 0xc(%rdx,%rcx)
movups %xmm0, (%rdx,%rcx)
incq %rbp
addq $0x1c, %r14
addq $0x1c, %rcx
jmp 0x183704
movq %r13, %rbp
incq %r13
jmp 0x18365b
movq %rbx, 0x8(%r15)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq