// File: /workspace/github_c_makefile_star2_O1/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 $0xa8, %rsp
movq %rcx, 0x18(%rsp)
movups 0x60c8(%rip), %xmm0     # 0x29608
movups %xmm0, 0xc(%rdi)
movdqu 0x60b0(%rip), %xmm0     # 0x295fc
movq %rdi, 0x28(%rsp)
movdqu %xmm0, (%rdi)
movq 0x18(%rsi), %rbx
movq 0x20(%rsi), %r14
movq 0x28(%rsi), %r15
movq 0x30(%rsi), %rbp
movq %rsi, 0x20(%rsp)
movslq 0x4(%rsi), %r11
testq %r11, %r11
jle 0x2359a
movq %r11, %rax
shlq $0x4, %rax
xorl %ecx, %ecx
pxor %xmm0, %xmm0
movdqa %xmm0, (%r15,%rcx)
movdqa %xmm0, (%rbx,%rcx)
movdqa %xmm0, (%rbp,%rcx)
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x23580
testl %edx, %edx
jle 0x238d1
movl 0xe8(%rsp), %eax
movd %eax, %xmm0
addl 0xe0(%rsp), %eax
addl %r9d, %r8d
movl 0xf0(%rsp), %ecx
movd %r8d, %xmm1
movd %r9d, %xmm2
movd %eax, %xmm3
pshuflw $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0,4,5,6,7]
pshuflw $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0,4,5,6,7]
pshuflw $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0,4,5,6,7]
pshuflw $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0,4,5,6,7]
movzwl %cx, %eax
movl %eax, 0xc(%rsp)
pshufd $0x0, %xmm1, %xmm5      # xmm5 = xmm1[0,0,0,0]
pshufd $0x0, %xmm2, %xmm6      # xmm6 = xmm2[0,0,0,0]
pshufd $0x0, %xmm3, %xmm7      # xmm7 = xmm3[0,0,0,0]
pshufd $0x0, %xmm0, %xmm8      # xmm8 = xmm0[0,0,0,0]
movl %edx, %eax
movq %rax, 0x50(%rsp)
movl %r11d, %r13d
shlq $0x4, %r13
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
xorl %r12d, %r12d
movq %r11, %rax
shlq $0x4, %rax
movq %rax, 0x58(%rsp)
xorl %r10d, %r10d
xorl %r9d, %r9d
movl $0x0, 0x10(%rsp)
xorl %edi, %edi
movq 0x18(%rsp), %r8
movq %r11, 0x30(%rsp)
movdqa %xmm5, 0x90(%rsp)
movdqa %xmm6, 0x80(%rsp)
movdqa %xmm7, 0x70(%rsp)
movdqa %xmm8, 0x60(%rsp)
testl %r11d, %r11d
jle 0x238c4
movq 0x58(%rsp), %rcx
movdqa -0x10(%rbx,%rcx), %xmm2
movzbl (%r8,%r12), %ecx
imull %r11d, %ecx
shlq $0x4, %rcx
movq 0x20(%rsp), %rdx
addq 0x10(%rdx), %rcx
pslldq $0x2, %xmm2             # xmm2 = zero,zero,xmm2[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
pxor %xmm0, %xmm0
xorl %edx, %edx
pxor %xmm1, %xmm1
paddsw (%rcx,%rdx), %xmm2
movdqa (%r15,%rdx), %xmm3
movdqa %xmm3, %xmm4
pmaxsw %xmm1, %xmm4
pmaxsw %xmm2, %xmm4
pmaxsw %xmm4, %xmm0
movdqa %xmm4, (%r14,%rdx)
psubusw %xmm6, %xmm3
movdqa %xmm4, %xmm2
psubusw %xmm5, %xmm2
pmaxsw %xmm3, %xmm2
movdqa %xmm2, (%r15,%rdx)
movdqa %xmm1, %xmm2
psubusw %xmm8, %xmm2
psubusw %xmm7, %xmm4
movdqa %xmm4, %xmm1
pmaxsw %xmm2, %xmm1
movdqa (%rbx,%rdx), %xmm2
addq $0x10, %rdx
cmpq %rdx, %r13
jne 0x23693
xorl %ecx, %ecx
pslldq $0x2, %xmm1             # xmm1 = zero,zero,xmm1[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
testl %r11d, %r11d
jle 0x2372d
xorl %edx, %edx
movdqa (%r14,%rdx), %xmm2
pmaxsw %xmm1, %xmm2
movdqa %xmm2, (%r14,%rdx)
psubusw %xmm7, %xmm2
psubusw %xmm8, %xmm1
movdqa %xmm1, %xmm3
pcmpgtw %xmm2, %xmm3
pmovmskb %xmm3, %esi
testl %esi, %esi
je 0x23734
addq $0x10, %rdx
cmpq %rdx, %r13
jne 0x236fb
incl %ecx
cmpl $0x10, %ecx
jne 0x236ef
btl $0x12, 0xf0(%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, %esi
jae 0x23854
cmpl %esi, 0xc(%rsp)
ja 0x23854
movslq %r9d, %rdx
testl %r9d, %r9d
je 0x237b2
movq -0x8(%rdi,%rdx,8), %rcx
movq %rdx, %rax
leal 0x1(%rcx), %edx
cmpq %r12, %rdx
movq %rax, %rdx
jne 0x237b2
shrq $0x20, %rcx
cmpl %esi, %ecx
jge 0x23854
decq %rdx
movq %rsi, %rcx
shlq $0x20, %rcx
orq %r12, %rcx
jmp 0x23850
cmpl %r10d, %r9d
jne 0x23843
leal (%r10,%r10), %ecx
testl %r10d, %r10d
movl $0x8, %eax
cmovel %eax, %ecx
movq %rcx, 0x38(%rsp)
leal (,%rcx,8), %eax
movq %rsi, 0x48(%rsp)
movslq %eax, %rsi
movq %rdx, 0x40(%rsp)
leaq 0x5e2e(%rip), %rdx      # 0x29618
movl $0x12f, %ecx            # imm = 0x12F
leaq 0x5e39(%rip), %r8       # 0x2962f
movl %r9d, 0x14(%rsp)
callq 0x22d64
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rsi
movdqa 0x60(%rsp), %xmm8
movdqa 0x70(%rsp), %xmm7
movdqa 0x80(%rsp), %xmm6
movdqa 0x90(%rsp), %xmm5
movq 0x18(%rsp), %r8
movl 0x14(%rsp), %r9d
movq 0x30(%rsp), %r11
movq %rax, %rdi
movq 0x38(%rsp), %rax
movl %eax, %r10d
movq %rsi, %rcx
shlq $0x20, %rcx
orq %r12, %rcx
incl %r9d
movq %rcx, (%rdi,%rdx,8)
movl 0x10(%rsp), %ecx
cmpl %esi, %ecx
jge 0x23893
testl %r11d, %r11d
jle 0x23878
xorl %ecx, %ecx
movdqa (%r14,%rcx), %xmm0
movdqa %xmm0, (%rbp,%rcx)
addq $0x10, %rcx
cmpq %rcx, %r13
jne 0x23863
btl $0x11, 0xf0(%rsp)
jae 0x2389c
movb $0x1, %dl
cmpl %esi, 0xc(%rsp)
ja 0x2389c
movq %r14, %rcx
movq %rbx, %r14
jmp 0x238a1
xorl %edx, %edx
movl %ecx, %esi
movq %rbx, %rcx
jmp 0x238a6
xorl %edx, %edx
movq %rbx, %rcx
movl %r12d, 0x8(%rsp)
testb %dl, %dl
jne 0x238e0
incq %r12
movl %esi, 0x10(%rsp)
movq %r14, %rbx
movq %rcx, %r14
cmpq 0x50(%rsp), %r12
jne 0x2365a
jmp 0x238e0
pxor %xmm1, %xmm1
pxor %xmm0, %xmm0
jmp 0x236ed
xorl %edi, %edi
xorl %esi, %esi
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
xorl %r9d, %r9d
movl %r9d, %ebx
movq 0x28(%rsp), %r14
movq %rsi, %r12
movl %esi, (%r14)
movl 0x8(%rsp), %r15d
movl %r15d, 0x4(%r14)
movl $0xffffffff, 0x8(%r14)  # imm = 0xFFFFFFFF
testl %r11d, %r11d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
jle 0x23969
leal (,%r11,8), %ecx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movzwl (%rbp,%rcx,2), %esi
cmpl %esi, %edx
jge 0x23942
movl %ecx, %edx
shrl $0x3, %edx
movl %ecx, %r9d
andl $0x7, %r9d
imull %r11d, %r9d
addl %edx, %r9d
jmp 0x23960
jne 0x2395e
movl %ecx, %esi
shrl $0x3, %esi
movl %ecx, %r8d
andl $0x7, %r8d
imull %r11d, %r8d
addl %esi, %r8d
cmpl %r9d, %r8d
cmovll %r8d, %r9d
movl %edx, %esi
incq %rcx
movl %esi, %edx
cmpl %ecx, %eax
jne 0x23924
movl %r9d, 0x8(%r14)
testq %rdi, %rdi
je 0x239e2
movq 0x20(%rsp), %rax
movzbl 0xa(%rax), %ecx
leal (%r12,%rcx), %eax
decl %eax
cltd
idivl %ecx
testl %ebx, %ebx
jle 0x239e2
movl %r15d, %ecx
subl %eax, %ecx
addl %r15d, %eax
movl 0xc(%r14), %r9d
movl 0x10(%r14), %edx
movl %ebx, %esi
xorl %r8d, %r8d
movl %r9d, %r10d
movl %edx, %r11d
movq (%rdi,%r8,8), %rbx
cmpl %ebx, %ecx
setg %r9b
cmpl %ebx, %eax
setl %bpl
orb %r9b, %bpl
movq %rbx, %r9
shrq $0x20, %r9
cmpl %r9d, %r10d
cmovgl %r10d, %r9d
cmovll %ebx, %edx
testb %bpl, %bpl
cmovel %r11d, %edx
cmovel %r10d, %r9d
incq %r8
cmpq %r8, %rsi
jne 0x2399d
movl %r9d, 0xc(%r14)
movl %edx, 0x10(%r14)
callq 0x3390
movq %r14, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq