// File: /workspace/github_c_makefile_star2_O1/nh13[P]ksw/src/parasail/src/sw_trace_scan.c
// Function: parasail_sw_trace_scan
parasail_result_t* ENAME(
        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)
{
    /* declare local variables */
    parasail_result_t *result = NULL;
    int * restrict s1 = NULL;
    int * restrict s2 = NULL;
    int * restrict HB = NULL;
    int * restrict H  = NULL;
    int * restrict E  = NULL;
    int * restrict HtB= NULL;
    int * restrict Ht = NULL;
    int8_t * restrict HT = NULL;
    int * restrict Ex = NULL;
    int i = 0;
    int j = 0;
    int s1Len = 0;
    int score = 0;
    int end_query = 0;
    int end_ref = 0;

    /* validate inputs */
    PARASAIL_CHECK_NULL(_s2);
    PARASAIL_CHECK_GT0(s2Len);
    PARASAIL_CHECK_GE0(open);
    PARASAIL_CHECK_GE0(gap);
    PARASAIL_CHECK_NULL(matrix);
    if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
        PARASAIL_CHECK_NULL(_s1);
        PARASAIL_CHECK_GT0(_s1Len);
    }

    /* initialize stack variables */
    i = 0;
    j = 0;
    s1Len = matrix->type == PARASAIL_MATRIX_TYPE_SQUARE ? _s1Len : matrix->length;
    score = NEG_INF_32;
    end_query = s1Len;
    end_ref = s2Len;

    /* initialize result */
    result = parasail_result_new_trace(s1Len, s2Len, 16, sizeof(int8_t));
    if (!result) return NULL;

    /* set known flags */
    result->flag |= PARASAIL_FLAG_SW | PARASAIL_FLAG_NOVEC_SCAN
        | PARASAIL_FLAG_TRACE
        | PARASAIL_FLAG_BITS_INT | PARASAIL_FLAG_LANES_1;

    /* initialize heap variables */
    s2 = parasail_memalign_int(16, s2Len);
    HB = parasail_memalign_int(16, s1Len+1);
    H  = HB+1;
    E  = parasail_memalign_int(16, s1Len);
    HtB= parasail_memalign_int(16, s1Len+1);
    Ht = HtB+1;
    Ex = parasail_memalign_int(16, s1Len);
    HT = (int8_t* restrict)result->trace->trace_table;

    /* validate heap variables */
    if (!s2) return NULL;
    if (!HB) return NULL;
    if (!E) return NULL;
    if (!HtB) return NULL;
    if (!Ex) return NULL;

    if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
        s1 = parasail_memalign_int(16, s1Len);
        if (!s1) return NULL;
        for (i=0; i<s1Len; ++i) {
            s1[i] = matrix->mapper[(unsigned char)_s1[i]];
        }
    }
    for (j=0; j<s2Len; ++j) {
        s2[j] = matrix->mapper[(unsigned char)_s2[j]];
    }

    /* initialize H */
    H[-1] = 0;
    Ht[-1] = 0;
    for (i=0; i<s1Len; ++i) {
        H[i] = 0;
    }

    /* initialize E */
    for (i=0; i<s1Len; ++i) {
        E[i] = NEG_INF_32;
    }

    /* iterate over database */
    for (j=0; j<s2Len; ++j) {
        int Ft = NEG_INF_32;
        /* calculate E */
        for (i=0; i<s1Len; ++i) {
            int E_opn = H[i]-open;
            int E_ext = E[i]-gap;
            E[i] = MAX(E_ext, E_opn);
            HT[1LL*i*s2Len + j] = (E_opn > E_ext) ? PARASAIL_DIAG_E
                                                  : PARASAIL_INS_E;
        }
        /* calculate Ht */
        Ht[-1] = 0;
        for (i=0; i<s1Len; ++i) {
            int matval = matrix->type == PARASAIL_MATRIX_TYPE_SQUARE ?
                         matrix->matrix[matrix->size*s1[i]+s2[j]] :
                         matrix->matrix[matrix->size*i+s2[j]];
            int H_dag = H[i-1]+matval;
            Ht[i] = MAX(H_dag, E[i]);
            Ex[i] = (E[i] > H_dag);
        }
        /* calculate H */
        for (i=0; i<s1Len; ++i) {
            int Ft_opn;
            int Ht_pre = Ht[i-1];
            int Ft_ext = Ft-gap;
            if (Ht_pre >= Ft_ext) {
                Ft = Ht_pre;
            }
            else {
                Ft = Ft_ext;
            }
            Ft_opn = Ft-open;
            if (H[i-1] > Ft_ext) {
                HT[1LL*i*s2Len + j] |= PARASAIL_DIAG_F;
            }
            else {
                HT[1LL*i*s2Len + j] |= PARASAIL_DEL_F;
            }
            if (Ht[i] > Ft_opn) {
                H[i] = Ht[i];
                HT[1LL*i*s2Len + j] |= Ex[i] ? PARASAIL_INS : PARASAIL_DIAG;
            }
            else {
                H[i] = Ft_opn;
                if (Ht[i] == Ft_opn) {
                    if (Ex[i]) {
                        HT[1LL*i*s2Len + j] |= PARASAIL_DEL;
                    }
                    else {
                        HT[1LL*i*s2Len + j] |= PARASAIL_DIAG;
                    }
                }
                else {
                    HT[1LL*i*s2Len + j] |= PARASAIL_DEL;
                }
            }
            if (0 >= H[i]) {
                H[i] = 0;
                HT[1LL*i*s2Len + j] &= PARASAIL_ZERO_MASK;
            }
            if (H[i] > score) {
                score = H[i];
                end_query = i;
                end_ref = j;
            }
        }
    }

    result->score = score;
    result->end_query = end_query;
    result->end_ref = end_ref;

    parasail_free(Ex);
    parasail_free(HtB);
    parasail_free(E);
    parasail_free(HB);
    parasail_free(s2);
    if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
        parasail_free(s1);
    }

    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
testq %rdx, %rdx
je 0x2aad85
movl %ecx, %ebx
testl %ecx, %ecx
jle 0x2aad8c
testl %r8d, %r8d
js 0x2aad93
movl %r9d, %ebp
testl %r9d, %r9d
js 0x2aad9a
movq 0xa0(%rsp), %rcx
testq %rcx, %rcx
je 0x2aada1
movq %rdi, %r14
movl %r8d, 0x44(%rsp)
movl 0x30(%rcx), %eax
testl %eax, %eax
jne 0x2aa97a
testq %r14, %r14
je 0x2aadb2
movl %esi, %r15d
testl %esi, %esi
jle 0x2aadb9
testl %eax, %eax
je 0x2aa97e
movl 0x34(%rcx), %r15d
movq %rdx, 0x20(%rsp)
movl $0x10, %edx
movl $0x1, %ecx
movl %r15d, %edi
movl %ebx, %esi
callq 0x2097d1
testq %rax, %rax
je 0x2aadbe
movl %ebp, 0x1c(%rsp)
movq %r14, 0x60(%rsp)
movq %rax, 0x48(%rsp)
orl $0x1480204, 0xc(%rax)   # imm = 0x1480204
movl %ebx, %esi
movl $0x10, %edi
movq %rsi, 0x58(%rsp)
callq 0x2094bf
movq %rax, 0x28(%rsp)
leal 0x1(%r15), %eax
movslq %eax, %r13
movl $0x10, %edi
movq %r13, %rsi
callq 0x2094bf
movq %rax, %r14
movslq %r15d, %rbp
movl $0x10, %edi
movq %rbp, %rsi
callq 0x2094bf
movq %rax, %r12
movl $0x10, %edi
movq %r13, %rsi
callq 0x2094bf
movq %rax, %r13
movl $0x10, %edi
movq %rbp, 0x8(%rsp)
movq %rbp, %rsi
callq 0x2094bf
movq %rax, 0x10(%rsp)
xorl %eax, %eax
cmpq $0x0, 0x28(%rsp)
je 0x2aadc0
testq %r14, %r14
je 0x2aadc0
testq %r12, %r12
je 0x2aadc0
testq %r13, %r13
je 0x2aadc0
cmpq $0x0, 0x10(%rsp)
je 0x2aadc0
movq %r14, %r8
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq %rax, 0x38(%rsp)
movq 0xa0(%rsp), %r11
cmpl $0x0, 0x30(%r11)
movl %r15d, %ebp
movq %r14, 0x30(%rsp)
je 0x2aad33
movq $0x0, 0x8(%rsp)
movq 0x20(%rsp), %r9
movq %r8, %r14
addq $0x4, %r14
movq 0x10(%r11), %rax
cmpl $0x2, %ebx
movl $0x1, %ecx
cmovgel %ebx, %ecx
xorl %edx, %edx
movq 0x28(%rsp), %rdi
movzbl (%r9,%rdx), %esi
movl (%rax,%rsi,4), %esi
movl %esi, (%rdi,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x2aaaab
xorl %eax, %eax
movl %eax, (%r8)
movl %eax, (%r13)
testl %r15d, %r15d
jle 0x2aaae6
leaq (,%rbp,4), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x19e090
movq 0xa0(%rsp), %r11
testl %r15d, %r15d
jle 0x2aaafd
xorl %eax, %eax
movl $0xc0000000, (%r12,%rax,4) # imm = 0xC0000000
incq %rax
cmpq %rax, %rbp
jne 0x2aaaed
cmpl $0x2, %ebx
movl $0x1, %eax
cmovgel %ebx, %eax
movq %rax, 0x20(%rsp)
movl $0xc0000000, %ecx       # imm = 0xC0000000
xorl %edx, %edx
movl %r15d, %esi
movq %r15, 0x50(%rsp)
testl %r15d, %r15d
movl 0x1c(%rsp), %eax
movl 0x44(%rsp), %r15d
jle 0x2aab67
movq 0x38(%rsp), %rdi
xorl %r8d, %r8d
movl (%r14,%r8,4), %r9d
subl %r15d, %r9d
movl (%r12,%r8,4), %r10d
subl %eax, %r10d
cmpl %r9d, %r10d
cmovlel %r9d, %r10d
movl %r10d, (%r12,%r8,4)
setge %r9b
shlb $0x3, %r9b
addb $0x8, %r9b
movb %r9b, (%rdi)
incq %r8
addq 0x58(%rsp), %rdi
cmpq %r8, %rbp
jne 0x2aab32
movl $0x0, (%r13)
cmpl $0x0, 0x50(%rsp)
movl %r15d, %eax
movq 0x28(%rsp), %r15
jle 0x2aabe7
movq 0x8(%r11), %rdi
xorl %r8d, %r8d
cmpl $0x0, 0x30(%r11)
je 0x2aab96
movl 0x18(%r11), %r9d
imull %r8d, %r9d
jmp 0x2aaba4
movq 0x8(%rsp), %r9
movl (%r9,%r8,4), %r9d
imull 0x18(%r11), %r9d
movslq (%r15,%rdx,4), %r10
movslq %r9d, %r9
addq %r10, %r9
movq 0x30(%rsp), %r10
movl (%r10,%r8,4), %r10d
addl (%rdi,%r9,4), %r10d
movl (%r12,%r8,4), %r9d
cmpl %r9d, %r10d
cmovgl %r10d, %r9d
movl %r9d, 0x4(%r13,%r8,4)
xorl %r9d, %r9d
cmpl %r10d, (%r12,%r8,4)
setg %r9b
movq 0x10(%rsp), %r10
movl %r9d, (%r10,%r8,4)
incq %r8
cmpq %r8, %rbp
jne 0x2aab85
movq 0x50(%rsp), %r15
testl %r15d, %r15d
jle 0x2aacc7
movl $0xc0000000, %r9d       # imm = 0xC0000000
movq 0x38(%rsp), %rdi
xorl %r8d, %r8d
movl %r9d, %r11d
movl (%r13,%r8,4), %r9d
subl 0x1c(%rsp), %r11d
cmpl %r11d, %r9d
cmovlel %r11d, %r9d
movl %r9d, %r10d
subl %eax, %r10d
cmpl %r11d, -0x4(%r14,%r8,4)
setle %r11b
shlb $0x5, %r11b
addb $0x20, %r11b
orb %r11b, (%rdi)
movl 0x4(%r13,%r8,4), %r11d
cmpl %r10d, %r11d
jle 0x2aac5c
movl %r11d, (%r14,%r8,4)
xorl %r10d, %r10d
movq 0x10(%rsp), %r11
cmpl $0x0, (%r11,%r8,4)
sete %r10b
leal (%r10,%r10,2), %r10d
incl %r10d
orb %r10b, (%rdi)
jmp 0x2aac83
movl %r10d, (%r14,%r8,4)
cmpl %r10d, 0x4(%r13,%r8,4)
jne 0x2aac80
movq 0x10(%rsp), %r10
cmpl $0x0, (%r10,%r8,4)
movq 0xa0(%rsp), %r11
je 0x2aacc2
orb $0x2, (%rdi)
jmp 0x2aac8b
orb $0x2, (%rdi)
movq 0xa0(%rsp), %r11
cmpl $0x0, (%r14,%r8,4)
jg 0x2aac9d
movl $0x0, (%r14,%r8,4)
andb $0x78, (%rdi)
movl (%r14,%r8,4), %r10d
cmpl %ecx, %r10d
cmovgl %r10d, %ecx
cmovgl %r8d, %esi
cmovgl %edx, %ebx
incq %r8
addq 0x58(%rsp), %rdi
cmpq %r8, %rbp
jne 0x2aac03
jmp 0x2aacc7
orb $0x4, (%rdi)
jmp 0x2aac8b
incq %rdx
incq 0x38(%rsp)
cmpq 0x20(%rsp), %rdx
jne 0x2aab1c
movq 0x48(%rsp), %r14
movl %ecx, (%r14)
movl %esi, 0x4(%r14)
movl %ebx, 0x8(%r14)
movq 0x10(%rsp), %rdi
movq %r11, %rbx
callq 0x2094b5
movq %r13, %rdi
callq 0x2094b5
movq %r12, %rdi
callq 0x2094b5
movq 0x30(%rsp), %rdi
callq 0x2094b5
movq 0x28(%rsp), %rdi
callq 0x2094b5
cmpl $0x0, 0x30(%rbx)
jne 0x2aad2b
movq 0x8(%rsp), %rdi
callq 0x2094b5
movq %r14, %rax
jmp 0x2aadc0
movl $0x10, %edi
movq 0x8(%rsp), %rsi
callq 0x2094bf
testq %rax, %rax
je 0x2aadbe
testl %r15d, %r15d
movq 0x20(%rsp), %r9
movq %rax, 0x8(%rsp)
movq 0xa0(%rsp), %r11
jle 0x2aada8
movq 0x10(%r11), %rsi
xorl %ecx, %ecx
movq 0x30(%rsp), %r8
movq 0x60(%rsp), %rdi
movzbl (%rdi,%rcx), %edx
movl (%rsi,%rdx,4), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %rbp
jne 0x2aad6e
jmp 0x2aaa8e
callq 0x1a244f
jmp 0x2aadbe
callq 0x1a2429
jmp 0x2aadbe
callq 0x1a2403
jmp 0x2aadbe
callq 0x1a23dd
jmp 0x2aadbe
callq 0x1a23b7
jmp 0x2aadbe
movq 0x30(%rsp), %r8
jmp 0x2aaa8e
callq 0x1a2391
jmp 0x2aadbe
callq 0x1a236b
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop