// File: /workspace/github_cpp_makefile_star2_O2/readmapping[P]alfalfa/src/ksw.c
// Function: ksw_u8
kswr_t ksw_u8(kswq_t *q, int tlen, const uint8_t *target, int _gapo, int _gape, 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, gapoe, gape, 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);
	gapoe = _mm_set1_epi8(_gapo + _gape);
	gape = _mm_set1_epi8(_gape);
	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, 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)
			h = _mm_subs_epu8(h, gapoe); // h=H'(i,j)-gapo
			e = _mm_subs_epu8(e, gape); // e=E'(i,j)-gape
			e = _mm_max_epu8(e, h); // 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, gape);
			f = _mm_max_epu8(f, h);
			// 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, gapoe);
				f = _mm_subs_epu8(f, gape);
				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, 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;
		//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 $0xa8, %rsp
movq %rcx, 0x60(%rsp)
movq %rsi, %r11
movups 0x9714(%rip), %xmm0     # 0x39a14
movups %xmm0, 0xc(%rdi)
movups 0x96fd(%rip), %xmm0     # 0x39a08
movq %rdi, 0x38(%rsp)
movups %xmm0, (%rdi)
addl %r9d, %r8d
movd %r8d, %xmm2
movd %r9d, %xmm1
movd 0x8(%rsi), %xmm0
movq 0x18(%rsi), %r13
movq 0x20(%rsi), %rax
movq 0x28(%rsi), %r14
movq 0x30(%rsi), %rbp
movslq 0x4(%rsi), %rcx
xorl %esi, %esi
testl %ecx, %ecx
movl $0x0, %ebx
cmovgl %ecx, %ebx
shlq $0x4, %rbx
pxor %xmm3, %xmm3
cmpq %rsi, %rbx
je 0x3036b
movdqa %xmm3, (%r14,%rsi)
movdqa %xmm3, (%r13,%rsi)
movdqa %xmm3, (%rbp,%rsi)
addq $0x10, %rsi
jmp 0x3034d
movl 0xe0(%rsp), %esi
movzwl %si, %esi
movl %esi, 0x14(%rsp)
punpcklbw %xmm2, %xmm2    # xmm2 = xmm2[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]
pshufd $0x0, %xmm2, %xmm5      # xmm5 = xmm2[0,0,0,0]
punpcklbw %xmm1, %xmm1    # xmm1 = xmm1[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
pshuflw $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm1, %xmm6      # xmm6 = xmm1[0,0,0,0]
punpcklbw %xmm0, %xmm0    # xmm0 = xmm0[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
pshuflw $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm0, %xmm7      # xmm7 = xmm0[0,0,0,0]
xorl %r10d, %r10d
testl %edx, %edx
cmovlel %r10d, %edx
movq %rdx, 0x68(%rsp)
leaq 0x10(%rbx), %r15
pushq $-0x1
popq %rdx
movl %edx, 0x10(%rsp)
movq %rcx, %rdx
shlq $0x4, %rdx
movq %rdx, 0x58(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r8d, %r8d
xorl %r9d, %r9d
movq $0x0, 0x28(%rsp)
movq %rcx, 0x30(%rsp)
movq %r11, 0x40(%rsp)
movdqa %xmm5, 0x90(%rsp)
movdqa %xmm6, 0x80(%rsp)
movdqa %xmm7, 0x70(%rsp)
cmpq 0x68(%rsp), %r10
je 0x3068f
movq %rax, %r12
movq 0x60(%rsp), %rax
movzbl (%rax,%r10), %eax
imull %ecx, %eax
cltq
shlq $0x4, %rax
addq 0x10(%r11), %rax
movq 0x58(%rsp), %rdx
movdqa -0x10(%r13,%rdx), %xmm2
pslldq $0x1, %xmm2             # xmm2 = zero,xmm2[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
pxor %xmm1, %xmm1
xorl %edx, %edx
pxor %xmm0, %xmm0
cmpq %rdx, %rbx
je 0x30493
paddusb (%rax,%rdx), %xmm2
psubusb %xmm7, %xmm2
movdqa (%r14,%rdx), %xmm3
movdqa %xmm3, %xmm4
pmaxub %xmm1, %xmm4
pmaxub %xmm2, %xmm4
pmaxub %xmm4, %xmm0
movdqa %xmm4, (%r12,%rdx)
psubusb %xmm5, %xmm4
psubusb %xmm6, %xmm3
pmaxub %xmm4, %xmm3
movdqa %xmm3, (%r14,%rdx)
psubusb %xmm6, %xmm1
pmaxub %xmm4, %xmm1
movdqa (%r13,%rdx), %xmm2
addq $0x10, %rdx
jmp 0x30442
movq %r10, 0x18(%rsp)
movq %r8, 0x20(%rsp)
xorl %eax, %eax
cmpl $0x10, %eax
je 0x304e9
pslldq $0x1, %xmm1             # xmm1 = zero,xmm1[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
xorl %edx, %edx
leaq 0x10(%rdx), %r10
cmpq %r10, %r15
je 0x304e5
movdqa (%r12,%rdx), %xmm2
pmaxub %xmm1, %xmm2
movdqa %xmm2, (%r12,%rdx)
psubusb %xmm5, %xmm2
psubusb %xmm6, %xmm1
pminub %xmm1, %xmm2
pcmpeqb %xmm1, %xmm2
pmovmskb %xmm2, %esi
movq %r10, %rdx
cmpl $0xffff, %esi           # imm = 0xFFFF
jne 0x304ab
jmp 0x304e9
incl %eax
jmp 0x3049f
btl $0x12, 0xe0(%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, %eax
movzbl %al, %edx
jae 0x30602
cmpl %edx, 0x14(%rsp)
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %r10
ja 0x3062e
movslq %r9d, %rdi
testl %r9d, %r9d
je 0x3058e
movq 0x8(%rsp), %rax
movq -0x8(%rax,%rdi,8), %rax
movq %rdx, %rsi
leal 0x1(%rax), %edx
cmpq %r10, %rdx
movq %rsi, %rdx
jne 0x3058e
shrq $0x20, %rax
cmpl %edx, %eax
jge 0x3062e
decq %rdi
movq %rdx, %rax
shlq $0x20, %rax
orq %r10, %rax
movq 0x8(%rsp), %rsi
movq %rax, (%rsi,%rdi,8)
jmp 0x3062e
movq %rdi, 0x50(%rsp)
cmpl 0x28(%rsp), %r9d
jne 0x3060e
movq %rdx, 0x48(%rsp)
leal (%r9,%r9), %edx
testl %r9d, %r9d
pushq $0x8
popq %rax
cmovel %eax, %edx
movq %rdx, 0x28(%rsp)
leal (,%rdx,8), %eax
movslq %eax, %rsi
movq 0x8(%rsp), %rdi
movq %r9, 0x8(%rsp)
callq 0x7590
movq 0x48(%rsp), %rdx
movdqa 0x70(%rsp), %xmm7
movdqa 0x80(%rsp), %xmm6
movq 0x18(%rsp), %r10
movdqa 0x90(%rsp), %xmm5
movq 0x20(%rsp), %r8
movq 0x30(%rsp), %rcx
movq 0x8(%rsp), %r9
movq 0x40(%rsp), %r11
jmp 0x30613
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %r10
jmp 0x3062e
movq 0x8(%rsp), %rax
movq %rdx, %rdi
shlq $0x20, %rdi
orq %r10, %rdi
incl %r9d
movq %rax, 0x8(%rsp)
movq 0x50(%rsp), %rsi
movq %rdi, (%rax,%rsi,8)
cmpl %edx, %r8d
jae 0x30681
xorl %eax, %eax
cmpq %rax, %rbx
je 0x3064c
movdqa (%r12,%rax), %xmm0
movdqa %xmm0, (%rbp,%rax)
addq $0x10, %rax
jmp 0x30635
cmpl %edx, 0x14(%rsp)
seta %r8b
btl $0x11, 0xe0(%rsp)
movq %rdx, %rsi
setae %dl
movzbl 0x8(%r11), %eax
addl %esi, %eax
orb %r8b, %dl
cmpb $0x1, %dl
jne 0x3069e
movl %esi, %r8d
movl %r10d, 0x10(%rsp)
cmpl $0xfe, %eax
ja 0x3069e
incq %r10
movq %r13, %rax
movq %r12, %r13
jmp 0x30402
movzbl 0x8(%r11), %eax
addl %r8d, %eax
movl 0x10(%rsp), %r15d
jmp 0x306a4
movl %esi, %r8d
movl %r10d, %r15d
movl $0xff, %edx
cmpl %edx, %eax
cmovgel %edx, %r8d
movq 0x38(%rsp), %r14
movl %r8d, (%r14)
movl %r15d, 0x4(%r14)
cmpl %edx, %r8d
movq 0x8(%rsp), %rdi
je 0x30794
movq %r8, %r12
movl %ecx, %eax
shll $0x4, %eax
movl 0x8(%r14), %ecx
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
pushq $-0x1
popq %rsi
cmpl %edx, %eax
je 0x30716
movzbl (%rbp,%rdx), %r8d
movq %r9, %rbx
movl %edx, %r9d
shrl $0x4, %r9d
movl %edx, %r10d
andl $0xf, %r10d
imull 0x30(%rsp), %r10d
addl %r9d, %r10d
movq %rbx, %r9
cmpl %r8d, %esi
cmovll %r10d, %ecx
cmovlel %r8d, %esi
incq %rdx
jmp 0x306df
movl %ecx, 0x8(%r14)
testq %rdi, %rdi
je 0x30794
movzbl 0xa(%r11), %ecx
leal (%r12,%rcx), %eax
decl %eax
cltd
idivl %ecx
movl %r15d, %ecx
subl %eax, %ecx
addl %r15d, %eax
movl 0xc(%r14), %r8d
movl 0x10(%r14), %edx
xorl %esi, %esi
testl %r9d, %r9d
cmovlel %esi, %r9d
cmpq %rsi, %r9
je 0x3078c
movq %r9, %r15
movq (%rdi,%rsi,8), %r9
cmpl %r9d, %ecx
setg %r10b
cmpl %r9d, %eax
setl %r11b
orb %r10b, %r11b
movq %r9, %r10
shrq $0x20, %r10
cmpl %r10d, %r8d
cmovgl %r8d, %r10d
movl %edx, %ebx
cmovll %r9d, %ebx
movq %r15, %r9
testb %r11b, %r11b
cmovnel %ebx, %edx
cmovel %r8d, %r10d
incq %rsi
movl %r10d, %r8d
jmp 0x30746
movl %r8d, 0xc(%r14)
movl %edx, 0x10(%r14)
callq 0x7360
movq %r14, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq