// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: push_graph_bin_back(overlap_region_alloc*, ul_idx_t const*, hpc_re_t*, double, unsigned long, unsigned long, unsigned long, asg64_v*, asg64_v*, mask_ul_ov_t*)
void push_graph_bin_back(overlap_region_alloc* ol, const ul_idx_t *udb, hpc_re_t *hre, 
double erate, uint64_t maxe, uint64_t minov, uint64_t rid, asg64_v *b0, asg64_v *b1, mask_ul_ov_t *mk)
{
	uint64_t k, i, m, si, sn, skip_n, zt, re; overlap_region t, *z; ul_ov_t p, *sa; int64_t npc;
	uint64_t q0l, t0l, e0l, q1l, t1l, e1l, mm, occ[3];
	b0->n = b1->n = 0; 
	
	mk->srt.n = mk->idx.n = 0;
	gen_src_shared_interval_adv(rid, udb->ug, &(mk->srt));
	kv_resize(uint64_t, mk->idx, ol->length);
	for (k = 0; k < ol->length; k++) {
		kv_push(uint64_t, mk->idx, (((uint64_t)ol->list[k].y_id)<<32)|((uint64_t)k));
	}
	radix_sort_gfa64(mk->idx.a, mk->idx.a+mk->idx.n);
	for (i = si = b0->n = 0, occ[0] = occ[1] = occ[2] = 0; i < mk->idx.n; i++) {
		k = (uint32_t)mk->idx.a[i];
		z = &(ol->list[k]);
		overlap_region2ul_ov_t(z, &p, udb->ug->u.a[z->y_id].len);
		if(((p.qe-p.qs) >= minov) && ((p.te-p.ts) >= minov)) {
			if(!trans_ovlp_connect(&p, udb->ug)) continue;//could be eaisly detected
			if(p.sec == 0) {///no error
				zt = ((uint32_t)-1); zt <<= 32; zt |= (i<<1); occ[2]++;
				kv_push(uint64_t, *b0, zt); continue;
			}
			sa = get_mask_interval(&p, &(mk->srt), &si, udb->ug->u.a[p.qn].len, udb->ug->u.a[p.tn].len, &sn, &skip_n);
			re = cal_exact_batch(z, sa, sn, udb->ug->u.a[p.tn].len, &q0l, &t0l, &e0l, &q1l, &t1l, &e1l, udb->ug);
			if(!re) {//no unmask errors
				zt = ((uint32_t)-1); zt <<= 32; zt |= (i<<1); occ[2]++;
				kv_push(uint64_t, *b0, zt); continue;
			}

			mm = ((re <= maxe)?((uint64_t)(0x8000000000000000)):(0));
			///left end
			if(q0l >= minov && t0l >= minov) {
				zt = MIN(q0l, t0l); zt <<= 32; zt |= (i<<1); zt |= mm; occ[!!mm]++;
				kv_push(uint64_t, *b0, zt);
			}

			///right end
			if(q1l >= minov && t1l >= minov) {
				zt = MIN(q1l, t1l); zt <<= 32; zt |= (i<<1); zt += 1; zt |= mm; occ[!!mm]++;
				kv_push(uint64_t, *b0, zt);
			}
		}
	}

	// radix_sort_gfa64(b0->a, b0->a+b0->n);
	// uint64_t *a0, *a1, *a2, a0n, a1n, a2n;
	// a2 = b0->a; a2n = occ[0];
	// a1 = a2 + a2n; a1n = occ[1];
	// a0 = a1 + a1n; a0n = occ[2];
	

	fprintf(stderr, "\n[M::%s]\tutg%.6lu%c\t#s::%u\n", __func__, rid+1, "lc"[udb->ug->u.a[rid].circ], (uint32_t)mk->srt.n);
	for (k = 0; k < mk->srt.n; k++) {
		fprintf(stderr, "[M::%s]\tutg%.6u%c\tq::[%u,\t%u)\t%c\tutg%.6u%c\tt::[%u,\t%u)\n", __func__, 
		mk->srt.a[k].qn+1, "lc"[udb->ug->u.a[mk->srt.a[k].qn].circ], mk->srt.a[k].qs, mk->srt.a[k].qe, "+-"[mk->srt.a[k].rev],
		mk->srt.a[k].tn+1, "lc"[udb->ug->u.a[mk->srt.a[k].tn].circ], mk->srt.a[k].ts, mk->srt.a[k].te);
	}



	for (k = m = 0; k < ol->length; k++) {
		z = &(ol->list[k]); 
		if(z->non_homopolymer_errors == 0) continue;
		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;
		}
		// gen_shared_interval(&p, udb->ug, bl);
		// gen_shared_interval_adv(&p, udb->ug, bl);
		// if(!trans_ovlp_connect(&p, udb->ug)) continue;
		// assert(z->x_id == rid);
		// if(!get_high_simi(z, erate, maxe, minov, &e0, &e1)) continue;
		npc = cal_npc_err(z, p.qs, p.qe, hre, udb->ug->u.a[p.tn].len);
		if(npc == INT32_MAX || npc < 0) continue;
		if(((uint64_t)npc <= maxe) && ((uint64_t)npc <= ((p.qe - p.qs)*erate)) && ((uint64_t)npc <= ((p.te - p.ts)*erate))) {
			;
		} else if(((uint64_t)npc > maxe) && ((p.qe - p.qs) > minov) && ((p.te - p.ts) > minov)) {
			;
		}
		
		// if((z->x_id == 44 && z->y_id == 45) || (z->x_id == 45 && z->y_id == 44)) 
		// if(npc <= 4)
		{
			// int64_t nnpc;
			// nnpc = cal_npc_err(z, p.qs, p.qe, NULL, udb->ug->u.a[p.tn].len);
			// fprintf(stderr, "[M::%s]\tutg%.6u%c->utg%.6u%c\terr::%u\tnpc::%ld\t#s::%u\n", __func__, p.qn+1, "lc"[udb->ug->u.a[p.qn].circ], 
			// 	p.tn+1, "lc"[udb->ug->u.a[p.tn].circ], ol->list[k].non_homopolymer_errors, npc, (uint32_t)bl->n);
			// uint64_t t;
			// for (t = 0; t < bl->n; t++) {
			// 	fprintf(stderr, "[M::%s]\tutg%.6u%c\tq::[%u,\t%u)\t%c\tutg%.6u%c\tt::[%u,\t%u)\n", __func__, 
			// 	bl->a[t].qn+1, "lc"[udb->ug->u.a[bl->a[t].qn].circ], bl->a[t].qs, bl->a[t].qe, "+-"[bl->a[t].rev],
			// 	bl->a[t].tn+1, "lc"[udb->ug->u.a[bl->a[t].tn].circ], bl->a[t].ts, bl->a[t].te);
			// }
		}
		
		if(k != m) {
			t = ol->list[k];
			ol->list[k] = ol->list[m];
			ol->list[m] = t;
		}
		m++;
	}
	ol->length = m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %r8, 0x18(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x38(%rsp)
movq %rdi, %r12
movq 0x1c0(%rsp), %r13
movq 0x1b8(%rsp), %rax
andq $0x0, (%rax)
movq 0x1b0(%rsp), %rax
andq $0x0, (%rax)
andq $0x0, (%r13)
leaq 0x18(%r13), %rdx
andq $0x0, 0x20(%r13)
movq %rsi, 0x10(%rsp)
movq (%rsi), %rsi
movq %r9, 0x68(%rsp)
movl %r9d, %edi
movq %rdx, 0x30(%rsp)
callq 0x1c90d8
movq 0x10(%r12), %rax
cmpq %rax, 0x8(%r13)
jae 0x23d37
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, 0x8(%r13)
movq 0x10(%r13), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r13)
movq 0x10(%r12), %rax
movq 0x10(%r13), %rdi
pushq $0x2
popq %r15
testq %rax, %rax
je 0x23da2
pushq $0x10
popq %rbx
xorl %r14d, %r14d
movq (%r13), %rax
cmpq 0x8(%r13), %rax
jne 0x23d77
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r15, %rsi
movq %rsi, 0x8(%r13)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%r13)
movq (%r13), %rax
movq (%r12), %rcx
movl (%rcx,%rbx), %ecx
shlq $0x20, %rcx
orq %r14, %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r13)
movq %rcx, (%rdi,%rax,8)
incq %r14
addq $0xa8, %rbx
cmpq 0x10(%r12), %r14
jb 0x23d4a
movq (%r13), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x19dd99
movq 0x1b0(%rsp), %rax
andq $0x0, (%rax)
andq $0x0, 0x98(%rsp)
xorps %xmm0, %xmm0
andq $0x0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
xorl %eax, %eax
cmpq %rax, (%r13)
movq %r12, 0x8(%rsp)
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %r10
jne 0x24018
movq 0x2711e6(%rip), %r15    # 0x294fe0
movq (%r15), %rdi
movq (%r9), %rax
movq 0x10(%rax), %rax
movq 0x68(%rsp), %rcx
imulq $0x28, %rcx, %rdx
incq %rcx
movl (%rax,%rdx), %eax
shrl $0x1f, %eax
leaq 0x21091e(%rip), %r12    # 0x23473b
movsbl (%rax,%r12), %r8d
movq %r9, %rbp
movl 0x20(%r13), %r9d
leaq 0x21e099(%rip), %rsi    # 0x241ec9
leaq 0x21e0ae(%rip), %rdx    # 0x241ee5
xorl %eax, %eax
callq 0x22b0
cmpq $0x0, 0x20(%r13)
je 0x23f0d
pushq $0x18
popq %rbx
xorl %r14d, %r14d
movq (%r15), %rdi
movq 0x18(%r13), %rax
movl -0x18(%rax,%rbx), %edx
movl -0x14(%rax,%rbx), %r9d
leal 0x1(%rdx), %ecx
movq (%rbp), %rsi
movq 0x10(%rsi), %rsi
imulq $0x28, %rdx, %rdx
movl (%rsi,%rdx), %edx
shrl $0x1f, %edx
movsbl (%rdx,%r12), %r8d
movl -0x10(%rax,%rbx), %edx
movq %rdx, 0x20(%rsp)
movl (%rax,%rbx), %edx
shrl $0x1f, %edx
leaq 0x20e444(%rip), %r10    # 0x2322d3
movsbl (%rdx,%r10), %edx
movq %rdx, 0x18(%rsp)
movl -0xc(%rax,%rbx), %edx
leal 0x1(%rdx), %r10d
movq %r10, 0x30(%rsp)
imulq $0x28, %rdx, %rdx
movl (%rsi,%rdx), %edx
shrl $0x1f, %edx
movsbl (%rdx,%r12), %r13d
movq %r15, %r10
movl -0x8(%rax,%rbx), %r15d
movq %rbp, %r11
movl -0x4(%rax,%rbx), %ebp
leaq 0x21e02e(%rip), %rsi    # 0x241ef9
leaq 0x21e013(%rip), %rdx    # 0x241ee5
xorl %eax, %eax
pushq %rbp
movq %r11, %rbp
pushq %r15
movq %r10, %r15
pushq %r13
pushq 0x48(%rsp)
movq 0x1e0(%rsp), %r13
pushq 0x38(%rsp)
pushq 0x48(%rsp)
callq 0x22b0
addq $0x30, %rsp
incq %r14
addq $0x1c, %rbx
cmpq 0x20(%r13), %r14
jb 0x23e4f
movq 0x8(%rsp), %rbx
cmpq $0x0, 0x10(%rbx)
je 0x243fe
xorl %r15d, %r15d
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
xorl %r14d, %r14d
xorl %ebp, %ebp
movq 0x38(%rsp), %r12
movq 0x10(%rsp), %r8
movq (%rbx), %rax
cmpl $0x0, 0x30(%rax,%r15)
je 0x23fff
leaq (%rax,%r15), %rdi
movl 0x10(%rax,%r15), %ecx
movl 0x4(%rax,%r15), %esi
movl 0x8(%rax,%r15), %edx
incl %edx
movq (%r8), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %r8d
andq %r13, %r8
movq %r12, %rcx
callq 0x1c80b6
cmpq %r13, %rax
sete %cl
testq %rax, %rax
sets %al
orb %cl, %al
jne 0x23ffa
cmpq %rbp, %r14
je 0x23ff7
movq 0x8(%rsp), %rax
movq (%rax), %r12
leaq (%r12,%r15), %r13
movl $0xa8, %edx
leaq 0xd0(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
movq %rbp, 0x20(%rsp)
imulq $0xa8, 0x20(%rsp), %rbp
addq %rbp, %r12
movl $0xa8, %edx
movq %r13, %rdi
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq %r12, %rsi
movq 0x38(%rsp), %r12
movq 0x8(%rsp), %rbx
callq 0x2160
addq (%rbx), %rbp
movl $0xa8, %edx
movq %rbp, %rdi
movq 0x20(%rsp), %rbp
leaq 0xd0(%rsp), %rsi
callq 0x2160
incq %rbp
movq 0x10(%rsp), %r8
incq %r14
addq $0xa8, %r15
cmpq 0x10(%rbx), %r14
jb 0x23f35
jmp 0x24400
xorl %r15d, %r15d
movq $0x0, 0x40(%rsp)
movq 0x10(%r13), %rax
movl (%rax,%r15,8), %eax
movq (%r12), %rcx
imulq $0xa8, %rax, %rdx
leaq (%rcx,%rdx), %rbp
movq (%r9), %rsi
movq 0x10(%rsi), %rbx
movl (%rcx,%rdx), %r14d
movl 0x10(%rcx,%rdx), %eax
imulq $0x28, %rax, %r11
movl (%rbx,%r11), %r12d
movl %r14d, 0x4c(%rsp)
movl %eax, 0x58(%rsp)
movl 0x30(%rcx,%rdx), %r13d
movl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
andl %eax, %r13d
movl 0x1c(%rcx,%rdx), %edi
shll $0x1f, %edi
leal (%rdi,%r13), %eax
addl $0x40000000, %eax       # imm = 0x40000000
movl %eax, 0x64(%rsp)
movl 0x4(%rcx,%rdx), %eax
movl %eax, 0x50(%rsp)
movl 0x8(%rcx,%rdx), %ecx
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
incl %ecx
movl %ecx, 0x54(%rsp)
testl %edi, %edi
je 0x240ad
movl 0x18(%rbp), %edx
notl %edx
addl %r12d, %edx
movl 0x14(%rbp), %r8d
movl %r12d, %edi
subl %r8d, %edi
jmp 0x240b5
movl 0x14(%rbp), %edx
movl 0x18(%rbp), %edi
incl %edi
movl %edx, 0x5c(%rsp)
movl %edi, 0x60(%rsp)
subl %eax, %ecx
cmpq %r10, %rcx
jb 0x2427e
subl %edx, %edi
cmpq %r10, %rdi
jb 0x2427e
movq %r11, 0x20(%rsp)
leaq 0x4c(%rsp), %rdi
callq 0x1c3c2d
testl %eax, %eax
je 0x24290
testl %r13d, %r13d
je 0x242ac
imulq $0x28, %r14, %rax
movl (%rbx,%rax), %ecx
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
andq %rbx, %rcx
subq $0x8, %rsp
leaq 0x54(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
movq %r12, %r8
leaq 0x98(%rsp), %r9
leaq 0xd0(%rsp), %rax
pushq %rax
callq 0x1cab51
addq $0x10, %rsp
movq 0x90(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
movq 0x20(%rsp), %rsi
movl (%rcx,%rsi), %ecx
andq %rbx, %rcx
subq $0x10, %rsp
movq %rbp, %rdi
movq %rax, %rsi
leaq 0x98(%rsp), %r8
leaq 0x90(%rsp), %r9
leaq 0xc8(%rsp), %rax
pushq %rax
leaq 0x88(%rsp), %rax
pushq %rax
leaq 0x98(%rsp), %rax
pushq %rax
leaq 0xe8(%rsp), %rax
pushq %rax
callq 0x1cb063
addq $0x30, %rsp
testq %rax, %rax
je 0x242fa
movq %rax, %r12
xorl %ebp, %ebp
cmpq 0x28(%rsp), %rax
setbe %bpl
shlq $0x3f, %rbp
movq 0x88(%rsp), %rax
movq 0x18(%rsp), %r10
cmpq %r10, %rax
movq 0x10(%rsp), %r9
movq 0x1c0(%rsp), %r13
jb 0x2425c
movq 0x80(%rsp), %rbx
cmpq %r10, %rbx
jb 0x2425c
xorl %ecx, %ecx
cmpq 0x28(%rsp), %r12
setbe %cl
cmpq %rbx, %rax
cmovbq %rax, %rbx
incq 0xa0(%rsp,%rcx,8)
movq 0x1b0(%rsp), %r14
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x24243
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r9
movq %rax, 0x10(%r14)
movq (%r14), %rcx
shlq $0x20, %rbx
leaq (%r15,%r15), %rdx
orq %rbx, %rdx
orq %rbp, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rdx, (%rax,%rcx,8)
movq 0x78(%rsp), %rax
cmpq %r10, %rax
jb 0x24274
movq 0x70(%rsp), %rbx
cmpq %r10, %rbx
jae 0x2437e
movq 0x8(%rsp), %r12
jmp 0x2436c
movq 0x8(%rsp), %r12
movq 0x1c0(%rsp), %r13
jmp 0x2436c
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r9
movq 0x1c0(%rsp), %r13
movq 0x18(%rsp), %r10
jmp 0x2436c
movq 0x1b0(%rsp), %rbx
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x242e6
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
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r9
movq 0x1c0(%rsp), %r13
jmp 0x24346
movq 0x1b0(%rsp), %rbx
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
movq 0x1c0(%rsp), %r13
jne 0x2433c
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
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %r10
leaq (%r15,%r15), %rdx
movabsq $-0x100000000, %rsi     # imm = 0xFFFFFFFF00000000
orq %rsi, %rdx
incq 0x40(%rsp)
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rdx, (%rax,%rcx,8)
incq %r15
cmpq (%r13), %r15
jb 0x24024
jmp 0x23df3
xorl %ecx, %ecx
cmpq 0x28(%rsp), %r12
setbe %cl
cmpq %rbx, %rax
cmovbq %rax, %rbx
incq 0xa0(%rsp,%rcx,8)
movq 0x1b0(%rsp), %r14
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x243db
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r9
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq 0x8(%rsp), %r12
shlq $0x20, %rbx
leaq (%r15,%r15), %rdx
orq %rbx, %rdx
orq %rbp, %rdx
orq $0x1, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
jmp 0x24368
xorl %ebp, %ebp
movq %rbp, 0x10(%rbx)
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq