// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %r8, 0x70(%rsp)
movq %rsi, 0x58(%rsp)
movq 0x48(%rdi), %rax
movq %rax, 0x50(%rsp)
testq %rax, %rax
je 0x17c89c
movq %rdx, %r11
movq %rdi, %rbx
movq 0x58(%rdi), %rdi
movl (%rdi), %eax
movslq %eax, %r8
movq %r8, 0x28(%rsp)
movslq 0x8(%rdi), %rdx
movq %rdx, 0x68(%rsp)
andq $0x0, 0x60(%rsp)
testq %rcx, %rcx
je 0x17c8a7
movq 0x10(%rcx), %rdx
movq 0x18(%rcx), %rcx
movl (%rbx), %esi
movl 0x10(%rbx), %r9d
movl (%rcx,%rsi,8), %r10d
movl 0x4(%rcx,%rsi,8), %r14d
imulq $0xc, %r10, %rsi
addq %rdx, %rsi
movq %rsi, 0x48(%rsp)
subl %r10d, %r14d
movq %r14, 0x40(%rsp)
movl (%rcx,%r9,8), %esi
movl 0x4(%rcx,%r9,8), %ecx
imulq $0xc, %rsi, %r9
addq %rdx, %r9
movq %r9, 0x30(%rsp)
subl %esi, %ecx
movq %rcx, 0x38(%rsp)
jmp 0x17c8cb
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
jmp 0x17cae9
movq $0x0, 0x48(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movq %r11, %r14
subq 0x58(%rsp), %r14
movq 0x50(%rsp), %rcx
testq %rcx, %rcx
movl $0x0, %edx
cmovgq %rcx, %rdx
negq %rdx
movq %rdx, 0x78(%rsp)
pushq $0x1
popq %r13
xorl %ebp, %ebp
xorl %r10d, %r10d
movq %r11, 0x20(%rsp)
movq %rbx, 0x10(%rsp)
movq 0x78(%rsp), %rcx
addq %r13, %rcx
cmpq $0x1, %rcx
je 0x17cae4
movslq %eax, %rsi
movslq 0x4(%rdi,%rbp), %r9
leaq 0x1(%r9), %rcx
movq 0x58(%rsp), %rdx
cmpq %rdx, %rsi
cmovleq %rdx, %rsi
cmpq %r11, %rcx
movq %r11, %rdx
cmovlq %rcx, %rdx
movq %rdx, %r12
subq %rsi, %r12
movl $0x0, %r15d
cmovleq %r15, %r12
testq %r12, %r12
je 0x17c9a0
movq %r10, 0x18(%rsp)
movl %ecx, %r15d
subl %eax, %r15d
movslq %r15d, %rbx
movl 0x8(%rdi,%rbp), %r8d
movl %r8d, 0xc(%rsp)
movl 0xc(%rdi,%rbp), %r10d
movswq 0x14(%rdi,%rbp), %r8
movl 0x1c(%rdi,%rbp), %r11d
cmpq $0x7fff, %r8            # imm = 0x7FFF
jne 0x17c9a8
testl %r11d, %r11d
jne 0x17c9b1
xorl %r11d, %r11d
cmpw $0x0, 0x12(%rdi,%rbp)
jns 0x17c9b1
subl 0xc(%rsp), %r10d
incl %r10d
cmpl %r10d, %r15d
cmovgl %r15d, %r10d
movslq %r10d, %r8
jmp 0x17ca76
movq %r8, %rcx
jmp 0x17caa9
testl %r11d, %r11d
je 0x17ca76
cmpq %rbx, %r12
jne 0x17c9cd
movq 0x18(%rsp), %r10
addq %r8, %r10
movq 0x20(%rsp), %r11
movq 0x10(%rsp), %rbx
jmp 0x17caa9
movl 0x18(%rdi,%rbp), %ecx
addq %rcx, %rcx
movq 0x10(%rsp), %rbx
addq 0x70(%rbx), %rcx
movq %rcx, 0x138(%rsp)
movl %r11d, %ecx
movq %rcx, 0x130(%rsp)
movq %rcx, 0x128(%rsp)
movl %eax, 0x9c(%rsp)
movl %r9d, 0xa0(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x90(%rsp)
movl %r10d, 0x94(%rsp)
movl %r8d, 0xac(%rsp)
movl %r8d, 0xa8(%rsp)
movl 0x1c(%rbx), %eax
leaq 0x80(%rsp), %rdi
leaq 0x28(%rsp), %rcx
leaq 0x68(%rsp), %r8
leaq 0x60(%rsp), %r9
pushq %rax
pushq 0x78(%rsp)
pushq 0x48(%rsp)
pushq 0x48(%rsp)
pushq 0x60(%rsp)
pushq 0x70(%rsp)
callq 0x17c57e
addq $0x30, %rsp
movq 0x18(%rsp), %r10
addq %rax, %r10
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %r11
jmp 0x17caa9
movq 0x18(%rsp), %r10
cmpq %rbx, %r12
movq 0x20(%rsp), %r11
jge 0x17caa1
cvtsi2sd %r12, %xmm0
cvtsi2sd %r15d, %xmm1
cvtsi2sd %r8d, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %r8
movq 0x10(%rsp), %rbx
addq %r8, %r10
subq %r12, %r14
cmpq %r11, %rcx
jge 0x17cae4
cmpq 0x50(%rsp), %r13
jge 0x17cae4
movq 0x58(%rbx), %rdi
movl 0x20(%rdi,%rbp), %eax
movslq %eax, %r8
movq %r8, 0x28(%rsp)
movslq 0x28(%rdi,%rbp), %rcx
andq $0x0, 0x60(%rsp)
movq %rcx, 0x68(%rsp)
incq %r13
addq $0x20, %rbp
jmp 0x17c8ff
testq %r14, %r14
jne 0x17cafe
movq %r10, %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x68fb2(%rip), %rdi     # 0x1e5ab7
leaq 0x75017(%rip), %rsi     # 0x1f1b23
leaq 0x7712f(%rip), %rcx     # 0x1f3c42
movl $0x2b96, %edx           # imm = 0x2B96
callq 0x2110