// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: comput_sc_partial_cigar(long, long, double, overlap_region*, long*, long*)
int64_t comput_sc_partial_cigar(int64_t sc, int64_t ol, double err_sc_r, overlap_region *z, int64_t *wi, int64_t *werr)
{
	int64_t k = wi?(*wi):0, wn = z->w_list.n, err = werr?(*werr):0, e = z->x_pos_s+ol;
	if(ol == 0) return sc;
	// int64_t pk, pe;
	if((int64_t)(z->x_pos_e + 1 - z->x_pos_s) <= ol) return 0;
	if(k == wn) {
		k--;
		if(z->w_list.a[k].y_end != -1) {
			err -= z->w_list.a[k].error;
		}
	}
	if(z->w_list.a[k].x_start >= e) {
		if(z->w_list.a[k].y_end != -1) err += z->w_list.a[k].error;
		for (;(k>=0) && (z->w_list.a[k].x_start>=e); k--) {
			if(z->w_list.a[k].y_end != -1) {
				err -= z->w_list.a[k].error;
			}
		}
	} else {
		for (;(k<wn) && (z->w_list.a[k].x_start<e); k++) {
			if(z->w_list.a[k].y_end != -1) {
				err += z->w_list.a[k].error;
			}
		}
		k--;
	}
	// pk = (*wi); pe = (*werr);
	if(k < 0) {
		k = 0; err = 0;
		if(wi) (*wi) = k; if(werr) (*werr) = err;
		// assert(e <= z->w_list.a[0].x_start);
	} else {
		if(z->w_list.a[k].y_end != -1) {
			err -= z->w_list.a[k].error;
		}
		if(wi) (*wi) = k; if(werr) (*werr) = err;
	
		// if(!(err >= 0 && k >= 0 && k < wn && z->w_list.a[k].x_start < e && z->w_list.a[k].x_end + 1 >= e)){
		// 	fprintf(stderr, "[M::%s] ol::%ld, e::%ld, z::[%u, %u], k::%ld, wn::%ld, w::[%d, %d], err::%ld\n", __func__, 
		// 	ol, e, z->x_pos_s, z->x_pos_e, k, wn, z->w_list.a[k].x_start, z->w_list.a[k].x_end, err);
		// }
		// assert(err >= 0 && k >= 0 && k < wn && z->w_list.a[k].x_start < e && 
		// 											(e <= z->w_list.a[k+1].x_start));
		
		if((int64_t)z->w_list.a[k].x_end+1 <= e) {
			if(z->w_list.a[k].y_end != -1) {
				err += z->w_list.a[k].error;
			}
		} else {
			// assert(z->w_list.a[k].x_start < e);
			if(z->w_list.a[k].y_end != -1) {
				err += (((double)(e-z->w_list.a[k].x_start))/
								((double)(z->w_list.a[k].x_end+1-z->w_list.a[k].x_start)))*z->w_list.a[k].error;
			} 
		}
	}

	// int64_t dbg_k, dbg_e = comput_err_partial_cigar(ol, z, &dbg_k);
	// if(err != dbg_e) {
	// 	fprintf(stderr, "[M::%s] ol::%ld, e::%ld, z::[%u, %u], k::%ld, wn::%ld, w::[%d, %d], err::%ld, dbg_e::%ld, dbg_k::%ld, pe::%ld, pk::%ld, sum_pk_err::%ld, sum_k_err::%ld, werr::%ld\n", 
	// 	__func__, ol, e, z->x_pos_s, z->x_pos_e, k, wn, z->w_list.a[k].x_start, z->w_list.a[k].x_end, err, dbg_e, dbg_k, pe, pk, 
	// 	sum_w_err(z->w_list.a, pk), sum_w_err(z->w_list.a, k), *werr);
	// }
	// assert(err == dbg_e);
	
	ol -= (err*err_sc_r); sc -= ol; if(sc <= 0) sc = 1;
	return sc;
}

// Assembly:
testq %rcx, %rcx
je 0x16fdb1
movq (%rcx), %r10
jmp 0x16fdb4
xorl %r10d, %r10d
testq %r8, %r8
je 0x16fdbe
movq (%r8), %rax
jmp 0x16fdc0
xorl %eax, %eax
testq %rsi, %rsi
je 0x16fe0d
movl 0x4(%rdx), %r9d
movl 0x8(%rdx), %r11d
subl %r9d, %r11d
incl %r11d
cmpq %rsi, %r11
jle 0x16fe0b
pushq %r15
pushq %r14
pushq %rbx
movq 0x48(%rdx), %r11
addq %rsi, %r9
cmpq %r11, %r10
jne 0x16fe11
decq %r10
movq 0x58(%rdx), %rdx
movq %r10, %rbx
shlq $0x5, %rbx
cmpl $-0x1, 0xc(%rdx,%rbx)
je 0x16fe15
addq %rdx, %rbx
movswq 0x14(%rbx), %rbx
subq %rbx, %rax
jmp 0x16fe15
xorl %edi, %edi
movq %rdi, %rax
retq
movq 0x58(%rdx), %rdx
movq %r10, %r14
shlq $0x5, %r14
movslq (%rdx,%r14), %rbx
cmpq %rbx, %r9
jle 0x16fe5b
cmpq %r11, %r10
movq %r11, %rbx
cmovgq %r10, %rbx
addq %rdx, %r14
addq $0x14, %r14
cmpq %r11, %r10
jge 0x16fe9e
movslq -0x14(%r14), %r15
cmpq %r15, %r9
jle 0x16fe9b
cmpl $-0x1, -0x8(%r14)
je 0x16fe52
movswq (%r14), %r15
addq %r15, %rax
incq %r10
addq $0x20, %r14
jmp 0x16fe36
leaq (%rdx,%r14), %r11
cmpl $-0x1, 0xc(%r11)
je 0x16fe6e
movswq 0x14(%r11), %r11
addq %r11, %rax
leaq (%r14,%rdx), %r11
addq $0x14, %r11
testq %r10, %r10
js 0x16feed
movslq -0x14(%r11), %rbx
cmpq %rbx, %r9
jg 0x16fea9
cmpl $-0x1, -0x8(%r11)
je 0x16fe92
movswq (%r11), %rbx
subq %rbx, %rax
decq %r10
addq $-0x20, %r11
jmp 0x16fe76
movq %r10, %rbx
testq %rbx, %rbx
jle 0x16feed
decq %rbx
movq %rbx, %r10
movq %r10, %rbx
shlq $0x5, %rbx
leaq (%rdx,%rbx), %r11
movl 0xc(%rdx,%rbx), %edx
cmpl $-0x1, %edx
je 0x16fec5
movswq 0x14(%r11), %rbx
subq %rbx, %rax
testq %rcx, %rcx
je 0x16fecd
movq %r10, (%rcx)
testq %r8, %r8
je 0x16fed5
movq %rax, (%r8)
movslq 0x4(%r11), %rcx
cmpq %rcx, %r9
jle 0x16ff03
cmpl $-0x1, %edx
je 0x16ff3a
movswq 0x14(%r11), %rcx
addq %rcx, %rax
jmp 0x16ff3a
testq %rcx, %rcx
je 0x16fef6
andq $0x0, (%rcx)
testq %r8, %r8
je 0x16feff
andq $0x0, (%r8)
xorl %eax, %eax
jmp 0x16ff3a
cmpl $-0x1, %edx
je 0x16ff3a
movslq (%r11), %rdx
subq %rdx, %r9
cvtsi2sd %r9, %xmm1
subl %edx, %ecx
incl %ecx
cvtsi2sd %ecx, %xmm2
divsd %xmm2, %xmm1
movswl 0x14(%r11), %ecx
cvtsi2sd %ecx, %xmm2
cvtsi2sd %rax, %xmm3
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %rax
cvtsi2sd %rax, %xmm1
cvtsi2sd %rsi, %xmm2
mulsd %xmm0, %xmm1
subsd %xmm1, %xmm2
cvttsd2si %xmm2, %rax
subq %rax, %rdi
cmpq $0x2, %rdi
movabsq $0x1, %rax
cmovgeq %rdi, %rax
popq %rbx
popq %r14
popq %r15
retq