// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: push_ul_ov_t(ul_idx_t*, u_trans_t*, unsigned long, unsigned long, st_mt_t*, overlap_region_alloc*, unsigned long, unsigned long, double, kv_ul_ov_t*)
void push_ul_ov_t(ul_idx_t *udb, u_trans_t *a, uint64_t a_n, uint64_t rid, st_mt_t *sp, overlap_region_alloc* ol, uint64_t len, uint64_t is_arc_filter, double max_err, kv_ul_ov_t *res)
{
	uint64_t cnt, z, k, l, m, spn; ul_ov_t *p; 
	sp->n = 0;
	if(a_n) {
		for (k = sp->n = 0; k < a_n; k++) {
			if(a[k].del) continue;
			z = a[k].tn; z <<= 1; z |= a[k].rev; z <<= 32; 
			z |= k; z |= ((uint64_t)0x80000000);
			kv_push(uint64_t, *sp, z);
		}
		for (k = 0; k < ol->length; k++) {
			z = ol->list[k].y_id; z <<= 1; 
			z |= ol->list[k].y_pos_strand; 
			z <<= 32; z |= k; 
			kv_push(uint64_t, *sp, z);
		}

		radix_sort_gfa64(sp->a, sp->a + sp->n); spn = sp->n;
		for (k = 1, l = m = 0; k <= spn; k++) {
			if(k == spn || (sp->a[l]>>32)!=(sp->a[k]>>32)) {
				if((sp->a[l]&((uint64_t)0x80000000))) {///no overlap within ol
					for (z = l; z < k; z++) {
						sp->a[m++] = (uint32_t)(sp->a[z]-((uint64_t)0x80000000));
					}
				}
				l = k;
			}
		}
		sp->n = m;
	}

	if(ol->length > 0) {
		cnt = res->n + ol->length; kv_resize(ul_ov_t, (*res), cnt); 
		for (k = 0/**, bn = res->n**/; k < ol->length; k++) {
			// if(b->olist.list[k].non_homopolymer_errors == 0) continue;///exact match is not trans
			kv_pushp(ul_ov_t, (*res), &p);
			p->qn = rid; p->tn = ol->list[k].y_id; p->el = 1; 
			p->sec = ol->list[k].non_homopolymer_errors; 
			p->rev = ol->list[k].y_pos_strand;
			p->qs = ol->list[k].x_pos_s; p->qe = ol->list[k].x_pos_e+1;
			if(p->rev) {
				p->ts = udb->ug->u.a[p->tn].len - (ol->list[k].y_pos_e+1); 
				p->te = udb->ug->u.a[p->tn].len - ol->list[k].y_pos_s;
			} else {
				p->ts = ol->list[k].y_pos_s; 
				p->te = ol->list[k].y_pos_e+1;
			}
			// fprintf(stderr, ">0<[M::%s] utg%.6u%c -> utg%.6u%c\n", __func__, 
			// 	p->qn+1, "lc"[s->udb.ug->u.a[p->qn].circ], 
			// 	p->tn+1, "lc"[s->udb.ug->u.a[p->tn].circ]);
			// if(p->ts >= p->te || p->qs >= p->qe)  {
			// 	fprintf(stderr, "+[M::%s]\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\n", __func__, 
			// 		p->qn+1, "lc"[udb->ug->u.a[p->qn].circ], udb->ug->u.a[p->qn].len, p->qs, p->qe, "+-"[p->rev],
			// 		p->tn+1, "lc"[udb->ug->u.a[p->tn].circ], udb->ug->u.a[p->tn].len, p->ts, p->te);
			// }
			if((is_arc_filter) && (!trans_ovlp_connect(p, udb->ug))) res->n--;
			// fprintf(stderr, ">1<[M::%s] utg%.6u%c -> utg%.6u%c\n", __func__, 
			// 	p->qn+1, "lc"[s->udb.ug->u.a[p->qn].circ], 
			// 	p->tn+1, "lc"[s->udb.ug->u.a[p->tn].circ]);
			// else {
			// 	if(i == 5) 
			// 	{
					// fprintf(stderr, ">>>utg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\n", 
					// 	p->qn+1, "lc"[udb->ug->u.a[p->qn].circ], udb->ug->u.a[p->qn].len, p->qs, p->qe, "+-"[p->rev],
					// 	p->tn+1, "lc"[udb->ug->u.a[p->tn].circ], udb->ug->u.a[p->tn].len, p->ts, p->te);
			// 	}
			// }
		}

		for (k = 0; k < sp->n; k++) {
			kv_pushp(ul_ov_t, (*res), &p);
			p->qn = a[sp->a[k]].qn; p->tn = a[sp->a[k]].tn;  
			p->rev = a[sp->a[k]].rev; p->el = 0;
			p->qs = a[sp->a[k]].qs; p->qe = a[sp->a[k]].qe;
			p->ts = a[sp->a[k]].ts; p->te = a[sp->a[k]].te;
			p->sec = (p->qe-p->qs)*max_err; 
			// if(p->ts >= p->te || p->qs >= p->qe)  {
			// 	fprintf(stderr, "-[M::%s]\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\n", __func__, 
			// 		p->qn+1, "lc"[udb->ug->u.a[p->qn].circ], udb->ug->u.a[p->qn].len, p->qs, p->qe, "+-"[p->rev],
			// 		p->tn+1, "lc"[udb->ug->u.a[p->tn].circ], udb->ug->u.a[p->tn].len, p->ts, p->te);
			// }
		}
		
		// if(is_sec_filter) {
		// 	cnt = filter_sec_trans_ovlp(res->a+bn, res->n-bn, rid, sec_rate) + bn;
		// 	res->n = cnt;
		// }	
		// fprintf(stderr, "-1-[M::%s] utg%.6u%c, # ov::%lu\n", 
		// 		__func__, (uint32_t)i+1, "lc"[s->ug->u.a[i].circ], cnt - bn);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movsd %xmm0, 0x18(%rsp)
movq %r9, %r15
movq %r8, %rbx
movq %rcx, 0x20(%rsp)
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
movq $0x0, (%r8)
testq %rdx, %rdx
je 0x1c5766
movl $0x80000000, %r12d      # imm = 0x80000000
movq %r14, (%rsp)
leaq 0x28(%r14), %r13
xorl %r14d, %r14d
movq %r15, 0x10(%rsp)
movzbl (%r13), %ebp
testb %bpl, %bpl
js 0x1c564c
movl -0x14(%r13), %r15d
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x1c5628
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
movq %rbx, %r12
movq %rdx, %rbx
callq 0x2370
movq %rbx, %rdx
movq %r12, %rbx
movl $0x80000000, %r12d      # imm = 0x80000000
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
shrl $0x6, %ebp
shlq $0x21, %r15
shlq $0x20, %rbp
orq %r14, %r15
orq %rbp, %r15
orq %r12, %r15
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %r15, (%rax,%rcx,8)
movq 0x10(%rsp), %r15
incq %r14
addq $0x30, %r13
cmpq %r14, %rdx
jne 0x1c55d4
cmpq $0x0, 0x10(%r15)
movq 0x10(%rbx), %rdi
je 0x1c56da
movl $0x1c, %r14d
xorl %r13d, %r13d
movq (%r15), %rax
movl -0xc(%rax,%r14), %ebp
movl (%rax,%r14), %r15d
movq (%rbx), %rax
cmpq 0x8(%rbx), %rax
jne 0x1c56ac
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
shlq $0x21, %rbp
shlq $0x20, %r15
orq %rbp, %r15
orq %r13, %r15
leaq 0x1(%rax), %rcx
movq %rcx, (%rbx)
movq %r15, (%rdi,%rax,8)
incq %r13
addq $0xa8, %r14
movq 0x10(%rsp), %r15
cmpq 0x10(%r15), %r13
jb 0x1c5670
movq (%rbx), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x19dd99
movq (%rbx), %rax
testq %rax, %rax
je 0x1c575d
leaq 0x1(%rax), %rcx
cmpq $0x3, %rcx
movl $0x2, %edx
cmovaeq %rcx, %rdx
movq 0x10(%rbx), %rsi
movl $0x1, %edi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq (%rsp), %r14
movq (%rsi,%r8,8), %r9
cmpq %rax, %rdi
je 0x1c5727
movq (%rsi,%rdi,8), %r10
xorq %r9, %r10
shrq $0x20, %r10
je 0x1c5753
testl %r9d, %r9d
setns %r9b
cmpq %rdi, %r8
setae %r10b
orb %r9b, %r10b
jne 0x1c5750
movl (%rsi,%r8,8), %r9d
xorq %r12, %r9
movq %r9, (%rsi,%rcx,8)
incq %rcx
incq %r8
cmpq %r8, %rdi
jne 0x1c573a
movq %rdi, %r8
incq %rdi
cmpq %rdx, %rdi
jne 0x1c5711
jmp 0x1c5763
xorl %ecx, %ecx
movq (%rsp), %r14
movq %rcx, (%rbx)
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x1c5a27
movq 0x70(%rsp), %rbp
addq 0x8(%rbp), %rax
cmpq %rax, 0x10(%rbp)
jae 0x1c57e5
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(%rbp)
movq (%rbp), %rdi
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax), %rsi
incq %rsi
callq 0x2370
movq %rax, (%rbp)
cmpq $0x0, 0x10(%r15)
je 0x1c5933
xorl %r13d, %r13d
xorl %r12d, %r12d
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
cmpq 0x10(%rbp), %rcx
jne 0x1c5828
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbp)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbp)
movq 0x8(%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbp)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
leaq (%rax,%rdx), %rdi
movq 0x20(%rsp), %rcx
movl %ecx, (%rax,%rdx)
movq (%r15), %rcx
movl 0x10(%rcx,%r13), %ecx
movl %ecx, 0xc(%rax,%rdx)
movl 0x18(%rax,%rdx), %esi
movl $0x40000000, %r10d      # imm = 0x40000000
orl %r10d, %esi
movl %esi, 0x18(%rax,%rdx)
movq (%r15), %r8
movl 0x30(%r8,%r13), %r8d
movl $0x3fffffff, %r9d       # imm = 0x3FFFFFFF
andl %r9d, %r8d
andl $0xc0000000, %esi       # imm = 0xC0000000
orl %r8d, %esi
movl %esi, 0x18(%rax,%rdx)
movq (%r15), %rsi
movl 0x1c(%rsi,%r13), %r9d
shll $0x1f, %r9d
orl %r9d, %r8d
orl %r10d, %r8d
movl %r8d, 0x18(%rax,%rdx)
movq (%r15), %rsi
movl 0x4(%rsi,%r13), %r8d
movl %r8d, 0x4(%rax,%rdx)
movl 0x8(%rsi,%r13), %r8d
incl %r8d
movl %r8d, 0x8(%rax,%rdx)
testl %r9d, %r9d
je 0x1c58f0
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %edx
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl %r9d, %edx
movl 0x18(%rsi,%r13), %r8d
notl %r8d
addl %edx, %r8d
movl %r8d, 0x10(%rdi)
movl (%rax,%rcx,8), %eax
andl %r9d, %eax
subl 0x14(%rsi,%r13), %eax
jmp 0x1c58ff
movl 0x14(%rsi,%r13), %eax
movl %eax, 0x10(%rdi)
movl 0x18(%rsi,%r13), %eax
incl %eax
movl %eax, 0x14(%rdi)
cmpq $0x0, 0x68(%rsp)
je 0x1c591f
movq 0x8(%rsp), %rax
movq (%rax), %rsi
callq 0x1c3c2d
testl %eax, %eax
jne 0x1c591f
decq 0x8(%rbp)
incq %r12
addq $0xa8, %r13
cmpq 0x10(%r15), %r12
jb 0x1c57eb
cmpq $0x0, (%rbx)
je 0x1c5a27
xorl %r15d, %r15d
movl $0x2, %r12d
movl $0x3fffffff, %r13d      # imm = 0x3FFFFFFF
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
cmpq 0x10(%rbp), %rdx
jne 0x1c5984
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
cmoveq %r12, %rcx
movq %rcx, 0x10(%rbp)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbp)
movq 0x8(%rbp), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x8(%rbp)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdx, %rcx
movq 0x10(%rbx), %rdx
movq (%rdx,%r15,8), %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
movl 0x8(%r14,%rdx), %esi
movl %esi, (%rax,%rcx)
movl 0x14(%r14,%rdx), %esi
movl %esi, 0xc(%rax,%rcx)
movl 0x28(%r14,%rdx), %edx
andl $-0x40, %edx
shll $0x19, %edx
movl 0x18(%rax,%rcx), %esi
andl %r13d, %esi
orl %edx, %esi
movl %esi, 0x18(%rax,%rcx)
movq 0x10(%rbx), %rsi
movq (%rsi,%r15,8), %rsi
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %rsi
movl (%r14,%rsi), %edi
movl %edi, 0x4(%rax,%rcx)
movl 0x4(%r14,%rsi), %r8d
movl %r8d, 0x8(%rax,%rcx)
movq 0xc(%r14,%rsi), %rsi
movq %rsi, 0x10(%rax,%rcx)
subl %edi, %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
mulsd 0x18(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
andl %r13d, %esi
orl %edx, %esi
movl %esi, 0x18(%rax,%rcx)
incq %r15
cmpq (%rbx), %r15
jb 0x1c594c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq