// File: /workspace/github_cpp_makefile_star2_O2/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 $0x168, %rsp            # imm = 0x168
movq %r9, %r13
movq %r8, 0x8(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
movq %rdi, %r15
movq 0x1b0(%rsp), %r12
movq 0x1a8(%rsp), %rax
andq $0x0, (%rax)
movq 0x1a0(%rsp), %rax
andq $0x0, (%rax)
andq $0x0, (%r12)
leaq 0x18(%r12), %rdx
andq $0x0, 0x20(%r12)
movq %rsi, (%rsp)
movq (%rsi), %rsi
movl %r13d, %edi
movq %rdx, 0x58(%rsp)
callq 0x17d688
movq 0x10(%r15), %rax
cmpq %rax, 0x8(%r12)
jae 0x17f98a
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(%r12)
movq 0x10(%r12), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r12)
pushq $0x10
popq %rbx
xorl %r14d, %r14d
pushq $0x2
popq %rbp
cmpq 0x10(%r15), %r14
jae 0x17f9f3
movq (%r12), %rcx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rcx
jne 0x17f9ce
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %rbp, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movq (%r12), %rcx
movq (%r15), %rdx
movl (%rdx,%rbx), %edx
shlq $0x20, %rdx
orq %r14, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r12)
movq %rdx, (%rax,%rcx,8)
incq %r14
addq $0xa8, %rbx
jmp 0x17f993
movq %r13, 0x50(%rsp)
movq (%r12), %rax
movq 0x10(%r12), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x156b0d
movq 0x1a0(%rsp), %rax
andq $0x0, (%rax)
leaq 0x88(%rsp), %rax
andq $0x0, (%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
andq $0x0, 0xa0(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x30(%rsp)
xorl %ebp, %ebp
movq (%rsp), %r14
cmpq (%r12), %rbp
jae 0x17fdb2
movq 0x10(%r12), %rax
movl (%rax,%rbx,4), %eax
movq (%r15), %rcx
imulq $0xa8, %rax, %rax
leaq (%rcx,%rax), %r13
movq (%r14), %rdx
movq 0x10(%rdx), %rdx
movl 0x10(%rcx,%rax), %eax
imulq $0x28, %rax, %rax
movl (%rdx,%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %r13, %rdi
leaq 0x14(%rsp), %rsi
callq 0x1a334e
movq 0x8(%rsp), %rcx
movl 0x1c(%rsp), %eax
subl 0x18(%rsp), %eax
cmpq %rcx, %rax
jb 0x17fda6
movl 0x28(%rsp), %eax
subl 0x24(%rsp), %eax
cmpq %rcx, %rax
jb 0x17fda6
movq (%r14), %r14
leaq 0x14(%rsp), %rdi
movq %r14, %rsi
callq 0x178842
testl %eax, %eax
je 0x17fce1
testl $0x3fffffff, 0x2c(%rsp) # imm = 0x3FFFFFFF
je 0x17fcea
movq 0x10(%r14), %rax
movl 0x14(%rsp), %ecx
movl 0x20(%rsp), %edx
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andq %rsi, %rcx
imulq $0x28, %rdx, %rdx
movl (%rax,%rdx), %r8d
andq %rsi, %r8
subq $0x8, %rsp
leaq 0x1c(%rsp), %rdi
movq 0x60(%rsp), %rsi
leaq 0x90(%rsp), %rdx
leaq 0x88(%rsp), %r9
leaq 0xc0(%rsp), %rax
pushq %rax
callq 0x17ecb8
addq $0x10, %rsp
movq 0x80(%rsp), %rdx
movq (%rsp), %r14
movq (%r14), %rcx
movq 0x10(%rcx), %rcx
movl 0x20(%rsp), %esi
imulq $0x28, %rsi, %rsi
movl (%rcx,%rsi), %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andq %rsi, %rcx
subq $0x10, %rsp
movq %r13, %rdi
movq %rax, %rsi
leaq 0x88(%rsp), %r8
leaq 0x80(%rsp), %r9
leaq 0xb8(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
leaq 0x88(%rsp), %rax
pushq %rax
leaq 0xd8(%rsp), %rax
pushq %rax
callq 0x17f0fe
addq $0x30, %rsp
testq %rax, %rax
je 0x17fd49
movq %rax, %r13
xorl %edi, %edi
cmpq 0x40(%rsp), %rax
setbe %dil
shlq $0x3f, %rdi
movq 0x78(%rsp), %rax
movq 0x8(%rsp), %rsi
cmpq %rsi, %rax
jb 0x17fc50
movq 0x70(%rsp), %r14
cmpq %rsi, %r14
jb 0x17fc50
xorl %ecx, %ecx
cmpq 0x40(%rsp), %r13
setbe %cl
cmpq %r14, %rax
cmovbq %rax, %r14
incq 0x90(%rsp,%rcx,8)
movq 0x1a0(%rsp), %r8
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0x17fc3b
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rdi, 0x38(%rsp)
movq %rax, %rdi
callq 0x2370
movq 0x1a0(%rsp), %r8
movq 0x38(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rax, 0x10(%r8)
movq (%r8), %rcx
shlq $0x20, %r14
orq %rbx, %r14
orq %rdi, %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %r14, (%rax,%rcx,8)
movq 0x68(%rsp), %rax
cmpq %rsi, %rax
jb 0x17fce1
movq 0x60(%rsp), %r14
cmpq %rsi, %r14
jb 0x17fce1
xorl %ecx, %ecx
cmpq 0x40(%rsp), %r13
setbe %cl
cmpq %r14, %rax
cmovbq %rax, %r14
incq 0x90(%rsp,%rcx,8)
movq 0x1a0(%rsp), %r13
movq (%r13), %rcx
movq 0x10(%r13), %rax
cmpq 0x8(%r13), %rcx
jne 0x17fcc7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r13)
shlq $0x3, %rsi
movq %rdi, 0x38(%rsp)
movq %rax, %rdi
callq 0x2370
movq 0x38(%rsp), %rdi
movq %rax, 0x10(%r13)
movq (%r13), %rcx
shlq $0x20, %r14
orq %rbx, %r14
orq %rdi, %r14
orq $0x1, %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%r13)
movq %r14, (%rax,%rcx,8)
movq (%rsp), %r14
jmp 0x17fda6
movq 0x1a0(%rsp), %r14
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x17fd24
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 %rax, 0x10(%r14)
movq (%r14), %rcx
movq %r14, %rdi
movq (%rsp), %r14
movq %rbx, %rdx
movabsq $-0x100000000, %rsi     # imm = 0xFFFFFFFF00000000
orq %rsi, %rdx
incq 0x30(%rsp)
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdi)
jmp 0x17fda2
movq 0x1a0(%rsp), %r13
movq (%r13), %rcx
movq 0x10(%r13), %rax
cmpq 0x8(%r13), %rcx
jne 0x17fd85
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r13)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r13)
movq (%r13), %rcx
movq %rbx, %rdx
movabsq $-0x100000000, %rsi     # imm = 0xFFFFFFFF00000000
orq %rsi, %rdx
incq 0x30(%rsp)
leaq 0x1(%rcx), %rsi
movq %rsi, (%r13)
movq %rdx, (%rax,%rcx,8)
incq %rbp
addq $0x2, %rbx
jmp 0x17fa47
movq 0xd3227(%rip), %rax     # 0x252fe0
movq (%rax), %rdi
movq (%r14), %rax
movq 0x10(%rax), %rax
movq 0x50(%rsp), %rcx
imulq $0x28, %rcx, %rdx
incq %rcx
movl (%rax,%rdx), %eax
shrl $0x1f, %eax
leaq 0x668af(%rip), %rdx     # 0x1e668b
movsbl (%rax,%rdx), %r8d
movl 0x20(%r12), %r9d
leaq 0x73fa4(%rip), %rsi     # 0x1f3d91
leaq 0x73fb9(%rip), %rdx     # 0x1f3dad
xorl %eax, %eax
callq 0x22b0
pushq $0x18
popq %r14
xorl %ebx, %ebx
cmpq 0x20(%r12), %rbx
jae 0x17fec4
movq 0xd31cd(%rip), %rax     # 0x252fe0
movq (%rax), %rdi
movq 0x58(%rsp), %rax
movq (%rax), %rax
movl -0x18(%rax,%r14), %edx
movl -0x14(%rax,%r14), %r9d
leal 0x1(%rdx), %ecx
movq (%rsp), %rsi
movq (%rsi), %rsi
movq 0x10(%rsi), %rsi
imulq $0x28, %rdx, %rdx
movl (%rsi,%rdx), %edx
shrl $0x1f, %edx
leaq 0x66844(%rip), %r10     # 0x1e668b
movsbl (%rdx,%r10), %r8d
movl -0x10(%rax,%r14), %edx
movq %rdx, 0x8(%rsp)
movl (%rax,%r14), %edx
shrl $0x1f, %edx
leaq 0x643bf(%rip), %r11     # 0x1e4223
movsbl (%rdx,%r11), %edx
movq %rdx, 0x30(%rsp)
movl -0xc(%rax,%r14), %edx
leal 0x1(%rdx), %r11d
imulq $0x28, %rdx, %rdx
movl (%rsi,%rdx), %edx
shrl $0x1f, %edx
movsbl (%rdx,%r10), %ebp
movl -0x8(%rax,%r14), %r10d
movl -0x4(%rax,%r14), %r13d
leaq 0x73f2a(%rip), %rsi     # 0x1f3dc1
leaq 0x73f0f(%rip), %rdx     # 0x1f3dad
xorl %eax, %eax
pushq %r13
pushq %r10
pushq %rbp
pushq %r11
pushq 0x50(%rsp)
pushq 0x30(%rsp)
callq 0x22b0
addq $0x30, %rsp
incq %rbx
addq $0x1c, %r14
jmp 0x17fe01
xorl %r12d, %r12d
movl $0x40000000, %ebp       # imm = 0x40000000
xorl %eax, %eax
xorl %ebx, %ebx
movq 0x48(%rsp), %r14
cmpq 0x10(%r15), %rbx
jae 0x18005f
movq (%r15), %rdi
cmpl $0x0, 0x30(%rdi,%r12)
je 0x180050
movq %rax, 0x8(%rsp)
movq 0x50(%rsp), %rax
movl %eax, 0x14(%rsp)
movl 0x10(%rdi,%r12), %eax
movl %eax, 0x20(%rsp)
movl 0x2c(%rsp), %ecx
orl %ebp, %ecx
movl %ecx, 0x2c(%rsp)
movl 0x30(%rdi,%r12), %edx
movl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
andl %esi, %edx
andl $0xc0000000, %ecx       # imm = 0xC0000000
orl %edx, %ecx
movl %ecx, 0x2c(%rsp)
movl 0x1c(%rdi,%r12), %r8d
shll $0x1f, %r8d
orl %r8d, %edx
orl %ebp, %edx
movl %edx, 0x2c(%rsp)
movl 0x4(%rdi,%r12), %esi
movl %esi, 0x18(%rsp)
movl 0x8(%rdi,%r12), %ecx
incl %ecx
movl %ecx, 0x1c(%rsp)
testl %r8d, %r8d
je 0x17ff90
movq (%rsp), %rdx
movq (%rdx), %rdx
movq 0x10(%rdx), %r8
imulq $0x28, %rax, %rdx
movl (%r8,%rdx), %r9d
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
andl %r11d, %r9d
movl 0x18(%rdi,%r12), %r10d
notl %r10d
addl %r9d, %r10d
movl %r10d, 0x24(%rsp)
movl (%r8,%rdx), %edx
andl %r11d, %edx
subl 0x14(%rdi,%r12), %edx
movl %edx, 0x28(%rsp)
jmp 0x17ffaf
movl 0x14(%rdi,%r12), %edx
movl %edx, 0x24(%rsp)
movl 0x18(%rdi,%r12), %edx
incl %edx
movl %edx, 0x28(%rsp)
movq (%rsp), %rdx
movq (%rdx), %rdx
movq 0x10(%rdx), %r8
addq %r12, %rdi
movl %ecx, %edx
imulq $0x28, %rax, %rax
movl (%r8,%rax), %r8d
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
andq %r13, %r8
movq %r14, %rcx
callq 0x17c802
cmpq %r13, %rax
sete %cl
testq %rax, %rax
sets %al
orb %cl, %al
je 0x17ffe4
movq 0x8(%rsp), %rax
jmp 0x180050
movq 0x8(%rsp), %rax
cmpq %rax, %rbx
je 0x18004d
movq (%r15), %r14
leaq (%r14,%r12), %r13
movl $0xa8, %edx
leaq 0xc0(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
imulq $0xa8, 0x8(%rsp), %rbp
addq %rbp, %r14
movl $0xa8, %edx
movq %r13, %rdi
movq %r14, %rsi
movq 0x48(%rsp), %r14
callq 0x2160
addq (%r15), %rbp
movl $0xa8, %edx
movq %rbp, %rdi
movl $0x40000000, %ebp       # imm = 0x40000000
leaq 0xc0(%rsp), %rsi
callq 0x2160
movq 0x8(%rsp), %rax
incq %rax
incq %rbx
addq $0xa8, %r12
jmp 0x17fed5
movq %rax, 0x10(%r15)
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq