// File: /workspace/github_cpp_makefile_star2_O1/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 %ecx, %r15d
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 (%r8,%r8), %eax
movl %eax, 0x4(%rsp)
xorl %eax, %eax
movq %rax, 0x278(%rsp)
movq %rax, 0x310(%rsp)
movq %rax, 0x2a8(%rsp)
movq %rax, 0x288(%rsp)
movq %r8, 0x8(%rsp)
leal 0x1(,%r8,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 0x9c0b1
movq %rdi, 0x60(%rsp)
leaq 0x70(%rsp), %r13
movq 0x208(%r13), %r14
movq 0x218(%r13), %r12
movq 0x2a0(%r13), %rbp
movq 0x238(%r13), %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
movl $0x800, %edx            # imm = 0x800
movq %r13, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r14, 0x208(%r13)
movq %r12, 0x218(%r13)
movq %rbp, 0x2a0(%r13)
movq 0x18(%rsp), %rax
movq %rax, 0x238(%r13)
movl $0x1, %esi
movl $0x1, %eax
movl 0x4(%rsp), %ecx
shlq %cl, %rax
movq %rax, 0x58(%rsp)
cmpl $0x2, %r15d
movq %r15, 0x38(%rsp)
jl 0x9c30b
movq 0x8(%rsp), %rax
leal (%rax,%rax,2), %edx
movl %edx, 0x20(%rsp)
leal -0x1(%r15), %edx
movq %rdx, 0x48(%rsp)
movzwl %ax, %edx
cmpl $0x2, %ecx
movl $0x1, %r11d
cmovgel %ecx, %r11d
movq %rax, %rcx
movq 0x10(%rsp), %rax
addl $-0x2, %eax
movl %eax, 0x24(%rsp)
movl %r15d, %eax
movq %rax, 0x50(%rsp)
movl %edx, %r10d
addl %r10d, %r10d
movq %rdx, 0x40(%rsp)
leaq -0x1(%rdx), %r8
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %edx, %edx
movq %r12, %rax
notq %rax
addq 0x50(%rsp), %rax
movq 0x30(%rsp), %rdi
movzbl (%rdi,%rax), %edi
movq 0x70(%rsp,%rdi,8), %r9
orq %r15, %r9
movq %r9, %rdi
andq %rdx, %rdi
addq %rdx, %rdi
xorq %rdx, %rdi
orq %r9, %rdi
testb $0x1, %dil
movq %r10, 0x18(%rsp)
jne 0x9c1e0
cmpl 0x20(%rsp), %ebx
jge 0x9c44d
incl %ebx
movq %rdi, %r9
andq %rdx, %r9
orq %rdi, %rdx
notq %rdx
movq %r15, %r14
orq %rdx, %r14
shrq %rdi
movq %r14, %r15
andq %rdi, %r15
orq %r14, %rdi
notq %rdi
movq %rdi, %rdx
orq %r9, %rdx
cmpl %ecx, %ebx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl %ebx, %r9d
cmovgl %r13d, %r9d
movq %r12, 0x68(%rsp)
movl %r12d, %r14d
cmovgl %r13d, %r14d
testw %cx, %cx
je 0x9c26b
xorl %ebp, %ebp
movl %ebx, %edi
btq %rbp, %rdx
adcl $0x0, %edi
btq %rbp, %r15
sbbl $0x0, %edi
cmpl %ecx, %edi
setg %r10b
cmpl %r9d, %edi
seta %r12b
orb %r10b, %r12b
leal (%rsi,%rbp), %r10d
testb %r12b, %r12b
cmovel %edi, %r9d
cmovel %r10d, %r14d
cmpq %rbp, %r8
leaq 0x1(%rbp), %r10
cmovel %edi, %r13d
movq %r10, %rbp
cmpq %r10, %r11
jne 0x9c22b
cmpl $-0x1, %r9d
movq 0x68(%rsp), %r12
je 0x9c2af
movq 0x40(%rsp), %rdi
addl %r12d, %edi
cmpl %r9d, %r13d
cmovnel %r14d, %edi
cmpl %ecx, %r13d
cmoval %r14d, %edi
movq 0x8b8(%rsp), %r10
movl %eax, (%r10)
notl %edi
addl 0x10(%rsp), %edi
movq 0x8b0(%rsp), %rax
movl %edi, (%rax)
movq 0x28(%rsp), %rax
movl %r9d, (%rax)
shrq 0x278(%rsp)
shrq 0x288(%rsp)
shrq 0x2a8(%rsp)
shrq 0x310(%rsp)
incq %r12
movl 0x24(%rsp), %eax
movq 0x18(%rsp), %r9
subl %r9d, %eax
cltq
movq 0x60(%rsp), %rdi
movzbl (%rdi,%rax), %eax
movq 0x58(%rsp), %rdi
orq %rdi, 0x70(%rsp,%rax,8)
leaq 0x1(%r9), %r10
incq %rsi
cmpq 0x48(%rsp), %r12
jne 0x9c19e
movl 0x4(%rsp), %ecx
jmp 0x9c313
xorl %edx, %edx
xorl %r15d, %r15d
xorl %r12d, %r12d
movl %r12d, %esi
notl %esi
addl 0x38(%rsp), %esi
movslq %esi, %rax
movq 0x30(%rsp), %rdi
movzbl (%rdi,%rax), %eax
movq 0x70(%rsp,%rax,8), %rax
orq %r15, %rax
movq %rax, %rdi
andq %rdx, %rdi
addq %rdx, %rdi
xorq %rdx, %rdi
orq %rax, %rdi
testb $0x1, %dil
jne 0x9c359
incl %ebx
movl %ebx, %eax
subl %ecx, %eax
movq 0x8(%rsp), %rcx
cmpl %ecx, %eax
jle 0x9c35e
jmp 0x9c44d
movq 0x8(%rsp), %rcx
cmpl %ecx, %ebx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %ebx, %eax
cmovgl %r9d, %eax
movl %r12d, %r8d
cmovgl %r9d, %r8d
testw %cx, %cx
je 0x9c40d
movq %rdi, %r9
andq %rdx, %r9
orq %rdi, %rdx
notq %rdx
orq %rdx, %r15
shrq %rdi
movq %r15, %rdx
andq %rdi, %rdx
orq %r15, %rdi
notq %rdi
orq %r9, %rdi
movq %rcx, %r15
movzwl %r15w, %ecx
movl 0x4(%rsp), %r9d
cmpl $0x2, %r9d
movl $0x1, %r10d
cmovgel %r9d, %r10d
movq %r12, %r13
movl %r12d, %r11d
incq %r11
decq %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
btq %r14, %rdi
adcl $0x0, %ebx
btq %r14, %rdx
sbbl $0x0, %ebx
cmpl %r15d, %ebx
setg %bpl
cmpl %eax, %ebx
seta %r12b
orb %bpl, %r12b
leal (%r11,%r14), %ebp
testb %r12b, %r12b
cmovel %ebx, %eax
cmovel %ebp, %r8d
cmpq %r14, %rcx
leaq 0x1(%r14), %r12
cmovel %ebx, %r9d
movq %r12, %r14
cmpq %r12, %r10
jne 0x9c3cc
jmp 0x9c413
movq %r12, %r13
movq %rcx, %r15
cmpl $-0x1, %eax
je 0x9c44d
addl %r15d, %r13d
cmpl %eax, %r9d
cmovnel %r8d, %r13d
cmpl %r15d, %r9d
cmoval %r8d, %r13d
movq 0x8b8(%rsp), %rcx
movl %esi, (%rcx)
notl %r13d
addl 0x10(%rsp), %r13d
movq 0x8b0(%rsp), %rcx
movl %r13d, (%rcx)
movq 0x28(%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