// File: chhylp123[P]hifiasm/inter.cpp
// Function: cal_npc_err(overlap_region*, long, long, hpc_re_t*, long)
int64_t cal_npc_err(overlap_region *z, int64_t xs, int64_t xe, hpc_re_t *hre, int64_t ylen)
{	
	int64_t wn = z->w_list.n, wk, xk, yk, ck; window_list *m; bit_extz_t ez; 
    int64_t ws, we, os, oe, ovlp, xl, yl, werr, err, tot;
	hpc_ss_t *fx = NULL, *fy = NULL; int64_t nx = 0, ny = 0;
    err = 0; tot = xe - xs; wk = xk = yk = ck = -1;
    if(!wn) return INT32_MAX;
	wk = 0; 
	if(wn) {
		xk = z->w_list.a[wk].x_start; yk = z->w_list.a[wk].y_start; ck = 0;
	}
	if(hre) {
		fx = hre->a + hre->idx[z->x_id].s; nx = hre->idx[z->x_id].e - hre->idx[z->x_id].s;
		fy = hre->a + hre->idx[z->y_id].s; ny = hre->idx[z->y_id].e - hre->idx[z->y_id].s;
	}
	// if(hre && ((z->x_id == 44 && z->y_id == 45) || (z->x_id == 45 && z->y_id == 44))) {
	// 	fprintf(stderr, "[M::%s]\tutg%.6ul\tq::[%u,\t%u)\t%c\tutg%.6ul\tt::[%u,\t%u)\terr::%u\twn::%u\n", 
	// 		__func__, z->x_id+1, z->x_pos_s, z->x_pos_e+1, "+-"[z->y_pos_strand], 
	// 		z->y_id+1, z->y_pos_s, z->y_pos_e+1, z->non_homopolymer_errors, (uint32_t)z->w_list.n);
	// }

	while(wk < wn) {
		m = &(z->w_list.a[wk]); 
		// if(hre && ((z->x_id == 44 && z->y_id == 45) || (z->x_id == 45 && z->y_id == 44))) {
		// 	fprintf(stderr, "[M::%s]k::%ld\tq::[%u,\t%u)\tt::[%u,\t%u)\terr::%u\tis_ualn::%u\tis_est::%u\n", 
		// 		__func__, wk, m->x_start, m->x_end+1, m->y_start, m->y_end+1, m->error, 
		// 		(is_ualn_win((*m))), is_est_aln((*m)));
		// }
		ws = m->x_start; we = m->x_end+1;
		os = MAX(xs, ws); oe = MIN(xe, we);
		ovlp = ((oe>os)? (oe-os):0);
		if(ovlp) {
			xl = m->x_end+1-m->x_start;
			yl = m->y_end+1-m->y_start;
			if((is_ualn_win((*m))) || (is_est_aln((*m)))) {
				if(is_ualn_win((*m))) { //unmapped
					werr = MAX(xl, yl);///gen_err_unaligned(xl, yl);
				} else {
					werr = m->error;//shared window
				}
				if(ovlp < xl) {
					werr = (((double)ovlp)/((double)xl))*((double)werr);
				}
				//skip the whole window
				err += werr; xk = m->x_end+1; yk = m->y_end+1; ck = m->clen;
			} else {
				if(ovlp == xl) { //skip the whole window
					err += m->error; xk = m->x_end+1; yk = m->y_end+1; ck = m->clen;
				} else {
					set_bit_extz_t(ez, (*z), wk); 
					err += retrieve_npc_cigar_err(&ez, os, oe, &xk, &yk, &ck, fx, nx, fy, ny, ylen, z->y_pos_strand);
				}                
			}
		}
		tot -= ovlp; 
		if(xk >= xe) break;//[min_w, max_w] && [s, e)
		wk++; if(wk >= wn) break;
		xk = z->w_list.a[wk].x_start; yk = z->w_list.a[wk].y_start; ck = 0;//reset
	}
	assert(!tot);
	return err;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq %r8, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x1a8(%rbp)
movq $0x0, -0x1b0(%rbp)
movq $0x0, -0x1b8(%rbp)
movq $0x0, -0x1c0(%rbp)
movq $0x0, -0x198(%rbp)
movq -0x38(%rbp), %rax
subq -0x30(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq $-0x1, -0x70(%rbp)
movq $-0x1, -0x68(%rbp)
movq $-0x1, -0x60(%rbp)
movq $-0x1, -0x58(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x2c441a
movq $0x7fffffff, -0x20(%rbp) # imm = 0x7FFFFFFF
jmp 0x2c4a18
movq $0x0, -0x58(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x2c4466
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x2c451a
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x28(%rbp), %rdx
movl (%rdx), %edx
movl (%rcx,%rdx,8), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x1a8(%rbp)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
movl 0x4(%rax,%rcx,8), %eax
movq -0x40(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x28(%rbp), %rdx
movl (%rdx), %edx
subl (%rcx,%rdx,8), %eax
movl %eax, %eax
movq %rax, -0x1b8(%rbp)
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x28(%rbp), %rdx
movl 0x10(%rdx), %edx
movl (%rcx,%rdx,8), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x1b0(%rbp)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0x10(%rcx), %ecx
movl 0x4(%rax,%rcx,8), %eax
movq -0x40(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x28(%rbp), %rdx
movl 0x10(%rdx), %edx
subl (%rcx,%rdx,8), %eax
movl %eax, %eax
movq %rax, -0x1c0(%rbp)
jmp 0x2c451c
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jge 0x2c49d9
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movslq (%rax), %rax
movq %rax, -0x158(%rbp)
movq -0x78(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x160(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x158(%rbp), %rax
jl 0x2c457c
movq -0x30(%rbp), %rax
movq %rax, -0x1c8(%rbp)
jmp 0x2c458a
movq -0x158(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x1c8(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x160(%rbp), %rax
jg 0x2c45b2
movq -0x38(%rbp), %rax
movq %rax, -0x1d0(%rbp)
jmp 0x2c45c0
movq -0x160(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq -0x1d0(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rax
cmpq -0x168(%rbp), %rax
jle 0x2c45f5
movq -0x170(%rbp), %rax
subq -0x168(%rbp), %rax
movq %rax, -0x1d8(%rbp)
jmp 0x2c4600
xorl %eax, %eax
movq %rax, -0x1d8(%rbp)
jmp 0x2c4600
movq -0x1d8(%rbp), %rax
movq %rax, -0x178(%rbp)
cmpq $0x0, -0x178(%rbp)
je 0x2c495b
movq -0x78(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movq -0x78(%rbp), %rcx
subl (%rcx), %eax
cltq
movq %rax, -0x180(%rbp)
movq -0x78(%rbp), %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
movq -0x78(%rbp), %rcx
subl 0x8(%rcx), %eax
cltq
movq %rax, -0x188(%rbp)
movq -0x78(%rbp), %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x2c4675
movq -0x78(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x2c4675
movq -0x78(%rbp), %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jl 0x2c4696
movq -0x78(%rbp), %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jge 0x2c478e
movq -0x78(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x2c478e
movq -0x78(%rbp), %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x2c46fa
movq -0x78(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x2c46fa
movq -0x78(%rbp), %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x2c46fa
movq -0x180(%rbp), %rax
cmpq -0x188(%rbp), %rax
jl 0x2c46dc
movq -0x180(%rbp), %rax
movq %rax, -0x1e0(%rbp)
jmp 0x2c46ea
movq -0x188(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq -0x1e0(%rbp), %rax
movq %rax, -0x190(%rbp)
jmp 0x2c470a
movq -0x78(%rbp), %rax
movswq 0x14(%rax), %rax
movq %rax, -0x190(%rbp)
movq -0x178(%rbp), %rax
cmpq -0x180(%rbp), %rax
jge 0x2c4749
cvtsi2sdq -0x178(%rbp), %xmm0
cvtsi2sdq -0x180(%rbp), %xmm1
divsd %xmm1, %xmm0
cvtsi2sdq -0x190(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x190(%rbp)
movq -0x190(%rbp), %rax
addq -0x198(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x78(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x60(%rbp)
movq -0x78(%rbp), %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x68(%rbp)
movq -0x78(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0x70(%rbp)
jmp 0x2c4959
movq -0x178(%rbp), %rax
cmpq -0x180(%rbp), %rax
jne 0x2c47e5
movq -0x78(%rbp), %rax
movswq 0x14(%rax), %rax
addq -0x198(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x78(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x60(%rbp)
movq -0x78(%rbp), %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x68(%rbp)
movq -0x78(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0x70(%rbp)
jmp 0x2c4957
jmp 0x2c47e7
movq -0x28(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x58(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
movq %rax, -0xa0(%rbp)
movq %rax, -0xa8(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x134(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x130(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x140(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x13c(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0x124(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0x128(%rbp)
movq -0x168(%rbp), %rsi
movq -0x170(%rbp), %rdx
movq -0x1a8(%rbp), %r15
movq -0x1b8(%rbp), %r14
movq -0x1b0(%rbp), %rbx
movq -0x1c0(%rbp), %r11
movq -0x48(%rbp), %r10
movq -0x28(%rbp), %rax
movl 0x1c(%rax), %eax
leaq -0x150(%rbp), %rdi
leaq -0x60(%rbp), %rcx
leaq -0x68(%rbp), %r8
leaq -0x70(%rbp), %r9
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x2c3ee0
addq -0x198(%rbp), %rax
movq %rax, -0x198(%rbp)
jmp 0x2c4959
jmp 0x2c495b
movq -0x178(%rbp), %rcx
movq -0x1a0(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x1a0(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x38(%rbp), %rax
jl 0x2c497f
jmp 0x2c49d9
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x2c4997
jmp 0x2c49d9
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
jmp 0x2c451c
cmpq $0x0, -0x1a0(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2c49ec
jmp 0x2c49ee
jmp 0x2c4a0d
leaq 0xbfd3a(%rip), %rdi     # 0x38472f
leaq 0xcbe33(%rip), %rsi     # 0x39082f
movl $0x2b96, %edx           # imm = 0x2B96
leaq 0xcdf46(%rip), %rcx     # 0x39294e
callq 0x2130
movq -0x198(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopw (%rax,%rax)