// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %r9, 0x38(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x8(%rcx), %rbx
cmpq $0x0, 0x8(%rdx)
movq %rbx, %r10
je 0x1aab5c
movq %rcx, %r8
movq %rbx, (%rsp)
movq %rdi, 0x28(%rsp)
movq (%rdi), %rax
movq %rax, 0x58(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %rcx, 0x18(%rsp)
movq %rdx, 0x8(%rsp)
movq (%rdx), %rcx
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %r11
addq %rbx, %r11
movslq 0xc(%rcx,%r11), %rax
testq %rax, %rax
movq %r11, 0x10(%rsp)
js 0x1aa888
addq %r11, %rcx
movq 0x60(%rsp), %rsi
movq (%rsi), %r14
movl (%rcx), %ecx
imulq $0xa8, %rcx, %rcx
cmpl %eax, 0x10(%r14,%rcx)
jne 0x1aab90
leaq (%rax,%rax,4), %r10
shlq $0x3, %r10
movq 0x58(%rsp), %rax
addq 0x10(%rax), %r10
addq %rcx, %r14
cmpl $0x0, 0x10(%r10)
je 0x1aa918
movq %rbx, 0x40(%rsp)
movq %r15, 0x20(%rsp)
movq 0x8(%r8), %rax
addq $0x2, %rax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x18(%r10), %rax
movq (%rax,%rbp,8), %rbx
movq %rbx, %rax
shrq $0x20, %rax
movl %eax, 0x4c(%rsp)
movl %r12d, 0x50(%rsp)
leaq 0xebb4e(%rip), %rax     # 0x296230
movq 0x18(%rax), %rax
movq %rbx, %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %ecx
addl %r12d, %ecx
movl %ecx, 0x54(%rsp)
movq (%rdx), %rax
cmpl %ecx, 0x10(%rax,%r11)
jae 0x1aa866
addq %r11, %rax
cmpl %r12d, 0x14(%rax)
jbe 0x1aa879
movq %r10, %r13
movq 0x28(%rsp), %rdi
leaq 0x4c(%rsp), %rsi
movq %r14, %rdx
movq %r8, %rcx
movsd 0x86f0b(%rip), %xmm0    # 0x231638
movq %r8, %r15
movq $-0x1, %r8
movq 0xa8(%rsp), %r9
callq 0x1a8db6
testq %rax, %rax
je 0x1aab71
movq 0x8(%rsp), %rdx
movq (%rdx), %rax
movq 0x10(%rsp), %r11
movl 0x18(%rax,%r11), %eax
movl $0x40000000, %ecx       # imm = 0x40000000
andl %ecx, %eax
movq (%r15), %rcx
movq 0x8(%r15), %rdi
leaq (%rdi,%rdi,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdi, %rsi
movl -0x4(%rcx,%rsi), %edi
movl $0xbfffffff, %r8d       # imm = 0xBFFFFFFF
andl %r8d, %edi
orl %eax, %edi
movl %edi, -0x4(%rcx,%rsi)
movq (%rdx), %rax
movl 0x18(%rax,%r11), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
movq (%r15), %rcx
movq 0x8(%r15), %rdi
leaq (%rdi,%rdi,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdi, %rsi
movl -0x4(%rcx,%rsi), %edi
movl $0xc0000000, %r8d       # imm = 0xC0000000
andl %r8d, %edi
orl %eax, %edi
movl %edi, -0x4(%rcx,%rsi)
movq (%r15), %rax
movq 0x8(%r15), %rdi
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
andl $0x7fffffff, -0x10(%rax,%rcx) # imm = 0x7FFFFFFF
movl (%r14), %esi
movl %esi, -0x1c(%rax,%rcx)
cmpq 0x30(%rsp), %rdi
movq %r15, %r8
movq %r13, %r10
jb 0x1aa866
addq %rcx, %rax
movq (%rdx), %rcx
cmpl $0x0, 0x18(%rcx,%r11)
js 0x1aa826
movl -0x18(%rax), %ecx
leaq 0x8c86e(%rip), %rdi     # 0x237073
cmpl %ecx, -0x34(%rax)
ja 0x1aa84f
movl -0x30(%rax), %esi
leaq 0x8c85f(%rip), %rdi     # 0x237073
cmpl %esi, %ecx
ja 0x1aa84f
leaq 0x8c854(%rip), %rdi     # 0x237073
cmpl -0x14(%rax), %esi
jbe 0x1aa866
jmp 0x1aa84f
movl -0x34(%rax), %ecx
leaq 0x92e0e(%rip), %rdi     # 0x23d63e
cmpl %ecx, -0x18(%rax)
ja 0x1aa84f
movl -0x14(%rax), %esi
leaq 0x92dff(%rip), %rdi     # 0x23d63e
cmpl %esi, %ecx
ja 0x1aa84f
leaq 0x92df4(%rip), %rdi     # 0x23d63e
cmpl -0x30(%rax), %esi
jbe 0x1aa866
callq 0x29375
movq 0x10(%rsp), %r11
movq %r13, %r10
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %r8
addq %rbx, %r12
incq %rbp
movl 0x10(%r10), %eax
cmpq %rax, %rbp
jb 0x1aa6c3
movq 0x20(%rsp), %r15
movq 0x40(%rsp), %rbx
jmp 0x1aa918
movq (%r8), %rax
movq 0x8(%r8), %rdi
cmpq 0x10(%r8), %rdi
jne 0x1aa8d5
leaq (%rdi,%rdi), %rcx
testq %rdi, %rdi
movl $0x2, %edx
cmoveq %rdx, %rcx
movq %rcx, 0x10(%r8)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x10(%rsp), %r11
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %r8
movq %rax, (%r8)
movq (%rdx), %rcx
movq 0x8(%r8), %rdi
leaq 0x1(%rdi), %rsi
movq %rsi, 0x8(%r8)
leaq (%rdi,%rdi,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdi, %rsi
movups (%rcx,%r11), %xmm0
movups 0xc(%rcx,%r11), %xmm1
movups %xmm1, 0xc(%rax,%rsi)
movups %xmm0, (%rax,%rsi)
movl (%r14), %eax
movq (%r8), %rcx
movq 0x8(%r8), %rdi
leaq (%rdi,%rdi,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdi, %rsi
movl %eax, -0x1c(%rcx,%rsi)
incq %r15
incq %rbx
cmpq 0x8(%rdx), %rbx
jb 0x1aa648
movq 0x8(%r8), %rax
movq (%rsp), %rbx
movq %rbx, %r10
cmpq %rbx, %rax
je 0x1aab5c
movq (%r8), %rsi
leaq (%rbx,%rbx,8), %rcx
leaq (%rcx,%rcx,2), %r14
addq %rbx, %r14
leaq (%rsi,%r14), %rdi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rsi
addq %rcx, %rsi
callq 0x19e2b4
movq 0x18(%rsp), %r8
movq 0x8(%r8), %r10
testq %r15, %r15
je 0x1aab5c
movq %rbx, %rax
cmpq %r10, %rbx
jae 0x1aab51
movq (%r8), %r9
leaq (%r9,%r14), %rdi
addq $0x1c, %rdi
movq %r14, 0x20(%rsp)
leaq -0x1c(%r9,%r14), %rbp
movq (%rsp), %rcx
movq %rcx, %r12
movq %r10, 0x30(%rsp)
movq %r9, 0x28(%rsp)
leaq (%r12,%r12,8), %rax
leaq (%rax,%rax,2), %rbx
addq %r12, %rbx
movslq 0xc(%r9,%rbx), %rdx
testq %rdx, %rdx
js 0x1aaad5
movq 0xa0(%rsp), %rax
movq 0x40(%rax), %rax
cmpb $0x0, (%rax,%rdx)
je 0x1aaad5
addq %r9, %rbx
leaq 0x1(%r12), %r15
cmpq %r10, %r15
movq %rdi, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
jge 0x1aaa57
movl 0x8(%rbx), %r14d
movq %rdi, %r13
cmpl %r14d, 0x8(%r13)
ja 0x1aaa57
cmpl $-0x1, (%r13)
je 0x1aaa4b
movl 0xc(%r13), %esi
testl %esi, %esi
jns 0x1aaa4b
movl 0x4(%rbx), %eax
cmpl 0x4(%r13), %eax
ja 0x1aaa4b
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq 0x38(%rsp), %rdi
movl $0x1, %ecx
callq 0x1a9fdf
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %r9
movq 0x30(%rsp), %r10
movq 0x18(%rsp), %r8
movq (%rsp), %rcx
testl %eax, %eax
je 0x1aaa4b
movl $0xffffffff, (%r13)     # imm = 0xFFFFFFFF
incq %r15
addq $0x1c, %r13
cmpq %r10, %r15
jl 0x1aa9ef
leaq -0x1(%r12), %r15
cmpq %rcx, %r15
jl 0x1aaad5
movl 0x4(%rbx), %r14d
movq %rbp, %r13
movl 0x8(%r13), %eax
cmpl %r14d, %eax
jbe 0x1aaad5
cmpl $-0x1, (%r13)
je 0x1aaac9
movl 0xc(%r13), %esi
testl %esi, %esi
jns 0x1aaac9
cmpl 0x4(%r13), %r14d
ja 0x1aaac9
cmpl %eax, 0x8(%rbx)
jb 0x1aaac9
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq 0x38(%rsp), %rdi
movl $0x1, %ecx
callq 0x1a9fdf
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %r9
movq 0x30(%rsp), %r10
movq 0x18(%rsp), %r8
movq (%rsp), %rcx
testl %eax, %eax
je 0x1aaac9
movl $0xffffffff, (%r13)     # imm = 0xFFFFFFFF
decq %r15
addq $-0x1c, %r13
cmpq %rcx, %r15
jge 0x1aaa68
incq %r12
addq $0x1c, %rdi
addq $0x1c, %rbp
cmpq %r10, %r12
jne 0x1aa9a2
movq %rcx, %rax
cmpq %r10, %rcx
movq 0x20(%rsp), %r9
jae 0x1aab51
movq (%r8), %rcx
movq (%rsp), %rax
movq %rax, %rdx
cmpl $-0x1, (%rcx,%r9)
je 0x1aab45
cmpq %rax, %rdx
je 0x1aab32
leaq (%rcx,%r9), %rsi
leaq (%rax,%rax,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rax, %rdi
movups (%rsi), %xmm0
movups 0xc(%rsi), %xmm1
movups %xmm1, 0xc(%rcx,%rdi)
movups %xmm0, (%rcx,%rdi)
movq (%r8), %rcx
movq 0x8(%r8), %r10
leaq (%rax,%rax,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rax, %rsi
andb $0x7f, 0xf(%rcx,%rsi)
incq %rax
incq %rdx
addq $0x1c, %r9
cmpq %r10, %rdx
jb 0x1aab00
movq %rax, 0x8(%r8)
movq %rax, %r10
movq (%rsp), %rbx
subq %rbx, %r10
movq %r10, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x95ad1(%rip), %rdi     # 0x240649
leaq 0x950dc(%rip), %rsi     # 0x23fc5b
leaq 0x95a2b(%rip), %rcx     # 0x2405b1
movl $0xc4a, %edx            # imm = 0xC4A
callq 0x2110
leaq 0x95a03(%rip), %rdi     # 0x24059a
leaq 0x950bd(%rip), %rsi     # 0x23fc5b
leaq 0x95a0c(%rip), %rcx     # 0x2405b1
movl $0xc42, %edx            # imm = 0xC42
callq 0x2110