// File: /workspace/github_cpp_makefile_star2_O3/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 0x1b9ba9
movq (%rcx), %r10
jmp 0x1b9bac
xorl %r10d, %r10d
testq %r8, %r8
je 0x1b9bb6
movq (%r8), %rax
jmp 0x1b9bb8
xorl %eax, %eax
testq %rsi, %rsi
je 0x1b9c03
movl 0x4(%rdx), %r9d
movl 0x8(%rdx), %r11d
subl %r9d, %r11d
incl %r11d
cmpq %rsi, %r11
jle 0x1b9c01
pushq %r14
pushq %rbx
movq 0x48(%rdx), %r11
addq %rsi, %r9
cmpq %r11, %r10
jne 0x1b9c07
decq %r10
movq 0x58(%rdx), %rdx
movq %r10, %rbx
shlq $0x5, %rbx
cmpl $-0x1, 0xc(%rdx,%rbx)
je 0x1b9c0b
addq %rdx, %rbx
movswq 0x14(%rbx), %rbx
subq %rbx, %rax
jmp 0x1b9c0b
xorl %edi, %edi
movq %rdi, %rax
retq
movq 0x58(%rdx), %rdx
movq %r10, %rbx
shlq $0x5, %rbx
movslq (%rdx,%rbx), %r14
cmpq %r14, %r9
jle 0x1b9ca7
cmpq %r11, %r10
jge 0x1b9c50
addq %rdx, %rbx
addq $0x14, %rbx
movslq -0x14(%rbx), %r14
cmpq %r14, %r9
jle 0x1b9c50
cmpl $-0x1, -0x8(%rbx)
je 0x1b9c41
movswq (%rbx), %r14
addq %r14, %rax
incq %r10
addq $0x20, %rbx
cmpq %r10, %r11
jne 0x1b9c2b
movq %r11, %r10
testq %r10, %r10
jle 0x1b9cee
decq %r10
movq %r10, %rbx
shlq $0x5, %rbx
leaq (%rdx,%rbx), %r11
movl 0xc(%rdx,%rbx), %edx
cmpl $-0x1, %edx
je 0x1b9c78
movswq 0x14(%r11), %rbx
subq %rbx, %rax
testq %rcx, %rcx
je 0x1b9c80
movq %r10, (%rcx)
testq %r8, %r8
je 0x1b9c88
movq %rax, (%r8)
movslq 0x4(%r11), %rcx
cmpq %rcx, %r9
jle 0x1b9d0a
cmpl $-0x1, %edx
je 0x1b9d44
movswq 0x14(%r11), %rcx
addq %rcx, %rax
jmp 0x1b9d44
leaq (%rdx,%rbx), %r11
cmpl $-0x1, 0xc(%r11)
je 0x1b9cba
movswq 0x14(%r11), %r11
addq %r11, %rax
testq %r10, %r10
js 0x1b9cee
leaq (%rbx,%rdx), %r11
addq $0x14, %r11
movslq -0x14(%r11), %rbx
cmpq %rbx, %r9
jg 0x1b9c5c
cmpl $-0x1, -0x8(%r11)
je 0x1b9cde
movswq (%r11), %rbx
subq %rbx, %rax
leaq -0x1(%r10), %rbx
addq $-0x20, %r11
testq %r10, %r10
movq %rbx, %r10
jg 0x1b9cc7
testq %rcx, %rcx
je 0x1b9cfa
movq $0x0, (%rcx)
testq %r8, %r8
je 0x1b9d06
movq $0x0, (%r8)
xorl %eax, %eax
jmp 0x1b9d44
cmpl $-0x1, %edx
je 0x1b9d44
movslq (%r11), %rdx
subq %rdx, %r9
cvtsi2sd %r9, %xmm1
subl %edx, %ecx
incl %ecx
cvtsi2sd %ecx, %xmm2
divsd %xmm2, %xmm1
movswl 0x14(%r11), %ecx
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
cvtsi2sd %rax, %xmm3
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %rax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %rsi, %xmm2
mulsd %xmm0, %xmm1
subsd %xmm1, %xmm2
cvttsd2si %xmm2, %rax
subq %rax, %rdi
cmpq $0x2, %rdi
movl $0x1, %eax
cmovgeq %rdi, %rax
popq %rbx
popq %r14
retq