// File: chhylp123[P]hifiasm/inter.cpp
// Function: cal_gchain_sc_adv(ma_ug_t const*, ul_idx_t const*, ug_opt_t const*, overlap_region*, mg_path_dst_t const*, mg_lchain_t const*, ul_ov_t*, mg_lchain_t*, long*, long, float, float, rtrace_iter*, long, long)
inline int32_t cal_gchain_sc_adv(const ma_ug_t *ug, const ul_idx_t *uref, const ug_opt_t *uopt, 
overlap_region *ol, const mg_path_dst_t *dj, const mg_lchain_t *li, ul_ov_t *ui, mg_lchain_t *lc, int64_t *f, 
int64_t b_w, float diff_thre, float chn_pen_gap, rtrace_iter *tc, int64_t trans_sc, int64_t sec_sec)
{
	// const mg_lchain_t *lj;
	int32_t gap; 
	float lin_pen, log_pen;
	if (dj->n_path == 0) return INT32_MIN;
	gap = dj->dist - dj->target_dist;
	// lj = &lc[dj->meta];
	if (gap < 0) gap = -gap;
	if ((gap > ((dj->target_dist)*diff_thre)) && (gap > b_w)) return INT32_MIN;
	// if (lj->qe <= li->qs) sc = li->score;
	// else sc = (int32_t)((double)(li->qe - lj->qe) / (li->qe - li->qs) * li->score + .499); // dealing with overlap on query
	int64_t trans_l = 0, sec_err = 0, qo, el = 0, sc, sc0; 
	mg_lchain_t *lj = &(lc[dj->meta]); ul_ov_t uj;
	
	trans_l = get_overlap_region_sub_err(&(ol[li->off]), tc, lj->qe, &sec_err);
	sc = (li->qe - lj->qe) - (trans_l*trans_sc) - (sec_err*sec_sec); sc += f[dj->meta];
	if(((trans_l > 0) || (sec_err > 0)) && (lj->qe > li->qs)) {
		set_ul_ov_t_by_mg_lchain_t(&uj, lj);
		qo = infer_rovlp(ui, &uj, NULL, NULL, NULL, (ma_ug_t *)ug);
		if((!get_ecov_el(uref, uopt, li->v^1, lj->v^1, b_w, N_GCHAIN_RATE, qo, 0, &el)) || (el == 0)) {
			rtrace_iter tr; tr.k = INT32_MAX; sc0 = sc;
			sc = f[dj->meta] + li->score;
			trans_l = get_overlap_region_sub_err(&(ol[lj->off]), &tr, li->qs, &sec_err);
            sc -= (((int64_t)(lj->qe - li->qs)) - (trans_l*trans_sc) - (sec_err*sec_sec));
            if(sc < sc0) sc = sc0;
		}
	}
	

	// int64_t trans_l_debug, sec_err_debug;
	// trans_l_debug = get_overlap_region_sub_err_debug(&(ol[li->off]), lc[dj->meta].qe, &sec_err_debug);
	// if(!(trans_l_debug == trans_l && sec_err_debug == sec_err)) {
	// 	fprintf(stderr, "[M::%s::] trans_l::%ld, trans_l_debug::%ld, sec_err::%ld, sec_err_debug::%ld\n", 
	// 	__func__, trans_l, trans_l_debug, sec_err, sec_err_debug);
	// }
	// assert(trans_l_debug == trans_l && sec_err_debug == sec_err);

	
	// sc = li->score;
	//sc += dj->mlen; // TODO: is this line the right thing to do?
	// if (dj->is_0) sc += ref_bonus;
	lin_pen = chn_pen_gap * (float)gap;
	log_pen = gap >= 2? mg_log2(gap) : 0.0f;
	sc -= (int32_t)(lin_pen + log_pen);
	return sc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
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)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movss %xmm0, -0x3c(%rbp)
movss %xmm1, -0x40(%rbp)
movq -0x30(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x0, %eax
jne 0x30ccd6
movl $0x80000000, -0x4(%rbp) # imm = 0x80000000
jmp 0x30cfb6
movq -0x30(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x30(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0x30ccf5
xorl %eax, %eax
subl -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
cvtsi2ssl -0x44(%rbp), %xmm0
movq -0x30(%rbp), %rax
cvtsi2ssl 0x4(%rax), %xmm1
mulss -0x3c(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x30cd23
movslq -0x44(%rbp), %rax
cmpq 0x28(%rbp), %rax
jle 0x30cd23
movl $0x80000000, -0x4(%rbp) # imm = 0x80000000
jmp 0x30cfb6
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x70(%rbp)
movq 0x18(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0xc(%rcx), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rax
movslq (%rax), %rax
imulq $0xa8, %rax, %rax
addq %rax, %rdi
movq 0x30(%rbp), %rsi
movq -0x88(%rbp), %rax
movslq 0x18(%rax), %rdx
leaq -0x60(%rbp), %rcx
callq 0x2a1dd0
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x88(%rbp), %rcx
subl 0x18(%rcx), %eax
cltq
movq -0x58(%rbp), %rcx
imulq 0x38(%rbp), %rcx
subq %rcx, %rax
movq -0x60(%rbp), %rcx
imulq 0x40(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x78(%rbp)
movq 0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0xc(%rcx), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x58(%rbp)
jg 0x30cded
cmpq $0x0, -0x60(%rbp)
jle 0x30cf4c
movq -0x88(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x38(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jle 0x30cf4c
movq -0x88(%rbp), %rsi
leaq -0xa4(%rbp), %rdi
callq 0x2a3930
movq 0x10(%rbp), %rdi
movq -0x10(%rbp), %r9
leaq -0xa4(%rbp), %rsi
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rdx
movq %r8, %rcx
callq 0x71a90
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x38(%rbp), %rax
movl 0x8(%rax), %edx
xorl $0x1, %edx
movq -0x88(%rbp), %rax
movl 0x8(%rax), %ecx
xorl $0x1, %ecx
movq 0x28(%rbp), %r8
movq -0x68(%rbp), %r9
movsd 0x721c7(%rip), %xmm0    # 0x37f030
xorl %eax, %eax
leaq -0x70(%rbp), %rax
movq $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x2a2170
cmpq $0x0, %rax
je 0x30ce92
cmpq $0x0, -0x70(%rbp)
jne 0x30cf4a
movq $0x7fffffff, -0x140(%rbp) # imm = 0x7FFFFFFF
movq -0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0xc(%rcx), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq -0x38(%rbp), %rcx
movslq 0x1c(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rdi
movq -0x88(%rbp), %rax
movslq (%rax), %rax
imulq $0xa8, %rax, %rax
addq %rax, %rdi
movq -0x38(%rbp), %rax
movslq 0x14(%rax), %rdx
leaq -0x140(%rbp), %rsi
leaq -0x60(%rbp), %rcx
callq 0x2a1dd0
movq %rax, -0x58(%rbp)
movq -0x88(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x38(%rbp), %rcx
subl 0x14(%rcx), %eax
movslq %eax, %rcx
movq -0x58(%rbp), %rax
imulq 0x38(%rbp), %rax
subq %rax, %rcx
movq -0x60(%rbp), %rax
imulq 0x40(%rbp), %rax
subq %rax, %rcx
movq -0x78(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x80(%rbp), %rax
jge 0x30cf48
movq -0x80(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x30cf4a
jmp 0x30cf4c
movss -0x40(%rbp), %xmm0
cvtsi2ssl -0x44(%rbp), %xmm1
mulss %xmm1, %xmm0
movss %xmm0, -0x48(%rbp)
cmpl $0x2, -0x44(%rbp)
jl 0x30cf79
cvtsi2ssl -0x44(%rbp), %xmm0
callq 0x30a670
movss %xmm0, -0x144(%rbp)
jmp 0x30cf86
xorps %xmm0, %xmm0
movss %xmm0, -0x144(%rbp)
jmp 0x30cf86
movss -0x144(%rbp), %xmm0
movss %xmm0, -0x4c(%rbp)
movss -0x48(%rbp), %xmm0
addss -0x4c(%rbp), %xmm0
cvttss2si %xmm0, %eax
movslq %eax, %rcx
movq -0x78(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)