// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movl %r9d, 0x10(%rsp)
movl %r8d, 0x8(%rsp)
movl %edx, %r8d
movl %esi, 0x18(%rsp)
movq %rdi, 0x40(%rsp)
movzbl 0x90(%rsp), %eax
movl %eax, 0xc(%rsp)
leal -0x1(%rcx), %r14d
leal 0xf(%rcx), %eax
pushq $0x10
popq %rcx
cltd
idivl %ecx
movl %eax, %r15d
movl %r8d, 0x24(%rsp)
movslq %r8d, %rbx
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x3310
movq %rax, 0x28(%rsp)
movslq %r15d, %rbp
pushq $0x10
popq %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3310
movq %rax, %r12
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3310
movq %rax, 0x30(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3310
movq %rax, %r13
movq %rbp, %rdi
pushq $0x10
popq %rsi
callq 0x3310
movd 0x8(%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 0x10(%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 0xc(%rsp), %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]
leal -0x1(%rbx), %ecx
leal -0x1(%rbp), %edx
xorl %r10d, %r10d
testl %ebp, %ebp
cmovlel %r10d, %ebp
xorl %esi, %esi
cmpb $0x1, 0x18(%rsp)
setne %sil
movslq %ecx, %rcx
movslq %edx, %r9
cmovneq %r10, %rcx
leaq -0x1(%rsi,%rsi), %rdx
movq 0x30(%rsp), %rsi
movq %rdx, 0x10(%rsp)
pushq $-0x1
popq %rdi
movq %rbx, 0x38(%rsp)
cmovneq %rbx, %rdi
shlq $0x4, %rbp
leaq 0x10(%rbp), %r8
pushq $-0x1
popq %rdx
movl %edx, 0x8(%rsp)
pxor %xmm4, %xmm4
shlq $0x4, %r9
movq %r9, 0x18(%rsp)
pxor %xmm3, %xmm3
movq %r12, %r9
movq %rsi, %r12
cmpq %rdi, %rcx
je 0xc1bf
movq 0x18(%rsp), %rdx
movdqa (%r9,%rdx), %xmm7
pslldq $0x1, %xmm7             # xmm7 = zero,xmm7[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
movq 0x40(%rsp), %rdx
movsbl (%rdx,%rcx), %esi
imull %r15d, %esi
movslq %esi, %r11
shlq $0x4, %r11
addq 0x80(%rsp), %r11
pxor %xmm6, %xmm6
xorl %ebx, %ebx
pxor %xmm5, %xmm5
cmpq %rbx, %rbp
je 0xbf59
paddusb (%r11,%rbx), %xmm7
psubusb %xmm2, %xmm7
movdqa (%r13,%rbx), %xmm8
movdqa %xmm8, %xmm9
pmaxub %xmm6, %xmm9
pmaxub %xmm7, %xmm9
pmaxub %xmm9, %xmm5
movdqa %xmm9, (%r12,%rbx)
psubusb %xmm0, %xmm9
psubusb %xmm1, %xmm8
pmaxub %xmm9, %xmm8
movdqa %xmm8, (%r13,%rbx)
psubusb %xmm1, %xmm6
pmaxub %xmm9, %xmm6
movdqa (%r9,%rbx), %xmm7
addq $0x10, %rbx
jmp 0xbefe
xorl %r11d, %r11d
cmpl $0x10, %r11d
je 0xbfab
pslldq $0x1, %xmm6             # xmm6 = zero,xmm6[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
xorl %ebx, %ebx
leaq 0x10(%rbx), %rsi
cmpq %rsi, %r8
je 0xbfa6
movdqa (%r12,%rbx), %xmm7
pmaxub %xmm6, %xmm7
pmaxub %xmm7, %xmm5
movdqa %xmm7, (%r12,%rbx)
psubusb %xmm0, %xmm7
psubusb %xmm1, %xmm6
movdqa %xmm6, %xmm8
pcmpgtb %xmm7, %xmm8
pmovmskb %xmm8, %edx
movq %rsi, %rbx
testl %edx, %edx
jne 0xbf69
jmp 0xbfab
incl %r11d
jmp 0xbf5c
pmaxub %xmm5, %xmm3
movdqa %xmm4, %xmm6
pcmpeqb %xmm3, %xmm6
pmovmskb %xmm6, %edx
xorl $0xffff, %edx           # imm = 0xFFFF
je 0xc039
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, %ebx
cmpb %r10b, %bl
jbe 0xc035
movzbl %bl, %edx
addl 0xc(%rsp), %edx
cmpl $0xfe, %edx
ja 0xc099
xorl %esi, %esi
cmpq %rsi, %rbp
je 0xc02b
movdqa (%r12,%rsi), %xmm4
movdqa %xmm4, (%rax,%rsi)
addq $0x10, %rsi
jmp 0xc015
movdqa %xmm3, %xmm4
movl %ecx, 0x8(%rsp)
jmp 0xc03c
movdqa %xmm3, %xmm4
movl %r10d, %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, %edx
movq 0x28(%rsp), %rsi
movb %dl, (%rsi,%rcx)
addq 0x10(%rsp), %rcx
movq %r9, %rsi
movl %ebx, %r10d
movq %r9, %r11
cmpb 0x88(%rsp), %dl
jne 0xbeb9
jmp 0xc09c
movq %r9, %r11
movl %r15d, %ecx
shll $0x4, %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
cmpl %edx, %ecx
je 0xc0ce
cmpb %bl, (%rax,%rdx)
jne 0xc0c9
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
jmp 0xc0a9
movq %rax, %rdi
movq %r11, %r15
callq 0x3070
movq %r13, %rdi
callq 0x3070
movq %r15, %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 0xc(%rsp), %edx
addl %eax, %edx
movl $0xff, %ecx
cmpl %ecx, %edx
cmovbl %eax, %ecx
movw %cx, (%r15)
movl 0x8(%rsp), %r10d
movl %r10d, 0x4(%r15)
movl %r14d, 0x8(%r15)
andw $0x0, 0xc(%r15)
andq $0x0, 0x10(%r15)
movl %r10d, %ecx
movl 0x98(%rsp), %r9d
subl %r9d, %ecx
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
xorl %edx, %edx
movq 0x38(%rsp), %r8
movq 0x28(%rsp), %rdi
cmpq %rdx, %rcx
je 0xc16d
movzbl (%rdi,%rdx), %esi
cmpw %si, %ax
jae 0xc168
movw %si, 0xc(%r15)
movl %edx, 0x10(%r15)
movl %esi, %eax
incq %rdx
jmp 0xc14f
addl %r9d, %r10d
movl 0x24(%rsp), %ecx
cmpl %r10d, %ecx
cmovll %ecx, %r10d
movslq %r10d, %rcx
leaq 0x1(%rcx), %rdx
cmpq %r8, %rdx
jge 0xc1a8
movzbl (%rdi,%rdx), %esi
incq %rdx
incl %ecx
cmpw %si, %ax
jae 0xc182
movw %si, 0xc(%r15)
movl %ecx, 0x10(%r15)
decq %rdx
movl %esi, %eax
movq %rdx, %rcx
jmp 0xc17e
callq 0x3070
movq %r15, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %r11
movq %r9, %r12
movl %r10d, %ebx
jmp 0xc09c