// File: /workspace/github_c_makefile_star2_O1/sc932[P]ALE/src/ssw.c
// Function: sw_sse2_word
static alignment_end* sw_sse2_word (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,
							 uint16_t terminate,
							 int32_t maskLen) {

#define max8(m, vm) (vm) = _mm_max_epi16((vm), _mm_srli_si128((vm), 8)); \
					(vm) = _mm_max_epi16((vm), _mm_srli_si128((vm), 4)); \
					(vm) = _mm_max_epi16((vm), _mm_srli_si128((vm), 2)); \
					(m) = _mm_extract_epi16((vm), 0)

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

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

	/* 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_epi16(weight_gapO);

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

	__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; /* 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, 2); /* Shift the 128-bit value in vH left by 2 byte. */

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

		__m128i vMaxColumn = vZero; /* vMaxColumn is used to record the max values of column i. */

		const __m128i* vP = vProfile + ref[i] * segLen; /* Right part of the vProfile */
		pvHLoad = pvHStore;
		pvHStore = pv;

		/* inner loop to process the query sequence */
		for (j = 0; LIKELY(j < segLen); j ++) {
			vH = _mm_adds_epi16(vH, _mm_load_si128(vP + j));

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

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

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

			/* Update vF value. */
			vF = _mm_subs_epu16(vF, vGapE);
			vF = _mm_max_epi16(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 < 8); ++k) {
			vF = _mm_slli_si128 (vF, 2);
			for (j = 0; LIKELY(j < segLen); ++j) {
				vH = _mm_load_si128(pvHStore + j);
				vH = _mm_max_epi16(vH, vF);
				vMaxColumn = _mm_max_epi16(vMaxColumn, vH); //newly added line
				_mm_store_si128(pvHStore + j, vH);
				vH = _mm_subs_epu16(vH, vGapO);
				vF = _mm_subs_epu16(vF, vGapE);
				if (UNLIKELY(! _mm_movemask_epi8(_mm_cmpgt_epi16(vF, vH)))) goto end;
			}
		}

end:
		vMaxScore = _mm_max_epi16(vMaxScore, vMaxColumn);
		vTemp = _mm_cmpeq_epi16(vMaxMark, vMaxScore);
		cmp = _mm_movemask_epi8(vTemp);
		if (cmp != 0xffff) {
			uint16_t temp;
			vMaxMark = vMaxScore;
			max8(temp, vMaxScore);
			vMaxScore = vMaxMark;

			if (LIKELY(temp > max)) {
				max = temp;
				end_ref = i;
				for (j = 0; LIKELY(j < segLen); ++j) pvHmax[j] = pvHStore[j];
			}
		}

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

	/* Trace the alignment ending position on read. */
	uint16_t *t = (uint16_t*)pvHmax;
	int32_t column_len = segLen * 8;
	for (i = 0; LIKELY(i < column_len); ++i, ++t) {
		int32_t temp;
		if (*t == max) {
			temp = i / 8 + i % 8 * 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;
	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; 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, 0x1c(%rsp)
movl %r8d, 0x18(%rsp)
movl %edx, %r8d
movl %edx, 0xc(%rsp)
movl %esi, 0x8(%rsp)
movq %rdi, 0x30(%rsp)
movq %rcx, 0x10(%rsp)
leal 0x7(%rcx), %eax
pushq $0x8
popq %rcx
cltd
idivl %ecx
movl %eax, %r15d
movslq %r8d, %r14
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0x3310
movq %rax, 0x20(%rsp)
movslq %r15d, %rbx
pushq $0x10
popq %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3310
movq %rax, %r12
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3310
movq %rax, %r13
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3310
movq %rax, %rbp
movq %rbx, %rdi
pushq $0x10
popq %rsi
callq 0x3310
movl 0x8(%rsp), %esi
movq %r14, 0x28(%rsp)
leal -0x1(%r14), %edi
xorl %ecx, %ecx
cmpb $0x1, %sil
cmovnel %ecx, %edi
pushq $-0x1
popq %rdx
cmovnel 0xc(%rsp), %edx
pushq $0x1
popq %rcx
cmpl %edx, %edi
je 0x42e2
movd 0x18(%rsp), %xmm0
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 0x1c(%rsp), %xmm1
pshuflw $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
xorl %r11d, %r11d
cmpb $0x1, %sil
setne %r11b
leal -0x1(%r15), %r8d
movslq %r8d, %r8
cmpl $0x2, %r15d
movl %ecx, %r9d
cmovgel %r15d, %r9d
movslq %edi, %r10
leaq -0x1(,%r11,2), %r11
shlq $0x4, %r9
pxor %xmm2, %xmm2
xorl %r14d, %r14d
shlq $0x4, %r8
movl $0x0, 0x8(%rsp)
movq %r12, %rdi
pxor %xmm3, %xmm3
movq %r13, %r12
movq %rdi, %r13
pxor %xmm4, %xmm4
pxor %xmm5, %xmm5
cmpl $0x0, 0x10(%rsp)
jle 0x40b7
movq 0x30(%rsp), %rsi
movsbl (%rsi,%r10), %edi
imull %r15d, %edi
movslq %edi, %rbx
shlq $0x4, %rbx
addq 0x70(%rsp), %rbx
movdqa (%r13,%r8), %xmm6
pslldq $0x2, %xmm6             # xmm6 = zero,zero,xmm6[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
xorl %edi, %edi
paddsw (%rbx,%rdi), %xmm6
movdqa (%rbp,%rdi), %xmm7
movdqa %xmm7, %xmm8
pmaxsw %xmm5, %xmm8
pmaxsw %xmm6, %xmm8
pmaxsw %xmm8, %xmm4
movdqa %xmm8, (%r12,%rdi)
psubusw %xmm0, %xmm8
psubusw %xmm1, %xmm7
pmaxsw %xmm8, %xmm7
movdqa %xmm7, (%rbp,%rdi)
psubusw %xmm1, %xmm5
pmaxsw %xmm8, %xmm5
movdqa (%r13,%rdi), %xmm6
addq $0x10, %rdi
cmpq %rdi, %r9
jne 0x4065
xorl %ebx, %ebx
pslldq $0x2, %xmm5             # xmm5 = zero,zero,xmm5[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
cmpl $0x0, 0x10(%rsp)
jle 0x40fc
xorl %edi, %edi
movdqa (%r12,%rdi), %xmm6
pmaxsw %xmm5, %xmm6
pmaxsw %xmm6, %xmm4
movdqa %xmm6, (%r12,%rdi)
psubusw %xmm0, %xmm6
psubusw %xmm1, %xmm5
movdqa %xmm5, %xmm7
pcmpgtw %xmm6, %xmm7
pmovmskb %xmm7, %esi
testl %esi, %esi
je 0x4103
addq $0x10, %rdi
cmpq %rdi, %r9
jne 0x40c7
incl %ebx
cmpl $0x8, %ebx
jne 0x40b9
pmaxsw %xmm4, %xmm2
movdqa %xmm3, %xmm5
pcmpeqw %xmm2, %xmm5
pmovmskb %xmm5, %esi
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x4189
movdqa %xmm2, %xmm3
psrldq $0x8, %xmm3             # xmm3 = xmm3[8,9,10,11,12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero
pmaxsw %xmm2, %xmm3
movdqa %xmm3, %xmm5
psrldq $0x4, %xmm5             # xmm5 = xmm5[4,5,6,7,8,9,10,11,12,13,14,15],zero,zero,zero,zero
pmaxsw %xmm3, %xmm5
movdqa %xmm5, %xmm3
psrldq $0x2, %xmm3             # xmm3 = xmm3[2,3,4,5,6,7,8,9,10,11,12,13,14,15],zero,zero
pmaxsw %xmm5, %xmm3
movd %xmm3, %edi
cmpw %r14w, %di
movl 0x8(%rsp), %esi
cmoval %r10d, %esi
movl %esi, 0x8(%rsp)
cmoval %edi, %r14d
movdqa %xmm2, %xmm3
jbe 0x4189
cmpl $0x0, 0x10(%rsp)
jle 0x4189
xorl %ebx, %ebx
movdqa (%r12,%rbx), %xmm3
movdqa %xmm3, (%rax,%rbx)
addq $0x10, %rbx
cmpq %rbx, %r9
jne 0x4169
movdqa %xmm2, %xmm3
movl %r10d, 0x8(%rsp)
movl %edi, %r14d
movdqa %xmm4, %xmm5
psrldq $0x8, %xmm5             # xmm5 = xmm5[8,9,10,11,12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero
pmaxsw %xmm4, %xmm5
movdqa %xmm5, %xmm4
psrldq $0x4, %xmm4             # xmm4 = xmm4[4,5,6,7,8,9,10,11,12,13,14,15],zero,zero,zero,zero
pmaxsw %xmm5, %xmm4
movdqa %xmm4, %xmm5
psrldq $0x2, %xmm5             # xmm5 = xmm5[2,3,4,5,6,7,8,9,10,11,12,13,14,15],zero,zero
pmaxsw %xmm4, %xmm5
movd %xmm5, %esi
movq 0x20(%rsp), %rdi
movw %si, (%rdi,%r10,2)
cmpw 0x78(%rsp), %si
je 0x41d4
addq %r11, %r10
movq %r12, %rdi
cmpl %r10d, %edx
jne 0x4028
movq 0x10(%rsp), %rdx
leal -0x1(%rdx), %ebx
testl %edx, %edx
jle 0x4213
leal (,%r15,8), %edx
cmpl $0x2, %edx
cmovgel %edx, %ecx
xorl %edx, %edx
cmpw %r14w, (%rax,%rdx,2)
jne 0x420c
movl %edx, %esi
shrl $0x3, %esi
movl %edx, %edi
andl $0x7, %edi
imull %r15d, %edi
addl %esi, %edi
cmpl %ebx, %edi
cmovll %edi, %ebx
incq %rdx
cmpl %edx, %ecx
jne 0x41f0
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
movw %r14w, (%rax)
movl 0x8(%rsp), %r9d
movl %r9d, 0x4(%rax)
movl %ebx, 0x8(%rax)
andw $0x0, 0xc(%rax)
andq $0x0, 0x10(%rax)
movl %r9d, %eax
movl 0x80(%rsp), %r10d
subl %r10d, %eax
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %rdi
jle 0x429a
movzwl 0xc(%r15), %ecx
movl %eax, %eax
xorl %edx, %edx
movzwl (%rdi,%rdx,2), %esi
cmpw %cx, %si
jbe 0x428d
movl %edx, 0x10(%r15)
movl %esi, %ecx
incq %rdx
cmpq %rdx, %rax
jne 0x427e
movw %cx, 0xc(%r15)
addl %r10d, %r9d
cmpl 0xc(%rsp), %r9d
jge 0x42cb
movzwl 0xc(%r15), %ecx
movslq %r9d, %rax
movzwl (%rdi,%rax,2), %edx
cmpw %cx, %dx
jbe 0x42bb
movl %r9d, 0x10(%r15)
movl %edx, %ecx
incq %rax
incl %r9d
cmpq %r8, %rax
jl 0x42ac
movw %cx, 0xc(%r15)
callq 0x3070
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0x8(%rsp)
xorl %r14d, %r14d
jmp 0x41d4