// File: chhylp123[P]hifiasm/inter.cpp
// Function: cal_cigar_err(overlap_region*, long, long, long)
int64_t cal_cigar_err(overlap_region *z, int64_t s, int64_t e, int64_t is_back)///need debugging
{
	int64_t wn = z->w_list.n, wk, xk, ck; window_list *m; bit_extz_t ez; 
	int64_t ws, we, os, oe, ovlp, xl, yl, werr, err, tot;
	err = 0; tot = e - s; wk = xk = ck = -1;
	if(!wn) return INT32_MAX;

	if(!is_back) {
		wk = 0; 
		if(wn) {
			xk = z->w_list.a[wk].x_start; ck = 0;
		}
		while(wk < wn) {
			m = &(z->w_list.a[wk]); 
        	ws = m->x_start; we = m->x_end+1;
			os = MAX(s, ws); oe = MIN(e, 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; ck = m->clen;
				} else {
					if(ovlp == xl) { //skip the whole window
						err += m->error; xk = m->x_end+1; ck = m->clen;
					} else {
						set_bit_extz_t(ez, (*z), wk); 
						err += retrieve_cigar_err_dir(&ez, os, oe, &xk, &ck, is_back);
					}                
				}
			}
			tot -= ovlp; 
			if(xk >= e) break;//[min_w, max_w] && [s, e)
			wk++; if(wk >= wn) break;
			xk = z->w_list.a[wk].x_start; ck = 0;//reset
		}
	} else {
		wk = wn-1;
		if(wn) {
			xk = z->w_list.a[wk].x_end+1; ck = z->w_list.a[wk].clen-1;
		}
		while(wk >= 0) {
			m = &(z->w_list.a[wk]); 
        	ws = m->x_start; we = m->x_end+1;
			os = MAX(s, ws); oe = MIN(e, 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_start; ck = -1;
				} else {
					if(ovlp == xl) { //skip the whole window
						err += m->error; xk = m->x_start; ck = -1;
					} else {
						set_bit_extz_t(ez, (*z), wk); 
						err += retrieve_cigar_err_dir(&ez, os, oe, &xk, &ck, is_back);
					}                
				}
			}
			tot -= ovlp; 
			if(xk <= s) break;//[s, e)
			wk--; if(wk < 0) break;
			xk = z->w_list.a[wk].x_end+1; ck = z->w_list.a[wk].clen-1;//reset
		}
	}
	assert(!tot);
	return err;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x170(%rbp)
movq -0x20(%rbp), %rax
subq -0x18(%rbp), %rax
movq %rax, -0x178(%rbp)
movq $-0x1, -0x48(%rbp)
movq $-0x1, -0x40(%rbp)
movq $-0x1, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x2c2f8d
movq $0x7fffffff, -0x8(%rbp) # imm = 0x7FFFFFFF
jmp 0x2c38d4
cmpq $0x0, -0x28(%rbp)
jne 0x2c3405
movq $0x0, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x2c2fc9
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
jmp 0x2c2fcb
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x2c3400
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movslq (%rax), %rax
movq %rax, -0x130(%rbp)
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x138(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x130(%rbp), %rax
jl 0x2c302b
movq -0x18(%rbp), %rax
movq %rax, -0x180(%rbp)
jmp 0x2c3039
movq -0x130(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x138(%rbp), %rax
jg 0x2c3061
movq -0x20(%rbp), %rax
movq %rax, -0x188(%rbp)
jmp 0x2c306f
movq -0x138(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rax
cmpq -0x140(%rbp), %rax
jle 0x2c30a4
movq -0x148(%rbp), %rax
subq -0x140(%rbp), %rax
movq %rax, -0x190(%rbp)
jmp 0x2c30af
xorl %eax, %eax
movq %rax, -0x190(%rbp)
jmp 0x2c30af
movq -0x190(%rbp), %rax
movq %rax, -0x150(%rbp)
cmpq $0x0, -0x150(%rbp)
je 0x2c339d
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movq -0x50(%rbp), %rcx
subl (%rcx), %eax
cltq
movq %rax, -0x158(%rbp)
movq -0x50(%rbp), %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
movq -0x50(%rbp), %rcx
subl 0x8(%rcx), %eax
cltq
movq %rax, -0x160(%rbp)
movq -0x50(%rbp), %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x2c3124
movq -0x50(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x2c3124
movq -0x50(%rbp), %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jl 0x2c3145
movq -0x50(%rbp), %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jge 0x2c322d
movq -0x50(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x2c322d
movq -0x50(%rbp), %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x2c31a9
movq -0x50(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x2c31a9
movq -0x50(%rbp), %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x2c31a9
movq -0x158(%rbp), %rax
cmpq -0x160(%rbp), %rax
jl 0x2c318b
movq -0x158(%rbp), %rax
movq %rax, -0x198(%rbp)
jmp 0x2c3199
movq -0x160(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x198(%rbp), %rax
movq %rax, -0x168(%rbp)
jmp 0x2c31b9
movq -0x50(%rbp), %rax
movswq 0x14(%rax), %rax
movq %rax, -0x168(%rbp)
movq -0x150(%rbp), %rax
cmpq -0x158(%rbp), %rax
jge 0x2c31f8
cvtsi2sdq -0x150(%rbp), %xmm0
cvtsi2sdq -0x158(%rbp), %xmm1
divsd %xmm1, %xmm0
cvtsi2sdq -0x168(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
addq -0x170(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0x48(%rbp)
jmp 0x2c339b
movq -0x150(%rbp), %rax
cmpq -0x158(%rbp), %rax
jne 0x2c3274
movq -0x50(%rbp), %rax
movswq 0x14(%rax), %rax
addq -0x170(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0x48(%rbp)
jmp 0x2c3399
jmp 0x2c3276
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x38(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
movq %rax, -0x78(%rbp)
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x10c(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x108(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x118(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x114(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0xfc(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0x100(%rbp)
movq -0x140(%rbp), %rsi
movq -0x148(%rbp), %rdx
movq -0x28(%rbp), %r9
leaq -0x128(%rbp), %rdi
leaq -0x40(%rbp), %rcx
leaq -0x48(%rbp), %r8
callq 0x2c29f0
addq -0x170(%rbp), %rax
movq %rax, -0x170(%rbp)
jmp 0x2c339b
jmp 0x2c339d
movq -0x150(%rbp), %rcx
movq -0x178(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x178(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x20(%rbp), %rax
jl 0x2c33c1
jmp 0x2c3400
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jl 0x2c33d9
jmp 0x2c3400
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
jmp 0x2c2fcb
jmp 0x2c3895
movq -0x30(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x2c3456
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
subl $0x1, %eax
movl %eax, %eax
movq %rax, -0x48(%rbp)
jmp 0x2c3458
cmpq $0x0, -0x38(%rbp)
jl 0x2c3893
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movslq (%rax), %rax
movq %rax, -0x130(%rbp)
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x138(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x130(%rbp), %rax
jl 0x2c34b5
movq -0x18(%rbp), %rax
movq %rax, -0x1a0(%rbp)
jmp 0x2c34c3
movq -0x130(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x1a0(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x138(%rbp), %rax
jg 0x2c34eb
movq -0x20(%rbp), %rax
movq %rax, -0x1a8(%rbp)
jmp 0x2c34f9
movq -0x138(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0x1a8(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rax
cmpq -0x140(%rbp), %rax
jle 0x2c352e
movq -0x148(%rbp), %rax
subq -0x140(%rbp), %rax
movq %rax, -0x1b0(%rbp)
jmp 0x2c3539
xorl %eax, %eax
movq %rax, -0x1b0(%rbp)
jmp 0x2c3539
movq -0x1b0(%rbp), %rax
movq %rax, -0x150(%rbp)
cmpq $0x0, -0x150(%rbp)
je 0x2c3817
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movq -0x50(%rbp), %rcx
subl (%rcx), %eax
cltq
movq %rax, -0x158(%rbp)
movq -0x50(%rbp), %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
movq -0x50(%rbp), %rcx
subl 0x8(%rcx), %eax
cltq
movq %rax, -0x160(%rbp)
movq -0x50(%rbp), %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x2c35ae
movq -0x50(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x2c35ae
movq -0x50(%rbp), %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jl 0x2c35cf
movq -0x50(%rbp), %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jge 0x2c36af
movq -0x50(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x2c36af
movq -0x50(%rbp), %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x2c3633
movq -0x50(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x2c3633
movq -0x50(%rbp), %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x2c3633
movq -0x158(%rbp), %rax
cmpq -0x160(%rbp), %rax
jl 0x2c3615
movq -0x158(%rbp), %rax
movq %rax, -0x1b8(%rbp)
jmp 0x2c3623
movq -0x160(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rax
movq %rax, -0x168(%rbp)
jmp 0x2c3643
movq -0x50(%rbp), %rax
movswq 0x14(%rax), %rax
movq %rax, -0x168(%rbp)
movq -0x150(%rbp), %rax
cmpq -0x158(%rbp), %rax
jge 0x2c3682
cvtsi2sdq -0x150(%rbp), %xmm0
cvtsi2sdq -0x158(%rbp), %xmm1
divsd %xmm1, %xmm0
cvtsi2sdq -0x168(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
addq -0x170(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x50(%rbp), %rax
movslq (%rax), %rax
movq %rax, -0x40(%rbp)
movq $-0x1, -0x48(%rbp)
jmp 0x2c3815
movq -0x150(%rbp), %rax
cmpq -0x158(%rbp), %rax
jne 0x2c36ee
movq -0x50(%rbp), %rax
movswq 0x14(%rax), %rax
addq -0x170(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x50(%rbp), %rax
movslq (%rax), %rax
movq %rax, -0x40(%rbp)
movq $-0x1, -0x48(%rbp)
jmp 0x2c3813
jmp 0x2c36f0
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x38(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
movq %rax, -0x78(%rbp)
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x10c(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x108(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x118(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x114(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0xfc(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0x100(%rbp)
movq -0x140(%rbp), %rsi
movq -0x148(%rbp), %rdx
movq -0x28(%rbp), %r9
leaq -0x128(%rbp), %rdi
leaq -0x40(%rbp), %rcx
leaq -0x48(%rbp), %r8
callq 0x2c29f0
addq -0x170(%rbp), %rax
movq %rax, -0x170(%rbp)
jmp 0x2c3815
jmp 0x2c3817
movq -0x150(%rbp), %rcx
movq -0x178(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x178(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x18(%rbp), %rax
jg 0x2c383b
jmp 0x2c3893
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jge 0x2c3850
jmp 0x2c3893
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
subl $0x1, %eax
movl %eax, %eax
movq %rax, -0x48(%rbp)
jmp 0x2c3458
jmp 0x2c3895
cmpq $0x0, -0x178(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2c38a8
jmp 0x2c38aa
jmp 0x2c38c9
leaq 0xc0e7e(%rip), %rdi     # 0x38472f
leaq 0xccf77(%rip), %rsi     # 0x39082f
movl $0x2af9, %edx           # imm = 0x2AF9
leaq 0xcf047(%rip), %rcx     # 0x39290b
callq 0x2130
movq -0x170(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
nopw %cs:(%rax,%rax)