// File: chhylp123[P]hifiasm/inter.cpp
// Function: comput_sc(mg128_t const*, mg128_t const*, int, int, int, float)
static inline int32_t comput_sc(const mg128_t *ai, const mg128_t *aj, int32_t max_dist_x, int32_t max_dist_y, int32_t bw, float chn_pen_gap)
{	
	int32_t dq = (int32_t)ai->y - (int32_t)aj->y, dr = (int32_t)ai->x - (int32_t)aj->x, dd, dg, q_span, sc;
	///ai and aj has already been sorted by x
	///which means ai->x >= aj->x
	if (dq <= 0 || dq > max_dist_x) return INT32_MIN;
    if (dr <= 0 || dr > max_dist_y) return INT32_MIN;
	dd = dr > dq? dr - dq : dq - dr; ///indel, dd is always >= 0
	if (dd > bw) return INT32_MIN;
	dg = dr < dq? dr : dq;///MIN(dr, dq)
	q_span = aj->y>>32&0xff;///query span; should be ai->y>>32&0xff, is it a bug?
    sc = normal_sc(aj->y>>MG_SEED_WT_SHIFT, (q_span<dg?q_span:dg)); ///positive part of sc
    ///dd: there are indels
    ///dg > q_span: there are some bases that are not covered between ai and aj
    ///it is if (dd || dg > q_span) in minigraph
	if (dd) {
		float lin_pen, log_pen;
		lin_pen = chn_pen_gap * (float)dd;
		log_pen = dd >= 2? mg_log2(dd) : 0.0f; // mg_log2() only works for dd>=2
        sc -= (int)(lin_pen + log_pen);
	}
	return sc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movss %xmm0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x8(%rcx), %rcx
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x284525
movl -0x2c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0x284531
movl $0x80000000, -0x4(%rbp) # imm = 0x80000000
jmp 0x284647
cmpl $0x0, -0x30(%rbp)
jle 0x28453f
movl -0x30(%rbp), %eax
cmpl -0x20(%rbp), %eax
jle 0x28454b
movl $0x80000000, -0x4(%rbp) # imm = 0x80000000
jmp 0x284647
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jle 0x28455e
movl -0x30(%rbp), %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x284567
movl -0x2c(%rbp), %eax
subl -0x30(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x24(%rbp), %eax
jle 0x284581
movl $0x80000000, -0x4(%rbp) # imm = 0x80000000
jmp 0x284647
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x284591
movl -0x30(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0x284597
movl -0x2c(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
andq $0xff, %rax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x38, %rax
movq %rax, -0x58(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x2845d2
movl -0x3c(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x2845d8
movl -0x38(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x58(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x30cad0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x284641
movss -0x28(%rbp), %xmm0
cvtsi2ssl -0x34(%rbp), %xmm1
mulss %xmm1, %xmm0
movss %xmm0, -0x44(%rbp)
cmpl $0x2, -0x34(%rbp)
jl 0x284617
cvtsi2ssl -0x34(%rbp), %xmm0
callq 0x30a670
movss %xmm0, -0x60(%rbp)
jmp 0x284621
xorps %xmm0, %xmm0
movss %xmm0, -0x60(%rbp)
jmp 0x284621
movss -0x60(%rbp), %xmm0
movss %xmm0, -0x48(%rbp)
movss -0x44(%rbp), %xmm0
addss -0x48(%rbp), %xmm0
cvttss2si %xmm0, %ecx
movl -0x40(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq