// File: /workspace/github_cpp_makefile_star2_O1/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 0x1b0954
movq (%rcx), %r10
jmp 0x1b0957
xorl %r10d, %r10d
testq %r8, %r8
je 0x1b0961
movq (%r8), %rax
jmp 0x1b0963
xorl %eax, %eax
testq %rsi, %rsi
je 0x1b09fb
movl 0x4(%rdx), %r9d
movl 0x8(%rdx), %r11d
subl %r9d, %r11d
incl %r11d
cmpq %rsi, %r11
jle 0x1b09f9
pushq %r14
pushq %rbx
movq 0x48(%rdx), %r11
addq %rsi, %r9
cmpq %r11, %r10
jne 0x1b09af
decq %r10
movq 0x58(%rdx), %rbx
movq %r10, %r14
shlq $0x5, %r14
cmpl $-0x1, 0xc(%rbx,%r14)
je 0x1b09af
addq %r14, %rbx
movswq 0x14(%rbx), %rbx
subq %rbx, %rax
movq 0x58(%rdx), %rdx
movq %r10, %rbx
shlq $0x5, %rbx
movslq (%rdx,%rbx), %r14
cmpq %r14, %r9
jle 0x1b09ff
cmpq %r11, %r10
jge 0x1b09f4
addq %rdx, %rbx
addq $0x14, %rbx
movslq -0x14(%rbx), %r14
cmpq %r14, %r9
jle 0x1b09f4
cmpl $-0x1, -0x8(%rbx)
je 0x1b09e5
movswq (%rbx), %r14
addq %r14, %rax
incq %r10
addq $0x20, %rbx
cmpq %r10, %r11
jne 0x1b09cf
movq %r11, %r10
decq %r10
jmp 0x1b0a4d
xorl %edi, %edi
movq %rdi, %rax
retq
leaq (%rdx,%rbx), %r11
cmpl $-0x1, 0xc(%r11)
je 0x1b0a12
movswq 0x14(%r11), %r11
addq %r11, %rax
testq %r10, %r10
js 0x1b0a4d
leaq (%rbx,%rdx), %r11
addq $0x14, %r11
movslq -0x14(%r11), %rbx
cmpq %rbx, %r9
jg 0x1b0a4d
cmpl $-0x1, -0x8(%r11)
je 0x1b0a36
movswq (%r11), %rbx
subq %rbx, %rax
leaq -0x1(%r10), %rbx
addq $-0x20, %r11
testq %r10, %r10
movq %rbx, %r10
jg 0x1b0a1f
movq $-0x1, %r10
testq %r10, %r10
js 0x1b0a96
movq %r10, %rbx
shlq $0x5, %rbx
leaq (%rdx,%rbx), %r11
movl 0xc(%rdx,%rbx), %edx
cmpl $-0x1, %edx
je 0x1b0a6e
movswq 0x14(%r11), %rbx
subq %rbx, %rax
testq %rcx, %rcx
je 0x1b0a76
movq %r10, (%rcx)
testq %r8, %r8
je 0x1b0a7e
movq %rax, (%r8)
movslq 0x4(%r11), %rcx
cmpq %rcx, %r9
jle 0x1b0ab2
cmpl $-0x1, %edx
je 0x1b0aec
movswq 0x14(%r11), %rcx
addq %rcx, %rax
jmp 0x1b0aec
testq %rcx, %rcx
je 0x1b0aa2
movq $0x0, (%rcx)
testq %r8, %r8
je 0x1b0aae
movq $0x0, (%r8)
xorl %eax, %eax
jmp 0x1b0aec
cmpl $-0x1, %edx
je 0x1b0aec
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