// File: /workspace/github_c_makefile_star2_O1/lh3[P]miniprot/chain.c
// Function: comput_sc
static inline int32_t comput_sc(uint64_t ai, uint64_t aj, int32_t max_dist_x, int32_t max_dist_y, int32_t bw, float chn_ceof_log, int32_t is_spliced, int32_t bbit, int32_t kmer)
{
	int32_t dq = (int32_t)ai - (int32_t)aj, dq3 = dq * 3, dr3, dd, sc, dd_signed = 0;
	if (dq <= 0 || dq3 > max_dist_x) return INT32_MIN;
	if (dq > max_dist_y) return INT32_MIN;
	if (bbit > 0) { // calculate the minimum gap size
		int32_t bs = 1<<bbit;
		dr3 = ((ai>>32) - (aj>>32)) << bbit;
		if (dq3 >= dr3 - bs && dq3 <= dr3 + bs) dd = dd_signed = 0;
		else if (dq3 < dr3 - bs) dd = dr3 - bs - dq3, dd_signed = -dd;
		else dd = dq3 - (dr3 + bs), dd_signed = dd;
	} else {
		dr3 = (ai>>32) - (aj>>32);
		if (dr3 == 0) return INT32_MIN;
		dd = dr3 > dq3? dr3 - dq3 : dq3 - dr3;
		dd_signed = dq3 - dr3;
	}
	if (dd > bw) return INT32_MIN;
	if (bbit > 0) {
		sc = kmer < dq? kmer : dq;
	} else if (kmer <= dq && kmer * 3 <= dr3) {
		sc = kmer;
	} else {
		int32_t dr = dr3 / 3, q = dr3 - dr * 3;
		int32_t dg = dr < dq? dr : dq;
		sc = dg < kmer? dg : kmer;
		if (q != 0) --sc; // frameshift
	}
	if (dd > 0) { // TODO: consider frameshift
		float lin_pen, log_pen;
		lin_pen = (float)dd * .33334f;
		log_pen = dd >= 2? chn_ceof_log * (mp_log2(dd + 1) - 1.0f) + 1.0f : (float)dd; // mp_log2(x) only works for x>=2
		if (is_spliced) {
			if (dd_signed < 0) sc -= (int)(lin_pen < log_pen? lin_pen : log_pen);
			else sc -= (int)(lin_pen + log_pen);
		} else sc -= (int)(lin_pen + log_pen);
	}
	if (bbit > 0 && ai>>32 == aj>>32 && dd == 0) sc += MP_BLOCK_BONUS;
	return sc;
}

// Assembly:
movl %edi, %r11d
subl %esi, %r11d
movl $0x80000000, %eax       # imm = 0x80000000
cmpl %ecx, %r11d
jg 0xe52e
testl %r11d, %r11d
jle 0xe52e
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
leal (%r11,%r11,2), %ebp
cmpl %edx, %ebp
jg 0xe528
movl 0x28(%rsp), %ecx
testl %ecx, %ecx
jle 0xe3d6
movl $0x1, %r14d
shll %cl, %r14d
movq %rdi, %rbx
shrq $0x20, %rbx
movq %rsi, %rdx
shrq $0x20, %rdx
subl %edx, %ebx
shlq %cl, %rbx
movl %ebx, %edx
subl %r14d, %edx
cmpl %ebp, %edx
setg %r15b
addl %ebx, %r14d
movl %ebp, %r10d
subl %r14d, %r10d
setg %r14b
orb %r15b, %r14b
cmpb $0x1, %r14b
jne 0xe3fa
subl %ebp, %edx
jle 0xe401
movl %edx, %r10d
negl %r10d
jmp 0xe404
movq %rdi, %rbx
shrq $0x20, %rbx
movq %rsi, %rdx
shrq $0x20, %rdx
subl %edx, %ebx
je 0xe528
subl %ebx, %ebp
movl %ebp, %edx
negl %edx
cmovsl %ebp, %edx
movl %ebp, %r10d
jmp 0xe404
xorl %r10d, %r10d
xorl %edx, %edx
jmp 0xe404
movl %r10d, %edx
cmpl %r8d, %edx
jg 0xe528
movl 0x30(%rsp), %eax
testl %ecx, %ecx
jle 0xe41e
cmpl %eax, %r11d
cmovll %r11d, %eax
jmp 0xe463
cmpl %eax, %r11d
jl 0xe42c
leal (%rax,%rax,2), %r8d
cmpl %ebx, %r8d
jle 0xe463
movslq %ebx, %r8
imulq $0x55555556, %r8, %r8   # imm = 0x55555556
movq %r8, %r14
shrq $0x3f, %r14
shrq $0x20, %r8
addl %r14d, %r8d
leal (%r8,%r8,2), %ebp
cmpl %r11d, %r8d
cmovgel %r11d, %r8d
cmpl %eax, %r8d
cmovgel %eax, %r8d
xorl %eax, %eax
cmpl %ebx, %ebp
setne %al
subl %eax, %r8d
movl %r8d, %eax
testl %edx, %edx
jle 0xe50d
cvtsi2ss %edx, %xmm1
movaps %xmm1, %xmm2
cmpl $0x1, %edx
je 0xe4e9
leal 0x1(%rdx), %r8d
xorps %xmm2, %xmm2
cvtsi2ss %r8d, %xmm2
movd %xmm2, %r8d
movl %r8d, %r11d
shrl $0x17, %r11d
addl $-0x80, %r11d
xorps %xmm2, %xmm2
cvtsi2ss %r11, %xmm2
andl $0x7fffff, %r8d         # imm = 0x7FFFFF
orl $0x3f800000, %r8d       # imm = 0x3F800000
movd %r8d, %xmm3
movss 0x575a(%rip), %xmm4     # 0x13c10
mulss %xmm3, %xmm4
addss 0x5752(%rip), %xmm4     # 0x13c14
mulss %xmm3, %xmm4
addss 0x574a(%rip), %xmm4     # 0x13c18
addss %xmm2, %xmm4
addss 0x5bca(%rip), %xmm4     # 0x140a4
mulss %xmm4, %xmm0
addss 0x5bc2(%rip), %xmm0     # 0x140a8
movaps %xmm0, %xmm2
mulss 0x5baf(%rip), %xmm1     # 0x140a0
testl %r9d, %r9d
je 0xe4fb
testl %r10d, %r10d
js 0xe501
addss %xmm2, %xmm1
jmp 0xe505
minss %xmm2, %xmm1
cvttss2si %xmm1, %r8d
subl %r8d, %eax
testl %ecx, %ecx
jle 0xe528
xorq %rdi, %rsi
shrq $0x20, %rsi
leal 0x2(%rax), %ecx
testl %edx, %edx
cmovnel %eax, %ecx
testq %rsi, %rsi
cmovnel %eax, %ecx
movl %ecx, %eax
popq %rbx
popq %r14
popq %r15
popq %rbp
retq