// File: /workspace/github_c_makefile_star2_O3/nh13[P]ksw/src/parasail/src/sw_blocked_sse41_128_32.c
// Function: parasail_sw_rowcol_blocked_sse41_128_32
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 = 4; /* 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_32;
    __m128i vGapO = _mm_set1_epi32(open);
    __m128i vGapE = _mm_set1_epi32(gap);
    __m128i vZero = _mm_setzero_si128();
    __m128i vNegInf = _mm_set1_epi32(NEG_INF_32);
    __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_32_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_32_t h;
            __m128i_32_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, 12); /* rshift 3 */
            vH = _mm_slli_si128(vH, 4); /* lshift 1 */
            vH = _mm_or_si128(vH, vX);
            vX = vT1;

            vP = _mm_load_si128(pvP + i);
            vH = _mm_add_epi32(vH, vP);
            vH = _mm_max_epi32(vH, vE);
            vH = _mm_max_epi32(vH, vZero);

            vF = _mm_srli_si128(vF, 12);
            vF = _mm_or_si128(vF, _mm_slli_si128(vH, 4));
            vF = _mm_sub_epi32(vF, vGapO);
            if (_mm_movemask_epi8(_mm_cmpgt_epi32(vF, vZero))) {
                __m128i vT2 = vF;
                while (_mm_movemask_epi8(_mm_cmpgt_epi32(vT2, vZero))) {
                    vT2 = _mm_slli_si128(vT2, 4);
                    vT2 = _mm_sub_epi32(vT2, vGapE);
                    vF = _mm_max_epi32(vF, vT2);
                }
                vH = _mm_max_epi32(vH, vF);
                vF = _mm_add_epi32(vF, vGapO);
                vF = _mm_sub_epi32(vF, vGapE);
                vF = _mm_max_epi32(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_epi32(vMaxH, vH);

            vH = _mm_sub_epi32(vH, vGapO);
            vE = _mm_sub_epi32(vE, vGapE);
            vE = _mm_max_epi32(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) {
        int32_t value = (int32_t) _mm_extract_epi32(vMaxH, 3);
        if (value > score) {
            score = value;
        }
        vMaxH = _mm_slli_si128(vMaxH, 4);
    }

    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, 0x10(%rsp)
movl %ecx, 0x14(%rsp)
movq %rdx, 0x18(%rsp)
movl %esi, %r13d
movq %rdi, %rbp
movq 0xb0(%rsp), %rbx
leal 0x3(%r13), %eax
leal 0x6(%r13), %r15d
testl %eax, %eax
cmovnsl %eax, %r15d
movl %r15d, %r14d
sarl $0x2, %r14d
leal (,%r14,8), %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rsi
movl $0x10, %edi
callq 0x2f600c
movq %rax, 0x30(%rsp)
movslq %r14d, %r12
movq %r12, 0x28(%rsp)
movl $0x10, %edi
movq %r12, %rsi
callq 0x2f600c
movq %rax, %r14
movl $0x10, %edi
movq %r12, %rsi
callq 0x2f600c
movq %rax, %r12
movss 0x10(%rsp), %xmm0
movaps %xmm0, 0x40(%rsp)
movd 0xc(%rsp), %xmm0
movdqa %xmm0, 0x50(%rsp)
andl $-0x4, %r15d
movl %r15d, %edi
movl 0x14(%rsp), %esi
callq 0x20ac53
movq %rax, 0x20(%rsp)
movslq %r13d, %rax
movq 0x28(%rsp), %rdx
cmpl $0x2, %edx
movl $0x1, %ecx
cmovgel %edx, %ecx
movl %ecx, 0xc(%rsp)
xorl %edx, %edx
xorl %esi, %esi
movq %r13, 0x38(%rsp)
incl %edx
testl %r13d, %r13d
jg 0x4616c4
incl %edx
cmpl $0x19, %edx
jne 0x4616b3
jmp 0x461799
movslq %esi, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %rdi, %r10
xorl %r11d, %r11d
cmpq %rax, %r10
jge 0x4616f9
movslq 0x18(%rbx), %r11
movq 0x8(%rbx), %r15
movq 0x10(%rbx), %r13
movzbl (%rbp,%r10), %ecx
movslq (%r13,%rcx,4), %rcx
imulq %r11, %rcx
movl (%r15,%rcx,4), %r11d
movl %r11d, 0x60(%rsp,%r9)
incq %r10
addq $0x4, %r9
cmpq $0x10, %r9
jne 0x4616d2
movq %rsi, %rcx
shlq $0x4, %rcx
movdqa 0x60(%rsp), %xmm0
movq 0x30(%rsp), %r9
movdqa %xmm0, (%r9,%rcx)
incq %rsi
incl %r8d
addq $0x4, %rdi
cmpl 0xc(%rsp), %r8d
jne 0x4616cc
cmpl $0x18, %edx
movq 0x38(%rsp), %r13
jne 0x4616b1
movl %r13d, %eax
andl $0x3, %eax
pshufd $0x0, 0x40(%rsp), %xmm0 # xmm0 = mem[0,0,0,0]
pshufd $0x0, 0x50(%rsp), %xmm1 # xmm1 = mem[0,0,0,0]
testl %r13d, %r13d
jle 0x4617b1
movl 0x10(%rsp), %ecx
negl %ecx
movl 0xc(%rsp), %edx
movd %ecx, %xmm2
pshufd $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
shlq $0x4, %rdx
xorl %esi, %esi
pxor %xmm3, %xmm3
movq 0x20(%rsp), %r15
movq 0x18(%rsp), %rbp
movdqa %xmm3, (%r14,%rsi)
movdqa %xmm2, (%r12,%rsi)
addq $0x10, %rsi
cmpq %rsi, %rdx
jne 0x461782
jmp 0x4617bb
pshufd $0x0, 0x40(%rsp), %xmm0 # xmm0 = mem[0,0,0,0]
pshufd $0x0, 0x50(%rsp), %xmm1 # xmm1 = mem[0,0,0,0]
movl %r13d, %eax
negl %eax
andl $0x3, %eax
negl %eax
movq 0x20(%rsp), %r15
movq 0x18(%rsp), %rbp
movl 0xc(%rsp), %ecx
movl 0x14(%rsp), %esi
testl %esi, %esi
jle 0x46194e
movdqa %xmm0, %xmm3
psubd %xmm1, %xmm3
movq 0x28(%rsp), %rdx
shlq $0x4, %rdx
addq %r14, %rdx
movl %esi, %esi
pxor %xmm4, %xmm4
xorl %edi, %edi
movdqa 0x168981(%rip), %xmm5   # 0x5ca170
pxor %xmm2, %xmm2
testl %r13d, %r13d
jle 0x461915
movq 0x10(%rbx), %r8
movzbl (%rbp,%rdi), %r9d
movslq (%r8,%r9,4), %r8
imulq 0x28(%rsp), %r8
shlq $0x4, %r8
addq 0x30(%rsp), %r8
pxor %xmm8, %xmm8
xorl %r9d, %r9d
movdqa %xmm5, %xmm9
movq %r9, %r10
shlq $0x4, %r10
movdqa (%r14,%r10), %xmm6
movdqa (%r12,%r10), %xmm7
movdqa %xmm6, %xmm10
pslldq $0x4, %xmm10            # xmm10 = zero,zero,zero,zero,xmm10[0,1,2,3,4,5,6,7,8,9,10,11]
por %xmm8, %xmm10
paddd (%r8,%r10), %xmm10
movdqa %xmm7, %xmm8
pmaxsd %xmm4, %xmm8
pmaxsd %xmm10, %xmm8
movdqa %xmm8, %xmm10
palignr $0xc, %xmm9, %xmm10     # xmm10 = xmm9[12,13,14,15],xmm10[0,1,2,3,4,5,6,7,8,9,10,11]
psubd %xmm0, %xmm10
movdqa %xmm10, %xmm9
pcmpgtd %xmm4, %xmm9
movmskps %xmm9, %r11d
testl %r11d, %r11d
je 0x4618ca
movdqa %xmm10, %xmm11
pslldq $0x4, %xmm10            # xmm10 = zero,zero,zero,zero,xmm10[0,1,2,3,4,5,6,7,8,9,10,11]
psubd %xmm1, %xmm10
pmaxsd %xmm10, %xmm11
movdqa %xmm10, %xmm9
pcmpgtd %xmm4, %xmm9
movmskps %xmm9, %r11d
testl %r11d, %r11d
jne 0x461889
movdqa %xmm8, %xmm9
pmaxsd %xmm11, %xmm9
paddd %xmm3, %xmm11
pmaxsd %xmm9, %xmm11
movdqa %xmm11, %xmm8
jmp 0x4618cf
movdqa %xmm8, %xmm9
leaq (%r14,%r10), %r11
addq %r12, %r10
psrldq $0xc, %xmm6             # xmm6 = xmm6[12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero
movdqa %xmm9, (%r11)
pmaxsd %xmm9, %xmm2
psubd %xmm0, %xmm9
psubd %xmm1, %xmm7
pmaxsd %xmm9, %xmm7
movdqa %xmm7, (%r10)
incq %r9
movdqa %xmm8, %xmm9
movdqa %xmm6, %xmm8
cmpq %rcx, %r9
jne 0x461826
movq 0x38(%rsp), %r13
movdqa -0x10(%rdx), %xmm6
testl %eax, %eax
jle 0x46192b
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 0x461921
pextrw $0x7, %xmm6, %r8d
movswl %r8w, %r8d
movq 0x10(%r15), %r9
movq (%r9), %r9
movl %r8d, (%r9,%rdi,4)
incq %rdi
cmpq %rsi, %rdi
jne 0x4617f3
jmp 0x461952
pxor %xmm2, %xmm2
testl %r13d, %r13d
jle 0x46197a
shlq $0x4, %rcx
xorl %eax, %eax
movq 0x10(%r15), %rdx
pmovsxwd (%r14,%rax), %xmm0
movq 0x8(%rdx), %rdx
movdqu %xmm0, (%rdx,%rax)
addq $0x10, %rax
cmpq %rax, %rcx
jne 0x46195d
movl $0xc0000000, %eax       # imm = 0xC0000000
movl $0x4, %ecx
pextrd $0x3, %xmm2, %edx
cmpl %eax, %edx
cmovgl %edx, %eax
pslldq $0x4, %xmm2             # xmm2 = zero,zero,zero,zero,xmm2[0,1,2,3,4,5,6,7,8,9,10,11]
decl %ecx
jne 0x461984
movl %eax, (%r15)
orl $0x8242004, 0xc(%r15)   # imm = 0x8242004
movq %r12, %rdi
callq 0x20a60d
movq %r14, %rdi
callq 0x20a60d
movq 0x30(%rsp), %rdi
callq 0x20a60d
movq %r15, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop