// File: /workspace/github_c_makefile_star2_O3/sc932[P]ALE/src/ssw.c
// Function: sw_sse2_byte
static alignment_end* sw_sse2_byte (const int8_t* ref,
							 int8_t ref_dir,	// 0: forward ref; 1: reverse ref
							 int32_t refLen,
							 int32_t readLen,
							 const uint8_t weight_gapO, /* will be used as - */
							 const uint8_t weight_gapE, /* will be used as - */
							 const __m128i* vProfile,
							 uint8_t terminate,	/* the best alignment score: used to terminate
												   the matrix calculation when locating the
												   alignment beginning point. If this score
												   is set to 0, it will not be used */
	 						 uint8_t bias,  /* Shift 0 point to a positive value. */
							 int32_t maskLen) {

// Put the largest number of the 16 numbers in vm into m.
#define max16(m, vm) (vm) = _mm_max_epu8((vm), _mm_srli_si128((vm), 8)); \
					  (vm) = _mm_max_epu8((vm), _mm_srli_si128((vm), 4)); \
					  (vm) = _mm_max_epu8((vm), _mm_srli_si128((vm), 2)); \
					  (vm) = _mm_max_epu8((vm), _mm_srli_si128((vm), 1)); \
					  (m) = _mm_extract_epi16((vm), 0)

	uint8_t max = 0;		                     /* the max alignment score */
	int32_t end_read = readLen - 1;
	int32_t end_ref = -1; /* 0_based best alignment ending point; Initialized as isn't aligned -1. */
	int32_t segLen = (readLen + 15) / 16; /* number of segment */

	/* array to record the largest score of each reference position */
	uint8_t* maxColumn = (uint8_t*) calloc(refLen, 1);

	/* array to record the alignment read ending position of the largest score of each reference position */
	int32_t* end_read_column = (int32_t*) calloc(refLen, sizeof(int32_t));

	/* Define 16 byte 0 vector. */
	__m128i vZero = _mm_set1_epi32(0);

	__m128i* pvHStore = (__m128i*) calloc(segLen, sizeof(__m128i));
	__m128i* pvHLoad = (__m128i*) calloc(segLen, sizeof(__m128i));
	__m128i* pvE = (__m128i*) calloc(segLen, sizeof(__m128i));
	__m128i* pvHmax = (__m128i*) calloc(segLen, sizeof(__m128i));

	int32_t i, j, k;
	/* 16 byte insertion begin vector */
	__m128i vGapO = _mm_set1_epi8(weight_gapO);

	/* 16 byte insertion extension vector */
	__m128i vGapE = _mm_set1_epi8(weight_gapE);

	/* 16 byte bias vector */
	__m128i vBias = _mm_set1_epi8(bias);

	__m128i vMaxScore = vZero; /* Trace the highest score of the whole SW matrix. */
	__m128i vMaxMark = vZero; /* Trace the highest score till the previous column. */
	__m128i vTemp;
	int32_t edge, begin = 0, end = refLen, step = 1;

	/* outer loop to process the reference sequence */
	if (ref_dir == 1) {
		begin = refLen - 1;
		end = -1;
		step = -1;
	}
	for (i = begin; LIKELY(i != end); i += step) {
		int32_t cmp;
		__m128i e, vF = vZero, vMaxColumn = vZero; /* Initialize F value to 0.
							   Any errors to vH values will be corrected in the Lazy_F loop.
							 */

		__m128i vH = pvHStore[segLen - 1];
		vH = _mm_slli_si128 (vH, 1); /* Shift the 128-bit value in vH left by 1 byte. */
		const __m128i* vP = vProfile + ref[i] * segLen; /* Right part of the vProfile */

		/* Swap the 2 H buffers. */
		__m128i* pv = pvHLoad;
		pvHLoad = pvHStore;
		pvHStore = pv;

		/* inner loop to process the query sequence */
		for (j = 0; LIKELY(j < segLen); ++j) {
			vH = _mm_adds_epu8(vH, _mm_load_si128(vP + j));
			vH = _mm_subs_epu8(vH, vBias); /* vH will be always > 0 */

			/* Get max from vH, vE and vF. */
			e = _mm_load_si128(pvE + j);
			vH = _mm_max_epu8(vH, e);
			vH = _mm_max_epu8(vH, vF);
			vMaxColumn = _mm_max_epu8(vMaxColumn, vH);

			/* Save vH values. */
			_mm_store_si128(pvHStore + j, vH);

			/* Update vE value. */
			vH = _mm_subs_epu8(vH, vGapO); /* saturation arithmetic, result >= 0 */
			e = _mm_subs_epu8(e, vGapE);
			e = _mm_max_epu8(e, vH);
			_mm_store_si128(pvE + j, e);

			/* Update vF value. */
			vF = _mm_subs_epu8(vF, vGapE);
			vF = _mm_max_epu8(vF, vH);

			/* Load the next vH. */
			vH = _mm_load_si128(pvHLoad + j);
		}

        /* Lazy_F loop: has been revised to disallow adjecent insertion and then deletion, so don't update E(i, j), learn from SWPS3 */
		for (k = 0; LIKELY(k < 16); ++k) {
			vF = _mm_slli_si128 (vF, 1);
			for (j = 0; LIKELY(j < segLen); ++j) {
				vH = _mm_load_si128(pvHStore + j);
				vH = _mm_max_epu8(vH, vF);
				vMaxColumn = _mm_max_epu8(vMaxColumn, vH);	// newly added line
				_mm_store_si128(pvHStore + j, vH);
				vH = _mm_subs_epu8(vH, vGapO);
				vF = _mm_subs_epu8(vF, vGapE);
				if (UNLIKELY(! _mm_movemask_epi8(_mm_cmpgt_epi8(vF, vH)))) goto end;
			}
		}

end:		

		vMaxScore = _mm_max_epu8(vMaxScore, vMaxColumn);
		vTemp = _mm_cmpeq_epi8(vMaxMark, vMaxScore);
		cmp = _mm_movemask_epi8(vTemp);
		if (cmp != 0xffff) {
			uint8_t temp;
			vMaxMark = vMaxScore;
			max16(temp, vMaxScore);
			vMaxScore = vMaxMark;

			if (LIKELY(temp > max)) {
				max = temp;
				if (max + bias >= 255) break;	//overflow
				end_ref = i;

				/* Store the column with the highest alignment score in order to trace the alignment ending position on read. */
				for (j = 0; LIKELY(j < segLen); ++j) pvHmax[j] = pvHStore[j];
			}
		}

		/* Record the max score of current column. */
		max16(maxColumn[i], vMaxColumn);
		if (maxColumn[i] == terminate) break;
	}

	/* Trace the alignment ending position on read. */
	uint8_t *t = (uint8_t*)pvHmax;
	int32_t column_len = segLen * 16;
	for (i = 0; LIKELY(i < column_len); ++i, ++t) {
		int32_t temp;
		if (*t == max) {
			temp = i / 16 + i % 16 * segLen;
			if (temp < end_read) end_read = temp;
		}
	}

	free(pvHmax);
	free(pvE);
	free(pvHLoad);
	free(pvHStore);

	/* Find the most possible 2nd best alignment. */
	alignment_end* bests = (alignment_end*) calloc(2, sizeof(alignment_end));
	bests[0].score = max + bias >= 255 ? 255 : max;
	bests[0].ref = end_ref;
	bests[0].read = end_read;

	bests[1].score = 0;
	bests[1].ref = 0;
	bests[1].read = 0;

	edge = (end_ref - maskLen) > 0 ? (end_ref - maskLen) : 0;
	for (i = 0; i < edge; i ++) {
		if (maxColumn[i] > bests[1].score) {
			bests[1].score = maxColumn[i];
			bests[1].ref = i;
		}
	}
	edge = (end_ref + maskLen) > refLen ? refLen : (end_ref + maskLen);
	for (i = edge + 1; i < refLen; i ++) {
		if (maxColumn[i] > bests[1].score) {
			bests[1].score = maxColumn[i];
			bests[1].ref = i;
		}
	}

	free(maxColumn);
	free(end_read_column);
	return bests;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, 0x2c(%rsp)
movl %r8d, 0x28(%rsp)
movl %edx, %r8d
movl %edx, 0x10(%rsp)
movl %esi, 0xc(%rsp)
movq %rdi, 0x30(%rsp)
movq %rcx, 0x18(%rsp)
leal 0xf(%rcx), %eax
pushq $0x10
popq %rcx
cltd
idivl %ecx
movl %eax, %r15d
movslq %r8d, %rbx
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x3310
movq %rax, 0x20(%rsp)
movslq %r15d, %r14
pushq $0x10
popq %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3310
movq %rax, %r12
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3310
movq %rax, %r13
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3310
movq %rax, %rbp
movq %r14, %rdi
pushq $0x10
popq %rsi
callq 0x3310
movl 0x80(%rsp), %r8d
movq %rbx, %r10
movl 0xc(%rsp), %edi
decl %r10d
xorl %ebx, %ebx
cmpb $0x1, %dil
cmovnel %ebx, %r10d
pushq $-0x1
popq %rsi
movl 0x10(%rsp), %edx
cmovel %esi, %edx
movzbl %r8b, %ecx
movl %ecx, 0x14(%rsp)
pushq $0x1
popq %rcx
cmpl %edx, %r10d
je 0x3e94
movd 0x28(%rsp), %xmm0
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, %xmm0      # xmm0 = xmm0[0,0,0,0]
movd 0x2c(%rsp), %xmm1
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, %xmm1      # xmm1 = xmm1[0,0,0,0]
movd %r8d, %xmm2
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, %xmm2      # xmm2 = xmm2[0,0,0,0]
xorl %esi, %esi
cmpb $0x1, %dil
setne %sil
leal -0x1(%r15), %r8d
movslq %r8d, %r8
cmpl $0x2, %r15d
movl %ecx, %r9d
cmovgel %r15d, %r9d
movslq %r10d, %r10
leaq -0x1(,%rsi,2), %r11
shlq $0x4, %r9
pxor %xmm3, %xmm3
pushq $-0x1
popq %rsi
movl %esi, 0xc(%rsp)
xorl %ebx, %ebx
shlq $0x4, %r8
movq %r12, %rsi
pxor %xmm4, %xmm4
movq %r13, %r12
movq %rsi, %r13
pxor %xmm5, %xmm5
pxor %xmm6, %xmm6
cmpl $0x0, 0x18(%rsp)
jle 0x3c17
movq 0x30(%rsp), %rsi
movsbl (%rsi,%r10), %esi
imull %r15d, %esi
movslq %esi, %r14
shlq $0x4, %r14
addq 0x70(%rsp), %r14
movdqa (%r13,%r8), %xmm7
pslldq $0x1, %xmm7             # xmm7 = zero,xmm7[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
xorl %esi, %esi
paddusb (%r14,%rsi), %xmm7
psubusb %xmm2, %xmm7
movdqa (%rbp,%rsi), %xmm8
movdqa %xmm8, %xmm9
pmaxub %xmm6, %xmm9
pmaxub %xmm7, %xmm9
pmaxub %xmm9, %xmm5
movdqa %xmm9, (%r12,%rsi)
psubusb %xmm0, %xmm9
psubusb %xmm1, %xmm8
pmaxub %xmm9, %xmm8
movdqa %xmm8, (%rbp,%rsi)
psubusb %xmm1, %xmm6
pmaxub %xmm9, %xmm6
movdqa (%r13,%rsi), %xmm7
addq $0x10, %rsi
cmpq %rsi, %r9
jne 0x3bbd
xorl %r14d, %r14d
pslldq $0x1, %xmm6             # xmm6 = zero,xmm6[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
cmpl $0x0, 0x18(%rsp)
jle 0x3c60
xorl %esi, %esi
movdqa (%r12,%rsi), %xmm7
pmaxub %xmm6, %xmm7
pmaxub %xmm7, %xmm5
movdqa %xmm7, (%r12,%rsi)
psubusb %xmm0, %xmm7
psubusb %xmm1, %xmm6
movdqa %xmm6, %xmm8
pcmpgtb %xmm7, %xmm8
pmovmskb %xmm8, %edi
testl %edi, %edi
je 0x3c69
addq $0x10, %rsi
cmpq %rsi, %r9
jne 0x3c28
incl %r14d
cmpl $0x10, %r14d
jne 0x3c1a
pmaxub %xmm5, %xmm3
movdqa %xmm4, %xmm6
pcmpeqb %xmm3, %xmm6
pmovmskb %xmm6, %esi
xorl $0xffff, %esi           # imm = 0xFFFF
je 0x3cfe
movdqa %xmm3, %xmm4
psrldq $0x8, %xmm4             # xmm4 = xmm4[8,9,10,11,12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero
pmaxub %xmm3, %xmm4
movdqa %xmm4, %xmm6
psrldq $0x4, %xmm6             # xmm6 = xmm6[4,5,6,7,8,9,10,11,12,13,14,15],zero,zero,zero,zero
pmaxub %xmm4, %xmm6
movdqa %xmm6, %xmm4
psrldq $0x2, %xmm4             # xmm4 = xmm4[2,3,4,5,6,7,8,9,10,11,12,13,14,15],zero,zero
pmaxub %xmm6, %xmm4
movdqa %xmm4, %xmm6
psrldq $0x1, %xmm6             # xmm6 = xmm6[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],zero
pmaxub %xmm4, %xmm6
movd %xmm6, %esi
cmpb %bl, %sil
jbe 0x3d57
movzbl %sil, %edi
addl 0x14(%rsp), %edi
cmpl $0xfe, %edi
ja 0x3d5d
cmpl $0x0, 0x18(%rsp)
jle 0x3cf3
xorl %ebx, %ebx
movdqa (%r12,%rbx), %xmm4
movdqa %xmm4, (%rax,%rbx)
addq $0x10, %rbx
cmpq %rbx, %r9
jne 0x3cdf
movdqa %xmm3, %xmm4
movl %r10d, 0xc(%rsp)
movl %esi, %ebx
movdqa %xmm5, %xmm6
psrldq $0x8, %xmm6             # xmm6 = xmm6[8,9,10,11,12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero
pmaxub %xmm5, %xmm6
movdqa %xmm6, %xmm5
psrldq $0x4, %xmm5             # xmm5 = xmm5[4,5,6,7,8,9,10,11,12,13,14,15],zero,zero,zero,zero
pmaxub %xmm6, %xmm5
movdqa %xmm5, %xmm6
psrldq $0x2, %xmm6             # xmm6 = xmm6[2,3,4,5,6,7,8,9,10,11,12,13,14,15],zero,zero
pmaxub %xmm5, %xmm6
movdqa %xmm6, %xmm5
psrldq $0x1, %xmm5             # xmm5 = xmm5[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],zero
pmaxub %xmm6, %xmm5
movd %xmm5, %esi
movq 0x20(%rsp), %rdi
movb %sil, (%rdi,%r10)
cmpb 0x78(%rsp), %sil
je 0x3d5f
addq %r11, %r10
movq %r12, %rsi
cmpl %r10d, %edx
jne 0x3b7c
jmp 0x3d5f
movdqa %xmm3, %xmm4
jmp 0x3cfe
movl %esi, %ebx
movq 0x18(%rsp), %rdx
leal -0x1(%rdx), %r14d
testl %edx, %edx
jle 0x3d9d
movl %r15d, %edx
shll $0x4, %edx
cmpl $0x2, %edx
cmovgel %edx, %ecx
xorl %edx, %edx
cmpb %bl, (%rax,%rdx)
jne 0x3d96
movl %edx, %esi
shrl $0x4, %esi
movl %edx, %edi
andl $0xf, %edi
imull %r15d, %edi
addl %esi, %edi
cmpl %r14d, %edi
cmovll %edi, %r14d
incq %rdx
cmpl %edx, %ecx
jne 0x3d7a
movq %rax, %rdi
callq 0x3070
movq %rbp, %rdi
callq 0x3070
movq %r13, %rdi
callq 0x3070
movq %r12, %rdi
callq 0x3070
pushq $0x2
popq %rdi
pushq $0xc
popq %rsi
callq 0x3310
movq %rax, %r15
movzbl %bl, %eax
movl 0x14(%rsp), %edx
addl %eax, %edx
movl $0xff, %ecx
cmpl %ecx, %edx
cmovbl %eax, %ecx
movw %cx, (%r15)
movl 0xc(%rsp), %r9d
movl %r9d, 0x4(%r15)
movl %r14d, 0x8(%r15)
andw $0x0, 0xc(%r15)
andq $0x0, 0x10(%r15)
movl %r9d, %eax
movl 0x88(%rsp), %r10d
subl %r10d, %eax
jle 0x3e38
movl %eax, %ecx
xorl %edx, %edx
xorl %eax, %eax
movl 0x10(%rsp), %r8d
movq 0x20(%rsp), %rdi
movzbl (%rdi,%rdx), %esi
cmpw %si, %ax
jae 0x3e29
movl %edx, 0x10(%r15)
movl %esi, %eax
incq %rdx
cmpq %rdx, %rcx
jne 0x3e1a
movw %ax, 0xc(%r15)
jmp 0x3e44
xorl %eax, %eax
movl 0x10(%rsp), %r8d
movq 0x20(%rsp), %rdi
addl %r10d, %r9d
cmpl %r8d, %r9d
cmovgel %r8d, %r9d
incl %r9d
cmpl %r8d, %r9d
jge 0x3e7d
movslq %r9d, %rdx
movq %rdi, %rcx
addq %rdx, %rcx
movzbl (%rcx), %edx
cmpw %dx, %ax
jae 0x3e6d
movl %r9d, 0x10(%r15)
movl %edx, %eax
incq %rcx
incl %r9d
cmpl %r9d, %r8d
jne 0x3e5f
movw %ax, 0xc(%r15)
callq 0x3070
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %esi, 0xc(%rsp)
jmp 0x3d5f