// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: cl_trans_gen(unsigned int, unsigned int, ul_vec_t*, kv_ul_ov_t*, ul_idx_t const*, ma_ug_t*, scaf_res_t*, ma_ug_t*, bubble_type*, kv_u_trans_t*, unsigned int, unsigned int)
void cl_trans_gen(uint32_t qid, uint32_t qlen, ul_vec_t *qstr, kv_ul_ov_t *res, const ul_idx_t *uref, ma_ug_t *ref, scaf_res_t *ref_sc, ma_ug_t *gfa, bubble_type *bub, kv_u_trans_t *ta, uint32_t is_self, uint32_t is_exact)
{
    uint64_t k, l, m = 0, mi, z, zn, a_n, bid, nw; uc_block_t *a; uint32_t avid = ((is_self)?(qid):((uint32_t)-1));
    res->n = 0;
	///identify bubble chain first
	for (k = 0, a = qstr->bb.a; k < qstr->bb.n; k++) {
		z = qstr->bb.a[k].ts; a_n = qstr->bb.a[k].te;
		while (z < a_n) {
			zn = 1;
			zn = pick_bubble(a + z, a_n - z, bub, &bid);
			if((!zn) || (!qry_bub_sc(uref, gfa, ref_sc, bub, bid, z, z + zn, qstr, avid, res))) {///found a bubble; a[z] -> beg; a[z + zn] -> sink
				zn = 1;
			}
			z += zn;
		}
	}

	if(res->n) {///matched bubbles
		for (k = 0; k < res->n; k++) res->a[k].tn = (((uint64_t)res->a[k].tn)<<1)|((uint64_t)res->a[k].rev);
		radix_sort_ul_ov_srt_tn(res->a, res->a + 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;
				a_n = k;
				if(k - l > 1) {///if there is only one alignment for a node, then no need for DP
					a_n = l + cc_bub_match(res->a+l, k-l);
				}
				
				for (z = l; z < a_n; z++) res->a[m++] = res->a[z];
				// fprintf(stderr, "l::%lu, k::%lu, m::%lu\n", l, k, m);
				l = k;
			}
		}
    	res->n = m;
		radix_sort_ul_ov_srt_qe(res->a, res->a + res->n);
		// for (k = 0; k < res->n; k++) {
		// 	fprintf(stderr, "***qsidx::%u(utg%.6u%c), qeidx::%u(utg%.6u%c)\n", res->a[k].qs, (qstr->bb.a[res->a[k].qs].hid)+1, "lc"[gfa->u.a[qstr->bb.a[res->a[k].qs].hid].circ], res->a[k].qe, (qstr->bb.a[res->a[k].qe].hid)+1, "lc"[gfa->u.a[qstr->bb.a[res->a[k].qe].hid].circ]);
		// }
		
	}

	for (k = 0; k < qstr->bb.n; k++) {
		z = qstr->bb.a[k].ts; a_n = qstr->bb.a[k].te;
		for (; z < a_n; z++) {
			///filter by bubble
			for (mi = 0; mi < m && z <= res->a[mi].qe; mi++) {
				if((z >= res->a[mi].qs) && (z <= res->a[mi].qe)) break;
			}
			if((mi < m) && (z >= res->a[mi].qs) && (z <= res->a[mi].qe)) continue;

			///exact match
			if(extract_scaf_res_t(&(qstr->bb.a[z]), uref, ref_sc, avid, res)) continue;

			///trans match
			extract_trans_scaf_res_t(&(qstr->bb.a[z]), uref, ref_sc, gfa, ta, avid, is_exact, res);
		}
	}

	for(k = 0; k < m; k++) {///reset bubble
		res->a[k].el = 1; res->a[k].qn = res->a[k].qs;

		res->a[k].qs = qstr->bb.a[res->a[k].qs].qs;
		res->a[k].qe = qstr->bb.a[res->a[k].qe].qe;

		res->a[k].ts = ref_sc->a[res->a[k].tn].bb.a[res->a[k].ts].qs;
		res->a[k].te = ref_sc->a[res->a[k].tn].bb.a[res->a[k].te].qe;

		nw = MAX(res->a[k].te-res->a[k].ts, res->a[k].qe-res->a[k].qs); nw *= CHAIN_MATCH; 
		res->a[k].sec = ((nw>=0x3fffffff)?(0x3fffffff):(nw));
	}

	// for (k = 0; k < res->n; k++) {
	// 	fprintf(stderr, "[M::%s]\tutg%.6ul(len::%u)\tq::[%u, %u)\t%c\tutg%.6ul(len::%u)\tt::[%u, %u)\tel::%u\ttp::%u\n", __func__, qid + 1, qlen, res->a[k].qs, res->a[k].qe, "+-"[res->a[k].rev], res->a[k].tn+1, ref->u.a[res->a[k].tn].len, res->a[k].ts, res->a[k].te, res->a[k].el, res->a[k].qn);
	// }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, 0x10(%rsp)
movq %rcx, %rbx
xorl %eax, %eax
cmpl $0x1, 0x80(%rsp)
sbbl %eax, %eax
orl %edi, %eax
movq %rax, 0x8(%rsp)
movq $0x0, 0x8(%rcx)
cmpq $0x0, 0x20(%rdx)
movq %rdx, (%rsp)
je 0x1cdf92
movq 0x70(%rsp), %rbp
movq 0x30(%rdx), %r14
xorl %ecx, %ecx
movq 0x30(%rdx), %rax
movq %rcx, 0x18(%rsp)
leaq (%rcx,%rcx,8), %rcx
movl 0xc(%rax,%rcx,4), %r12d
movl 0x10(%rax,%rcx,4), %r15d
cmpl %r15d, %r12d
jae 0x1cdf7c
leaq (%r12,%r12,8), %rax
leaq (%r14,%rax,4), %rdi
movq %r15, %rsi
subq %r12, %rsi
movq %rbp, %rdx
leaq 0x20(%rsp), %rcx
callq 0x1cd1a3
testq %rax, %rax
je 0x1cdf6e
movq %rax, %r13
movq 0x20(%rsp), %r8
addq %r12, %rax
movq 0x10(%rsp), %rdi
movq 0x60(%rsp), %rdx
movq %rbp, %rcx
movq %r12, %r9
pushq %rbx
pushq 0x10(%rsp)
pushq 0x10(%rsp)
pushq %rax
callq 0x1cd259
addq $0x20, %rsp
testq %rax, %rax
jne 0x1cdf74
movl $0x1, %r13d
addq %r13, %r12
cmpq %r15, %r12
jb 0x1cdf1b
movq 0x18(%rsp), %rcx
incq %rcx
movq (%rsp), %rdx
cmpq 0x20(%rdx), %rcx
jb 0x1cdeff
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1ce0de
movq (%rbx), %rcx
addq $0xc, %rcx
movq %rax, %rdx
movq (%rsp), %r14
movl 0xc(%rcx), %esi
shldl $0x1, %esi, (%rcx)
addq $0x1c, %rcx
decq %rdx
jne 0x1cdfad
movq (%rbx), %rdi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rax, %rsi
addq %rdi, %rsi
callq 0x195abc
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1ce0e7
movl $0x1, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
cmpq %rax, %r15
je 0x1ce01f
movq (%rbx), %rax
leaq (%r15,%r15,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r15, %rcx
movl 0xc(%rax,%rcx), %ecx
leaq (,%r13,8), %rdx
addq %r13, %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r13, %rdx
cmpl 0xc(%rax,%rdx), %ecx
je 0x1ce0cc
movq %r15, %rsi
subq %r13, %rsi
jbe 0x1ce054
movq (%rbx), %rax
leaq (,%r13,8), %rcx
addq %r13, %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r13, %rcx
addq %rcx, %rax
addq $0xc, %rax
movq %r13, %rcx
shrl (%rax)
incq %rcx
addq $0x1c, %rax
cmpq %rcx, %r15
jne 0x1ce046
movq %r15, %rax
cmpq $0x2, %rsi
jb 0x1ce07a
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rdi
addq %r13, %rdi
addq (%rbx), %rdi
callq 0x1cd86b
addq %r13, %rax
movq %r13, %rcx
subq %rax, %rcx
jae 0x1ce0c9
leaq (%r12,%r12,8), %rax
leaq (%rax,%rax,2), %rax
addq %r12, %rax
leaq (,%r13,8), %rdx
addq %r13, %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r13, %rdx
xorl %esi, %esi
movq (%rbx), %rdi
movups (%rdi,%rdx), %xmm0
movups 0xc(%rdi,%rdx), %xmm1
movups %xmm1, 0xc(%rdi,%rax)
movups %xmm0, (%rdi,%rax)
decq %rsi
addq $0x1c, %rax
addq $0x1c, %rdx
cmpq %rsi, %rcx
jne 0x1ce0a1
subq %rsi, %r12
movq %r15, %r13
incq %r15
movq 0x8(%rbx), %rax
cmpq %rax, %r15
jbe 0x1cdfec
jmp 0x1ce0ea
xorl %r12d, %r12d
movq (%rsp), %r14
jmp 0x1ce104
xorl %r12d, %r12d
movq %r12, 0x8(%rbx)
movq (%rbx), %rdi
leaq (%r12,%r12,8), %rax
leaq (%rax,%rax,2), %rsi
addq %r12, %rsi
addq %rdi, %rsi
callq 0x195511
cmpq $0x0, 0x20(%r14)
je 0x1ce1e5
xorl %ecx, %ecx
movq 0x30(%r14), %rax
movq %rcx, (%rsp)
leaq (%rcx,%rcx,8), %rcx
movl 0xc(%rax,%rcx,4), %ebp
movl 0x10(%rax,%rcx,4), %r13d
cmpl %r13d, %ebp
jae 0x1ce1d4
testq %r12, %r12
je 0x1ce164
movq (%rbx), %rax
addq $0x4, %rax
movq %r12, %rcx
movl 0x4(%rax), %edx
cmpq %rdx, %rbp
ja 0x1ce158
movl (%rax), %esi
cmpq %rsi, %rbp
jae 0x1ce158
addq $0x1c, %rax
decq %rcx
jne 0x1ce13e
jmp 0x1ce164
cmpq %rdx, %rbp
ja 0x1ce164
movl (%rax), %eax
cmpq %rax, %rbp
jae 0x1ce1c8
leaq (,%rbp,8), %r15
addq %rbp, %r15
shlq $0x2, %r15
movq 0x30(%r14), %rdi
addq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %rbx, %r8
callq 0x1cd9fc
testq %rax, %rax
jne 0x1ce1c8
addq 0x30(%r14), %r15
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0x68(%rsp), %rcx
movq 0x78(%rsp), %r8
movq 0x8(%rsp), %r9
pushq %rbx
movl 0x90(%rsp), %eax
pushq %rax
callq 0x1cdc9b
addq $0x10, %rsp
incq %rbp
cmpq %r13, %rbp
jne 0x1ce12f
movq (%rsp), %rcx
incq %rcx
cmpq 0x20(%r14), %rcx
jb 0x1ce111
testq %r12, %r12
je 0x1ce29b
movl $0x18, %eax
movl $0xc0000000, %ecx       # imm = 0xC0000000
movq (%rbx), %rdx
orl $0x40000000, (%rdx,%rax) # imm = 0x40000000
movq (%rbx), %rdx
movl -0x14(%rdx,%rax), %esi
movl -0x10(%rdx,%rax), %edi
movl %esi, -0x18(%rdx,%rax)
movq 0x30(%r14), %r8
leaq (%rsi,%rsi,8), %rsi
movl 0x4(%r8,%rsi,4), %r9d
movl %r9d, -0x14(%rdx,%rax)
leaq (%rdi,%rdi,8), %rsi
movl 0x8(%r8,%rsi,4), %esi
movl %esi, -0x10(%rdx,%rax)
movq 0x60(%rsp), %rdi
movq (%rdi), %rdi
movl -0xc(%rdx,%rax), %r8d
imulq $0x58, %r8, %r8
movq 0x30(%rdi,%r8), %rdi
movl -0x8(%rdx,%rax), %r8d
leaq (%r8,%r8,8), %r8
movl 0x4(%rdi,%r8,4), %r8d
movl %r8d, -0x8(%rdx,%rax)
movl -0x4(%rdx,%rax), %r10d
leaq (%r10,%r10,8), %r10
movl 0x8(%rdi,%r10,4), %edi
movl %edi, -0x4(%rdx,%rax)
subl %r8d, %edi
subl %r9d, %esi
cmpl %esi, %edi
cmoval %edi, %esi
movl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
cmpl %edi, %esi
jb 0x1ce284
movl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
movl (%rdx,%rax), %edi
andl %ecx, %edi
orl %esi, %edi
movl %edi, (%rdx,%rax)
addq $0x1c, %rax
decq %r12
jne 0x1ce1f8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq