// File: /workspace/github_c_makefile_star2_O2/nh13[P]ksw/src/parasail/contrib/ssw/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;
	/* 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;
	uint8_t *t;
	int32_t column_len;
    alignment_end* bests;

	/* 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) {
		const __m128i* vP;
		__m128i* pv;
		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. */
		vP = vProfile + ref[i] * segLen; /* Right part of the vProfile */

		/* Swap the 2 H buffers. */
		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 */
        /* reset pointers to the start of the saved data */
        j = 0;
        vH = _mm_load_si128 (pvHStore + j);

        /*  the computed vF value is for the given column.  since */
        /*  we are at the end, we need to shift the vF value over */
        /*  to the next column. */
        vF = _mm_slli_si128 (vF, 1);
        vTemp = _mm_subs_epu8 (vH, vGapO);
		vTemp = _mm_subs_epu8 (vF, vTemp);
		vTemp = _mm_cmpeq_epi8 (vTemp, vZero);
		cmp  = _mm_movemask_epi8 (vTemp);

        while (cmp != 0xffff)
        {
            vH = _mm_max_epu8 (vH, vF);
			vMaxColumn = _mm_max_epu8(vMaxColumn, vH);
            _mm_store_si128 (pvHStore + j, vH);
            vF = _mm_subs_epu8 (vF, vGapE);
            j++;
            if (j >= segLen)
            {
                j = 0;
                vF = _mm_slli_si128 (vF, 1);
            }
            vH = _mm_load_si128 (pvHStore + j);

            vTemp = _mm_subs_epu8 (vH, vGapO);
            vTemp = _mm_subs_epu8 (vF, vTemp);
            vTemp = _mm_cmpeq_epi8 (vTemp, vZero);
            cmp  = _mm_movemask_epi8 (vTemp);
        }

		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. */
	t = (uint8_t*)pvHmax;
	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. */
	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, 0x10(%rsp)
movl %r8d, 0x18(%rsp)
movl %edx, %r8d
movl %esi, 0x8(%rsp)
movq %rdi, 0x30(%rsp)
movzbl 0x80(%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, 0x1c(%rsp)
movslq %r8d, %rbx
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x29b0e0
movq %rax, 0x20(%rsp)
movslq %r15d, %rbp
pushq $0x10
popq %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x29b0e0
movq %rax, %r12
movq %rbp, %rdi
movq %r13, %rsi
callq 0x29b0e0
movq %rax, 0x28(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x29b0e0
movq %rax, %r13
movq %rbp, %rdi
pushq $0x10
popq %rsi
callq 0x29b0e0
movd 0x18(%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), %edx
leal -0x1(%rbp), %esi
xorl %ecx, %ecx
testl %ebp, %ebp
cmovlel %ecx, %ebp
xorl %edi, %edi
cmpb $0x1, 0x8(%rsp)
setne %dil
movslq %edx, %rdx
movslq %esi, %rsi
cmovneq %rcx, %rdx
leaq -0x1(%rdi,%rdi), %rdi
pushq $-0x1
popq %r8
movq %rbx, 0x10(%rsp)
cmovneq %rbx, %r8
shlq $0x4, %rbp
pushq $-0x1
popq %r9
movl %r9d, 0x8(%rsp)
pxor %xmm3, %xmm3
shlq $0x4, %rsi
pxor %xmm4, %xmm4
xorl %r10d, %r10d
movq %r12, %r9
movq 0x28(%rsp), %r12
cmpq %r8, %rdx
je 0x29d120
movdqa (%r9,%rsi), %xmm7
pslldq $0x1, %xmm7             # xmm7 = zero,xmm7[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
movq 0x30(%rsp), %r11
movsbl (%r11,%rdx), %r11d
imull %r15d, %r11d
movslq %r11d, %r11
shlq $0x4, %r11
addq 0x70(%rsp), %r11
pxor %xmm6, %xmm6
xorl %ebx, %ebx
pxor %xmm5, %xmm5
cmpq %rbx, %rbp
je 0x29cea4
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 0x29ce49
pslldq $0x1, %xmm6             # xmm6 = zero,xmm6[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
xorl %r11d, %r11d
movq %r12, %rbx
movdqa (%rbx), %xmm7
movdqa %xmm7, %xmm8
psubusb %xmm0, %xmm8
pminub %xmm6, %xmm8
pcmpeqb %xmm6, %xmm8
pmovmskb %xmm8, %ebx
cmpl $0xffff, %ebx           # imm = 0xFFFF
je 0x29cf0d
pmaxub %xmm6, %xmm7
movslq %r11d, %r11
movq %r11, %rbx
shlq $0x4, %rbx
movdqa %xmm7, (%r12,%rbx)
psubusb %xmm1, %xmm6
incl %r11d
cmpl %r15d, %r11d
cmovgel %ecx, %r11d
jl 0x29cefd
pslldq $0x1, %xmm6             # xmm6 = zero,xmm6[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
pmaxub %xmm7, %xmm5
movslq %r11d, %rbx
shlq $0x4, %rbx
addq %r12, %rbx
jmp 0x29ceaf
pmaxub %xmm5, %xmm3
movdqa %xmm4, %xmm6
pcmpeqb %xmm3, %xmm6
pmovmskb %xmm6, %r11d
xorl $0xffff, %r11d          # imm = 0xFFFF
je 0x29cfa2
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 0x29cf9e
movzbl %bl, %r10d
addl 0xc(%rsp), %r10d
cmpl $0xfe, %r10d
ja 0x29cffd
xorl %r10d, %r10d
cmpq %r10, %rbp
je 0x29cf94
movdqa (%r12,%r10), %xmm4
movdqa %xmm4, (%rax,%r10)
addq $0x10, %r10
jmp 0x29cf7d
movdqa %xmm3, %xmm4
movl %edx, 0x8(%rsp)
jmp 0x29cfa5
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, %r11d
movq 0x20(%rsp), %r10
movb %r11b, (%r10,%rdx)
addq %rdi, %rdx
movq %r9, 0x28(%rsp)
movl %ebx, %r10d
cmpb 0x78(%rsp), %r11b
jne 0x29ce09
movl %r15d, %ecx
shll $0x4, %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
cmpl %edx, %ecx
je 0x29d02f
cmpb %bl, (%rax,%rdx)
jne 0x29d02a
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 0x29d00a
movq %rax, %rdi
movq %r9, %r15
callq 0x29b170
movq %r13, %rdi
callq 0x29b170
movq %r15, %rdi
callq 0x29b170
movq %r12, %rdi
callq 0x29b170
pushq $0x2
popq %rdi
pushq $0xc
popq %rsi
callq 0x29b0e0
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 0x88(%rsp), %r9d
subl %r9d, %ecx
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
xorl %edx, %edx
movq 0x10(%rsp), %r8
movq 0x20(%rsp), %rdi
cmpq %rdx, %rcx
je 0x29d0ce
movzbl (%rdi,%rdx), %esi
cmpw %si, %ax
jae 0x29d0c9
movw %si, 0xc(%r15)
movl %edx, 0x10(%r15)
movl %esi, %eax
incq %rdx
jmp 0x29d0b0
addl %r9d, %r10d
movl 0x1c(%rsp), %ecx
cmpl %r10d, %ecx
cmovll %ecx, %r10d
movslq %r10d, %rcx
leaq 0x1(%rcx), %rdx
cmpq %r8, %rdx
jge 0x29d109
movzbl (%rdi,%rdx), %esi
incq %rdx
incl %ecx
cmpw %si, %ax
jae 0x29d0e3
movw %si, 0xc(%r15)
movl %ecx, 0x10(%r15)
decq %rdx
movl %esi, %eax
movq %rdx, %rcx
jmp 0x29d0df
callq 0x29b170
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rcx
movq %r9, %r12
movq %rcx, %r9
movl %r10d, %ebx
jmp 0x29cffd