// File: /workspace/github_c_makefile_star2_O1/nh13[P]ksw/src/parasail/src/sw_blocked_sse41_128_16.c
// Function: parasail_sw_rowcol_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 $0x88, %rsp
movl %r9d, 0x8(%rsp)
movl %r8d, %ebp
movl %ecx, %r12d
movq %rdx, 0x48(%rsp)
movl %esi, %ebx
movq %rdi, %r15
movq 0xc0(%rsp), %r13
leal 0x7(%rbx), %eax
leal 0xe(%rbx), %ecx
testl %eax, %eax
movl %eax, 0x28(%rsp)
cmovnsl %eax, %ecx
movl %ecx, %r14d
sarl $0x3, %r14d
andl $-0x8, %ecx
movq %rcx, 0x18(%rsp)
leal (%rcx,%rcx,2), %eax
movslq %eax, %rsi
movl $0x10, %edi
callq 0x2ea474
movq %rax, 0x20(%rsp)
movl %r14d, 0x14(%rsp)
movslq %r14d, %r14
movl $0x10, %edi
movq %r14, %rsi
callq 0x2ea474
movq %rax, 0x58(%rsp)
movl $0x10, %edi
movq %r14, %rsi
callq 0x2ea474
movq %rax, 0x50(%rsp)
movd %ebp, %xmm0
movdqa %xmm0, 0x70(%rsp)
movd 0x8(%rsp), %xmm0
movdqa %xmm0, 0x60(%rsp)
movq 0x18(%rsp), %rdi
movl %r12d, 0x2c(%rsp)
movl %r12d, %esi
callq 0x209630
movq %rax, 0x18(%rsp)
movq %rbx, 0x8(%rsp)
movslq %ebx, %rax
cmpl $0x2, %r14d
movl $0x1, %ecx
movq %r14, 0x40(%rsp)
cmovgel %r14d, %ecx
xorl %edx, %edx
xorl %esi, %esi
cmpl $0x0, 0x8(%rsp)
jle 0x43bd57
movslq %esi, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %rdi, %r10
cmpq %rax, %r10
jge 0x43bd1a
movslq 0x18(%r13), %r11
movq 0x8(%r13), %rbx
movq 0x10(%r13), %r12
movzbl (%r15,%r10), %r14d
movslq (%r12,%r14,4), %r14
imulq %r11, %r14
movzwl (%rbx,%r14,4), %r11d
jmp 0x43bd1d
xorl %r11d, %r11d
movw %r11w, 0x30(%rsp,%r9)
incq %r10
addq $0x2, %r9
cmpq $0x10, %r9
jne 0x43bcf5
movq %rsi, %r9
shlq $0x4, %r9
movdqa 0x30(%rsp), %xmm0
movq 0x20(%rsp), %r10
movdqa %xmm0, (%r10,%r9)
incq %rsi
incl %r8d
addq $0x8, %rdi
cmpl %ecx, %r8d
jne 0x43bcef
incl %edx
cmpl $0x18, %edx
jne 0x43bce0
movq 0x8(%rsp), %rax
testl %eax, %eax
movl 0x28(%rsp), %esi
cmovnsl %eax, %esi
movq 0x58(%rsp), %rbx
movq 0x18(%rsp), %r14
movq 0x50(%rsp), %rdi
movl 0x2c(%rsp), %r8d
jle 0x43bdc8
negl %ebp
movl 0x14(%rsp), %ecx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
xorl %ecx, %ecx
pxor %xmm0, %xmm0
xorl %edx, %edx
movw %bp, 0x30(%rsp,%rdx,2)
incq %rdx
cmpq $0x8, %rdx
jne 0x43bd9b
movq %rcx, %rdx
shlq $0x4, %rdx
movdqa %xmm0, (%rbx,%rdx)
movdqa 0x30(%rsp), %xmm1
movdqa %xmm1, (%rdi,%rdx)
incq %rcx
cmpq %rax, %rcx
jne 0x43bd99
testl %r8d, %r8d
jle 0x43bf8d
andl $-0x8, %esi
pshuflw $0x0, 0x70(%rsp), %xmm0 # xmm0 = mem[0,0,0,0,4,5,6,7]
pshuflw $0x0, 0x60(%rsp), %xmm2 # xmm2 = mem[0,0,0,0,4,5,6,7]
movq 0x8(%rsp), %rax
subl %esi, %eax
pshufd $0x0, %xmm0, %xmm1      # xmm1 = xmm0[0,0,0,0]
pshufd $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
movdqa %xmm1, %xmm3
psubw %xmm2, %xmm3
movq 0x40(%rsp), %rbp
movq %rbp, %rcx
shlq $0x4, %rcx
addq %rbx, %rcx
movl 0x14(%rsp), %esi
cmpl $0x2, %esi
movl $0x1, %edx
cmovgel %esi, %edx
movl %r8d, %esi
pxor %xmm4, %xmm4
xorl %r15d, %r15d
movdqa 0x1bbf75(%rip), %xmm5   # 0x5f7da0
pxor %xmm0, %xmm0
movq 0x48(%rsp), %r12
cmpl $0x0, 0x8(%rsp)
jle 0x43bf54
movq 0x10(%r13), %r8
movzbl (%r12,%r15), %r9d
movslq (%r8,%r9,4), %r8
imulq %rbp, %r8
shlq $0x4, %r8
addq 0x20(%rsp), %r8
pxor %xmm8, %xmm8
xorl %r9d, %r9d
movdqa %xmm5, %xmm10
movq %r9, %r10
shlq $0x4, %r10
movdqa (%rbx,%r10), %xmm6
movdqa (%rdi,%r10), %xmm7
movdqa %xmm6, %xmm9
pslldq $0x2, %xmm9             # xmm9 = zero,zero,xmm9[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
por %xmm8, %xmm9
paddw (%r8,%r10), %xmm9
movdqa %xmm7, %xmm8
pmaxsw %xmm4, %xmm8
pmaxsw %xmm9, %xmm8
movdqa %xmm8, %xmm9
palignr $0xe, %xmm10, %xmm9     # xmm9 = xmm10[14,15],xmm9[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
psubw %xmm1, %xmm9
movdqa %xmm9, %xmm10
pcmpgtw %xmm4, %xmm10
pmovmskb %xmm10, %r11d
testl %r11d, %r11d
je 0x43bef0
je 0x43bef7
movdqa %xmm9, %xmm10
pslldq $0x2, %xmm9             # xmm9 = zero,zero,xmm9[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
psubw %xmm2, %xmm9
pmaxsw %xmm9, %xmm10
movdqa %xmm9, %xmm11
pcmpgtw %xmm4, %xmm11
pmovmskb %xmm11, %r11d
testl %r11d, %r11d
jne 0x43beca
jmp 0x43befc
movdqa %xmm8, %xmm9
jmp 0x43bf15
movdqa %xmm9, %xmm10
movdqa %xmm8, %xmm9
pmaxsw %xmm10, %xmm9
paddw %xmm3, %xmm10
pmaxsw %xmm9, %xmm10
movdqa %xmm10, %xmm8
leaq (%rbx,%r10), %r11
addq %rdi, %r10
psrldq $0xe, %xmm6             # xmm6 = xmm6[14,15],zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero
movdqa %xmm9, (%r11)
pmaxsw %xmm9, %xmm0
psubw %xmm1, %xmm9
psubw %xmm2, %xmm7
pmaxsw %xmm9, %xmm7
movdqa %xmm7, (%r10)
incq %r9
movdqa %xmm8, %xmm10
movdqa %xmm6, %xmm8
cmpq %rdx, %r9
jne 0x43be66
movdqa -0x10(%rcx), %xmm6
testl %eax, %eax
jle 0x43bf6a
movl %eax, %r8d
pslldq $0x2, %xmm6             # xmm6 = zero,zero,xmm6[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
decl %r8d
jne 0x43bf60
pextrw $0x7, %xmm6, %r8d
movswl %r8w, %r8d
movq 0x10(%r14), %r9
movq (%r9), %r9
movl %r8d, (%r9,%r15,4)
incq %r15
cmpq %rsi, %r15
jne 0x43be34
jmp 0x43bf91
pxor %xmm0, %xmm0
cmpl $0x0, 0x8(%rsp)
jle 0x43c025
movl 0x14(%rsp), %ecx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
shlq $0x4, %rax
xorl %ecx, %ecx
movdqa (%rbx,%rcx), %xmm1
movq 0x10(%r14), %rdx
movq 0x8(%rdx), %rdx
movd %xmm1, %esi
movswl %si, %esi
movl %esi, (%rdx,%rcx,2)
pextrw $0x1, %xmm1, %esi
movswl %si, %esi
movl %esi, 0x4(%rdx,%rcx,2)
pextrw $0x2, %xmm1, %esi
movswl %si, %esi
movl %esi, 0x8(%rdx,%rcx,2)
pextrw $0x3, %xmm1, %esi
movswl %si, %esi
movl %esi, 0xc(%rdx,%rcx,2)
pextrw $0x4, %xmm1, %esi
movswl %si, %esi
movl %esi, 0x10(%rdx,%rcx,2)
pextrw $0x5, %xmm1, %esi
movswl %si, %esi
movl %esi, 0x14(%rdx,%rcx,2)
pextrw $0x6, %xmm1, %esi
movswl %si, %esi
movl %esi, 0x18(%rdx,%rcx,2)
pextrw $0x7, %xmm1, %esi
movswl %si, %esi
movl %esi, 0x1c(%rdx,%rcx,2)
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x43bfb1
movl $0xffffc000, %ecx       # imm = 0xFFFFC000
movl $0x8, %eax
movl %ecx, %edx
pextrw $0x7, %xmm0, %ecx
movswl %cx, %ecx
cmpl %ecx, %edx
cmovgl %edx, %ecx
pslldq $0x2, %xmm0             # xmm0 = zero,zero,xmm0[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
decl %eax
jne 0x43c02f
movl %ecx, (%r14)
orl $0x8242004, 0xc(%r14)   # imm = 0x8242004
callq 0x2094b5
movq %rbx, %rdi
callq 0x2094b5
movq 0x20(%rsp), %rdi
callq 0x2094b5
movq %r14, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop