// File: /workspace/github_c_makefile_star2_O1/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 $0x98, %rsp
movl %r9d, 0x8(%rsp)
movl %r8d, %ebp
movl %ecx, 0x14(%rsp)
movq %rdx, 0x50(%rsp)
movl %esi, %ebx
movq %rdi, %r15
movq 0xd0(%rsp), %r13
leal 0x3(%rbx), %eax
leal 0x6(%rbx), %r12d
testl %eax, %eax
movl %eax, 0x2c(%rsp)
cmovnsl %eax, %r12d
movl %r12d, %r14d
sarl $0x2, %r14d
leal (,%r14,8), %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rsi
movl $0x10, %edi
callq 0x2ea474
movq %rax, 0x20(%rsp)
movq %r14, 0x18(%rsp)
movslq %r14d, %r14
movl $0x10, %edi
movq %r14, %rsi
callq 0x2ea474
movq %rax, 0x68(%rsp)
movl $0x10, %edi
movq %r14, %rsi
callq 0x2ea474
movq %rax, 0x58(%rsp)
movd %ebp, %xmm0
movdqa %xmm0, 0x80(%rsp)
movd 0x8(%rsp), %xmm0
movdqa %xmm0, 0x70(%rsp)
andl $-0x4, %r12d
movl %r12d, %edi
movl 0x14(%rsp), %esi
callq 0x209630
movq %rax, 0x60(%rsp)
movq %rbx, 0x8(%rsp)
movslq %ebx, %rax
cmpl $0x2, %r14d
movl $0x1, %ecx
movq %r14, 0x48(%rsp)
cmovgel %r14d, %ecx
xorl %edx, %edx
xorl %esi, %esi
cmpl $0x0, 0x8(%rsp)
jle 0x43b917
movslq %esi, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %rdi, %r10
xorl %r11d, %r11d
cmpq %rax, %r10
jge 0x43b8de
movslq 0x18(%r13), %r11
movq 0x8(%r13), %rbx
movq 0x10(%r13), %r12
movzbl (%r15,%r10), %r14d
movslq (%r12,%r14,4), %r14
imulq %r11, %r14
movl (%rbx,%r14,4), %r11d
movl %r11d, 0x30(%rsp,%r9)
incq %r10
addq $0x4, %r9
cmpq $0x10, %r9
jne 0x43b8b9
movq %rsi, %r9
shlq $0x4, %r9
movdqa 0x30(%rsp), %xmm0
movq 0x20(%rsp), %r10
movdqa %xmm0, (%r10,%r9)
incq %rsi
incl %r8d
addq $0x4, %rdi
cmpl %ecx, %r8d
jne 0x43b8b3
incl %edx
cmpl $0x18, %edx
jne 0x43b8a4
movq 0x8(%rsp), %rax
testl %eax, %eax
movl 0x2c(%rsp), %esi
cmovnsl %eax, %esi
movq 0x68(%rsp), %rbx
movq 0x60(%rsp), %r14
movq 0x58(%rsp), %rdi
movl 0x14(%rsp), %r8d
jle 0x43b988
negl %ebp
movq 0x18(%rsp), %rcx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
xorl %ecx, %ecx
pxor %xmm0, %xmm0
xorl %edx, %edx
movl %ebp, 0x30(%rsp,%rdx,4)
incq %rdx
cmpq $0x4, %rdx
jne 0x43b95c
movq %rcx, %rdx
shlq $0x4, %rdx
movdqa %xmm0, (%rbx,%rdx)
movdqa 0x30(%rsp), %xmm1
movdqa %xmm1, (%rdi,%rdx)
incq %rcx
cmpq %rax, %rcx
jne 0x43b95a
testl %r8d, %r8d
jle 0x43bb4c
andl $-0x4, %esi
movq 0x8(%rsp), %rax
subl %esi, %eax
pshufd $0x0, 0x80(%rsp), %xmm1 # xmm1 = mem[0,0,0,0]
pshufd $0x0, 0x70(%rsp), %xmm2 # xmm2 = mem[0,0,0,0]
movdqa %xmm1, %xmm3
psubd %xmm2, %xmm3
movq 0x48(%rsp), %rbp
movq %rbp, %rcx
shlq $0x4, %rcx
addq %rbx, %rcx
movq 0x18(%rsp), %rsi
cmpl $0x2, %esi
movl $0x1, %edx
cmovgel %esi, %edx
movl %r8d, %esi
pxor %xmm4, %xmm4
xorl %r15d, %r15d
movdqa 0x1ba3cb(%rip), %xmm5   # 0x5f5db0
pxor %xmm0, %xmm0
movq 0x50(%rsp), %r12
cmpl $0x0, 0x8(%rsp)
jle 0x43bb13
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 $0x4, %xmm9             # xmm9 = zero,zero,zero,zero,xmm9[0,1,2,3,4,5,6,7,8,9,10,11]
por %xmm8, %xmm9
paddd (%r8,%r10), %xmm9
movdqa %xmm7, %xmm8
pmaxsd %xmm4, %xmm8
pmaxsd %xmm9, %xmm8
movdqa %xmm8, %xmm9
palignr $0xc, %xmm10, %xmm9     # xmm9 = xmm10[12,13,14,15],xmm9[0,1,2,3,4,5,6,7,8,9,10,11]
psubd %xmm1, %xmm9
movdqa %xmm9, %xmm10
pcmpgtd %xmm4, %xmm10
movmskps %xmm10, %r11d
testl %r11d, %r11d
je 0x43baab
je 0x43bab2
movdqa %xmm9, %xmm10
pslldq $0x4, %xmm9             # xmm9 = zero,zero,zero,zero,xmm9[0,1,2,3,4,5,6,7,8,9,10,11]
psubd %xmm2, %xmm9
pmaxsd %xmm9, %xmm10
movdqa %xmm9, %xmm11
pcmpgtd %xmm4, %xmm11
movmskps %xmm11, %r11d
testl %r11d, %r11d
jne 0x43ba85
jmp 0x43bab7
movdqa %xmm8, %xmm9
jmp 0x43bad2
movdqa %xmm9, %xmm10
movdqa %xmm8, %xmm9
pmaxsd %xmm10, %xmm9
paddd %xmm3, %xmm10
pmaxsd %xmm9, %xmm10
movdqa %xmm10, %xmm8
leaq (%rbx,%r10), %r11
addq %rdi, %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, %xmm0
psubd %xmm1, %xmm9
psubd %xmm2, %xmm7
pmaxsd %xmm9, %xmm7
movdqa %xmm7, (%r10)
incq %r9
movdqa %xmm8, %xmm10
movdqa %xmm6, %xmm8
cmpq %rdx, %r9
jne 0x43ba20
movdqa -0x10(%rcx), %xmm6
testl %eax, %eax
jle 0x43bb29
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 0x43bb1f
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 0x43b9ee
jmp 0x43bb50
pxor %xmm0, %xmm0
cmpl $0x0, 0x8(%rsp)
jle 0x43bbb1
movq 0x18(%rsp), %rcx
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)
pextrw $0x1, %xmm1, %esi
movswl %si, %esi
movl %esi, 0x4(%rdx,%rcx)
pextrw $0x2, %xmm1, %esi
movswl %si, %esi
movl %esi, 0x8(%rdx,%rcx)
pextrw $0x3, %xmm1, %esi
movswl %si, %esi
movl %esi, 0xc(%rdx,%rcx)
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x43bb6d
movl $0xc0000000, %eax       # imm = 0xC0000000
movl $0x4, %ecx
pextrd $0x3, %xmm0, %edx
cmpl %eax, %edx
cmovgl %edx, %eax
pslldq $0x4, %xmm0             # xmm0 = zero,zero,zero,zero,xmm0[0,1,2,3,4,5,6,7,8,9,10,11]
decl %ecx
jne 0x43bbbb
movl %eax, (%r14)
orl $0x8242004, 0xc(%r14)   # imm = 0x8242004
callq 0x2094b5
movq %rbx, %rdi
callq 0x2094b5
movq 0x20(%rsp), %rdi
callq 0x2094b5
movq %r14, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop