// File: /workspace/github_cpp_makefile_star2_O1/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, 0x20(%rsp)
movq %r9, %r15
movq %r8, %rbx
movq %rcx, 0x18(%rsp)
movq %rsi, %r14
movq %rdi, 0x10(%rsp)
movq $0x0, (%r8)
testq %rdx, %rdx
je 0x1bb8f4
movq %r15, 0x8(%rsp)
movl $0x80000000, %r12d      # imm = 0x80000000
movq $0x0, (%rbx)
movq %r14, (%rsp)
leaq 0x28(%r14), %r13
xorl %r14d, %r14d
movzbl (%r13), %r15d
testb %r15b, %r15b
js 0x1bb7ce
movl -0x14(%r13), %ebp
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1bb7a7
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
movq %rdx, %r12
callq 0x2370
movq %r12, %rdx
movl $0x80000000, %r12d      # imm = 0x80000000
movq %rax, 0x10(%rbx)
shrl $0x6, %r15d
shlq $0x21, %rbp
shlq $0x20, %r15
orq %r14, %rbp
orq %r15, %rbp
orq %r12, %rbp
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rsi
movq %rsi, (%rbx)
movq %rbp, (%rcx,%rax,8)
incq %r14
addq $0x30, %r13
cmpq %r14, %rdx
jne 0x1bb75f
movq 0x8(%rsp), %r15
cmpq $0x0, 0x10(%r15)
je 0x1bb85e
movl $0x1c, %r14d
xorl %r13d, %r13d
movq (%r15), %rax
movl -0xc(%rax,%r14), %ebp
movl (%rax,%r14), %r15d
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1bb829
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
shlq $0x21, %rbp
shlq $0x20, %r15
orq %rbp, %r15
orq %r13, %r15
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movq %r15, (%rcx,%rax,8)
incq %r13
addq $0xa8, %r14
movq 0x8(%rsp), %r15
cmpq 0x10(%r15), %r13
jb 0x1bb7ef
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x194fef
movq (%rbx), %rax
testq %rax, %rax
je 0x1bb8eb
leaq 0x1(%rax), %rcx
cmpq $0x3, %rcx
movl $0x2, %edx
cmovaeq %rcx, %rdx
movl $0x1, %esi
xorl %ecx, %ecx
xorl %edi, %edi
movq (%rsp), %r14
cmpq %rax, %rsi
je 0x1bb8ab
movq 0x10(%rbx), %r8
movq (%r8,%rsi,8), %r9
xorq (%r8,%rdi,8), %r9
shrq $0x20, %r9
je 0x1bb8e1
movq 0x10(%rbx), %r8
movb 0x3(%r8,%rdi,8), %r9b
shrb $0x7, %r9b
cmpq %rsi, %rdi
setb %r10b
andb %r9b, %r10b
cmpb $0x1, %r10b
jne 0x1bb8de
movl (%r8,%rdi,8), %r9d
xorq %r12, %r9
movq %r9, (%r8,%rcx,8)
incq %rcx
incq %rdi
cmpq %rdi, %rsi
jne 0x1bb8c8
movq %rsi, %rdi
incq %rsi
cmpq %rdx, %rsi
jne 0x1bb894
jmp 0x1bb8f1
xorl %ecx, %ecx
movq (%rsp), %r14
movq %rcx, (%rbx)
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x1bbbc0
movq 0x70(%rsp), %rbp
addq 0x8(%rbp), %rax
cmpq %rax, 0x10(%rbp)
jae 0x1bb968
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 0x1bbac2
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x10(%rbp), %rax
cmpq %rax, 0x8(%rbp)
jne 0x1bb9af
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rbp)
movq (%rbp), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq %rax, (%rbp)
movq (%rbp), %rax
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 0x18(%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 0x1bba7f
movq 0x10(%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 0x1bba8e
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 0x1bbaae
movq 0x10(%rsp), %rax
movq (%rax), %rsi
callq 0x1b9e17
testl %eax, %eax
jne 0x1bbaae
decq 0x8(%rbp)
incq %r12
addq $0xa8, %r13
cmpq 0x10(%r15), %r12
jb 0x1bb979
cmpq $0x0, (%rbx)
je 0x1bbbc0
xorl %r15d, %r15d
movl $0x2, %r12d
movl $0x3fffffff, %r13d      # imm = 0x3FFFFFFF
movq 0x10(%rbp), %rax
cmpq %rax, 0x8(%rbp)
jne 0x1bbb0c
leaq (%rax,%rax), %rcx
testq %rax, %rax
cmoveq %r12, %rcx
movq %rcx, 0x10(%rbp)
movq (%rbp), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq %rax, (%rbp)
movq (%rbp), %rax
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)
movl 0xc(%r14,%rsi), %r9d
movl %r9d, 0x10(%rax,%rcx)
movl 0x10(%r14,%rsi), %esi
movl %esi, 0x14(%rax,%rcx)
subl %edi, %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
mulsd 0x20(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
andl %r13d, %esi
orl %edx, %esi
movl %esi, 0x18(%rax,%rcx)
incq %r15
cmpq (%rbx), %r15
jb 0x1bbadb
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq