// File: chhylp123[P]hifiasm/inter.cpp
// Function: cal_gl_chain_lin_sc(ul_ov_t*, int, int, rtrace_iter*, overlap_region*, All_reads*, ma_ug_t*, ul_idx_t const*, ug_opt_t const*, long, double, unsigned long, long, long, int*, long)
int64_t cal_gl_chain_lin_sc(ul_ov_t *a, int32_t ai, int32_t aj, rtrace_iter *tc, overlap_region *ol, All_reads *ridx, ma_ug_t *ug, 
const ul_idx_t *uref, const ug_opt_t *uopt, int64_t bw, double diff_ec_ul, uint64_t mode, int64_t trans_sc, int64_t sec_sec, 
int32_t *f, int64_t debug_i)
{
	ul_ov_t *li = &(a[ai]), *lj = &(a[aj]); 
	///li is the suffix of lj
	if(lj->qs >= li->qs) return INT32_MIN;
	uint32_t li_v = (li->tn<<1)|li->rev, lj_v = (lj->tn<<1)|lj->rev;
	int64_t qo = infer_rovlp(li, lj, NULL, NULL, ridx, ug), trans_l = 0, sec_err = 0, sc, sc0, el; ///overlap length in query (UL read)
	///li_v == lj_v is possiable
	if(get_ecov_el(uref, uopt, li_v^1, lj_v^1, bw, diff_ec_ul, qo, mode, &el)) {
		trans_l = get_overlap_region_sub_err(&(ol[li->qn]), tc, lj->qe, &sec_err);
		sc = f[aj] + ((int64_t)(li->qe - lj->qe)) - (trans_l*trans_sc) - (sec_err*sec_sec);

		// char *as = NULL; 
		// asprintf(&as, "+[M::utg%.6dl] utg%.6dl, liq::[%u, %u), ljq::[%u, %u), trans_l::%ld, sec_err::%ld, sc::%ld, f[aj]::%d\n", 
		// (int32_t)li->tn+1, (int32_t)lj->tn+1, li->qs, li->qe, lj->qs, lj->qe, trans_l, sec_err, sc, f[aj]);
		// push_vlog(&(overall_zdbg->a[debug_i]), as); free(as); as = NULL;
		// fprintf(stderr, "+[M::utg%.6dl] utg%.6dl, liq::[%u, %u), ljq::[%u, %u), trans_l::%ld, sec_err::%ld, sc::%ld, f[aj]::%d\n", 
		// 	(int32_t)li->tn+1, (int32_t)lj->tn+1, li->qs, li->qe, lj->qs, lj->qe, trans_l, sec_err, sc, f[aj]);
		if((el == 0) && ((trans_l > 0) || (sec_err > 0)) && (lj->qe > li->qs)) {
			rtrace_iter tr; tr.k = INT32_MAX; sc0 = sc;
			sc = f[aj] + aln_sc(ol[(*li).qn], trans_sc, sec_sec);
			trans_l = get_overlap_region_sub_err(&(ol[lj->qn]), &tr, li->qs, &sec_err);
			sc -= (((int64_t)(lj->qe - li->qs)) - (trans_l*trans_sc) - (sec_err*sec_sec));


			// char *as = NULL; 
			// asprintf(&as, "-[M::utg%.6dl] utg%.6dl, liq::[%u, %u), ljq::[%u, %u), trans_l::%ld, sec_err::%ld, sc::%ld, f[aj]::%d\n", 
			// (int32_t)li->tn+1, (int32_t)lj->tn+1, li->qs, li->qe, lj->qs, lj->qe, trans_l, sec_err, sc, f[aj]);
			// push_vlog(&(overall_zdbg->a[debug_i]), as); free(as); as = NULL;

			if(sc < sc0) sc = sc0;
			// fprintf(stderr, "-[M::utg%.6dl] utg%.6dl, liq::[%u, %u), ljq::[%u, %u), trans_l::%ld, sec_err::%ld, sc::%ld, f[aj]::%d, aln_sc::%ld\n", 
			// (int32_t)li->tn+1, (int32_t)lj->tn+1, li->qs, li->qe, lj->qs, lj->qe, trans_l, sec_err, sc, f[aj], aln_sc(ol[(*li).qn], trans_sc, sec_sec));
		}
		// int64_t trans_l_debug, sec_err_debug;
		// trans_l_debug = get_overlap_region_sub_err_debug(&(ol[li->qn]), lj->qe, &sec_err_debug);
		// if(!(trans_l_debug == trans_l && sec_err_debug == sec_err)) {
		// 	fprintf(stderr, "[M::%s::qs->%u] trans_l::%ld, trans_l_debug::%ld, sec_err::%ld, sec_err_debug::%ld\n", 
		// 	__func__, lj->qe, trans_l, trans_l_debug, sec_err, sec_err_debug);
		// }
		// assert(trans_l_debug == trans_l && sec_err_debug == sec_err);
		// if(li->tn == 308 || li->tn == 311 || lj->tn == 305 || lj->tn == 304) {
		// 	fprintf(stderr, "[M::%s::utg%.6dl] utg%.6dl, liq::[%u, %u), ljq::[%u, %u), trans_l::%ld, sec_err::%ld, sc::%ld\n", __func__, 
        //         (int32_t)li->tn+1, (int32_t)lj->tn+1, li->qs, li->qe, lj->qs, lj->qe, trans_l, sec_err, sc);
		// }
		return sc;
	}
	return INT32_MIN;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x40(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jb 0x2a25dd
movq $-0x80000000, -0x8(%rbp) # imm = 0x80000000
jmp 0x2a2838
movq -0x40(%rbp), %rax
movl 0xc(%rax), %eax
shll %eax
movq -0x40(%rbp), %rcx
movl 0x18(%rcx), %ecx
shrl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movl 0xc(%rax), %eax
shll %eax
movq -0x48(%rbp), %rcx
movl 0x18(%rcx), %ecx
shrl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x50(%rbp)
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x30(%rbp), %r8
movq 0x10(%rbp), %r9
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x71a90
movq %rax, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq 0x18(%rbp), %rdi
movq 0x20(%rbp), %rsi
movl -0x4c(%rbp), %edx
xorl $0x1, %edx
movl -0x50(%rbp), %ecx
xorl $0x1, %ecx
movq 0x28(%rbp), %r8
movsd -0x38(%rbp), %xmm0
movq -0x58(%rbp), %r9
movq 0x30(%rbp), %r10
leaq -0x80(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x2a2170
cmpq $0x0, %rax
je 0x2a2830
movq -0x28(%rbp), %rdi
movq -0x40(%rbp), %rax
movl (%rax), %eax
imulq $0xa8, %rax, %rax
addq %rax, %rdi
movq -0x20(%rbp), %rsi
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
leaq -0x68(%rbp), %rcx
callq 0x2a1dd0
movq %rax, -0x60(%rbp)
movq 0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movq -0x40(%rbp), %rcx
movl 0x8(%rcx), %ecx
movq -0x48(%rbp), %rdx
subl 0x8(%rdx), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq -0x60(%rbp), %rcx
imulq 0x38(%rbp), %rcx
subq %rcx, %rax
movq -0x68(%rbp), %rcx
imulq 0x40(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0x2a2826
cmpq $0x0, -0x60(%rbp)
jg 0x2a2704
cmpq $0x0, -0x68(%rbp)
jle 0x2a2826
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x40(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jbe 0x2a2826
movq $0x7fffffff, -0x118(%rbp) # imm = 0x7FFFFFFF
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rdx
movl (%rdx), %edx
imulq $0xa8, %rdx, %rdx
addq %rdx, %rcx
movl 0x28(%rcx), %ecx
movq -0x28(%rbp), %rdx
movq -0x40(%rbp), %rsi
movl (%rsi), %esi
imulq $0xa8, %rsi, %rsi
addq %rsi, %rdx
movl 0x20(%rdx), %edx
movq -0x28(%rbp), %rsi
movq -0x40(%rbp), %rdi
movl (%rdi), %edi
imulq $0xa8, %rdi, %rdi
addq %rdi, %rsi
subl 0x28(%rsi), %edx
movl %edx, %edx
imulq 0x38(%rbp), %rdx
subq %rdx, %rcx
movq -0x28(%rbp), %rdx
movq -0x40(%rbp), %rsi
movl (%rsi), %esi
imulq $0xa8, %rsi, %rsi
addq %rsi, %rdx
movl 0x30(%rdx), %edx
imulq 0x40(%rbp), %rdx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x28(%rbp), %rdi
movq -0x48(%rbp), %rax
movl (%rax), %eax
imulq $0xa8, %rax, %rax
addq %rax, %rdi
movq -0x40(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %edx
leaq -0x118(%rbp), %rsi
leaq -0x68(%rbp), %rcx
callq 0x2a1dd0
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x40(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x60(%rbp), %rax
imulq 0x38(%rbp), %rax
subq %rax, %rcx
movq -0x68(%rbp), %rax
imulq 0x40(%rbp), %rax
subq %rax, %rcx
movq -0x70(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0x2a2824
movq -0x78(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x2a2826
movq -0x70(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2a2838
movq $-0x80000000, -0x8(%rbp) # imm = 0x80000000
movq -0x8(%rbp), %rax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)