// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movsd %xmm0, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
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
cmpq %rax, 0x10(%r14)
jae 0x1ccc88
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(%r14)
movq (%r14), %rdi
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax), %rsi
incq %rsi
callq 0x2370
movq %rax, (%r14)
movq 0x8(%rbx), %rax
movq %rax, 0x8(%r14)
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1cce81
movq %r14, 0x40(%rsp)
movl $0x1, %r13d
xorl %ebp, %ebp
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
movsd 0x5889a(%rip), %xmm2    # 0x225558
xorl %r12d, %r12d
cmpq %rax, %r13
je 0x1cccf4
movq (%rbx), %rax
leaq (,%r13,8), %rcx
addq %r13, %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r13, %rcx
movl 0xc(%rax,%rcx), %ecx
leaq (%r12,%r12,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r12, %rdx
cmpl 0xc(%rax,%rdx), %ecx
je 0x1cce6f
movq %r13, %rsi
subq %r12, %rsi
jbe 0x1ccd22
movq (%rbx), %rax
leaq (%r12,%r12,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r12, %rcx
addq %rcx, %rax
addq $0xc, %rax
movq %r12, %rcx
shrl (%rax)
incq %rcx
addq $0x1c, %rax
cmpq %rcx, %r13
jne 0x1ccd14
movq %r13, %rax
cmpq $0x2, %rsi
jb 0x1ccd9a
leaq (%r12,%r12,8), %rax
leaq (%rax,%rax,2), %rdx
addq %r12, %rdx
movq (%rbx), %rdi
addq %rdx, %rdi
movq 0x40(%rsp), %rax
addq (%rax), %rdx
movq (%r15), %rax
movq %r12, 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 0x1cc55a
movsd 0x587c7(%rip), %xmm2    # 0x225558
movl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
addq %r12, %rax
subq %r12, %rax
jbe 0x1cce6c
leaq (%r12,%r12,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r12, %rcx
addq $0x18, %rcx
movq (%rbx), %rdx
movl -0x4(%rdx,%rcx), %r8d
subl -0x8(%rdx,%rcx), %r8d
jbe 0x1cce5f
movq (%r15), %rsi
movq 0x10(%rsi), %rsi
movl -0xc(%rdx,%rcx), %edi
leaq (%rdi,%rdi,4), %rdi
movl (%rsi,%rdi,8), %r9d
andl %r14d, %r9d
subl %r8d, %r9d
jb 0x1cce5f
cmpl $0x10, %r9d
ja 0x1cce5f
movl (%rdx,%rcx), %r8d
andl %r10d, %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
leaq (%rsi,%rdi,8), %rsi
movl 0x10(%rsi), %esi
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1cce5f
leaq (%rdx,%rcx), %rsi
addq $-0x18, %rsi
movl $0x0, 0x10(%rsi)
movq (%r15), %rdi
movq 0x10(%rdi), %rdi
movl 0xc(%rsi), %r8d
leaq (%r8,%r8,4), %r8
movl (%rdi,%r8,8), %edi
andl %r14d, %edi
movl %edi, 0x14(%rsi)
leaq (,%rbp,8), %rdi
addq %rbp, %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rbp, %rdi
incq %rbp
movupd (%rsi), %xmm0
movupd 0xc(%rsi), %xmm1
movupd %xmm1, 0xc(%rdx,%rdi)
movupd %xmm0, (%rdx,%rdi)
addq $0x1c, %rcx
decq %rax
jne 0x1ccdb2
movq %r13, %r12
incq %r13
movq 0x8(%rbx), %rax
cmpq %rax, %r13
jbe 0x1cccc1
jmp 0x1cce83
xorl %ebp, %ebp
movq %rbp, 0x8(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq