// File: /workspace/github_c_makefile_star2_O2/ToniWestbrook[P]paladin/ksw.c
// Function: ksw_i16
kswr_t ksw_i16(kswq_t *q, int tlen, const uint8_t *target, int _o_del, int _e_del, int _o_ins, int _e_ins, int xtra) // the first gap costs -(_o+_e)
{
	int slen, i, m_b, n_b, te = -1, gmax = 0, minsc, endsc;
	uint64_t *b;
	__m128i zero, oe_del, e_del, oe_ins, e_ins, *H0, *H1, *E, *Hmax;
	kswr_t r;

#define __max_8(ret, xx) do { \
		(xx) = _mm_max_epi16((xx), _mm_srli_si128((xx), 8)); \
		(xx) = _mm_max_epi16((xx), _mm_srli_si128((xx), 4)); \
		(xx) = _mm_max_epi16((xx), _mm_srli_si128((xx), 2)); \
    	(ret) = _mm_extract_epi16((xx), 0); \
	} while (0)

	// initialization
	r = g_defr;
	minsc = (xtra&KSW_XSUBO)? xtra&0xffff : 0x10000;
	endsc = (xtra&KSW_XSTOP)? xtra&0xffff : 0x10000;
	m_b = n_b = 0; b = 0;
	zero = _mm_set1_epi32(0);
	oe_del = _mm_set1_epi16(_o_del + _e_del);
	e_del = _mm_set1_epi16(_e_del);
	oe_ins = _mm_set1_epi16(_o_ins + _e_ins);
	e_ins = _mm_set1_epi16(_e_ins);
	H0 = q->H0; H1 = q->H1; E = q->E; Hmax = q->Hmax;
	slen = q->slen;
	for (i = 0; i < slen; ++i) {
		_mm_store_si128(E + i, zero);
		_mm_store_si128(H0 + i, zero);
		_mm_store_si128(Hmax + i, zero);
	}
	// the core loop
	for (i = 0; i < tlen; ++i) {
		int j, k, imax;
		__m128i e, t, h, f = zero, max = zero, *S = q->qp + target[i] * slen; // s is the 1st score vector
		h = _mm_load_si128(H0 + slen - 1); // h={2,5,8,11,14,17,-1,-1} in the above example
		h = _mm_slli_si128(h, 2);
		for (j = 0; LIKELY(j < slen); ++j) {
			h = _mm_adds_epi16(h, *S++);
			e = _mm_load_si128(E + j);
			h = _mm_max_epi16(h, e);
			h = _mm_max_epi16(h, f);
			max = _mm_max_epi16(max, h);
			_mm_store_si128(H1 + j, h);
			e = _mm_subs_epu16(e, e_del);
			t = _mm_subs_epu16(h, oe_del);
			e = _mm_max_epi16(e, t);
			_mm_store_si128(E + j, e);
			f = _mm_subs_epu16(f, e_ins);
			t = _mm_subs_epu16(h, oe_ins);
			f = _mm_max_epi16(f, t);
			h = _mm_load_si128(H0 + j);
		}
		for (k = 0; LIKELY(k < 16); ++k) {
			f = _mm_slli_si128(f, 2);
			for (j = 0; LIKELY(j < slen); ++j) {
				h = _mm_load_si128(H1 + j);
				h = _mm_max_epi16(h, f);
				_mm_store_si128(H1 + j, h);
				h = _mm_subs_epu16(h, oe_ins);
				f = _mm_subs_epu16(f, e_ins);
				if(UNLIKELY(!_mm_movemask_epi8(_mm_cmpgt_epi16(f, h)))) goto end_loop8;
			}
		}
end_loop8:
		__max_8(imax, max);
		if (imax >= minsc) {
			if (n_b == 0 || (int32_t)b[n_b-1] + 1 != i) {
				if (n_b == m_b) {
					m_b = m_b? m_b<<1 : 8;
					b = (uint64_t*)realloc(b, 8 * m_b);
				}
				b[n_b++] = (uint64_t)imax<<32 | i;
			} else if ((int)(b[n_b-1]>>32) < imax) b[n_b-1] = (uint64_t)imax<<32 | i; // modify the last
		}
		if (imax > gmax) {
			gmax = imax; te = i;
			for (j = 0; LIKELY(j < slen); ++j)
				_mm_store_si128(Hmax + j, _mm_load_si128(H1 + j));
			if (gmax >= endsc) break;
		}
		S = H1; H1 = H0; H0 = S;
	}
	r.score = gmax; r.te = te;
	{
		int max = -1, tmp, low, high, qlen = slen * 8;
		uint16_t *t = (uint16_t*)Hmax;
		for (i = 0, r.qe = -1; i < qlen; ++i, ++t)
			if ((int)*t > max) max = *t, r.qe = i / 8 + i % 8 * slen;
			else if ((int)*t == max && (tmp = i / 8 + i % 8 * slen) < r.qe) r.qe = tmp; 
		if (b) {
			i = (r.score + q->max - 1) / q->max;
			low = te - i; high = te + i;
			for (i = 0; i < n_b; ++i) {
				int e = (int32_t)b[i];
				if ((e < low || e > high) && (int)(b[i]>>32) > r.score2)
					r.score2 = b[i]>>32, r.te2 = e;
			}
		}
	}
	free(b);
	return r;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rcx, 0x30(%rsp)
movq %rsi, %r10
movl 0xf8(%rsp), %eax
movups 0x5d93(%rip), %xmm0     # 0x246b8
movups %xmm0, 0xc(%rdi)
movups 0x5d7c(%rip), %xmm0     # 0x246ac
movups %xmm0, (%rdi)
addl %r9d, %r8d
movd %r8d, %xmm2
movd %r9d, %xmm1
movd %eax, %xmm0
addl 0xf0(%rsp), %eax
movd %eax, %xmm3
movq 0x18(%rsi), %r13
movq 0x20(%rsi), %rax
movq 0x28(%rsi), %r12
movq 0x30(%rsi), %rbp
movslq 0x4(%rsi), %r9
xorl %ecx, %ecx
testl %r9d, %r9d
movl $0x0, %ebx
cmovgl %r9d, %ebx
shlq $0x4, %rbx
pxor %xmm4, %xmm4
cmpq %rcx, %rbx
je 0x1e997
movdqa %xmm4, (%r12,%rcx)
movdqa %xmm4, (%r13,%rcx)
movdqa %xmm4, (%rbp,%rcx)
addq $0x10, %rcx
jmp 0x1e979
movq %rdi, 0x40(%rsp)
movl 0x100(%rsp), %ecx
movzwl %cx, %ecx
movl %ecx, 0x1c(%rsp)
pshuflw $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm2, %xmm5      # xmm5 = xmm2[0,0,0,0]
pshuflw $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm1, %xmm6      # xmm6 = xmm1[0,0,0,0]
pshuflw $0x0, %xmm3, %xmm1      # xmm1 = xmm3[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm1, %xmm7      # xmm7 = xmm1[0,0,0,0]
pshuflw $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm0, %xmm8      # xmm8 = xmm0[0,0,0,0]
xorl %ecx, %ecx
testl %edx, %edx
cmovlel %ecx, %edx
movq %rdx, 0x68(%rsp)
pushq $-0x1
popq %rsi
movq %r9, %rdx
shlq $0x4, %rdx
movq %rdx, 0x60(%rsp)
xorl %edi, %edi
movq $0x0, 0x8(%rsp)
xorl %r11d, %r11d
movq $0x0, 0x38(%rsp)
movq %r9, 0x20(%rsp)
movq 0x30(%rsp), %r8
movq %r10, 0x48(%rsp)
movdqa %xmm5, 0xa0(%rsp)
movdqa %xmm6, 0x90(%rsp)
movdqa %xmm7, 0x80(%rsp)
movdqa %xmm8, 0x70(%rsp)
cmpq 0x68(%rsp), %rcx
je 0x1eca0
movq %rax, %r15
movq %rcx, 0x10(%rsp)
movzbl (%r8,%rcx), %eax
imull %r9d, %eax
cltq
shlq $0x4, %rax
addq 0x10(%r10), %rax
movq 0x60(%rsp), %rcx
movdqa -0x10(%r13,%rcx), %xmm2
pslldq $0x2, %xmm2             # xmm2 = zero,zero,xmm2[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
pxor %xmm1, %xmm1
xorl %ecx, %ecx
pxor %xmm0, %xmm0
cmpq %rcx, %rbx
je 0x1eacd
paddsw (%rax,%rcx), %xmm2
movdqa (%r12,%rcx), %xmm3
movdqa %xmm3, %xmm4
pmaxsw %xmm1, %xmm4
pmaxsw %xmm2, %xmm4
pmaxsw %xmm4, %xmm0
movdqa %xmm4, (%r15,%rcx)
psubusw %xmm6, %xmm3
movdqa %xmm4, %xmm2
psubusw %xmm5, %xmm2
pmaxsw %xmm3, %xmm2
movdqa %xmm2, (%r12,%rcx)
psubusw %xmm8, %xmm1
psubusw %xmm7, %xmm4
pmaxsw %xmm4, %xmm1
movdqa (%r13,%rcx), %xmm2
addq $0x10, %rcx
jmp 0x1ea77
xorl %eax, %eax
cmpl $0x10, %eax
je 0x1eb13
pslldq $0x2, %xmm1             # xmm1 = zero,zero,xmm1[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
xorl %ecx, %ecx
cmpq %rcx, %rbx
je 0x1eb0f
movdqa (%r15,%rcx), %xmm2
pmaxsw %xmm1, %xmm2
movdqa %xmm2, (%r15,%rcx)
psubusw %xmm7, %xmm2
psubusw %xmm8, %xmm1
movdqa %xmm1, %xmm3
pcmpgtw %xmm2, %xmm3
pmovmskb %xmm3, %edx
testl %edx, %edx
je 0x1eb13
addq $0x10, %rcx
jmp 0x1eadb
incl %eax
jmp 0x1eacf
btl $0x12, 0x100(%rsp)
movdqa %xmm0, %xmm1
psrldq $0x8, %xmm1             # xmm1 = xmm1[8,9,10,11,12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero
pmaxsw %xmm0, %xmm1
movdqa %xmm1, %xmm0
psrldq $0x4, %xmm0             # xmm0 = xmm0[4,5,6,7,8,9,10,11,12,13,14,15],zero,zero,zero,zero
pmaxsw %xmm1, %xmm0
movdqa %xmm0, %xmm1
psrldq $0x2, %xmm1             # xmm1 = xmm1[2,3,4,5,6,7,8,9,10,11,12,13,14,15],zero,zero
pmaxsw %xmm0, %xmm1
pextrw $0x0, %xmm1, %r14d
jae 0x1ec41
cmpl %r14d, 0x1c(%rsp)
movq 0x10(%rsp), %rcx
ja 0x1ec46
movslq %r11d, %rdx
testl %r11d, %r11d
je 0x1eb9a
movq -0x8(%rdi,%rdx,8), %rax
leal 0x1(%rax), %ecx
cmpq 0x10(%rsp), %rcx
movq 0x10(%rsp), %rcx
jne 0x1eb9a
shrq $0x20, %rax
cmpl %r14d, %eax
jge 0x1ec46
decq %rdx
movq %r14, %rax
shlq $0x20, %rax
orq %rcx, %rax
jmp 0x1ec3b
cmpl 0x38(%rsp), %r11d
jne 0x1ec2e
leal (%r11,%r11), %ecx
testl %r11d, %r11d
pushq $0x8
popq %rax
cmovel %eax, %ecx
movq %rcx, 0x38(%rsp)
leal (,%rcx,8), %eax
movl %esi, 0x2c(%rsp)
movslq %eax, %rsi
movq %rdx, 0x50(%rsp)
leaq 0x5af7(%rip), %rdx      # 0x246c8
movl $0x12f, %ecx            # imm = 0x12F
leaq 0x5b02(%rip), %r8       # 0x246df
movq %r11, 0x58(%rsp)
callq 0x1e0b4
movq 0x50(%rsp), %rdx
movdqa 0x70(%rsp), %xmm8
movdqa 0x80(%rsp), %xmm7
movq 0x10(%rsp), %rcx
movdqa 0x90(%rsp), %xmm6
movdqa 0xa0(%rsp), %xmm5
movq 0x30(%rsp), %r8
movl 0x2c(%rsp), %esi
movq 0x20(%rsp), %r9
movq 0x58(%rsp), %r11
movq 0x48(%rsp), %r10
movq %rax, %rdi
movq %r14, %rax
shlq $0x20, %rax
orq %rcx, %rax
incl %r11d
movq %rax, (%rdi,%rdx,8)
jmp 0x1ec46
movq 0x10(%rsp), %rcx
cmpl %r14d, 0x8(%rsp)
jae 0x1ec8b
xorl %eax, %eax
cmpq %rax, %rbx
je 0x1ec66
movdqa (%r15,%rax), %xmm0
movdqa %xmm0, (%rbp,%rax)
addq $0x10, %rax
jmp 0x1ec4f
btl $0x11, 0x100(%rsp)
jae 0x1ec84
movl %r14d, %eax
movq %rax, 0x8(%rsp)
movl %ecx, %esi
cmpl %r14d, 0x1c(%rsp)
ja 0x1ec8b
jmp 0x1ec99
movq %r14, 0x8(%rsp)
movl %ecx, %esi
incq %rcx
movq %r13, %rax
movq %r15, %r13
jmp 0x1ea36
movq %r14, 0x8(%rsp)
movl %ecx, %esi
movq 0x40(%rsp), %r14
movq 0x8(%rsp), %rax
movl %eax, (%r14)
movl %esi, %ebx
movl %esi, 0x4(%r14)
leal (,%r9,8), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
pushq $-0x1
popq %rdx
movl %edx, %esi
cmpl %ecx, %eax
je 0x1ed18
movzwl (%rbp,%rcx,2), %r8d
cmpl %r8d, %esi
jge 0x1ece8
movl %ecx, %esi
shrl $0x3, %esi
movl %ecx, %edx
andl $0x7, %edx
imull %r9d, %edx
addl %esi, %edx
jmp 0x1ed10
jne 0x1ed0d
movl %ecx, %r8d
shrl $0x3, %r8d
movl %ecx, %r9d
andl $0x7, %r9d
imull 0x20(%rsp), %r9d
addl %r8d, %r9d
cmpl %edx, %r9d
cmovll %r9d, %edx
movq 0x20(%rsp), %r9
movl %esi, %r8d
incq %rcx
movl %r8d, %esi
jmp 0x1ecc7
movl %edx, 0x8(%r14)
testq %rdi, %rdi
je 0x1ed97
movzbl 0xa(%r10), %ecx
movq 0x8(%rsp), %rax
addl %ecx, %eax
decl %eax
cltd
idivl %ecx
movl %ebx, %ecx
subl %eax, %ecx
addl %ebx, %eax
movl 0xc(%r14), %r8d
movl 0x10(%r14), %edx
xorl %esi, %esi
testl %r11d, %r11d
cmovlel %esi, %r11d
cmpq %rsi, %r11
je 0x1ed8f
movq (%rdi,%rsi,8), %r9
cmpl %r9d, %ecx
setg %r10b
cmpl %r9d, %eax
movq %r11, %r15
setl %r11b
orb %r10b, %r11b
movq %r9, %r10
shrq $0x20, %r10
cmpl %r10d, %r8d
cmovgl %r8d, %r10d
movl %edx, %ebx
cmovll %r9d, %ebx
testb %r11b, %r11b
movq %r15, %r11
cmovnel %ebx, %edx
cmovel %r8d, %r10d
incq %rsi
movl %r10d, %r8d
jmp 0x1ed49
movl %r8d, 0xc(%r14)
movl %edx, 0x10(%r14)
callq 0x3390
movq %r14, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq