// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %edi, %r15d
subl %esi, %r15d
movl $0x80000000, %eax       # imm = 0x80000000
cmpl %ecx, %r15d
jg 0xce42
testl %r15d, %r15d
jle 0xce42
leal (%r15,%r15,2), %ebx
cmpl %edx, %ebx
jg 0xce42
movl 0x40(%rsp), %r11d
movl 0x38(%rsp), %ecx
testl %ecx, %ecx
jle 0xcd03
movl $0x1, %r10d
shll %cl, %r10d
movq %rdi, %r14
shrq $0x20, %r14
movq %rsi, %rdx
shrq $0x20, %rdx
subl %edx, %r14d
shlq %cl, %r14
movl %r14d, %edx
subl %r10d, %edx
cmpl %ebx, %edx
setg %r12b
addl %r14d, %r10d
movl %ebx, %ebp
subl %r10d, %ebp
setg %r13b
orb %r12b, %r13b
xorl %r10d, %r10d
movl $0x0, %r14d
cmpb $0x1, %r13b
jne 0xcd72
subl %ebx, %edx
jle 0xcd6c
movl %edx, %r10d
negl %r10d
movl %edx, %r14d
jmp 0xcd72
movq %rdi, %r10
shrq $0x20, %r10
movq %rsi, %rdx
shrq $0x20, %rdx
subl %edx, %r10d
je 0xce42
subl %r10d, %ebx
movl %ebx, %r14d
negl %r14d
cmovsl %ebx, %r14d
cmpl %r8d, %r14d
jg 0xce42
cmpl %r11d, %r15d
jl 0xcd3e
leal (%r11,%r11,2), %eax
cmpl %r10d, %eax
jle 0xcd85
movl $0x3, %r8d
movl %r10d, %eax
cltd
idivl %r8d
imull $-0x3, %eax, %edx
cmpl %r15d, %eax
cmovll %eax, %r15d
cmpl %r11d, %r15d
cmovgel %r11d, %r15d
xorl %eax, %eax
addl %r10d, %edx
setne %al
subl %eax, %r15d
movl %r15d, %r11d
jmp 0xcd85
movl %ebp, %r10d
movl %ebp, %r14d
cmpl %r8d, %r14d
jg 0xce42
cmpl %r11d, %r15d
cmovll %r15d, %r11d
movl %r10d, %ebx
testl %r14d, %r14d
jle 0xce20
cvtsi2ss %r14d, %xmm1
movaps %xmm1, %xmm2
cmpl $0x1, %r14d
je 0xcdfe
leal 0x1(%r14), %eax
cvtsi2ss %eax, %xmm2
movd %xmm2, %eax
movl %eax, %edx
shrl $0x17, %edx
addl $-0x80, %edx
cvtsi2ss %rdx, %xmm2
andl $0x7fffff, %eax         # imm = 0x7FFFFF
orl $0x3f800000, %eax       # imm = 0x3F800000
movd %eax, %xmm3
movss 0x5e95(%rip), %xmm4     # 0x12c60
mulss %xmm3, %xmm4
addss 0x5e8d(%rip), %xmm4     # 0x12c64
mulss %xmm3, %xmm4
addss 0x5e85(%rip), %xmm4     # 0x12c68
addss %xmm2, %xmm4
addss 0x6305(%rip), %xmm4     # 0x130f4
mulss %xmm4, %xmm0
addss 0x62fd(%rip), %xmm0     # 0x130f8
movaps %xmm0, %xmm2
mulss 0x62ea(%rip), %xmm1     # 0x130f0
testl %r9d, %r9d
je 0xce0f
testl %ebx, %ebx
js 0xce15
addss %xmm2, %xmm1
jmp 0xce19
minss %xmm2, %xmm1
cvttss2si %xmm1, %eax
subl %eax, %r11d
testl %ecx, %ecx
jle 0xce3f
xorq %rdi, %rsi
shrq $0x20, %rsi
leal 0x2(%r11), %eax
testl %r14d, %r14d
cmovnel %r11d, %eax
testq %rsi, %rsi
cmovnel %r11d, %eax
jmp 0xce42
movl %r11d, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq