// File: /workspace/github_cpp_makefile_star2_O1/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 $0x138, %rsp            # imm = 0x138
movq %r8, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq 0x48(%rdi), %rbp
movq $-0x1, %rax
movq %rax, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq %rax, 0x8(%rsp)
testq %rbp, %rbp
je 0x1be317
movq %rdx, %r9
movq %rdi, %rbx
movq 0x58(%rdi), %rax
movslq (%rax), %rdx
movq %rdx, 0x8(%rsp)
movslq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
testq %rcx, %rcx
je 0x1be322
movq 0x10(%rcx), %rax
movq 0x18(%rcx), %rcx
movl (%rbx), %edx
movl 0x10(%rbx), %esi
movl (%rcx,%rdx,8), %edi
movl 0x4(%rcx,%rdx,8), %r8d
leaq (%rdi,%rdi,2), %rdx
leaq (%rax,%rdx,4), %rdx
movq %rdx, 0x48(%rsp)
subl %edi, %r8d
movq %r8, 0x40(%rsp)
movl (%rcx,%rsi,8), %edx
movl 0x4(%rcx,%rsi,8), %esi
leaq (%rdx,%rdx,2), %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, 0x30(%rsp)
subl %edx, %esi
movq %rsi, 0x38(%rsp)
jmp 0x1be346
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
jmp 0x1be56a
movq $0x0, 0x48(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movq %r9, %r14
subq 0x50(%rsp), %r14
testq %rbp, %rbp
jle 0x1be562
decq %rbp
xorl %r13d, %r13d
xorl %r10d, %r10d
movq %r9, 0x20(%rsp)
movq 0x58(%rbx), %rax
movslq (%rax,%r13), %r8
movslq 0x4(%rax,%r13), %rcx
incq %rcx
movq 0x50(%rsp), %rsi
cmpq %rsi, %r8
cmovgq %r8, %rsi
cmpq %r9, %rcx
movq %r9, %rdx
cmovlq %rcx, %rdx
movq %rdx, %r15
subq %rsi, %r15
movl $0x0, %edi
cmovleq %rdi, %r15
testq %r15, %r15
je 0x1be523
movq %r10, 0x28(%rsp)
movl %ecx, %r10d
subl %r8d, %r10d
movslq %r10d, %r11
movslq 0xc(%rax,%r13), %rdi
incq %rdi
movl %edi, %r12d
subl 0x8(%rax,%r13), %r12d
movswq 0x14(%rax,%r13), %r9
cmpq $0x7fff, %r9            # imm = 0x7FFF
jne 0x1be3ea
cmpl $0x0, 0x1c(%rax,%r13)
jne 0x1be3e1
cmpw $0x0, 0x12(%rax,%r13)
js 0x1be412
cmpl $0x7fff, %r9d           # imm = 0x7FFF
je 0x1be3f2
cmpl $0x0, 0x1c(%rax,%r13)
je 0x1be412
cmpq %r11, %r15
jne 0x1be481
movq 0x28(%rsp), %r10
addq %r9, %r10
movq %rcx, 0x8(%rsp)
movslq %edi, %rcx
movq %rcx, 0x18(%rsp)
jmp 0x1be472
cmpl $0x7fff, %r9d           # imm = 0x7FFF
jne 0x1be436
cmpl $0x0, 0x1c(%rax,%r13)
jne 0x1be436
cmpw $0x0, 0x12(%rax,%r13)
jns 0x1be436
cmpl %r12d, %r10d
cmovgl %r10d, %r12d
movslq %r12d, %r9
cmpq %r11, %r15
jge 0x1be460
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r10d, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r9d, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %r9
movq 0x28(%rsp), %r10
addq %r9, %r10
movq %rcx, 0x8(%rsp)
movq %rdi, 0x18(%rsp)
movl 0x1c(%rax,%r13), %eax
movq %rax, 0x10(%rsp)
jmp 0x1be51e
movl 0x18(%rax,%r13), %ecx
addq %rcx, %rcx
addq 0x70(%rbx), %rcx
movq %rcx, 0x118(%rsp)
movl 0x1c(%rax,%r13), %ecx
movq %rcx, 0x110(%rsp)
movq %rcx, 0x108(%rsp)
movl %r8d, 0x7c(%rsp)
movl 0x4(%rax,%r13), %ecx
movl %ecx, 0x80(%rsp)
movl 0x8(%rax,%r13), %ecx
movl %ecx, 0x70(%rsp)
movl 0xc(%rax,%r13), %ecx
movl %ecx, 0x74(%rsp)
movswl 0x14(%rax,%r13), %eax
movl %eax, 0x8c(%rsp)
movl %eax, 0x88(%rsp)
movl 0x1c(%rbx), %eax
leaq 0x60(%rsp), %rdi
leaq 0x8(%rsp), %rcx
leaq 0x18(%rsp), %r8
leaq 0x10(%rsp), %r9
pushq %rax
pushq 0x60(%rsp)
pushq 0x48(%rsp)
pushq 0x48(%rsp)
pushq 0x60(%rsp)
pushq 0x70(%rsp)
callq 0x1bdfde
addq $0x30, %rsp
movq 0x28(%rsp), %r10
addq %rax, %r10
movq 0x20(%rsp), %r9
subq %r15, %r14
subq $0x1, %rbp
setb %al
cmpq %r9, 0x8(%rsp)
jge 0x1be565
testb %al, %al
jne 0x1be565
movq 0x58(%rbx), %rax
movslq 0x20(%rax,%r13), %rcx
movq %rcx, 0x8(%rsp)
movslq 0x28(%rax,%r13), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
addq $0x20, %r13
jmp 0x1be365
xorl %r10d, %r10d
testq %r14, %r14
jne 0x1be57f
movq %r10, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x694d1(%rip), %rdi     # 0x227a57
leaq 0x753fe(%rip), %rsi     # 0x23398b
leaq 0x77516(%rip), %rcx     # 0x235aaa
movl $0x2b96, %edx           # imm = 0x2B96
callq 0x2110