// File: /workspace/github_cpp_makefile_star2_O3/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
movl %r8d, %r15d
movq %rcx, 0x8(%rsp)
movq %rdx, 0x30(%rsp)
movq 0x8b8(%rsp), %rcx
movq 0x8b0(%rsp), %rdx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %r9, 0x28(%rsp)
movl %eax, (%r9)
movl %eax, (%rdx)
movl %eax, (%rcx)
leal (%r15,%r15), %eax
movl %eax, 0x4(%rsp)
xorl %eax, %eax
movq %rax, 0x278(%rsp)
movq %rax, 0x310(%rsp)
movq %rax, 0x2a8(%rsp)
movq %rax, 0x288(%rsp)
leal 0x1(,%r15,2), %eax
movq %rsi, 0x10(%rsp)
leal -0x1(%rsi), %ecx
movl $0x1, %edx
movslq %ecx, %rcx
movzbl (%rdi,%rcx), %esi
orq %rdx, 0x70(%rsp,%rsi,8)
addq %rdx, %rdx
decl %ecx
decq %rax
jne 0x9c7cf
movq %rdi, 0x58(%rsp)
leaq 0x70(%rsp), %r12
movq 0x208(%r12), %r14
movq 0x218(%r12), %r13
movq 0x2a0(%r12), %rbp
movq 0x238(%r12), %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
movl $0x800, %edx            # imm = 0x800
movq %r12, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r14, 0x208(%r12)
movq %r13, 0x218(%r12)
movq %rbp, 0x2a0(%r12)
movq 0x18(%rsp), %rax
movq %rax, 0x238(%r12)
movl 0x4(%rsp), %ecx
movl %ecx, %edx
movl $0x1, %edi
movl $0x1, %eax
shlq %cl, %rax
movq %rax, 0x50(%rsp)
movq 0x8(%rsp), %r8
cmpl $0x2, %r8d
jl 0x9ca3a
leal -0x1(%r8), %ecx
leal (%r15,%r15,2), %eax
movl %eax, 0x20(%rsp)
movzwl %r15w, %esi
movq 0x10(%rsp), %rax
addl $-0x2, %eax
movl %eax, 0x24(%rsp)
movl %r8d, %eax
movq %rax, 0x48(%rsp)
movl %ecx, (%rsp)
movl %ecx, %eax
movq %rax, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
leaq -0x1(%rsi), %r14
xorl %r13d, %r13d
movq %rdx, %r9
xorl %ebx, %ebx
xorl %ecx, %ecx
xorl %esi, %esi
movq %r13, %r10
notq %r10
addq 0x48(%rsp), %r10
movq 0x30(%rsp), %rax
movzbl (%rax,%r10), %eax
movq 0x70(%rsp,%rax,8), %r8
orq %rcx, %r8
movq %r8, %rax
andq %rsi, %rax
addq %rsi, %rax
xorq %rsi, %rax
orq %r8, %rax
testb $0x1, %al
movq %r9, 0x18(%rsp)
movq %r10, 0x60(%rsp)
jne 0x9c8fa
cmpl 0x20(%rsp), %ebx
jge 0x9cb6a
incl %ebx
movq %rax, %r8
andq %rsi, %r8
orq %rax, %rsi
notq %rsi
movq %rcx, %r10
orq %rsi, %r10
shrq %rax
movq %r10, %rcx
andq %rax, %rcx
orq %r10, %rax
notq %rax
movq %rax, %rsi
orq %r8, %rsi
cmpl %r15d, %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %ebx, %r8d
cmovgl %eax, %r8d
movq %r13, 0x68(%rsp)
movl %r13d, %r12d
cmovgl %eax, %r12d
movl %r8d, %r11d
testw %r15w, %r15w
je 0x9c98e
xorl %r10d, %r10d
movl %ebx, %ebp
btq %r10, %rsi
adcl $0x0, %ebp
btq %r10, %rcx
sbbl $0x0, %ebp
cmpl %r15d, %ebp
setg %r13b
cmpl %r11d, %ebp
seta %r9b
orb %r13b, %r9b
leal (%rdi,%r10), %r13d
testb %r9b, %r9b
cmovel %ebp, %r8d
cmovel %ebp, %r11d
cmovel %r13d, %r12d
cmpq %r10, %r14
leaq 0x1(%r10), %r9
cmovel %ebp, %eax
movq %r9, %r10
cmpq %r9, %rdx
jne 0x9c94a
cmpl $-0x1, %r8d
movq 0x68(%rsp), %r13
je 0x9c9da
movq 0x38(%rsp), %r9
addl %r13d, %r9d
cmpl %r11d, %eax
cmovnel %r12d, %r9d
cmpl %r15d, %eax
cmoval %r12d, %r9d
movq 0x8b8(%rsp), %rax
movq 0x60(%rsp), %r10
movl %r10d, (%rax)
notl %r9d
addl 0x10(%rsp), %r9d
movq 0x8b0(%rsp), %rax
movl %r9d, (%rax)
movq 0x28(%rsp), %rax
movl %r8d, (%rax)
shrq 0x278(%rsp)
shrq 0x288(%rsp)
shrq 0x2a8(%rsp)
shrq 0x310(%rsp)
incq %r13
movl 0x24(%rsp), %eax
movq 0x18(%rsp), %r9
subl %r9d, %eax
cltq
movq 0x58(%rsp), %r8
movzbl (%r8,%rax), %eax
movq 0x50(%rsp), %r8
orq %r8, 0x70(%rsp,%rax,8)
incq %r9
incq %rdi
cmpq 0x40(%rsp), %r13
jne 0x9c8b4
movl (%rsp), %eax
movq 0x8(%rsp), %r8
jmp 0x9ca40
xorl %esi, %esi
xorl %ecx, %ecx
xorl %eax, %eax
movl %eax, (%rsp)
notl %eax
addl %r8d, %eax
movslq %eax, %rdi
movq 0x30(%rsp), %r8
movzbl (%r8,%rdi), %edi
movq 0x70(%rsp,%rdi,8), %rdi
orq %rcx, %rdi
movq %rdi, %r8
andq %rsi, %r8
addq %rsi, %r8
xorq %rsi, %r8
orq %rdi, %r8
testb $0x1, %r8b
jne 0x9ca83
incl %ebx
movl %ebx, %edi
subl 0x4(%rsp), %edi
cmpl %r15d, %edi
jg 0x9cb6a
cmpl %r15d, %ebx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl %ebx, %edi
cmovgl %r10d, %edi
movl (%rsp), %r9d
cmovgl %r10d, %r9d
testw %r15w, %r15w
je 0x9cb2e
movq %r8, %r10
andq %rsi, %r10
orq %r8, %rsi
notq %rsi
orq %rsi, %rcx
shrq %r8
movq %rcx, %rsi
andq %r8, %rsi
orq %rcx, %r8
notq %r8
orq %r10, %r8
movzwl %r15w, %r11d
movq 0x8(%rsp), %rcx
cmpl $0x2, %ecx
movl $0x1, %r14d
cmovgel %ecx, %r14d
decq %r11
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
xorl %r12d, %r12d
movl %edi, %ecx
btq %r12, %r8
adcl $0x0, %ebx
btq %r12, %rsi
sbbl $0x0, %ebx
cmpl %r15d, %ebx
setg %bpl
cmpl %ecx, %ebx
seta %r13b
orb %bpl, %r13b
leal (%r14,%r12), %ebp
testb %r13b, %r13b
cmovel %ebx, %edi
cmovel %ebx, %ecx
cmovel %ebp, %r9d
cmpq %r12, %r11
leaq 0x1(%r12), %r13
cmovel %ebx, %r10d
movq %r13, %r12
cmpq %r13, %rdx
jne 0x9cae9
jmp 0x9cb30
movl %edi, %ecx
cmpl $-0x1, %edi
je 0x9cb6a
movl (%rsp), %edx
addl %r15d, %edx
cmpl %ecx, %r10d
cmovnel %r9d, %edx
cmpl %r15d, %r10d
cmoval %r9d, %edx
movq 0x8b8(%rsp), %rcx
movl %eax, (%rcx)
notl %edx
addl 0x10(%rsp), %edx
movq 0x8b0(%rsp), %rax
movl %edx, (%rax)
movq 0x28(%rsp), %rax
movl %edi, (%rax)
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