// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movsd %xmm0, 0x30(%rsp)
movq %r9, %r15
movq %r8, %rbx
movq %rcx, 0x28(%rsp)
andq $0x0, (%r8)
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
testq %rdx, %rdx
je 0x17a2ac
movl $0x80000000, %r12d      # imm = 0x80000000
movq %r14, 0x18(%rsp)
leaq 0x28(%r14), %r13
xorl %ebp, %ebp
movq %r15, 0x10(%rsp)
movq %rdx, 0x20(%rsp)
cmpq %rbp, %rdx
je 0x17a1d5
movzbl (%r13), %r14d
testb %r14b, %r14b
js 0x17a1cc
movl -0x14(%r13), %r15d
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x17a1a7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x20(%rsp), %rdx
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
shrl $0x6, %r14d
shlq $0x21, %r15
shlq $0x20, %r14
orq %rbp, %r15
orq %r14, %r15
orq %r12, %r15
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %r15, (%rax,%rcx,8)
movq 0x10(%rsp), %r15
incq %rbp
addq $0x30, %r13
jmp 0x17a15d
pushq $0x1c
popq %r13
xorl %ebp, %ebp
cmpq 0x10(%r15), %rbp
jae 0x17a249
movq (%r15), %rax
movl -0xc(%rax,%r13), %r15d
movl (%rax,%r13), %r14d
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x17a21f
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
shlq $0x21, %r15
shlq $0x20, %r14
orq %r15, %r14
orq %rbp, %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r14, (%rax,%rcx,8)
incq %rbp
addq $0xa8, %r13
movq 0x10(%rsp), %r15
jmp 0x17a1db
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x156b0d
movq (%rbx), %rax
pushq $0x1
popq %rcx
xorl %edx, %edx
xorl %esi, %esi
movq 0x18(%rsp), %r14
cmpq %rax, %rcx
ja 0x17a2a9
movq 0x10(%rbx), %rdi
movq (%rdi,%rdx,8), %r8
je 0x17a284
movq (%rdi,%rcx,8), %r9
xorq %r8, %r9
shrq $0x20, %r9
je 0x17a28c
testl %r8d, %r8d
js 0x17a291
movq %rcx, %rdx
incq %rcx
jmp 0x17a268
cmpq %rcx, %rdx
jae 0x17a289
movl (%rdi,%rdx,8), %r8d
xorq %r12, %r8
movq %r8, (%rdi,%rsi,8)
incq %rsi
incq %rdx
jmp 0x17a291
movq %rsi, (%rbx)
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x17a527
movq 0x80(%rsp), %rbp
addq 0x8(%rbp), %rax
cmpq %rax, 0x10(%rbp)
jae 0x17a317
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
incq %rcx
movq %rcx, 0x10(%rbp)
movq (%rbp), %rdi
imulq $0x1c, %rcx, %rsi
callq 0x2370
movq %rax, (%rbp)
xorl %r13d, %r13d
xorl %r12d, %r12d
cmpq 0x10(%r15), %r12
jae 0x17a45a
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
cmpq 0x10(%rbp), %rcx
jne 0x17a35b
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbp)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbp)
movq 0x8(%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbp)
imulq $0x1c, %rcx, %rdx
leaq (%rax,%rdx), %rdi
movq 0x28(%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 0x17a41c
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %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), %eax
andl %r9d, %eax
subl 0x14(%rsi,%r13), %eax
jmp 0x17a42b
movl 0x14(%rsi,%r13), %eax
movl %eax, 0x10(%rdi)
movl 0x18(%rsi,%r13), %eax
incl %eax
movl %eax, 0x14(%rdi)
cmpq $0x0, 0x78(%rsp)
je 0x17a44b
movq 0x8(%rsp), %rax
movq (%rax), %rsi
callq 0x178842
testl %eax, %eax
jne 0x17a44b
decq 0x8(%rbp)
incq %r12
addq $0xa8, %r13
jmp 0x17a31d
xorl %r15d, %r15d
movl $0x3fffffff, %r12d      # imm = 0x3FFFFFFF
cmpq (%rbx), %r15
jae 0x17a527
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
cmpq 0x10(%rbp), %rcx
jne 0x17a4a0
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbp)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbp)
movq 0x8(%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbp)
imulq $0x1c, %rcx, %rcx
movq 0x10(%rbx), %rdx
imulq $0x30, (%rdx,%r15,8), %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 %r12d, %esi
orl %edx, %esi
movl %esi, 0x18(%rax,%rcx)
movq 0x10(%rbx), %rsi
imulq $0x30, (%rsi,%r15,8), %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
subl %edi, %r8d
cvtsi2sd %r8, %xmm0
movq %rsi, 0x10(%rax,%rcx)
mulsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
andl %r12d, %esi
orl %edx, %esi
movl %esi, 0x18(%rax,%rcx)
incq %r15
jmp 0x17a463
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq