// File: /workspace/github_c_makefile_star2_O1/ToniWestbrook[P]paladin/ksw.c
// Function: ksw_u8
kswr_t ksw_u8(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, shift, *H0, *H1, *E, *Hmax;
	kswr_t r;

#define __max_16(ret, xx) do { \
		(xx) = _mm_max_epu8((xx), _mm_srli_si128((xx), 8)); \
		(xx) = _mm_max_epu8((xx), _mm_srli_si128((xx), 4)); \
		(xx) = _mm_max_epu8((xx), _mm_srli_si128((xx), 2)); \
		(xx) = _mm_max_epu8((xx), _mm_srli_si128((xx), 1)); \
    	(ret) = _mm_extract_epi16((xx), 0) & 0x00ff; \
	} 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_epi8(_o_del + _e_del);
	e_del = _mm_set1_epi8(_e_del);
	oe_ins = _mm_set1_epi8(_o_ins + _e_ins);
	e_ins = _mm_set1_epi8(_e_ins);
	shift = _mm_set1_epi8(q->shift);
	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, cmp, imax;
		__m128i e, h, t, 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, 1); // h=H(i-1,-1); << instead of >> because x64 is little-endian
		for (j = 0; LIKELY(j < slen); ++j) {
			/* SW cells are computed in the following order:
			 *   H(i,j)   = max{H(i-1,j-1)+S(i,j), E(i,j), F(i,j)}
			 *   E(i+1,j) = max{H(i,j)-q, E(i,j)-r}
			 *   F(i,j+1) = max{H(i,j)-q, F(i,j)-r}
			 */
			// compute H'(i,j); note that at the beginning, h=H'(i-1,j-1)
			h = _mm_adds_epu8(h, _mm_load_si128(S + j));
			h = _mm_subs_epu8(h, shift); // h=H'(i-1,j-1)+S(i,j)
			e = _mm_load_si128(E + j); // e=E'(i,j)
			h = _mm_max_epu8(h, e);
			h = _mm_max_epu8(h, f); // h=H'(i,j)
			max = _mm_max_epu8(max, h); // set max
			_mm_store_si128(H1 + j, h); // save to H'(i,j)
			// now compute E'(i+1,j)
			e = _mm_subs_epu8(e, e_del); // e=E'(i,j) - e_del
			t = _mm_subs_epu8(h, oe_del); // h=H'(i,j) - o_del - e_del
			e = _mm_max_epu8(e, t); // e=E'(i+1,j)
			_mm_store_si128(E + j, e); // save to E'(i+1,j)
			// now compute F'(i,j+1)
			f = _mm_subs_epu8(f, e_ins);
			t = _mm_subs_epu8(h, oe_ins); // h=H'(i,j) - o_ins - e_ins
			f = _mm_max_epu8(f, t);
			// get H'(i-1,j) and prepare for the next j
			h = _mm_load_si128(H0 + j); // h=H'(i-1,j)
		}
		// NB: we do not need to set E(i,j) as we disallow adjecent insertion and then deletion
		for (k = 0; LIKELY(k < 16); ++k) { // this block mimics SWPS3; NB: H(i,j) updated in the lazy-F loop cannot exceed max
			f = _mm_slli_si128(f, 1);
			for (j = 0; LIKELY(j < slen); ++j) {
				h = _mm_load_si128(H1 + j);
				h = _mm_max_epu8(h, f); // h=H'(i,j)
				_mm_store_si128(H1 + j, h);
				h = _mm_subs_epu8(h, oe_ins);
				f = _mm_subs_epu8(f, e_ins);
				cmp = _mm_movemask_epi8(_mm_cmpeq_epi8(_mm_subs_epu8(f, h), zero));
				if (UNLIKELY(cmp == 0xffff)) goto end_loop16;
			}
		}
end_loop16:
		//int k;for (k=0;k<16;++k)printf("%d ", ((uint8_t*)&max)[k]);printf("\n");
		__max_16(imax, max); // imax is the maximum number in max
		if (imax >= minsc) { // write the b array; this condition adds branching unfornately
			if (n_b == 0 || (int32_t)b[n_b-1] + 1 != i) { // then append
				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; // te is the end position on the target
			for (j = 0; LIKELY(j < slen); ++j) // keep the H1 vector
				_mm_store_si128(Hmax + j, _mm_load_si128(H1 + j));
			if (gmax + q->shift >= 255 || gmax >= endsc) break;
		}
		S = H1; H1 = H0; H0 = S; // swap H0 and H1
	}
	r.score = gmax + q->shift < 255? gmax : 255;
	r.te = te;
	if (r.score != 255) { // get a->qe, the end of query match; find the 2nd best score
		int max = -1, tmp, low, high, qlen = slen * 16;
		uint8_t *t = (uint8_t*)Hmax;
		for (i = 0; i < qlen; ++i, ++t)
			if ((int)*t > max) max = *t, r.qe = i / 16 + i % 16 * slen;
			else if ((int)*t == max && (tmp = i / 16 + i % 16 * slen) < r.qe) r.qe = tmp; 
		//printf("%d,%d\n", max, gmax);
		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, 0x58(%rsp)
movq %rsi, %r10
movups 0x6620(%rip), %xmm0     # 0x29608
movups %xmm0, 0xc(%rdi)
movups 0x6609(%rip), %xmm0     # 0x295fc
movq %rdi, 0x20(%rsp)
movups %xmm0, (%rdi)
movd 0x8(%rsi), %xmm0
movq 0x18(%rsi), %r14
movq 0x20(%rsi), %r15
movq 0x28(%rsi), %rbx
movq 0x30(%rsi), %rbp
movslq 0x4(%rsi), %r11
testq %r11, %r11
jle 0x23040
movq %r11, %rax
shlq $0x4, %rax
xorl %ecx, %ecx
pxor %xmm1, %xmm1
movdqa %xmm1, (%rbx,%rcx)
movdqa %xmm1, (%r14,%rcx)
movdqa %xmm1, (%rbp,%rcx)
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x23026
testl %edx, %edx
jle 0x233dc
movl 0xf8(%rsp), %eax
addl %r9d, %r8d
movd %eax, %xmm1
addl 0xf0(%rsp), %eax
movd %r8d, %xmm2
movd %r9d, %xmm3
movd %eax, %xmm4
movl 0x100(%rsp), %eax
punpcklbw %xmm2, %xmm2    # xmm2 = xmm2[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
punpcklbw %xmm3, %xmm3    # xmm3 = xmm3[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
punpcklbw %xmm4, %xmm4    # xmm4 = xmm4[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
punpcklbw %xmm1, %xmm1    # xmm1 = xmm1[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
punpcklbw %xmm0, %xmm0    # xmm0 = xmm0[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,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, %xmm4, %xmm4      # xmm4 = xmm4[0,0,0,0,4,5,6,7]
pshuflw $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0,4,5,6,7]
pshuflw $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0,4,5,6,7]
movzwl %ax, %eax
movl %eax, 0x14(%rsp)
pshufd $0x0, %xmm2, %xmm5      # xmm5 = xmm2[0,0,0,0]
pshufd $0x0, %xmm3, %xmm6      # xmm6 = xmm3[0,0,0,0]
pshufd $0x0, %xmm4, %xmm7      # xmm7 = xmm4[0,0,0,0]
pshufd $0x0, %xmm1, %xmm8      # xmm8 = xmm1[0,0,0,0]
pshufd $0x0, %xmm0, %xmm9      # xmm9 = xmm0[0,0,0,0]
movl %edx, %eax
movq %rax, 0x48(%rsp)
movl %r11d, %r13d
shlq $0x4, %r13
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %r12d, %r12d
movq %r11, %rax
shlq $0x4, %rax
movq %rax, 0x50(%rsp)
xorl %eax, %eax
xorl %r8d, %r8d
movq $0x0, 0x8(%rsp)
xorl %edi, %edi
movq %r10, 0x30(%rsp)
movq %r11, 0x28(%rsp)
movdqa %xmm5, 0xa0(%rsp)
movdqa %xmm6, 0x90(%rsp)
movdqa %xmm7, 0x80(%rsp)
movdqa %xmm8, 0x70(%rsp)
movdqa %xmm9, 0x60(%rsp)
testl %r11d, %r11d
jle 0x233cf
movq 0x58(%rsp), %rcx
movzbl (%rcx,%r12), %ecx
imull %r11d, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq 0x10(%r10), %rcx
movq 0x50(%rsp), %rdx
movdqa -0x10(%r14,%rdx), %xmm3
pslldq $0x1, %xmm3             # xmm3 = zero,xmm3[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
pxor %xmm0, %xmm0
xorl %edx, %edx
pxor %xmm2, %xmm2
paddusb (%rcx,%rdx), %xmm3
psubusb %xmm9, %xmm3
movdqa (%rbx,%rdx), %xmm4
movdqa %xmm4, %xmm1
pmaxub %xmm2, %xmm1
pmaxub %xmm3, %xmm1
pmaxub %xmm1, %xmm0
movdqa %xmm1, (%r15,%rdx)
psubusb %xmm6, %xmm4
movdqa %xmm1, %xmm3
psubusb %xmm5, %xmm3
pmaxub %xmm4, %xmm3
movdqa %xmm3, (%rbx,%rdx)
psubusb %xmm8, %xmm2
psubusb %xmm7, %xmm1
pmaxub %xmm2, %xmm1
movdqa (%r14,%rdx), %xmm3
addq $0x10, %rdx
movdqa %xmm1, %xmm2
cmpq %rdx, %r13
jne 0x23164
xorl %ecx, %ecx
pslldq $0x1, %xmm1             # xmm1 = zero,xmm1[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
testl %r11d, %r11d
jle 0x23202
xorl %edx, %edx
movdqa (%r15,%rdx), %xmm2
pmaxub %xmm1, %xmm2
movdqa %xmm2, (%r15,%rdx)
psubusb %xmm7, %xmm2
psubusb %xmm8, %xmm1
pminub %xmm1, %xmm2
pcmpeqb %xmm1, %xmm2
pmovmskb %xmm2, %esi
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x23209
addq $0x10, %rdx
cmpq %rdx, %r13
jne 0x231cc
incl %ecx
cmpl $0x10, %ecx
jne 0x231c0
movq %r12, (%rsp)
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
pmaxub %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
pmaxub %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
pmaxub %xmm0, %xmm1
movdqa %xmm1, %xmm0
psrldq $0x1, %xmm0             # xmm0 = xmm0[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],zero
pmaxub %xmm1, %xmm0
movd %xmm0, %ecx
movzbl %cl, %r12d
jae 0x23341
cmpl %r12d, 0x14(%rsp)
ja 0x23341
movslq %r8d, %rsi
testl %r8d, %r8d
je 0x23299
movq -0x8(%rdi,%rsi,8), %rcx
leal 0x1(%rcx), %edx
cmpq (%rsp), %rdx
jne 0x23299
shrq $0x20, %rcx
cmpl %r12d, %ecx
jge 0x23341
decq %rsi
movq %r12, %rcx
shlq $0x20, %rcx
orq (%rsp), %rcx
jmp 0x2333d
cmpl %eax, %r8d
jne 0x2332f
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x8, %eax
cmovel %eax, %ecx
movq %rcx, 0x38(%rsp)
leal (,%rcx,8), %eax
movq %rsi, 0x40(%rsp)
movslq %eax, %rsi
leaq 0x634e(%rip), %rdx      # 0x29618
movl $0xc5, %ecx
movl %r8d, 0x1c(%rsp)
leaq 0x634d(%rip), %r8       # 0x29628
movl %r9d, 0x18(%rsp)
callq 0x22d64
movq 0x40(%rsp), %rsi
movdqa 0x60(%rsp), %xmm9
movdqa 0x70(%rsp), %xmm8
movdqa 0x80(%rsp), %xmm7
movdqa 0x90(%rsp), %xmm6
movdqa 0xa0(%rsp), %xmm5
movl 0x18(%rsp), %r9d
movl 0x1c(%rsp), %r8d
movq 0x28(%rsp), %r11
movq 0x30(%rsp), %r10
movq %rax, %rdi
movq 0x38(%rsp), %rax
movq %r12, %rcx
shlq $0x20, %rcx
orq (%rsp), %rcx
incl %r8d
movq %rcx, (%rdi,%rsi,8)
cmpl %r12d, 0x8(%rsp)
jge 0x2339b
testl %r11d, %r11d
jle 0x23364
xorl %ecx, %ecx
movdqa (%r15,%rcx), %xmm0
movdqa %xmm0, (%rbp,%rcx)
addq $0x10, %rcx
cmpq %rcx, %r13
jne 0x2334f
cmpl %r12d, 0x14(%rsp)
seta %cl
btl $0x11, 0x100(%rsp)
setae %dl
orb %cl, %dl
movb $0x1, %cl
cmpb $0x1, %dl
jne 0x233a9
movzbl 0x8(%r10), %edx
addl %r12d, %edx
cmpl $0xfe, %edx
ja 0x233a9
xorl %ecx, %ecx
movq %r14, %rdx
movq %r15, %r14
jmp 0x233ac
xorl %ecx, %ecx
movq %r14, %rdx
movq %r15, %r14
movq (%rsp), %r12
jmp 0x233b8
movq %r15, %rdx
movq %r12, 0x8(%rsp)
movq (%rsp), %r12
movl %r12d, %r9d
testb %cl, %cl
jne 0x233f0
incq %r12
movq %rdx, %r15
cmpq 0x48(%rsp), %r12
jne 0x23127
jmp 0x233f0
pxor %xmm1, %xmm1
pxor %xmm0, %xmm0
jmp 0x231be
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %edi, %edi
movq $0x0, 0x8(%rsp)
xorl %r8d, %r8d
movzbl 0x8(%r10), %eax
movq 0x8(%rsp), %rdx
addl %edx, %eax
movl $0xff, %ecx
cmpl %ecx, %eax
cmovgel %ecx, %edx
movq 0x20(%rsp), %r14
movl %edx, (%r14)
movl %r9d, 0x4(%r14)
cmpl %ecx, %edx
je 0x23509
movq %rdx, %r12
testl %r11d, %r11d
jle 0x23496
movl %r8d, %ebx
movl %r11d, %edx
shll $0x4, %edx
movl 0x8(%r14), %ecx
cmpl $0x2, %edx
movl $0x1, %eax
cmovgel %edx, %eax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
movzbl (%rbp,%rdx), %r8d
cmpl %r8d, %esi
jge 0x2345e
movl %edx, %esi
shrl $0x4, %esi
movl %edx, %ecx
andl $0xf, %ecx
imull %r11d, %ecx
addl %esi, %ecx
jmp 0x23485
jne 0x23482
movl %edx, %r8d
shrl $0x4, %r8d
movl %r9d, %r15d
movl %edx, %r9d
andl $0xf, %r9d
imull %r11d, %r9d
addl %r8d, %r9d
cmpl %ecx, %r9d
cmovll %r9d, %ecx
movl %r15d, %r9d
movl %esi, %r8d
incq %rdx
movl %r8d, %esi
cmpl %edx, %eax
jne 0x23441
movl %ecx, 0x8(%r14)
movl %ebx, %r8d
testq %rdi, %rdi
je 0x23509
movzbl 0xa(%r10), %ecx
leal (%r12,%rcx), %eax
decl %eax
cltd
idivl %ecx
testl %r8d, %r8d
jle 0x23509
movl %r9d, %ecx
subl %eax, %ecx
addl %r9d, %eax
movl 0xc(%r14), %r9d
movl 0x10(%r14), %edx
movl %r8d, %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 0x234c4
movl %r9d, 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