// File: /workspace/github_c_makefile_star2_O2/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 $0x88, %rsp
movl %r9d, 0x4(%rsp)
movl %r8d, %r15d
movl %ecx, %ebp
movq %rdx, 0x48(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, 0x38(%rsp)
movq 0xc0(%rsp), %r12
leal 0x7(%rsi), %eax
pushq $0x8
popq %rcx
cltd
idivl %ecx
movl %eax, %r14d
imull $0x18, %eax, %eax
movslq %eax, %rsi
pushq $0x10
popq %r13
movq %r13, %rdi
callq 0x297cd8
movq %rax, 0x10(%rsp)
movslq %r14d, %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x297cd8
movq %rax, %r14
movq %r13, %rdi
movq %rbx, %rsi
callq 0x297cd8
movq %rax, %r13
movd %r15d, %xmm0
movdqa %xmm0, 0x50(%rsp)
movss 0x4(%rsp), %xmm0
movaps %xmm0, 0x60(%rsp)
leal (,%rbx,8), %edi
movq %rbp, 0x30(%rsp)
movl %ebp, %esi
callq 0x1ab876
movq %rax, %rbp
movslq 0x8(%rsp), %rcx
xorl %edx, %edx
testl %ebx, %ebx
movl $0x0, %eax
movq %rbx, 0x40(%rsp)
cmovgl %ebx, %eax
movq %rax, 0x18(%rsp)
xorl %eax, %eax
cmpl $0x18, %eax
je 0x3bb594
movl %eax, 0x4(%rsp)
movslq %edx, %rdi
movq 0x18(%rsp), %rax
addl %edi, %eax
movq %rax, 0x8(%rsp)
movslq %eax, %r8
movq 0x38(%rsp), %r9
xorl %r10d, %r10d
cmpq %r8, %rdi
je 0x3bb584
xorl %r11d, %r11d
cmpq $0x8, %r11
je 0x3bb562
leaq (%r10,%r11), %rax
cmpq %rcx, %rax
jge 0x3bb555
movslq 0x18(%r12), %rax
movq 0x8(%r12), %rdx
movq 0x10(%r12), %rsi
movzbl (%r9,%r11), %ebx
movslq (%rsi,%rbx,4), %rsi
imulq %rax, %rsi
movzwl (%rdx,%rsi,4), %eax
jmp 0x3bb557
xorl %eax, %eax
movw %ax, 0x20(%rsp,%r11,2)
incq %r11
jmp 0x3bb524
movq %rdi, %rax
shlq $0x4, %rax
movaps 0x20(%rsp), %xmm0
movq 0x10(%rsp), %rdx
movaps %xmm0, (%rdx,%rax)
incq %rdi
addq $0x8, %r10
addq $0x8, %r9
jmp 0x3bb51c
movl 0x4(%rsp), %eax
incl %eax
movq 0x8(%rsp), %rdx
jmp 0x3bb4f5
pshuflw $0x0, 0x50(%rsp), %xmm0 # xmm0 = mem[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
negl %r15d
movl 0x18(%rsp), %eax
xorl %ecx, %ecx
cmpq %rax, %rcx
je 0x3bb5e8
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x3bb5c7
andw $0x0, 0x20(%rsp,%rdx,2)
movw %r15w, 0x70(%rsp,%rdx,2)
incq %rdx
jmp 0x3bb5b0
movq %rcx, %rdx
shlq $0x4, %rdx
movaps 0x20(%rsp), %xmm1
movaps %xmm1, (%r14,%rdx)
movaps 0x70(%rsp), %xmm1
movaps %xmm1, (%r13,%rdx)
incq %rcx
jmp 0x3bb5a9
pshuflw $0x0, 0x60(%rsp), %xmm1 # xmm1 = mem[0,0,0,0,4,5,6,7]
pshufd $0x0, %xmm1, %xmm2      # xmm2 = xmm1[0,0,0,0]
movdqa %xmm0, %xmm3
movq 0x30(%rsp), %r15
movslq %r15d, %rcx
xorl %edx, %edx
testl %r15d, %r15d
cmovlel %edx, %r15d
psubw %xmm2, %xmm3
pxor %xmm4, %xmm4
movdqa 0x20e8d7(%rip), %xmm5   # 0x5c9ef0
pxor %xmm1, %xmm1
cmpq %r15, %rdx
je 0x3bb83e
movq 0x10(%r12), %rsi
movq 0x48(%rsp), %rdi
movzbl (%rdi,%rdx), %edi
movslq (%rsi,%rdi,4), %rsi
imulq 0x40(%rsp), %rsi
shlq $0x4, %rsi
addq 0x10(%rsp), %rsi
pxor %xmm9, %xmm9
xorl %edi, %edi
movdqa %xmm5, %xmm11
cmpq %rax, %rdi
je 0x3bb836
movq %rdi, %r8
shlq $0x4, %r8
movdqa (%r14,%r8), %xmm6
movdqa (%r13,%r8), %xmm7
movdqa %xmm6, %xmm8
pslldq $0x2, %xmm8             # xmm8 = zero,zero,xmm8[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
por %xmm8, %xmm9
paddw (%rsi,%r8), %xmm9
movdqa %xmm7, %xmm8
pmaxsw %xmm4, %xmm8
pmaxsw %xmm9, %xmm8
movdqa %xmm8, %xmm10
palignr $0xe, %xmm11, %xmm10    # xmm10 = xmm11[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 0x3bb700
movdqa %xmm10, %xmm9
movdqa %xmm9, %xmm11
pcmpgtw %xmm4, %xmm11
pmovmskb %xmm11, %r9d
testl %r9d, %r9d
je 0x3bb6e5
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
jmp 0x3bb6bf
movdqa %xmm8, %xmm9
pmaxsw %xmm10, %xmm9
paddw %xmm3, %xmm10
pmaxsw %xmm9, %xmm10
movdqa %xmm10, %xmm8
jmp 0x3bb705
movdqa %xmm8, %xmm9
leaq (%r14,%r8), %r9
movdqa %xmm9, (%r9)
movq 0x10(%rbp), %r9
movq (%r9), %r9
movd %xmm9, %r10d
movswl %r10w, %r10d
leaq (,%rdi,8), %r11
imulq %rcx, %r11
leaq (%r9,%r11,4), %r11
movl %r10d, (%r11,%rdx,4)
pextrw $0x1, %xmm9, %r10d
movswl %r10w, %r10d
leaq 0x1(,%rdi,8), %r11
imulq %rcx, %r11
leaq (%r9,%r11,4), %r11
movl %r10d, (%r11,%rdx,4)
pextrw $0x2, %xmm9, %r10d
movswl %r10w, %r10d
leaq 0x2(,%rdi,8), %r11
imulq %rcx, %r11
leaq (%r9,%r11,4), %r11
pextrw $0x3, %xmm9, %ebx
movl %r10d, (%r11,%rdx,4)
movswl %bx, %r10d
leaq 0x3(,%rdi,8), %r11
imulq %rcx, %r11
leaq (%r9,%r11,4), %r11
movl %r10d, (%r11,%rdx,4)
pextrw $0x4, %xmm9, %r10d
addq %r13, %r8
movswl %r10w, %r10d
leaq 0x4(,%rdi,8), %r11
imulq %rcx, %r11
leaq (%r9,%r11,4), %r11
movl %r10d, (%r11,%rdx,4)
pextrw $0x5, %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 0x5(,%rdi,8), %r11
imulq %rcx, %r11
leaq (%r9,%r11,4), %r11
movl %r10d, (%r11,%rdx,4)
pextrw $0x6, %xmm9, %r10d
movswl %r10w, %r10d
leaq 0x6(,%rdi,8), %r11
imulq %rcx, %r11
leaq (%r9,%r11,4), %r11
movl %r10d, (%r11,%rdx,4)
pextrw $0x7, %xmm9, %r10d
movswl %r10w, %r10d
leaq 0x7(,%rdi,8), %r11
imulq %rcx, %r11
leaq (%r9,%r11,4), %r9
movl %r10d, (%r9,%rdx,4)
pmaxsw %xmm9, %xmm1
psubw %xmm0, %xmm9
psubw %xmm2, %xmm7
pmaxsw %xmm9, %xmm7
movdqa %xmm7, (%r8)
incq %rdi
movdqa %xmm6, %xmm9
movdqa %xmm8, %xmm11
jmp 0x3bb653
incq %rdx
jmp 0x3bb61d
movl $0xffffc000, %eax       # imm = 0xFFFFC000
pushq $0x8
popq %rdx
subl $0x1, %edx
jb 0x3bb85f
pextrw $0x7, %xmm1, %ecx
movswl %cx, %ecx
cmpl %ecx, %eax
cmovlel %ecx, %eax
pslldq $0x2, %xmm1             # xmm1 = zero,zero,xmm1[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
jmp 0x3bb846
movl %eax, (%rbp)
orl $0x8222004, 0xc(%rbp)   # imm = 0x8222004
movq %r13, %rdi
callq 0x1ab763
movq %r14, %rdi
callq 0x1ab763
movq 0x10(%rsp), %rdi
callq 0x1ab763
movq %rbp, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq