// File: /workspace/github_c_makefile_star2_O3/nh13[P]ksw/src/parasail/src/sw_blocked_sse41_128_16.c
// Function: parasail_sw_table_blocked_sse41_128_16
parasail_result_t* FNAME(
        const char * const restrict s1, const int s1Len,
        const char * const restrict s2, const int s2Len,
        const int open, const int gap, const parasail_matrix_t *matrix)
{
    int32_t i = 0;
    int32_t j = 0;
    int32_t k = 0;
    int32_t segNum = 0;
    const int32_t n = 24; /* number of amino acids in table */
    const int32_t segWidth = 8; /* number of values in vector unit */
    const int32_t segLen = (s1Len + segWidth - 1) / segWidth;
    __m128i* const restrict vProfile = parasail_memalign___m128i(16, n * segLen);
    __m128i* restrict pvH = parasail_memalign___m128i(16, segLen);
    __m128i* const restrict pvE = parasail_memalign___m128i(16, segLen);
    int score = NEG_INF_16;
    __m128i vGapO = _mm_set1_epi16(open);
    __m128i vGapE = _mm_set1_epi16(gap);
    __m128i vZero = _mm_setzero_si128();
    __m128i vNegInf = _mm_set1_epi16(NEG_INF_16);
    __m128i vMaxH = vZero;
#ifdef PARASAIL_TABLE
    parasail_result_t *result = parasail_result_new_table1(segLen*segWidth, s2Len);
#else
#ifdef PARASAIL_ROWCOL
    parasail_result_t *result = parasail_result_new_rowcol1(segLen*segWidth, s2Len);
    const int32_t offset = segLen - 1;
    const int32_t position = s1Len % segWidth;
#else
    parasail_result_t *result = parasail_result_new();
#endif
#endif

    /* Generate query profile.
     * Rearrange query sequence & calculate the weight of match/mismatch.
     * Don't alias. */
    {
        int32_t index = 0;
        for (k=0; k<n; ++k) {
            for (i=0; i<segLen; ++i) {
                __m128i_16_t t;
                j = i*segWidth;
                for (segNum=0; segNum<segWidth; ++segNum) {
                    t.v[segNum] = j >= s1Len ? 0 : matrix->matrix[matrix->size*matrix->mapper[(unsigned char)s1[j]]];
                    j += 1;
                }
                _mm_store_si128(&vProfile[index], t.m);
                ++index;
            }
        }
    }

    /* initialize H and E */
    {
        int32_t index = 0;
        for (i=0; i<segLen; ++i) {
            __m128i_16_t h;
            __m128i_16_t e;
            for (segNum=0; segNum<segWidth; ++segNum) {
                h.v[segNum] = 0;
                e.v[segNum] = -open;
            }
            _mm_store_si128(&pvH[index], h.m);
            _mm_store_si128(&pvE[index], e.m);
            ++index;
        }
    }

    /* outer loop over database sequence */
    for (j=0; j<s2Len; ++j) {
        __m128i vX = vZero;
        __m128i vF = vNegInf;
        const __m128i* pvP = vProfile + matrix->mapper[(unsigned char)s2[j]] * segLen;
        for (i=0; i<segLen; ++i) {
            __m128i vP;
            __m128i vH;
            __m128i vE;
            __m128i vT1;

            vH = _mm_load_si128(pvH + i);
            vE = _mm_load_si128(pvE + i);

            vT1 = _mm_srli_si128(vH, 14); /* rshift 3 */
            vH = _mm_slli_si128(vH, 2); /* lshift 1 */
            vH = _mm_or_si128(vH, vX);
            vX = vT1;

            vP = _mm_load_si128(pvP + i);
            vH = _mm_add_epi16(vH, vP);
            vH = _mm_max_epi16(vH, vE);
            vH = _mm_max_epi16(vH, vZero);

            vF = _mm_srli_si128(vF, 14);
            vF = _mm_or_si128(vF, _mm_slli_si128(vH, 2));
            vF = _mm_sub_epi16(vF, vGapO);
            if (_mm_movemask_epi8(_mm_cmpgt_epi16(vF, vZero))) {
                __m128i vT2 = vF;
                while (_mm_movemask_epi8(_mm_cmpgt_epi16(vT2, vZero))) {
                    vT2 = _mm_slli_si128(vT2, 2);
                    vT2 = _mm_sub_epi16(vT2, vGapE);
                    vF = _mm_max_epi16(vF, vT2);
                }
                vH = _mm_max_epi16(vH, vF);
                vF = _mm_add_epi16(vF, vGapO);
                vF = _mm_sub_epi16(vF, vGapE);
                vF = _mm_max_epi16(vH, vF);
            }
            else {
                vF = vH;
            }

            _mm_store_si128(pvH + i, vH);
#ifdef PARASAIL_TABLE
            arr_store_si128(result->tables->score_table, vH, i, segWidth, j, s2Len);
#endif
            vMaxH = _mm_max_epi16(vMaxH, vH);

            vH = _mm_sub_epi16(vH, vGapO);
            vE = _mm_sub_epi16(vE, vGapE);
            vE = _mm_max_epi16(vE, vH);
            _mm_store_si128(pvE + i, vE);

        }

#ifdef PARASAIL_ROWCOL
        /* extract last value from the column */
        {
            __m128i vH = _mm_load_si128(pvH + offset);
            for (k=0; k<position; ++k) {
                vH = _mm_slli_si128(vH, 2);
            }
            result->rowcols->score_row[j] = (int16_t) _mm_extract_epi16 (vH, 7);
        }
#endif
    }

#ifdef PARASAIL_ROWCOL
    for (i=0; i<segLen; ++i) {
        __m128i vH = _mm_load_si128(pvH + i);
        arr_store_col(result->rowcols->score_col, vH, i, segWidth);
    }
#endif

    /* max in vec */
    for (j=0; j<segWidth; ++j) {
        int16_t value = (int16_t) _mm_extract_epi16(vMaxH, 7);
        if (value > score) {
            score = value;
        }
        vMaxH = _mm_slli_si128(vMaxH, 2);
    }

    result->score = score;
    result->flag |= PARASAIL_FLAG_SW | PARASAIL_FLAG_BLOCKED
        | PARASAIL_FLAG_BITS_16 | PARASAIL_FLAG_LANES_8;
#ifdef PARASAIL_TABLE
    result->flag |= PARASAIL_FLAG_TABLE;
#endif
#ifdef PARASAIL_ROWCOL
    result->flag |= PARASAIL_FLAG_ROWCOL;
#endif

    parasail_free(pvE);
    parasail_free(pvH);
    parasail_free(vProfile);

    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r9d, 0xc(%rsp)
movl %r8d, 0x14(%rsp)
movl %ecx, 0x10(%rsp)
movq %rdx, 0x38(%rsp)
movl %esi, %r12d
movq %rdi, %r15
movq 0xb0(%rsp), %rbx
leal 0x7(%r12), %eax
leal 0xe(%r12), %r13d
testl %eax, %eax
cmovnsl %eax, %r13d
movl %r13d, %ebp
sarl $0x3, %ebp
andl $-0x8, %r13d
leal (%r13,%r13,2), %eax
movslq %eax, %rsi
movl $0x10, %edi
callq 0x2f600c
movq %rax, 0x18(%rsp)
movslq %ebp, %rbp
movq %rbp, 0x20(%rsp)
movl $0x10, %edi
movq %rbp, %rsi
callq 0x2f600c
movq %rax, %r14
movl $0x10, %edi
movq %rbp, %rsi
callq 0x2f600c
movq %rax, %rbp
movss 0x14(%rsp), %xmm0
movaps %xmm0, 0x40(%rsp)
movd 0xc(%rsp), %xmm0
movdqa %xmm0, 0x50(%rsp)
movl %r13d, %edi
movl 0x10(%rsp), %esi
callq 0x20abc2
movq %rax, 0x28(%rsp)
movslq %r12d, %rax
movq 0x20(%rsp), %rdx
cmpl $0x2, %edx
movl $0x1, %ecx
cmovgel %edx, %ecx
movl %ecx, 0xc(%rsp)
xorl %edx, %edx
xorl %esi, %esi
movq %r12, 0x30(%rsp)
incl %edx
testl %r12d, %r12d
jg 0x4178c7
incl %edx
cmpl $0x19, %edx
jne 0x4178b6
jmp 0x4179a1
movslq %esi, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %rdi, %r10
cmpq %rax, %r10
jge 0x4178fa
movslq 0x18(%rbx), %r11
movq 0x8(%rbx), %r12
movq 0x10(%rbx), %rcx
movzbl (%r15,%r10), %r13d
movslq (%rcx,%r13,4), %rcx
imulq %r11, %rcx
movzwl (%r12,%rcx,4), %r11d
jmp 0x4178fd
xorl %r11d, %r11d
movw %r11w, 0x60(%rsp,%r9)
incq %r10
addq $0x2, %r9
cmpq $0x10, %r9
jne 0x4178d5
movq %rsi, %rcx
shlq $0x4, %rcx
movdqa 0x60(%rsp), %xmm0
movq 0x18(%rsp), %r9
movdqa %xmm0, (%r9,%rcx)
incq %rsi
incl %r8d
addq $0x8, %rdi
cmpl 0xc(%rsp), %r8d
jne 0x4178cf
cmpl $0x18, %edx
movq 0x30(%rsp), %r12
jne 0x4178b4
pshuflw $0x0, 0x40(%rsp), %xmm0 # xmm0 = mem[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
pshuflw $0x0, 0x50(%rsp), %xmm1 # xmm1 = mem[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
testl %r12d, %r12d
jle 0x4179b9
movl 0x14(%rsp), %ecx
negl %ecx
movl 0xc(%rsp), %eax
movd %ecx, %xmm2
pshuflw $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
shlq $0x4, %rax
xorl %edx, %edx
pxor %xmm3, %xmm3
movl 0x10(%rsp), %ecx
movdqa %xmm3, (%r14,%rdx)
movdqa %xmm2, (%rbp,%rdx)
addq $0x10, %rdx
cmpq %rdx, %rax
jne 0x41798a
jmp 0x4179bd
pshuflw $0x0, 0x40(%rsp), %xmm0 # xmm0 = mem[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
pshuflw $0x0, 0x50(%rsp), %xmm1 # xmm1 = mem[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
movl 0x10(%rsp), %ecx
testl %ecx, %ecx
jle 0x417c0f
movdqa %xmm0, %xmm3
psubw %xmm1, %xmm3
movl %ecx, %eax
movl 0xc(%rsp), %ecx
pxor %xmm4, %xmm4
xorl %edx, %edx
movdqa 0x21473f(%rip), %xmm5   # 0x62c120
pxor %xmm2, %xmm2
testl %r12d, %r12d
jle 0x417c01
movq 0x10(%rbx), %rsi
movq 0x38(%rsp), %rdi
movzbl (%rdi,%rdx), %edi
movslq (%rsi,%rdi,4), %rsi
imulq 0x20(%rsp), %rsi
shlq $0x4, %rsi
addq 0x18(%rsp), %rsi
pxor %xmm8, %xmm8
xorl %edi, %edi
movdqa %xmm5, %xmm9
movq 0x28(%rsp), %r12
movq %rdi, %r8
shlq $0x4, %r8
movdqa (%r14,%r8), %xmm6
movdqa (%rbp,%r8), %xmm7
movdqa %xmm6, %xmm10
pslldq $0x2, %xmm10            # xmm10 = zero,zero,xmm10[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
por %xmm8, %xmm10
paddw (%rsi,%r8), %xmm10
movdqa %xmm7, %xmm8
pmaxsw %xmm4, %xmm8
pmaxsw %xmm10, %xmm8
movdqa %xmm8, %xmm10
palignr $0xe, %xmm9, %xmm10     # xmm10 = xmm9[14,15],xmm10[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
psubw %xmm0, %xmm10
movdqa %xmm10, %xmm9
pcmpgtw %xmm4, %xmm9
pmovmskb %xmm9, %r9d
testl %r9d, %r9d
je 0x417ac1
movdqa %xmm10, %xmm11
pslldq $0x2, %xmm10            # xmm10 = zero,zero,xmm10[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
psubw %xmm1, %xmm10
pmaxsw %xmm10, %xmm11
movdqa %xmm10, %xmm9
pcmpgtw %xmm4, %xmm9
pmovmskb %xmm9, %r9d
testl %r9d, %r9d
jne 0x417a82
movdqa %xmm8, %xmm9
pmaxsw %xmm11, %xmm9
paddw %xmm3, %xmm11
pmaxsw %xmm9, %xmm11
movdqa %xmm11, %xmm8
jmp 0x417ac6
movdqa %xmm8, %xmm9
leaq (%r14,%r8), %r9
movdqa %xmm9, (%r9)
movq 0x10(%r12), %r9
movq (%r9), %r9
movd %xmm9, %r10d
movswl %r10w, %r10d
leaq (,%rdi,8), %r11
imulq %rax, %r11
leaq (%r9,%r11,4), %r11
movl %r10d, (%r11,%rdx,4)
pextrw $0x1, %xmm9, %r10d
movswl %r10w, %r10d
leaq 0x1(,%rdi,8), %r11
imulq %rax, %r11
leaq (%r9,%r11,4), %r11
pextrw $0x2, %xmm9, %r15d
movl %r10d, (%r11,%rdx,4)
movswl %r15w, %r10d
leaq 0x2(,%rdi,8), %r11
imulq %rax, %r11
leaq (%r9,%r11,4), %r11
movl %r10d, (%r11,%rdx,4)
pextrw $0x3, %xmm9, %r10d
addq %rbp, %r8
movswl %r10w, %r10d
leaq 0x3(,%rdi,8), %r11
imulq %rax, %r11
leaq (%r9,%r11,4), %r11
movl %r10d, (%r11,%rdx,4)
pextrw $0x4, %xmm9, %r10d
psrldq $0xe, %xmm6             # xmm6 = xmm6[14,15],zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero
movswl %r10w, %r10d
leaq 0x4(,%rdi,8), %r11
imulq %rax, %r11
leaq (%r9,%r11,4), %r11
movl %r10d, (%r11,%rdx,4)
pextrw $0x5, %xmm9, %r10d
movswl %r10w, %r10d
leaq 0x5(,%rdi,8), %r11
imulq %rax, %r11
leaq (%r9,%r11,4), %r11
movl %r10d, (%r11,%rdx,4)
pextrw $0x6, %xmm9, %r10d
movswl %r10w, %r10d
leaq 0x6(,%rdi,8), %r11
imulq %rax, %r11
leaq (%r9,%r11,4), %r11
movl %r10d, (%r11,%rdx,4)
pextrw $0x7, %xmm9, %r10d
movswl %r10w, %r10d
leaq 0x7(,%rdi,8), %r11
imulq %rax, %r11
leaq (%r9,%r11,4), %r9
movl %r10d, (%r9,%rdx,4)
pmaxsw %xmm9, %xmm2
psubw %xmm0, %xmm9
psubw %xmm1, %xmm7
pmaxsw %xmm9, %xmm7
movdqa %xmm7, (%r8)
incq %rdi
movdqa %xmm8, %xmm9
movdqa %xmm6, %xmm8
cmpq %rcx, %rdi
jne 0x417a1f
movq 0x30(%rsp), %r12
incq %rdx
cmpq %rax, %rdx
jne 0x4179e5
jmp 0x417c13
pxor %xmm2, %xmm2
movl $0xffffc000, %ecx       # imm = 0xFFFFC000
movl $0x8, %eax
movl %ecx, %edx
pextrw $0x7, %xmm2, %ecx
movswl %cx, %ecx
cmpl %ecx, %edx
cmovgl %edx, %ecx
pslldq $0x2, %xmm2             # xmm2 = zero,zero,xmm2[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
decl %eax
jne 0x417c1d
movq 0x28(%rsp), %rbx
movl %ecx, (%rbx)
orl $0x8222004, 0xc(%rbx)   # imm = 0x8222004
movq %rbp, %rdi
callq 0x20a60d
movq %r14, %rdi
callq 0x20a60d
movq 0x18(%rsp), %rdi
callq 0x20a60d
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop