// File: /workspace/github_c_makefile_star2_O2/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, (%rsp)
movl %r8d, %r13d
movq %rcx, 0x18(%rsp)
movq %rdx, 0x58(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, 0x50(%rsp)
movq 0xd0(%rsp), %rbp
leal 0x3(%rsi), %eax
pushq $0x4
popq %rcx
cltd
idivl %ecx
movl %eax, %r14d
imull $0x18, %eax, %eax
movslq %eax, %rsi
pushq $0x10
popq %r15
movq %r15, %rdi
callq 0x297cd8
movq %rax, %rbx
movslq %r14d, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x297cd8
movq %rax, %r14
movq %r15, %rdi
movq %r12, %rsi
callq 0x297cd8
movq %rax, %r15
movd %r13d, %xmm0
movdqa %xmm0, 0x70(%rsp)
movss (%rsp), %xmm0
movaps %xmm0, 0x60(%rsp)
leal (,%r12,4), %edi
movq 0x18(%rsp), %rsi
callq 0x1ab93b
movq %rax, 0x48(%rsp)
movslq 0x10(%rsp), %rcx
xorl %edx, %edx
testl %r12d, %r12d
movl $0x0, %eax
movq %r12, 0x28(%rsp)
cmovgl %r12d, %eax
movq %rax, 0x20(%rsp)
xorl %eax, %eax
cmpl $0x18, %eax
je 0x40634d
movl %eax, 0xc(%rsp)
movslq %edx, %rdi
movq 0x20(%rsp), %rax
addl %edi, %eax
movq %rax, (%rsp)
movslq %eax, %r8
movq 0x50(%rsp), %r9
xorl %r10d, %r10d
cmpq %r8, %rdi
je 0x40633e
xorl %r11d, %r11d
cmpq $0x4, %r11
je 0x406321
leaq (%r10,%r11), %r12
xorl %eax, %eax
cmpq %rcx, %r12
jge 0x406317
movslq 0x18(%rbp), %rax
movq 0x8(%rbp), %r12
movq 0x10(%rbp), %rdx
movzbl (%r9,%r11), %esi
movslq (%rdx,%rsi,4), %rdx
imulq %rax, %rdx
movl (%r12,%rdx,4), %eax
movl %eax, 0x30(%rsp,%r11,4)
incq %r11
jmp 0x4062e9
movq %rdi, %rax
shlq $0x4, %rax
movaps 0x30(%rsp), %xmm0
movaps %xmm0, (%rbx,%rax)
incq %rdi
addq $0x4, %r10
addq $0x4, %r9
jmp 0x4062e1
movl 0xc(%rsp), %eax
incl %eax
movq (%rsp), %rdx
jmp 0x4062bb
pshufd $0x0, 0x60(%rsp), %xmm0 # xmm0 = mem[0,0,0,0]
negl %r13d
movl 0x20(%rsp), %ecx
xorl %eax, %eax
pushq $0x4
popq %r11
cmpq %rcx, %rax
je 0x4063a5
xorl %edx, %edx
cmpq $0x4, %rdx
je 0x406380
andl $0x0, 0x30(%rsp,%rdx,4)
movl %r13d, 0x80(%rsp,%rdx,4)
incq %rdx
jmp 0x406368
movq %rax, %rdx
shlq $0x4, %rdx
movaps 0x30(%rsp), %xmm1
movaps %xmm1, (%r14,%rdx)
movdqa 0x80(%rsp), %xmm1
movdqa %xmm1, (%r15,%rdx)
incq %rax
jmp 0x406361
pshufd $0x0, 0x70(%rsp), %xmm2 # xmm2 = mem[0,0,0,0]
movq 0x10(%rsp), %rax
cltd
idivl %r11d
movq 0x28(%rsp), %rax
shlq $0x4, %rax
addq %r14, %rax
xorl %esi, %esi
testl %edx, %edx
cmovlel %esi, %edx
movdqa %xmm2, %xmm3
movq 0x18(%rsp), %r13
testl %r13d, %r13d
cmovlel %esi, %r13d
psubd %xmm0, %xmm3
pxor %xmm4, %xmm4
movdqa 0x1c1ac8(%rip), %xmm5   # 0x5c7eb0
pxor %xmm1, %xmm1
movq 0x48(%rsp), %r12
cmpq %r13, %rsi
je 0x406546
movq 0x10(%rbp), %rdi
movq 0x58(%rsp), %r8
movzbl (%r8,%rsi), %r8d
movslq (%rdi,%r8,4), %rdi
imulq 0x28(%rsp), %rdi
shlq $0x4, %rdi
addq %rbx, %rdi
pxor %xmm9, %xmm9
xorl %r8d, %r8d
movdqa %xmm5, %xmm10
cmpq %rcx, %r8
je 0x406517
movq %r8, %r9
shlq $0x4, %r9
movdqa (%r14,%r9), %xmm6
movdqa (%r15,%r9), %xmm7
movdqa %xmm6, %xmm8
pslldq $0x4, %xmm8             # xmm8 = zero,zero,zero,zero,xmm8[0,1,2,3,4,5,6,7,8,9,10,11]
por %xmm8, %xmm9
paddd (%rdi,%r9), %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 %xmm2, %xmm9
movdqa %xmm9, %xmm10
pcmpgtd %xmm4, %xmm10
movmskps %xmm10, %r10d
testl %r10d, %r10d
je 0x4064d5
movdqa %xmm9, %xmm10
movdqa %xmm10, %xmm11
pcmpgtd %xmm4, %xmm11
movmskps %xmm11, %r10d
testl %r10d, %r10d
je 0x4064b8
pslldq $0x4, %xmm10            # xmm10 = zero,zero,zero,zero,xmm10[0,1,2,3,4,5,6,7,8,9,10,11]
psubd %xmm0, %xmm10
pmaxsd %xmm10, %xmm9
jmp 0x406492
movdqa %xmm8, %xmm10
pmaxsd %xmm9, %xmm10
paddd %xmm3, %xmm9
pmaxsd %xmm10, %xmm9
movdqa %xmm9, %xmm8
jmp 0x4064da
movdqa %xmm8, %xmm10
leaq (%r14,%r9), %r10
addq %r15, %r9
psrldq $0xc, %xmm6             # xmm6 = xmm6[12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero
movdqa %xmm10, (%r10)
pmaxsd %xmm10, %xmm1
psubd %xmm2, %xmm10
psubd %xmm0, %xmm7
pmaxsd %xmm10, %xmm7
movdqa %xmm7, (%r9)
incq %r8
movdqa %xmm6, %xmm9
movdqa %xmm8, %xmm10
jmp 0x406426
movdqa -0x10(%rax), %xmm6
movl %edx, %edi
subl $0x1, %edi
jb 0x40652a
pslldq $0x2, %xmm6             # xmm6 = zero,zero,xmm6[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
jmp 0x40651e
pextrw $0x7, %xmm6, %edi
movswl %di, %edi
movq 0x10(%r12), %r8
movq (%r8), %r8
movl %edi, (%r8,%rsi,4)
incq %rsi
jmp 0x4063f1
shlq $0x4, %rcx
xorl %eax, %eax
cmpq %rax, %rcx
je 0x40656c
movq 0x10(%r12), %rdx
movq 0x8(%rdx), %rdx
pmovsxwd (%r14,%rax), %xmm0
movdqu %xmm0, (%rdx,%rax)
addq $0x10, %rax
jmp 0x40654c
movl $0xc0000000, %eax       # imm = 0xC0000000
subl $0x1, %r11d
jb 0x406589
pextrd $0x3, %xmm1, %ecx
cmpl %eax, %ecx
cmovgl %ecx, %eax
pslldq $0x4, %xmm1             # xmm1 = zero,zero,zero,zero,xmm1[0,1,2,3,4,5,6,7,8,9,10,11]
jmp 0x406571
movl %eax, (%r12)
orl $0x8242004, 0xc(%r12)   # imm = 0x8242004
movq %r15, %rdi
callq 0x1ab763
movq %r14, %rdi
callq 0x1ab763
movq %rbx, %rdi
callq 0x1ab763
movq %r12, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop