// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0xa8, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
imulq $0x1c, 0x8(%rax), %rax
addq %rax, %rsi
callq 0x27bdd0
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x2deb08
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
shrq %rcx
movq -0x18(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x2, %rcx
movq -0x18(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x4, %rcx
movq -0x18(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x8, %rcx
movq -0x18(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x10, %rcx
movq -0x18(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
imulq $0x1c, 0x10(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0x2deb0a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq $0x1, -0x48(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
ja 0x2ded62
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
je 0x2deb7c
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x50(%rbp), %rdx
addq %rdx, %rcx
cmpl 0xc(%rcx), %eax
je 0x2ded4f
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0x2debb3
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x58(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
shrl %ecx
movl %ecx, 0xc(%rax)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2deb84
movq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
subq -0x50(%rbp), %rax
cmpq $0x1, %rax
jbe 0x2dec60
movq -0x50(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
imulq $0x1c, -0x50(%rbp), %rax
addq %rax, %rdi
movq -0x48(%rbp), %rsi
subq -0x50(%rbp), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
imulq $0x1c, -0x50(%rbp), %rax
addq %rax, %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x30(%rbp), %r9
movsd -0x38(%rbp), %xmm0
movq -0x40(%rbp), %rbx
movq 0x10(%rbp), %r11
movq -0x20(%rbp), %rax
movq (%rax), %r10
movq -0x50(%rbp), %rax
movq %rbx, (%rsp)
movq $0x64, 0x8(%rsp)
movq $0x1388, 0x10(%rsp)     # imm = 0x1388
movq $0xc350, 0x18(%rsp)     # imm = 0xC350
movq %r11, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x2ddff0
movq %rax, %rcx
movq -0x70(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x2ded47
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
imulq $0x1c, -0x58(%rbp), %rax
addq %rax, %rsi
callq 0x30d7f0
cmpq $0x0, %rax
je 0x2ded34
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x58(%rbp), %rcx
addq %rcx, %rax
movl $0x0, 0x10(%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x58(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x58(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x14(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
imulq $0x1c, -0x58(%rbp), %rax
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x68(%rbp), %rdx
movq %rdx, %rsi
addq $0x1, %rsi
movq %rsi, -0x68(%rbp)
imulq $0x1c, %rdx, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rax)
movl 0x18(%rcx), %ecx
movl %ecx, 0x18(%rax)
jmp 0x2ded36
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2dec68
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x2ded51
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x2deb32
movq -0x68(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
addq $0xa8, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax,%rax)