// File: /workspace/github_cpp_makefile_star2_O1/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, 0x28(%rsp)
movq %rcx, %r8
movq %rsi, 0x58(%rsp)
movq 0x8(%rcx), %r15
cmpq $0x0, 0x8(%rdx)
movq %rcx, 0x10(%rsp)
je 0x1a1a05
movq %r15, 0x8(%rsp)
movq %rdi, 0x60(%rsp)
movq (%rdi), %rax
movq %rax, 0x50(%rsp)
xorl %ebx, %ebx
leaq 0xe9b33(%rip), %r15     # 0x28b230
xorl %r9d, %r9d
xorl %r14d, %r14d
movq %rdx, 0x18(%rsp)
movq (%rdx), %rcx
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %r10
addq %rbx, %r10
movslq 0xc(%rcx,%r10), %rax
testq %rax, %rax
movq %r10, 0x20(%rsp)
js 0x1a1955
addq %r10, %rcx
movq 0x58(%rsp), %rsi
movq (%rsi), %r9
movl (%rcx), %ecx
imulq $0xa8, %rcx, %rcx
cmpl %eax, 0x10(%r9,%rcx)
jne 0x1a1c33
leaq (%rax,%rax,4), %r13
shlq $0x3, %r13
movq 0x50(%rsp), %rax
addq 0x10(%rax), %r13
addq %rcx, %r9
cmpl $0x0, 0x10(%r13)
je 0x1a19eb
movq %r14, 0x30(%rsp)
movq %rbx, 0x38(%rsp)
movq 0x8(%r8), %rax
addq $0x2, %rax
movq %rax, 0x68(%rsp)
xorl %ebp, %ebp
xorl %r12d, %r12d
movq %r13, 0x70(%rsp)
movq 0x18(%r13), %rax
movq (%rax,%rbp,8), %rbx
movq %rbx, %rax
shrq $0x20, %rax
movl %eax, 0x44(%rsp)
movl %r12d, 0x48(%rsp)
movq 0x18(%r15), %rax
movq %rbx, %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %ecx
addl %r12d, %ecx
movl %ecx, 0x4c(%rsp)
movq (%rdx), %rax
cmpl %ecx, 0x10(%rax,%r10)
jae 0x1a1933
addq %r10, %rax
cmpl %r12d, 0x14(%rax)
jbe 0x1a1946
movq %r15, %r14
movq 0x60(%rsp), %rdi
leaq 0x44(%rsp), %rsi
movq %r9, %rdx
movq %r8, %rcx
movsd 0x83d4d(%rip), %xmm0    # 0x225538
movq %r8, %r15
movq $-0x1, %r8
movq %r9, %r13
movq 0xb8(%rsp), %r9
callq 0x1a005a
testq %rax, %rax
je 0x1a1c14
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
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,%r10), %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 (%r13), %esi
movl %esi, -0x1c(%rax,%rcx)
cmpq 0x68(%rsp), %rdi
movq %r15, %r8
movq %r14, %r15
movq %r13, %r9
movq 0x70(%rsp), %r13
jb 0x1a1933
addq %rcx, %rax
movq (%rdx), %rcx
cmpl $0x0, 0x18(%rcx,%r10)
js 0x1a18f0
movl -0x18(%rax), %ecx
leaq 0x895b4(%rip), %rdi     # 0x22ae83
cmpl %ecx, -0x34(%rax)
ja 0x1a1919
movl -0x30(%rax), %esi
leaq 0x895a5(%rip), %rdi     # 0x22ae83
cmpl %esi, %ecx
ja 0x1a1919
leaq 0x8959a(%rip), %rdi     # 0x22ae83
cmpl -0x14(%rax), %esi
jbe 0x1a1933
jmp 0x1a1919
movl -0x34(%rax), %ecx
leaq 0x8faf4(%rip), %rdi     # 0x2313ee
cmpl %ecx, -0x18(%rax)
ja 0x1a1919
movl -0x14(%rax), %esi
leaq 0x8fae5(%rip), %rdi     # 0x2313ee
cmpl %esi, %ecx
ja 0x1a1919
leaq 0x8fada(%rip), %rdi     # 0x2313ee
cmpl -0x30(%rax), %esi
jbe 0x1a1933
movq %r9, %r14
callq 0x29fb6
movq 0x20(%rsp), %r10
movq %r14, %r9
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %r8
addq %rbx, %r12
incq %rbp
movl 0x10(%r13), %eax
cmpq %rax, %rbp
jb 0x1a1788
movq 0x38(%rsp), %rbx
movq 0x30(%rsp), %r14
jmp 0x1a19eb
movq 0x10(%r8), %rax
cmpq %rax, 0x8(%r8)
jne 0x1a199e
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%r8)
movq (%r8), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
movq %r9, %r12
callq 0x2370
movq 0x20(%rsp), %r10
movq %r12, %r9
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %r8
movq %rax, (%r8)
movq (%rdx), %rax
movq (%r8), %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 (%rax,%r10), %xmm0
movups 0xc(%rax,%r10), %xmm1
movups %xmm1, 0xc(%rcx,%rsi)
movups %xmm0, (%rcx,%rsi)
movl (%r9), %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 %r14
incq %rbx
cmpq 0x8(%rdx), %rbx
jb 0x1a1708
testq %r14, %r14
sete %bl
movq 0x8(%rsp), %r15
jmp 0x1a1a07
movb $0x1, %bl
movq 0x8(%r8), %rax
cmpq %r15, %rax
je 0x1a1bfe
movq (%r8), %rsi
leaq (%r15,%r15,8), %rcx
leaq (%rcx,%rcx,2), %r14
leaq (%rsi,%r15), %rdi
addq %r14, %rdi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rsi
addq %rcx, %rsi
callq 0x195511
movq 0x10(%rsp), %r8
testb %bl, %bl
jne 0x1a1bfe
movq %r15, 0x8(%rsp)
cmpq 0x8(%r8), %r15
jae 0x1a1b8f
movq 0x8(%rsp), %r13
leaq (%r14,%r13), %r15
movq (%r8), %rbx
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rax
addq %r13, %rax
movslq 0xc(%rbx,%rax), %rcx
testq %rcx, %rcx
js 0x1a1b7e
movq 0xb0(%rsp), %rdx
movq 0x40(%rdx), %rdx
cmpb $0x0, (%rdx,%rcx)
je 0x1a1b7e
addq %rax, %rbx
leaq 0x1(%r13), %rbp
cmpq 0x8(%r8), %rbp
jge 0x1a1b09
movq %r15, %r12
movq (%r8), %rax
movl 0x24(%rax,%r12), %ecx
cmpl 0x8(%rbx), %ecx
ja 0x1a1b09
cmpl $-0x1, 0x1c(%rax,%r12)
je 0x1a1afc
movl 0x28(%rax,%r12), %esi
testl %esi, %esi
jns 0x1a1afc
movl 0x4(%rbx), %ecx
cmpl 0x20(%rax,%r12), %ecx
ja 0x1a1afc
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl 0xc(%rbx), %edx
movq 0x28(%rsp), %rdi
movl $0x1, %ecx
callq 0x1a1266
movq 0x10(%rsp), %r8
testl %eax, %eax
je 0x1a1afc
movq (%r8), %rax
movl $0xffffffff, 0x1c(%rax,%r12) # imm = 0xFFFFFFFF
incq %rbp
addq $0x1c, %r12
cmpq 0x8(%r8), %rbp
jl 0x1a1aa7
leaq -0x1(%r13), %rbp
cmpq 0x8(%rsp), %rbp
jl 0x1a1b7e
movq %r15, %r12
movq (%r8), %rcx
movl -0x14(%rcx,%r12), %eax
movl 0x4(%rbx), %edx
cmpl %edx, %eax
jbe 0x1a1b7e
cmpl $-0x1, -0x1c(%rcx,%r12)
je 0x1a1b70
movl -0x10(%rcx,%r12), %esi
testl %esi, %esi
jns 0x1a1b70
cmpl -0x18(%rcx,%r12), %edx
ja 0x1a1b70
cmpl %eax, 0x8(%rbx)
jb 0x1a1b70
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl 0xc(%rbx), %edx
movq 0x28(%rsp), %rdi
movl $0x1, %ecx
callq 0x1a1266
movq 0x10(%rsp), %r8
testl %eax, %eax
je 0x1a1b70
movq (%r8), %rax
movl $0xffffffff, -0x1c(%rax,%r12) # imm = 0xFFFFFFFF
decq %rbp
addq $-0x1c, %r12
cmpq 0x8(%rsp), %rbp
jge 0x1a1b17
incq %r13
addq $0x1c, %r15
cmpq 0x8(%r8), %r13
jb 0x1a1a5e
movq 0x8(%rsp), %r15
movq %r15, %rax
cmpq 0x8(%r8), %r15
jae 0x1a1bfa
addq %r15, %r14
movq %r15, %rcx
movq %r15, %rax
movq (%r8), %rdx
cmpl $-0x1, (%rdx,%r14)
je 0x1a1bed
cmpq %rax, %rcx
je 0x1a1bd4
leaq (%rdx,%r14), %rsi
leaq (%rax,%rax,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rax, %rdi
movups (%rsi), %xmm0
movups 0xc(%rsi), %xmm1
movups %xmm1, 0xc(%rdx,%rdi)
movups %xmm0, (%rdx,%rdi)
movq (%r8), %rdx
leaq (%rax,%rax,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rax, %rsi
andl $0x7fffffff, 0xc(%rdx,%rsi) # imm = 0x7FFFFFFF
incq %rax
incq %rcx
addq $0x1c, %r14
cmpq 0x8(%r8), %rcx
jb 0x1a1ba6
movq %rax, 0x8(%r8)
movq 0x8(%r8), %rax
subq %r15, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9275e(%rip), %rdi     # 0x234379
leaq 0x91d69(%rip), %rsi     # 0x23398b
leaq 0x926b8(%rip), %rcx     # 0x2342e1
movl $0xc4a, %edx            # imm = 0xC4A
callq 0x2110
leaq 0x92690(%rip), %rdi     # 0x2342ca
leaq 0x91d4a(%rip), %rsi     # 0x23398b
leaq 0x92699(%rip), %rcx     # 0x2342e1
movl $0xc42, %edx            # imm = 0xC42
callq 0x2110