// File: /workspace/github_c_makefile_star2_O3/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, %ebx
subl %esi, %ebx
movl $0x80000000, %eax       # imm = 0x80000000
cmpl %ecx, %ebx
jg 0xe862
testl %ebx, %ebx
jle 0xe862
leal (%rbx,%rbx,2), %r10d
cmpl %edx, %r10d
jg 0xe862
movl 0x40(%rsp), %edx
movl 0x38(%rsp), %ecx
testl %ecx, %ecx
jle 0xe71c
movl $0x1, %r11d
shll %cl, %r11d
movq %rdi, %r14
shrq $0x20, %r14
movq %rsi, %r15
shrq $0x20, %r15
subl %r15d, %r14d
shlq %cl, %r14
movl %r14d, %ebp
subl %r11d, %ebp
cmpl %r10d, %ebp
setg %r12b
addl %r14d, %r11d
movl %r10d, %r15d
subl %r11d, %r15d
setg %r13b
orb %r12b, %r13b
xorl %r14d, %r14d
movl $0x0, %r11d
cmpb $0x1, %r13b
jne 0xe78f
subl %r10d, %ebp
jle 0xe789
movl %ebp, %r14d
negl %r14d
movl %ebp, %r11d
jmp 0xe78f
movq %rdi, %r14
shrq $0x20, %r14
movq %rsi, %r11
shrq $0x20, %r11
subl %r11d, %r14d
je 0xe862
subl %r14d, %r10d
movl %r10d, %r11d
negl %r11d
cmovsl %r10d, %r11d
cmpl %r8d, %r11d
jg 0xe862
cmpl %edx, %ebx
jl 0xe755
leal (%rdx,%rdx,2), %eax
cmpl %r14d, %eax
jle 0xe7a0
movslq %r14d, %rax
imulq $0x55555556, %rax, %rax # imm = 0x55555556
movq %rax, %r8
shrq $0x3f, %r8
shrq $0x20, %rax
addl %r8d, %eax
leal (%rax,%rax,2), %r8d
cmpl %ebx, %eax
cmovgel %ebx, %eax
cmpl %edx, %eax
cmovgel %edx, %eax
xorl %edx, %edx
cmpl %r14d, %r8d
setne %dl
subl %edx, %eax
movl %eax, %edx
jmp 0xe7a0
movl %r15d, %r14d
movl %r15d, %r11d
cmpl %r8d, %r11d
jg 0xe862
cmpl %edx, %ebx
cmovll %ebx, %edx
movl %r14d, %r10d
testl %r11d, %r11d
jle 0xe844
cvtsi2ss %r11d, %xmm1
movaps %xmm1, %xmm2
cmpl $0x1, %r11d
je 0xe822
leal 0x1(%r11), %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
movd %xmm2, %eax
movl %eax, %r8d
shrl $0x17, %r8d
addl $-0x80, %r8d
xorps %xmm2, %xmm2
cvtsi2ss %r8, %xmm2
andl $0x7fffff, %eax         # imm = 0x7FFFFF
orl $0x3f800000, %eax       # imm = 0x3F800000
movd %eax, %xmm3
movss 0x6471(%rip), %xmm4     # 0x14c60
mulss %xmm3, %xmm4
addss 0x6469(%rip), %xmm4     # 0x14c64
mulss %xmm3, %xmm4
addss 0x6461(%rip), %xmm4     # 0x14c68
addss %xmm2, %xmm4
addss 0x68e1(%rip), %xmm4     # 0x150f4
mulss %xmm4, %xmm0
addss 0x68d9(%rip), %xmm0     # 0x150f8
movaps %xmm0, %xmm2
mulss 0x68c6(%rip), %xmm1     # 0x150f0
testl %r9d, %r9d
je 0xe834
testl %r10d, %r10d
js 0xe83a
addss %xmm2, %xmm1
jmp 0xe83e
minss %xmm2, %xmm1
cvttss2si %xmm1, %eax
subl %eax, %edx
testl %ecx, %ecx
jle 0xe860
xorq %rdi, %rsi
shrq $0x20, %rsi
leal 0x2(%rdx), %eax
testl %r11d, %r11d
cmovnel %edx, %eax
testq %rsi, %rsi
cmovnel %edx, %eax
jmp 0xe862
movl %edx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq