// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %rcx, 0x50(%rsp)
movq %rdx, 0x68(%rsp)
movq %rdi, %r13
movq 0x1c0(%rsp), %r15
movq 0x1b8(%rsp), %rax
andq $0x0, (%rax)
movq 0x1b0(%rsp), %rax
andq $0x0, (%rax)
andq $0x0, (%r15)
leaq 0x18(%r15), %rdx
andq $0x0, 0x20(%r15)
movq %rsi, (%rsp)
movq (%rsi), %rsi
movq %r9, 0x60(%rsp)
movl %r9d, %edi
movq %rdx, 0x58(%rsp)
callq 0x1bf2ca
movq 0x10(%r13), %rax
cmpq %rax, 0x8(%r15)
jae 0x24b38
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(%r15)
movq 0x10(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r15)
cmpq $0x0, 0x10(%r13)
pushq $0x2
popq %r12
je 0x24ba2
pushq $0x10
popq %rbx
xorl %r14d, %r14d
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x24b72
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r13), %rax
movl (%rax,%rbx), %eax
shlq $0x20, %rax
orq %r14, %rax
movq (%r15), %rcx
movq 0x10(%r15), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movq %rax, (%rdx,%rcx,8)
incq %r14
addq $0xa8, %rbx
cmpq 0x10(%r13), %r14
jb 0x24b49
movq (%r15), %rax
movq 0x10(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x194fef
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
movq $0x0, 0x20(%rsp)
cmpq %rax, (%r15)
movq %r13, 0x8(%rsp)
movq (%rsp), %r9
movq 0x10(%rsp), %r10
jne 0x24efc
movq 0x20(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x2653cf(%rip), %rbx    # 0x289fe0
movq (%rbx), %rdi
movq (%r9), %rax
movq 0x10(%rax), %rax
movq 0x60(%rsp), %rcx
imulq $0x28, %rcx, %rdx
incq %rcx
movl (%rax,%rdx), %eax
shrl $0x1f, %eax
leaq 0x2039f7(%rip), %r12    # 0x22862b
movsbl (%rax,%r12), %r8d
movq %r9, %r13
movl 0x20(%r15), %r9d
leaq 0x210fb2(%rip), %rsi    # 0x235bf9
leaq 0x210fc7(%rip), %rdx    # 0x235c15
xorl %eax, %eax
callq 0x22b0
cmpq $0x0, 0x20(%r15)
je 0x24d27
pushq $0x18
popq %r14
xorl %ebp, %ebp
movq (%rbx), %rdi
movq 0x18(%r15), %rax
movl -0x18(%rax,%r14), %edx
movl -0x14(%rax,%r14), %r9d
leal 0x1(%rdx), %ecx
movq (%r13), %rsi
movq 0x10(%rsi), %rsi
imulq $0x28, %rdx, %rdx
movl (%rsi,%rdx), %edx
shrl $0x1f, %edx
movsbl (%rdx,%r12), %r8d
movl -0x10(%rax,%r14), %edx
movq %rdx, 0x48(%rsp)
movl (%rax,%r14), %edx
shrl $0x1f, %edx
leaq 0x20151a(%rip), %r10    # 0x2261c3
movsbl (%rdx,%r10), %edx
movq %rdx, 0x18(%rsp)
movl -0xc(%rax,%r14), %edx
leal 0x1(%rdx), %r13d
imulq $0x28, %rdx, %rdx
movl (%rsi,%rdx), %edx
shrl $0x1f, %edx
movsbl (%rdx,%r12), %r11d
movq %rbx, %r10
movl -0x8(%rax,%r14), %ebx
movq %r12, %r15
movl -0x4(%rax,%r14), %r12d
leaq 0x210f47(%rip), %rsi    # 0x235c29
leaq 0x210f2c(%rip), %rdx    # 0x235c15
xorl %eax, %eax
pushq %r12
movq %r15, %r12
pushq %rbx
movq %r10, %rbx
pushq %r11
movq 0x1d8(%rsp), %r15
pushq %r13
movq 0x20(%rsp), %r13
pushq 0x38(%rsp)
pushq 0x70(%rsp)
callq 0x22b0
addq $0x30, %rsp
incq %rbp
addq $0x1c, %r14
cmpq 0x20(%r15), %rbp
jb 0x24c66
movl 0x2c(%rsp), %eax
movq 0x8(%rsp), %r13
cmpq $0x0, 0x10(%r13)
je 0x25263
movl 0x38(%rsp), %edx
movl 0x44(%rsp), %ecx
movl %ecx, 0x20(%rsp)
movl 0x30(%rsp), %ecx
movq %rcx, 0x58(%rsp)
movl 0x34(%rsp), %ebp
movl 0x3c(%rsp), %ecx
movl %ecx, 0x10(%rsp)
movl 0x40(%rsp), %r15d
xorl %r12d, %r12d
xorl %ebx, %ebx
movq $0x0, 0x18(%rsp)
movq (%rsp), %r8
movq (%r13), %rcx
cmpl $0x0, 0x30(%rcx,%r12)
je 0x24eb8
movl 0x10(%rcx,%r12), %r9d
movl 0x1c(%rcx,%r12), %eax
shll $0x1f, %eax
je 0x24dbe
movq (%r8), %rdx
movq 0x10(%rdx), %rdx
imulq $0x28, %r9, %rsi
movl (%rdx,%rsi), %r15d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %r15d
movl 0x18(%rcx,%r12), %edx
notl %edx
addl %r15d, %edx
movl %edx, 0x10(%rsp)
subl 0x14(%rcx,%r12), %r15d
jmp 0x24dcf
movl 0x14(%rcx,%r12), %edx
movl %edx, 0x10(%rsp)
movl 0x18(%rcx,%r12), %r15d
incl %r15d
leaq (%rcx,%r12), %rdi
movl 0x30(%rcx,%r12), %edx
movl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
andl %esi, %edx
addl %edx, %eax
addl $0x40000000, %eax       # imm = 0x40000000
movl %eax, 0x20(%rsp)
movl 0x4(%rcx,%r12), %esi
movl 0x8(%rcx,%r12), %ebp
incl %ebp
movl %ebp, %edx
movq (%r8), %rax
movq 0x10(%rax), %rax
movq %r9, 0x48(%rsp)
imulq $0x28, %r9, %rcx
movl (%rax,%rcx), %r8d
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
andq %r14, %r8
movq %rsi, 0x58(%rsp)
movq 0x68(%rsp), %rcx
callq 0x1be26d
cmpq %r14, %rax
sete %cl
testq %rax, %rax
sets %al
orb %cl, %al
jne 0x24eaa
movq 0x18(%rsp), %rax
cmpq %rax, %rbx
je 0x24ea2
movq 0x8(%rsp), %rax
movq (%rax), %r13
leaq (%r12,%r13), %rsi
movq %rsi, 0x50(%rsp)
movl $0xa8, %edx
leaq 0xd0(%rsp), %rdi
callq 0x2160
imulq $0xa8, 0x18(%rsp), %r14
addq %r14, %r13
movl $0xa8, %edx
movq 0x50(%rsp), %rdi
movq %r13, %rsi
movq 0x8(%rsp), %r13
callq 0x2160
addq (%r13), %r14
movl $0xa8, %edx
movq %r14, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x2160
movq 0x18(%rsp), %rax
incq %rax
movq %rax, 0x18(%rsp)
movq 0x60(%rsp), %rax
movq (%rsp), %r8
movq 0x48(%rsp), %rdx
incq %rbx
addq $0xa8, %r12
cmpq 0x10(%r13), %rbx
jb 0x24d73
movl %edx, 0x38(%rsp)
movl 0x20(%rsp), %ecx
movl %ecx, 0x44(%rsp)
movq 0x58(%rsp), %rcx
movl %ecx, 0x30(%rsp)
movl %ebp, 0x34(%rsp)
movl 0x10(%rsp), %ecx
movl %ecx, 0x3c(%rsp)
movl %r15d, 0x40(%rsp)
movq 0x18(%rsp), %rcx
jmp 0x25265
movl $0x40000000, %ebx       # imm = 0x40000000
leaq 0x2c(%rsp), %rax
orl 0x18(%rax), %ebx
xorl %r12d, %r12d
movq $0x0, 0x20(%rsp)
movq 0x10(%r15), %rax
movl (%rax,%r12,8), %eax
movq (%r13), %rcx
imulq $0xa8, %rax, %rdx
leaq (%rcx,%rdx), %rbp
movq (%r9), %rsi
movq 0x10(%rsi), %rdi
movl (%rcx,%rdx), %r13d
movl 0x10(%rcx,%rdx), %eax
imulq $0x28, %rax, %r11
movq %rdi, 0x48(%rsp)
movl (%rdi,%r11), %r15d
movl %r13d, 0x2c(%rsp)
movl %eax, 0x38(%rsp)
movl %ebx, 0x44(%rsp)
movl 0x30(%rcx,%rdx), %r14d
movl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
andl %eax, %r14d
andl $0xc0000000, %ebx       # imm = 0xC0000000
orl %r14d, %ebx
movl %ebx, 0x44(%rsp)
movl 0x1c(%rcx,%rdx), %edi
shll $0x1f, %edi
leal (%r14,%rdi), %ebx
addl $0x40000000, %ebx       # imm = 0x40000000
movl %ebx, 0x44(%rsp)
movl 0x4(%rcx,%rdx), %eax
movl %eax, 0x30(%rsp)
movl 0x8(%rcx,%rdx), %ecx
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
incl %ecx
movl %ecx, 0x34(%rsp)
testl %edi, %edi
je 0x24fb9
movl 0x18(%rbp), %edx
notl %edx
addl %r15d, %edx
movl %edx, 0x3c(%rsp)
movl 0x14(%rbp), %r8d
movl %r15d, %edi
subl %r8d, %edi
jmp 0x24fc5
movl 0x14(%rbp), %edx
movl %edx, 0x3c(%rsp)
movl 0x18(%rbp), %edi
incl %edi
movl %edi, 0x40(%rsp)
subl %eax, %ecx
cmpq %r10, %rcx
jb 0x25176
subl %edx, %edi
cmpq %r10, %rdi
jb 0x25176
movq %r11, 0x18(%rsp)
leaq 0x2c(%rsp), %rdi
callq 0x1b9e17
testl %eax, %eax
je 0x251e8
testl %r14d, %r14d
je 0x2517d
imulq $0x28, %r13, %rax
movq 0x48(%rsp), %rcx
movl (%rcx,%rax), %ecx
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
andq %r14, %rcx
subq $0x8, %rsp
leaq 0x34(%rsp), %rdi
movq 0x60(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
movq %r15, %r8
leaq 0x98(%rsp), %r9
leaq 0xd0(%rsp), %rax
pushq %rax
callq 0x1c0b65
addq $0x10, %rsp
movq 0x90(%rsp), %rdx
movq (%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
movq 0x18(%rsp), %rsi
movl (%rcx,%rsi), %ecx
andq %r14, %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 0x1c1081
addq $0x30, %rsp
testq %rax, %rax
je 0x2517d
movq %rax, %r15
xorl %r14d, %r14d
cmpq 0x50(%rsp), %rax
setbe %r14b
shlq $0x3f, %r14
movq 0x88(%rsp), %rax
movq 0x10(%rsp), %r10
cmpq %r10, %rax
movq (%rsp), %r9
jb 0x25168
movq 0x80(%rsp), %r13
cmpq %r10, %r13
jb 0x25168
xorl %ecx, %ecx
cmpq 0x50(%rsp), %r15
setbe %cl
cmpq %r13, %rax
cmovbq %rax, %r13
incq 0xa0(%rsp,%rcx,8)
movq 0x1b0(%rsp), %rbp
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0x25146
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbp)
movq 0x10(%rbp), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r10
movq (%rsp), %r9
movq %rax, 0x10(%rbp)
shlq $0x20, %r13
leaq (%r12,%r12), %rax
orq %r13, %rax
orq %r14, %rax
movq (%rbp), %rcx
movq 0x10(%rbp), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbp)
movq %rax, (%rdx,%rcx,8)
movq 0x78(%rsp), %rcx
cmpq %r10, %rcx
jae 0x2520f
movq 0x8(%rsp), %r13
jmp 0x251f6
leaq (%r12,%r12), %r15
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
orq %rax, %r15
incq 0x20(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x8(%rdi), %rax
cmpq %rax, (%rdi)
movq 0x8(%rsp), %r13
jne 0x251d4
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rdi)
movq 0x10(%rdi), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x1b0(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rax
movq 0x10(%rdi), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rdi)
movq %r15, (%rcx,%rax,8)
jmp 0x251ed
movq 0x8(%rsp), %r13
movq (%rsp), %r9
movq 0x10(%rsp), %r10
incq %r12
movq 0x1c0(%rsp), %r15
cmpq (%r15), %r12
jb 0x24f15
jmp 0x24bfd
movq 0x70(%rsp), %rax
cmpq %r10, %rax
movq 0x8(%rsp), %r13
jb 0x251f6
xorl %edx, %edx
cmpq 0x50(%rsp), %r15
setbe %dl
cmpq %rax, %rcx
cmovbq %rcx, %rax
shlq $0x20, %rax
leaq (%r12,%r12), %r15
orq %rax, %r15
orq %r14, %r15
incq 0xa0(%rsp,%rdx,8)
orq $0x1, %r15
movq 0x1b0(%rsp), %rdi
movq 0x8(%rdi), %rax
cmpq %rax, (%rdi)
je 0x251a9
jmp 0x251d4
xorl %ecx, %ecx
movl %eax, 0x2c(%rsp)
movq %rcx, 0x10(%r13)
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq