// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: infer_read_ovlp(ul_idx_t const*, overlap_region_alloc*, kv_ul_ov_t*, kv_ul_ov_t*, double, long, ug_opt_t const*, ul_contain*, void*)
uint64_t infer_read_ovlp(const ul_idx_t *uref, overlap_region_alloc* olist, kv_ul_ov_t *in, kv_ul_ov_t *res, double diff_ec_ul, int64_t winLen, const ug_opt_t *uopt, ul_contain *ct, void *km)
{
	uint64_t t, k, l, t_0, pb, cut = res->n, c_occ = 0;; 
	ma_ug_t *ug = uref->ug;
	ma_utg_t *u = NULL; 
	overlap_region* o = NULL;
	ul_ov_t *z = NULL;
	utg_ct_t p;
	// res->n = 0;
	for (t = 0; t < in->n; t++) {
		if(!(in->a[t].tn&(uint32_t)(0x80000000))) {///uid
			u = &(ug->u.a[in->a[t].tn]); o = &(olist->list[in->a[t].qn]);
			assert(o->y_id == in->a[t].tn);
			for (k = l = 0, pb = res->n+2; k < u->n; k++) {
				p.x = u->a[k]>>32; 
				p.s = l; p.e = l + Get_READ_LENGTH(R_INF, (u->a[k]>>33));
				l += (uint32_t)u->a[k];
				if(p.e <= in->a[t].ts) continue;
				if(p.s >= in->a[t].te) break;
				t_0 = gen_contain_chain(uref, &p, o, res, -1, -1, km);	
				assert(t_0 > 0);
				// if(t_0 == 0) {
				// 	fprintf(stderr, "ERROR-2, o->x_id:%u, o->y_id:%u, k:%lu, u->n:%lu, p.s:%u, p.e:%u, ts:%u, te:%u, rev:%u\n", 
				// 	o->x_id, o->y_id, k, (uint64_t)u->n, p.s, p.e, in->a[t].ts, in->a[t].te, in->a[t].rev);
				// }
				res->a[res->n-1].el = in->a[t].el;
				res->a[res->n-1].sec = in->a[t].sec;
				res->a[res->n-1].tn <<= 1; 
				res->a[res->n-1].tn >>= 1;
				res->a[res->n-1].qn = o->x_id;
				if(res->n >= pb) {
					if(in->a[t].rev == 0) {
						// if(res->a[res->n-1].qs > res->a[res->n-2].qe) fprintf(stderr, "ERROR-3\n");
						if(!(res->a[res->n-2].qs<=res->a[res->n-1].qs && res->a[res->n-1].qs <= res->a[res->n-2].qe 
							&& res->a[res->n-2].qe <= res->a[res->n-1].qe)) {
								fprintf(stderr, "ERROR-3\n");
						}
						// if(res->a[res->n-1].qs == res->a[res->n-2].qe) {
						// 	if(res->a[res->n-2].qe < in->a[t].qe) res->a[res->n-2].qe++;
						// 	else if(res->a[res->n-1].qs > 0) res->a[res->n-1].qs--;
						// }
					} else {
						if(!(res->a[res->n-1].qs<=res->a[res->n-2].qs && res->a[res->n-2].qs <= res->a[res->n-1].qe 
							&& res->a[res->n-1].qe <= res->a[res->n-2].qe)) {
								fprintf(stderr, "ERROR-4\n");
						}
					}
					
					// debug_infer_read_ovlp(uopt, diff_ec_ul, 
					// 	in->a[t].rev?&(res->a[res->n-2]):&(res->a[res->n-1]), 
					// 			in->a[t].rev?&(res->a[res->n-1]):&(res->a[res->n-2]), u, k, k-1);
				}
			}
		} else {///rid
			kv_push(ul_ov_t, *res, in->a[t]);
			// res->a[res->n-1].tn <<= 1; 
			// res->a[res->n-1].tn >>= 1;
			res->a[res->n-1].qn = o->x_id;
			c_occ++;
		}
	}
	if(res->n != cut) {
		radix_sort_ul_ov_srt_qe(res->a + cut, res->a + res->n);
		if(c_occ) {
			int64_t ci, cn = cut;
			for (k = cut; k < res->n; k++) {
				z = &(res->a[k]);
				if(z->tn&((uint32_t)(0x80000000))) continue;
				if(ct->is_c.a[z->tn] == 0) continue;

				for (ci = k+1; ci < (int64_t)(res->n); ci++) {
					if(res->a[ci].qe > z->qe) break;
					if(res->a[ci].qn == (uint32_t)-1) continue;
					if(!(res->a[ci].tn&((uint32_t)(0x80000000)))) continue;
					if(z->qs <= res->a[ci].qs && z->qe >= res->a[ci].qe) {
						if(check_contain_pair(uopt, (res->a[ci].tn<<1)>>1, z->tn,  1)) {
							res->a[ci].qn = (uint32_t)-1;
						}
					}
				}

				for (ci = k-1; ci >= cn; ci--) {
					if(res->a[ci].qe <= z->qs) break;
					if(res->a[ci].qn == (uint32_t)-1) continue;
					if(!(res->a[ci].tn&((uint32_t)(0x80000000)))) continue;
					if(z->qs <= res->a[ci].qs && z->qe >= res->a[ci].qe) {
						if(check_contain_pair(uopt, (res->a[ci].tn<<1)>>1, z->tn,  1)) {
							res->a[ci].qn = (uint32_t)-1;
						}
					}
				}
			}

			// debug_contain_ovlps(res->a+cut, res->n-cut, uopt);

			for (k = l = cut; k < res->n; k++) {
				if(res->a[k].qn == (uint32_t)-1) continue;
				if(k != l) {
					res->a[l] = res->a[k];
				}
				res->a[l].tn <<= 1; res->a[l].tn >>= 1;
				++l;
			}
			res->n = l;
		}
	}

	return res->n - cut;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, 0x30(%rsp)
movq %rcx, %r9
movq %rsi, 0x60(%rsp)
movq 0x8(%rcx), %rax
movq %rax, 0x10(%rsp)
movq %rdi, 0x68(%rsp)
movq (%rdi), %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x38(%rsp)
leaq 0xf1d7e(%rip), %r15     # 0x254230
xorl %r12d, %r12d
xorl %ebx, %ebx
movq %rcx, 0x8(%rsp)
movq %rdx, 0x18(%rsp)
cmpq 0x8(%rdx), %rbx
jae 0x162777
movq (%rdx), %rcx
imulq $0x1c, %rbx, %r10
movslq 0xc(%rcx,%r10), %rax
testq %rax, %rax
movq %r10, 0x20(%rsp)
js 0x1626f6
movq %rbx, 0x40(%rsp)
addq %r10, %rcx
movq 0x60(%rsp), %rsi
movq (%rsi), %r12
movl (%rcx), %ecx
imulq $0xa8, %rcx, %rcx
cmpl %eax, 0x10(%r12,%rcx)
jne 0x16298c
addq %rcx, %r12
imulq $0x28, %rax, %r13
movq 0x58(%rsp), %rax
addq 0x10(%rax), %r13
movq 0x8(%r9), %rax
addq $0x2, %rax
movq %rax, 0x70(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
movl 0x10(%r13), %eax
cmpq %rax, %rbp
jae 0x1626ef
movq 0x18(%r13), %rax
movq (%rax,%rbp,8), %rbx
movq %rbx, %rax
shrq $0x20, %rax
movl %eax, 0x4c(%rsp)
movl %r14d, 0x50(%rsp)
movq 0x18(%r15), %rax
movq %rbx, %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %ecx
addl %r14d, %ecx
movl %ecx, 0x54(%rsp)
movq (%rdx), %rax
cmpl %ecx, 0x10(%rax,%r10)
jae 0x1626e4
addq %r10, %rax
cmpl %r14d, 0x14(%rax)
jbe 0x1626ef
movq %r14, 0x28(%rsp)
movq %rbx, %r14
movq %rbp, %rbx
movq %r13, %rbp
movq %r15, %r13
movq 0x68(%rsp), %rdi
leaq 0x4c(%rsp), %rsi
movq %r12, %rdx
movq %r9, %rcx
movsd 0x80fd7(%rip), %xmm0    # 0x1e3580
pushq $-0x1
popq %r8
movq %r9, %r15
movq 0xb8(%rsp), %r9
callq 0x161004
testq %rax, %rax
je 0x16296d
movq 0x18(%rsp), %rdx
movq (%rdx), %rax
movq 0x20(%rsp), %r10
movl 0x18(%rax,%r10), %eax
movl $0x40000000, %ecx       # imm = 0x40000000
andl %ecx, %eax
movq (%r15), %rcx
imulq $0x1c, 0x8(%r15), %rdi
movl -0x4(%rcx,%rdi), %esi
movl $0xbfffffff, %r8d       # imm = 0xBFFFFFFF
andl %r8d, %esi
orl %eax, %esi
movl %esi, -0x4(%rcx,%rdi)
movq (%rdx), %rax
movl 0x18(%rax,%r10), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
movq (%r15), %rcx
imulq $0x1c, 0x8(%r15), %rdi
movl -0x4(%rcx,%rdi), %esi
movl $0xc0000000, %r8d       # imm = 0xC0000000
andl %r8d, %esi
orl %eax, %esi
movl %esi, -0x4(%rcx,%rdi)
movq (%r15), %rax
movq 0x8(%r15), %rdi
imulq $0x1c, %rdi, %rcx
andl $0x7fffffff, -0x10(%rax,%rcx) # imm = 0x7FFFFFFF
movl (%r12), %esi
movl %esi, -0x1c(%rax,%rcx)
cmpq 0x70(%rsp), %rdi
movq %r15, %r9
movq %r13, %r15
movq %rbp, %r13
movq %rbx, %rbp
movq %r14, %rbx
movq 0x28(%rsp), %r14
jb 0x1626e4
addq %rcx, %rax
movq (%rdx), %rcx
cmpl $0x0, 0x18(%rcx,%r10)
js 0x162697
movl -0x18(%rax), %ecx
leaq 0x868fd(%rip), %rdi     # 0x1e8f73
cmpl %ecx, -0x34(%rax)
ja 0x1626c0
movl -0x30(%rax), %esi
leaq 0x868ee(%rip), %rdi     # 0x1e8f73
cmpl %esi, %ecx
ja 0x1626c0
leaq 0x868e3(%rip), %rdi     # 0x1e8f73
cmpl -0x14(%rax), %esi
jbe 0x1626e4
jmp 0x1626c0
movl -0x34(%rax), %ecx
leaq 0x8ce5d(%rip), %rdi     # 0x1ef4fe
cmpl %ecx, -0x18(%rax)
ja 0x1626c0
movl -0x14(%rax), %esi
leaq 0x8ce4e(%rip), %rdi     # 0x1ef4fe
cmpl %esi, %ecx
ja 0x1626c0
leaq 0x8ce43(%rip), %rdi     # 0x1ef4fe
cmpl -0x30(%rax), %esi
jbe 0x1626e4
movq 0xf0919(%rip), %rax     # 0x252fe0
movq (%rax), %rcx
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x20(%rsp), %r10
movq 0x18(%rsp), %rdx
movq 0x8(%rsp), %r9
addq %rbx, %r14
incq %rbp
jmp 0x16252b
movq 0x40(%rsp), %rbx
jmp 0x16276f
movq (%r9), %rax
movq 0x8(%r9), %rdi
cmpq 0x10(%r9), %rdi
jne 0x16273a
leaq (%rdi,%rdi), %rcx
testq %rdi, %rdi
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x10(%r9)
imulq $0x1c, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x20(%rsp), %r10
movq 0x18(%rsp), %rdx
movq 0x8(%rsp), %r9
movq %rax, (%r9)
movq (%rdx), %rcx
movq 0x8(%r9), %rdi
leaq 0x1(%rdi), %rsi
movq %rsi, 0x8(%r9)
imulq $0x1c, %rdi, %rsi
movups (%rcx,%r10), %xmm0
movups 0xc(%rcx,%r10), %xmm1
movups %xmm1, 0xc(%rax,%rsi)
movups %xmm0, (%rax,%rsi)
movl (%r12), %eax
movq (%r9), %rcx
imulq $0x1c, 0x8(%r9), %rsi
movl %eax, -0x1c(%rcx,%rsi)
incq 0x38(%rsp)
incq %rbx
jmp 0x1624c1
movq 0x8(%r9), %rcx
movq 0x10(%rsp), %rbx
movq %rbx, %rax
cmpq %rbx, %rcx
je 0x16295b
movq (%r9), %rax
imulq $0x1c, %rbx, %r14
leaq (%rax,%r14), %rdi
imulq $0x1c, %rcx, %rsi
addq %rax, %rsi
movq %r9, %r12
callq 0x156f5c
cmpq $0x0, 0x38(%rsp)
je 0x162956
leaq 0x1(%rbx), %r15
movq %rbx, %r13
leaq -0x1(%rbx), %r8
movq %r12, %r9
movq 0x8(%r9), %rcx
cmpq %rcx, %r13
jae 0x1628f7
movq (%r9), %rbp
movq %r13, 0x28(%rsp)
imulq $0x1c, %r13, %rax
movslq 0xc(%rbp,%rax), %rdx
testq %rdx, %rdx
js 0x1628e0
movq 0xb0(%rsp), %rsi
movq 0x40(%rsi), %rsi
cmpb $0x0, (%rsi,%rdx)
je 0x1628e0
addq %rax, %rbp
movq %r14, %rbx
movq %r15, %r12
cmpq %rcx, %r12
jge 0x16286f
movq (%r9), %rax
movl 0x24(%rax,%rbx), %ecx
cmpl 0x8(%rbp), %ecx
ja 0x16286f
cmpl $-0x1, 0x1c(%rax,%rbx)
je 0x162862
movl 0x28(%rax,%rbx), %esi
testl %esi, %esi
jns 0x162862
movl 0x4(%rbp), %ecx
cmpl 0x20(%rax,%rbx), %ecx
ja 0x162862
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl 0xc(%rbp), %edx
movq 0x30(%rsp), %rdi
pushq $0x1
popq %rcx
movq %r14, %r13
movq %r8, %r14
callq 0x162035
movq %r14, %r8
movq %r13, %r14
movq 0x8(%rsp), %r9
testl %eax, %eax
je 0x162862
movq (%r9), %rax
orl $-0x1, 0x1c(%rax,%rbx)
movq 0x8(%r9), %rcx
incq %r12
addq $0x1c, %rbx
jmp 0x162806
movq %r14, %rbx
movq %r8, %r12
cmpq 0x10(%rsp), %r12
jl 0x1628e0
movq (%r9), %rcx
movl -0x14(%rcx,%rbx), %eax
movl 0x4(%rbp), %edx
cmpl %edx, %eax
jbe 0x1628e0
cmpl $-0x1, -0x1c(%rcx,%rbx)
je 0x1628d7
movl -0x10(%rcx,%rbx), %esi
testl %esi, %esi
jns 0x1628d7
cmpl -0x18(%rcx,%rbx), %edx
ja 0x1628d7
cmpl %eax, 0x8(%rbp)
jb 0x1628d7
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl 0xc(%rbp), %edx
movq 0x30(%rsp), %rdi
pushq $0x1
popq %rcx
movq %r14, %r13
movq %r8, %r14
callq 0x162035
movq %r14, %r8
movq %r13, %r14
movq 0x8(%rsp), %r9
testl %eax, %eax
je 0x1628d7
movq (%r9), %rax
orl $-0x1, -0x1c(%rax,%rbx)
decq %r12
addq $-0x1c, %rbx
jmp 0x162875
movq 0x28(%rsp), %r13
incq %r13
incq %r15
addq $0x1c, %r14
incq %r8
jmp 0x1627c0
movq 0x10(%rsp), %rbx
imulq $0x1c, %rbx, %rdx
movq %rbx, %rax
movq %rbx, %rsi
cmpq %rcx, %rsi
jae 0x162950
movq (%r9), %rdi
cmpl $-0x1, (%rdi,%rdx)
je 0x162947
cmpq %rax, %rsi
je 0x16293a
leaq (%rdi,%rdx), %rcx
imulq $0x1c, %rax, %r8
movups (%rcx), %xmm0
movups 0xc(%rcx), %xmm1
movups %xmm1, 0xc(%rdi,%r8)
movups %xmm0, (%rdi,%r8)
movq (%r9), %rdi
movq 0x8(%r9), %rcx
imulq $0x1c, %rax, %r8
andb $0x7f, 0xf(%rdi,%r8)
incq %rax
incq %rsi
addq $0x1c, %rdx
jmp 0x162906
movq %rax, 0x8(%r9)
jmp 0x16295b
movq 0x8(%r12), %rax
subq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8fb9d(%rip), %rdi     # 0x1f2511
leaq 0x8f1a8(%rip), %rsi     # 0x1f1b23
leaq 0x8faf7(%rip), %rcx     # 0x1f2479
movl $0xc4a, %edx            # imm = 0xC4A
callq 0x2110
leaq 0x8facf(%rip), %rdi     # 0x1f2462
leaq 0x8f189(%rip), %rsi     # 0x1f1b23
leaq 0x8fad8(%rip), %rcx     # 0x1f2479
movl $0xc42, %edx            # imm = 0xC42
callq 0x2110