// File: /workspace/github_cpp_makefile_star2_O3/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 $0x148, %rsp            # imm = 0x148
movq %r8, 0x68(%rsp)
movq %rsi, 0x50(%rsp)
movq 0x48(%rdi), %rbp
testq %rbp, %rbp
je 0x1c8151
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, 0x60(%rsp)
movq $0x0, 0x58(%rsp)
testq %rcx, %rcx
je 0x1c815c
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
leaq (%r10,%r10,2), %rsi
leaq (%rdx,%rsi,4), %rsi
movq %rsi, 0x48(%rsp)
subl %r10d, %r14d
movq %r14, 0x40(%rsp)
movl (%rcx,%r9,8), %esi
movl 0x4(%rcx,%r9,8), %r9d
leaq (%rsi,%rsi,2), %rcx
leaq (%rdx,%rcx,4), %rcx
movq %rcx, 0x30(%rsp)
subl %esi, %r9d
movq %r9, 0x38(%rsp)
jmp 0x1c8180
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
jmp 0x1c838c
movq $0x0, 0x48(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movq %r11, %r14
subq 0x50(%rsp), %r14
testq %rbp, %rbp
jle 0x1c8384
decq %rbp
xorl %r12d, %r12d
xorl %r10d, %r10d
movq %r11, 0x20(%rsp)
movq %rbx, 0x10(%rsp)
movslq %eax, %rsi
movslq 0x4(%rdi,%r12), %r9
leaq 0x1(%r9), %rcx
movq 0x50(%rsp), %rdx
cmpq %rdx, %rsi
cmovleq %rdx, %rsi
cmpq %r11, %rcx
movq %r11, %rdx
cmovlq %rcx, %rdx
movq %rdx, %r13
subq %rsi, %r13
movl $0x0, %r15d
cmovleq %r15, %r13
testq %r13, %r13
je 0x1c8234
movq %r10, 0x18(%rsp)
movl %ecx, %r15d
subl %eax, %r15d
movslq %r15d, %rbx
movl 0x8(%rdi,%r12), %r8d
movl %r8d, 0xc(%rsp)
movl 0xc(%rdi,%r12), %r10d
movswq 0x14(%rdi,%r12), %r8
movl 0x1c(%rdi,%r12), %r11d
cmpq $0x7fff, %r8            # imm = 0x7FFF
jne 0x1c823c
testl %r11d, %r11d
jne 0x1c8245
xorl %r11d, %r11d
cmpw $0x0, 0x12(%rdi,%r12)
jns 0x1c8245
subl 0xc(%rsp), %r10d
incl %r10d
cmpl %r10d, %r15d
cmovgl %r15d, %r10d
movslq %r10d, %r8
jmp 0x1c8308
movq %r8, %rcx
jmp 0x1c8344
testl %r11d, %r11d
je 0x1c8308
cmpq %rbx, %r13
jne 0x1c8261
movq 0x18(%rsp), %r10
addq %r8, %r10
movq 0x20(%rsp), %r11
movq 0x10(%rsp), %rbx
jmp 0x1c8344
movl 0x18(%rdi,%r12), %ecx
addq %rcx, %rcx
movq 0x10(%rsp), %rbx
addq 0x70(%rbx), %rcx
movq %rcx, 0x128(%rsp)
movl %r11d, %ecx
movq %rcx, 0x120(%rsp)
movq %rcx, 0x118(%rsp)
movl %eax, 0x8c(%rsp)
movl %r9d, 0x90(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x80(%rsp)
movl %r10d, 0x84(%rsp)
movl %r8d, 0x9c(%rsp)
movl %r8d, 0x98(%rsp)
movl 0x1c(%rbx), %eax
leaq 0x70(%rsp), %rdi
leaq 0x28(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0x58(%rsp), %r9
pushq %rax
pushq 0x70(%rsp)
pushq 0x48(%rsp)
pushq 0x48(%rsp)
pushq 0x60(%rsp)
pushq 0x70(%rsp)
callq 0x1c7e0f
addq $0x30, %rsp
movq 0x18(%rsp), %r10
addq %rax, %r10
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %r11
jmp 0x1c8344
movq 0x18(%rsp), %r10
cmpq %rbx, %r13
movq 0x20(%rsp), %r11
jge 0x1c833c
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r8d, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %r8
movq 0x10(%rsp), %rbx
addq %r8, %r10
subq %r13, %r14
subq $0x1, %rbp
setb %al
cmpq %r11, %rcx
jge 0x1c8387
testb %al, %al
jne 0x1c8387
movq 0x58(%rbx), %rdi
movl 0x20(%rdi,%r12), %eax
movslq %eax, %r8
movq %r8, 0x28(%rsp)
movslq 0x28(%rdi,%r12), %rcx
movq %rcx, 0x60(%rsp)
movq $0x0, 0x58(%rsp)
addq $0x20, %r12
jmp 0x1c81a4
xorl %r10d, %r10d
testq %r14, %r14
jne 0x1c83a1
movq %r10, %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6b7bf(%rip), %rdi     # 0x233b67
leaq 0x778ac(%rip), %rsi     # 0x23fc5b
leaq 0x799c4(%rip), %rcx     # 0x241d7a
movl $0x2b96, %edx           # imm = 0x2B96
callq 0x2110