// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/./Levenshtein_distance.h
// Function: Reserve_Banded_BPM_Extension_REV(char*, int, char*, int, unsigned short, unsigned int*, int*, int*)
inline int Reserve_Banded_BPM_Extension_REV
(char *pattern, int p_length, char *text, int t_length, unsigned short errthold, 
unsigned int* return_err, int* return_p_end, int* return_t_end)
{
    (*return_err) = (unsigned int)-1;
    (*return_p_end) = -1;
    (*return_t_end) = -1;

    Word Peq[256];

    unsigned int line_error = (unsigned int)-1;
    int return_site;
    int band_length = (errthold << 1) + 1;
    int i = 0;
    Word tmp_Peq_1 = (Word)1;

    Peq[(uint8_t)'A'] = (Word)0;
    Peq[(uint8_t)'T'] = (Word)0;
    Peq[(uint8_t)'G'] = (Word)0;
    Peq[(uint8_t)'C'] = (Word)0;


    Word Peq_A;
    Word Peq_T;
    Word Peq_C;
    Word Peq_G;

    ///band_length = 2k + 1
    for (i = 0; i<band_length; i++)
    {
        Peq[(uint8_t)pattern[p_length-i-1]] = Peq[(uint8_t)pattern[p_length-i-1]] | tmp_Peq_1;
        tmp_Peq_1 = tmp_Peq_1 << 1;
    }

    Peq_A = Peq[(uint8_t)'A'];
    Peq_C = Peq[(uint8_t)'C'];
    Peq_T = Peq[(uint8_t)'T'];
    Peq_G = Peq[(uint8_t)'G'];


    memset(Peq, 0, sizeof(Word)* 256);


    Peq[(uint8_t)'A'] = Peq_A;
    Peq[(uint8_t)'C'] = Peq_C;
    Peq[(uint8_t)'T'] = Peq_T;
    Peq[(uint8_t)'G'] = Peq_G;


    

    Word Mask = ((Word)1 << (errthold << 1));

    Word VP = 0;
    Word VN = 0;
    Word X = 0;
    Word D0 = 0;
    Word HN = 0;
    Word HP = 0;


    i = 0;

    int err = 0;

    Word err_mask = (Word)1;

    int i_bd = (errthold << 1);


    int last_high = (errthold << 1);

    int t_length_1 = t_length - 1;

    while (i<t_length_1)
    {
        X = Peq[(uint8_t)text[t_length-i-1]] | VN;

        D0 = ((VP + (X&VP)) ^ VP) | X;

        HN = VP&D0;
        HP = VN | ~(VP | D0);

        X = D0 >> 1;
        VN = X&HP;
        VP = HN | ~(X | HP);

        if (!(D0&err_mask))
        {
            ++err;
            if ((err - last_high)>errthold)
            {
                return (*return_t_end);
            }
        }
        get_error(i + 1, errthold, err, VP, VN, &line_error, &return_site);
        if(line_error != (unsigned int)-1)
        {
            (*return_t_end) = t_length-i-1;
            (*return_p_end) = p_length-return_site-1;
            (*return_err) = line_error;
        }

        Peq[(uint8_t)'A'] = Peq[(uint8_t)'A'] >> 1;
        Peq[(uint8_t)'C'] = Peq[(uint8_t)'C'] >> 1;
        Peq[(uint8_t)'G'] = Peq[(uint8_t)'G'] >> 1;
        Peq[(uint8_t)'T'] = Peq[(uint8_t)'T'] >> 1;


        ++i;
        ++i_bd;
        Peq[(uint8_t)pattern[p_length-i_bd-1]] = Peq[(uint8_t)pattern[p_length-i_bd-1]] | Mask;
    }





    X = Peq[(uint8_t)text[t_length-i-1]] | VN;
    D0 = ((VP + (X&VP)) ^ VP) | X;
    HN = VP&D0;
    HP = VN | ~(VP | D0);
    X = D0 >> 1;
    VN = X&HP;
    VP = HN | ~(X | HP);
    if (!(D0&err_mask))
    {
        ++err;
        if ((err - last_high)>errthold)
        {
            return (*return_t_end);
        }
    }
    ///i = t_length - 1
    get_error(i + 1, errthold, err, VP, VN, &line_error, &return_site);
    if(line_error != (unsigned int)-1)
    {
        (*return_t_end) = t_length-i-1;
        (*return_p_end) = p_length-return_site-1;
        (*return_err) = line_error;
    }

    return (*return_t_end);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x878, %rsp            # imm = 0x878
movq %r9, 0x48(%rsp)
orl $-0x1, (%r9)
movq 0x8b0(%rsp), %rax
orl $-0x1, (%rax)
movq 0x8b8(%rsp), %rax
orl $-0x1, (%rax)
orl $-0x1, 0x18(%rsp)
movq %rcx, 0x40(%rsp)
andq $0x0, 0x278(%rsp)
movq %rdx, 0x50(%rsp)
andq $0x0, 0x310(%rsp)
andq $0x0, 0x2a8(%rsp)
andq $0x0, 0x288(%rsp)
leal (%r8,%r8), %eax
movl %eax, 0x1c(%rsp)
movq %r8, 0x20(%rsp)
leal 0x1(,%r8,2), %eax
movq %rsi, 0x28(%rsp)
leal -0x1(%rsi), %ecx
pushq $0x1
popq %rdx
subq $0x1, %rax
jb 0x65d0d
movslq %ecx, %rcx
movzbl (%rdi,%rcx), %esi
orq %rdx, 0x70(%rsp,%rsi,8)
addq %rdx, %rdx
decl %ecx
jmp 0x65cf4
movq %rdi, 0x60(%rsp)
leaq 0x70(%rsp), %rbx
movq 0x208(%rbx), %r14
movq 0x218(%rbx), %r15
movq 0x2a0(%rbx), %r13
movq 0x238(%rbx), %rbp
xorl %r12d, %r12d
movl $0x800, %edx            # imm = 0x800
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r14, 0x208(%rbx)
movq %r15, 0x218(%rbx)
movq %r13, 0x2a0(%rbx)
pushq $0x1
popq %rax
movl 0x1c(%rsp), %ecx
shlq %cl, %rax
movq %rax, 0x58(%rsp)
movq %rbp, 0x238(%rbx)
movq 0x20(%rsp), %rax
leal (%rax,%rax,2), %eax
movl %eax, 0x34(%rsp)
movq 0x40(%rsp), %rax
cmpl $0x2, %eax
pushq $0x1
popq %rdx
cmovgel %eax, %edx
movq %rdx, 0x38(%rsp)
decl %edx
movq %rdx, 0x68(%rsp)
leal -0x1(%rax), %r13d
movq 0x28(%rsp), %rbp
subl %ecx, %ebp
addl $-0x2, %ebp
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %r14d, %r14d
cmpq %r12, 0x68(%rsp)
je 0x65eab
movslq %r13d, %rax
movq 0x50(%rsp), %rcx
movzbl (%rcx,%rax), %eax
movq 0x70(%rsp,%rax,8), %rcx
orq %rbx, %rcx
movq %rcx, %rax
andq %r15, %rax
addq %r15, %rax
xorq %r15, %rax
orq %rcx, %rax
testb $0x1, %al
jne 0x65dec
cmpl 0x34(%rsp), %r14d
jge 0x65f5f
incl %r14d
movq %rax, %rcx
andq %r15, %rcx
orq %rax, %r15
notq %r15
movq %rbx, %rdx
orq %r15, %rdx
shrq %rax
movq %rdx, %rbx
andq %rax, %rbx
orq %rdx, %rax
notq %rax
movq %rax, %r15
orq %rcx, %r15
leal 0x1(%r12), %edi
leaq 0x14(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rsi
movl %r14d, %edx
movq %r15, %rcx
movq %rbx, %r8
leaq 0x18(%rsp), %r9
callq 0x64e10
movl 0x18(%rsp), %eax
cmpl $-0x1, %eax
je 0x65e68
movq 0x8b8(%rsp), %rcx
movl %r13d, (%rcx)
movl 0x14(%rsp), %ecx
notl %ecx
addl 0x28(%rsp), %ecx
movq 0x8b0(%rsp), %rdx
movl %ecx, (%rdx)
movq 0x48(%rsp), %rcx
movl %eax, (%rcx)
shrq 0x278(%rsp)
shrq 0x288(%rsp)
shrq 0x2a8(%rsp)
shrq 0x310(%rsp)
movslq %ebp, %rbp
movq 0x60(%rsp), %rax
movzbl (%rax,%rbp), %eax
movq 0x58(%rsp), %rcx
orq %rcx, 0x70(%rsp,%rax,8)
decl %r13d
decl %ebp
incq %r12
jmp 0x65dac
movq 0x40(%rsp), %rax
subl 0x38(%rsp), %eax
movq %rax, %r12
cltq
movq 0x50(%rsp), %rcx
movzbl (%rcx,%rax), %eax
movq 0x70(%rsp,%rax,8), %rax
orq %rbx, %rax
movq %rax, %rcx
andq %r15, %rcx
addq %r15, %rcx
xorq %r15, %rcx
orq %rax, %rcx
testb $0x1, %cl
jne 0x65eee
incl %r14d
movl %r14d, %eax
subl 0x1c(%rsp), %eax
cmpl 0x20(%rsp), %eax
jg 0x65f5f
movq %rcx, %rax
andq %r15, %rax
orq %rcx, %r15
notq %r15
orq %r15, %rbx
shrq %rcx
movq %rbx, %r8
andq %rcx, %r8
orq %rbx, %rcx
notq %rcx
orq %rax, %rcx
leaq 0x14(%rsp), %rax
movq %rax, (%rsp)
leaq 0x18(%rsp), %rbx
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl %r14d, %edx
movq %rbx, %r9
callq 0x64e10
movl (%rbx), %eax
cmpl $-0x1, %eax
je 0x65f5f
movq 0x8b8(%rsp), %rcx
movl %r12d, (%rcx)
movl 0x14(%rsp), %ecx
notl %ecx
addl 0x28(%rsp), %ecx
movq 0x8b0(%rsp), %rdx
movl %ecx, (%rdx)
movq 0x48(%rsp), %rcx
movl %eax, (%rcx)
movq 0x8b8(%rsp), %rax
movl (%rax), %eax
addq $0x878, %rsp            # imm = 0x878
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq