// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: gen_linear_chains_ctg(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_ctg(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)
			for (z = l; z < k; z++) res->a[z].tn>>=1;
			an = k;
			if(k - l > 1) {///if there is only one alignment for a node, then no need for DP
				an = l + linear_ctg_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++) {
				if(is_available_ctg_aln(uref, &(res->a[z]))) {
					res->a[z].ts = 0; res->a[z].te = uref->ug->u.a[res->a[z].tn].len;
					res->a[m++] = res->a[z];
				}
			}
			l = k;
		}
	}
	res->n = m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movsd %xmm0, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, %rbx
movq %rdi, %r15
movq (%rdi), %rdi
imulq $0x1c, 0x8(%r15), %rsi
addq %rdi, %rsi
callq 0x1574e1
movq 0x8(%r15), %rax
cmpq %rax, 0x10(%rbx)
jae 0x18a24b
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(%rbx)
movq (%rbx), %rdi
imulq $0x1c, %rcx, %rsi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%r15), %rax
movq %rbx, 0x48(%rsp)
movq %rax, 0x8(%rbx)
pushq $0x1
popq %r13
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x40(%rsp), %rbx
cmpq 0x8(%r15), %r13
ja 0x18a391
je 0x18a287
movq (%r15), %rax
imulq $0x1c, %r13, %rcx
movl 0xc(%rax,%rcx), %ecx
imulq $0x1c, %rbp, %rdx
cmpl 0xc(%rax,%rdx), %ecx
je 0x18a389
imulq $0x1c, %rbp, %r14
leaq 0xc(%r14), %rax
movq %rbp, %rcx
cmpq %r13, %rcx
jae 0x18a2a6
movq (%r15), %rdx
shrl (%rdx,%rax)
incq %rcx
addq $0x1c, %rax
jmp 0x18a292
movq %r13, %rsi
subq %rbp, %rsi
movq %r13, 0x60(%rsp)
cmpq $0x2, %rsi
jb 0x18a37c
imulq $0x1c, %rbp, %rdx
movq (%r15), %rdi
addq %rdx, %rdi
movq 0x48(%rsp), %rax
addq (%rax), %rdx
movq (%rbx), %rax
movq %rbp, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq $0xc350, 0x18(%rsp)     # imm = 0xC350
movq $0x1388, 0x10(%rsp)     # imm = 0x1388
movq $0x64, 0x8(%rsp)
movq 0x50(%rsp), %r9
movsd 0x58(%rsp), %xmm0
callq 0x189c1e
movq 0x40(%rsp), %rbx
movq %rax, %r13
addq %rbp, %r13
jmp 0x18a37c
movq (%r15), %rsi
addq %r14, %rsi
movq %rbx, %rdi
callq 0x1a352d
testq %rax, %rax
je 0x18a375
movq (%r15), %rax
andl $0x0, 0x10(%rax,%r14)
movq (%rbx), %rcx
movq 0x10(%rcx), %rcx
movl 0xc(%rax,%r14), %edx
imulq $0x28, %rdx, %rdx
movl (%rcx,%rdx), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
movl %ecx, 0x14(%rax,%r14)
imulq $0x1c, %r12, %rcx
incq %r12
movups (%rax,%r14), %xmm0
movups %xmm0, (%rax,%rcx)
movups 0xc(%rax,%r14), %xmm0
movups %xmm0, 0xc(%rax,%rcx)
incq %rbp
addq $0x1c, %r14
cmpq %r13, %rbp
jb 0x18a31f
movq 0x60(%rsp), %r13
movq %r13, %rbp
incq %r13
jmp 0x18a262
movq %r12, 0x8(%r15)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq