// File: 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
movq %rsp, %rbp
subq $0x8d0, %rsp            # imm = 0x8D0
movw %r8w, %ax
movq 0x18(%rbp), %r8
movq 0x10(%rbp), %r8
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movw %ax, -0x26(%rbp)
movq %r9, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x10(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0xffffffff, -0x834(%rbp) # imm = 0xFFFFFFFF
movzwl -0x26(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x83c(%rbp)
movl $0x0, -0x840(%rbp)
movq $0x1, -0x848(%rbp)
movq $0x0, -0x628(%rbp)
movq $0x0, -0x590(%rbp)
movq $0x0, -0x5f8(%rbp)
movq $0x0, -0x618(%rbp)
movl $0x0, -0x840(%rbp)
movl -0x840(%rbp), %eax
cmpl -0x83c(%rbp), %eax
jge 0xc73d4
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl -0x840(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x830(%rbp,%rax,8), %rcx
orq -0x848(%rbp), %rcx
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %edx
subl -0x840(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movzbl (%rax,%rdx), %eax
movq %rcx, -0x830(%rbp,%rax,8)
movq -0x848(%rbp), %rax
shlq %rax
movq %rax, -0x848(%rbp)
movl -0x840(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x840(%rbp)
jmp 0xc735f
movq -0x628(%rbp), %rax
movq %rax, -0x850(%rbp)
movq -0x618(%rbp), %rax
movq %rax, -0x860(%rbp)
movq -0x590(%rbp), %rax
movq %rax, -0x858(%rbp)
movq -0x5f8(%rbp), %rax
movq %rax, -0x868(%rbp)
leaq -0x830(%rbp), %rdi
xorl %esi, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x2100
movq -0x850(%rbp), %rax
movq %rax, -0x628(%rbp)
movq -0x860(%rbp), %rax
movq %rax, -0x618(%rbp)
movq -0x858(%rbp), %rax
movq %rax, -0x590(%rbp)
movq -0x868(%rbp), %rax
movq %rax, -0x5f8(%rbp)
movzwl -0x26(%rbp), %eax
shll %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x870(%rbp)
movq $0x0, -0x878(%rbp)
movq $0x0, -0x880(%rbp)
movq $0x0, -0x888(%rbp)
movq $0x0, -0x890(%rbp)
movq $0x0, -0x898(%rbp)
movq $0x0, -0x8a0(%rbp)
movl $0x0, -0x840(%rbp)
movl $0x0, -0x8a4(%rbp)
movq $0x1, -0x8b0(%rbp)
movzwl -0x26(%rbp), %eax
shll %eax
movl %eax, -0x8b4(%rbp)
movzwl -0x26(%rbp), %eax
shll %eax
movl %eax, -0x8b8(%rbp)
movl -0x24(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x8bc(%rbp)
movl -0x840(%rbp), %eax
cmpl -0x8bc(%rbp), %eax
jge 0xc7746
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
subl -0x840(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x830(%rbp,%rax,8), %rax
orq -0x880(%rbp), %rax
movq %rax, -0x888(%rbp)
movq -0x878(%rbp), %rax
movq -0x888(%rbp), %rcx
andq -0x878(%rbp), %rcx
addq %rcx, %rax
xorq -0x878(%rbp), %rax
orq -0x888(%rbp), %rax
movq %rax, -0x890(%rbp)
movq -0x878(%rbp), %rax
andq -0x890(%rbp), %rax
movq %rax, -0x898(%rbp)
movq -0x880(%rbp), %rax
movq -0x878(%rbp), %rcx
orq -0x890(%rbp), %rcx
xorq $-0x1, %rcx
orq %rcx, %rax
movq %rax, -0x8a0(%rbp)
movq -0x890(%rbp), %rax
shrq %rax
movq %rax, -0x888(%rbp)
movq -0x888(%rbp), %rax
andq -0x8a0(%rbp), %rax
movq %rax, -0x880(%rbp)
movq -0x898(%rbp), %rax
movq -0x888(%rbp), %rcx
orq -0x8a0(%rbp), %rcx
xorq $-0x1, %rcx
orq %rcx, %rax
movq %rax, -0x878(%rbp)
movq -0x890(%rbp), %rax
andq -0x8b0(%rbp), %rax
cmpq $0x0, %rax
jne 0xc7629
movl -0x8a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8a4(%rbp)
movl -0x8a4(%rbp), %eax
subl -0x8b8(%rbp), %eax
movzwl -0x26(%rbp), %ecx
cmpl %ecx, %eax
jle 0xc7627
movq 0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0xc78df
jmp 0xc7629
movl -0x840(%rbp), %edi
addl $0x1, %edi
movzwl -0x26(%rbp), %esi
movl -0x8a4(%rbp), %edx
movq -0x878(%rbp), %rcx
movq -0x880(%rbp), %r8
leaq -0x834(%rbp), %r9
leaq -0x838(%rbp), %rax
movq %rax, (%rsp)
callq 0xc5b40
cmpl $-0x1, -0x834(%rbp)
je 0xc769a
movl -0x24(%rbp), %ecx
subl -0x840(%rbp), %ecx
subl $0x1, %ecx
movq 0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x14(%rbp), %ecx
subl -0x838(%rbp), %ecx
subl $0x1, %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movl -0x834(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x628(%rbp), %rax
shrq %rax
movq %rax, -0x628(%rbp)
movq -0x618(%rbp), %rax
shrq %rax
movq %rax, -0x618(%rbp)
movq -0x5f8(%rbp), %rax
shrq %rax
movq %rax, -0x5f8(%rbp)
movq -0x590(%rbp), %rax
shrq %rax
movq %rax, -0x590(%rbp)
movl -0x840(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x840(%rbp)
movl -0x8b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8b4(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl -0x8b4(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x830(%rbp,%rax,8), %rcx
orq -0x870(%rbp), %rcx
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %edx
subl -0x8b4(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movzbl (%rax,%rdx), %eax
movq %rcx, -0x830(%rbp,%rax,8)
jmp 0xc74f5
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
subl -0x840(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x830(%rbp,%rax,8), %rax
orq -0x880(%rbp), %rax
movq %rax, -0x888(%rbp)
movq -0x878(%rbp), %rax
movq -0x888(%rbp), %rcx
andq -0x878(%rbp), %rcx
addq %rcx, %rax
xorq -0x878(%rbp), %rax
orq -0x888(%rbp), %rax
movq %rax, -0x890(%rbp)
movq -0x878(%rbp), %rax
andq -0x890(%rbp), %rax
movq %rax, -0x898(%rbp)
movq -0x880(%rbp), %rax
movq -0x878(%rbp), %rcx
orq -0x890(%rbp), %rcx
xorq $-0x1, %rcx
orq %rcx, %rax
movq %rax, -0x8a0(%rbp)
movq -0x890(%rbp), %rax
shrq %rax
movq %rax, -0x888(%rbp)
movq -0x888(%rbp), %rax
andq -0x8a0(%rbp), %rax
movq %rax, -0x880(%rbp)
movq -0x898(%rbp), %rax
movq -0x888(%rbp), %rcx
orq -0x8a0(%rbp), %rcx
xorq $-0x1, %rcx
orq %rcx, %rax
movq %rax, -0x878(%rbp)
movq -0x890(%rbp), %rax
andq -0x8b0(%rbp), %rax
cmpq $0x0, %rax
jne 0xc7865
movl -0x8a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8a4(%rbp)
movl -0x8a4(%rbp), %eax
subl -0x8b8(%rbp), %eax
movzwl -0x26(%rbp), %ecx
cmpl %ecx, %eax
jle 0xc7863
movq 0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0xc78df
jmp 0xc7865
movl -0x840(%rbp), %edi
addl $0x1, %edi
movzwl -0x26(%rbp), %esi
movl -0x8a4(%rbp), %edx
movq -0x878(%rbp), %rcx
movq -0x880(%rbp), %r8
leaq -0x834(%rbp), %r9
leaq -0x838(%rbp), %rax
movq %rax, (%rsp)
callq 0xc5b40
cmpl $-0x1, -0x834(%rbp)
je 0xc78d6
movl -0x24(%rbp), %ecx
subl -0x840(%rbp), %ecx
subl $0x1, %ecx
movq 0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x14(%rbp), %ecx
subl -0x838(%rbp), %ecx
subl $0x1, %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movl -0x834(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq 0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x8d0, %rsp            # imm = 0x8D0
popq %rbp
retq
nopl (%rax,%rax)