// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/./Levenshtein_distance.h
// Function: Reserve_Banded_BPM_Extension(char*, int, char*, int, unsigned short, unsigned int*, int*, int*)
inline int Reserve_Banded_BPM_Extension
(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[i]] = Peq[(uint8_t)pattern[i]] | 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[i]] | 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) = i;
			(*return_p_end) = return_site;
			(*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[i_bd]] = Peq[(uint8_t)pattern[i_bd]] | Mask;
	}





	X = Peq[(uint8_t)text[i]] | 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) = i;
		(*return_p_end) = return_site;
		(*return_err) = line_error;
	}

	return (*return_t_end);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x868, %rsp            # imm = 0x868
movl %r8d, %r14d
movq %rcx, 0x18(%rsp)
movq %rdx, 0x28(%rsp)
movq 0x8a8(%rsp), %rcx
movq 0x8a0(%rsp), %rdx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %r9, 0x20(%rsp)
movl %eax, (%r9)
movl %eax, (%rdx)
movl %eax, (%rcx)
leal (%r14,%r14), %eax
movl %eax, 0x4(%rsp)
xorl %eax, %eax
movq %rax, 0x268(%rsp)
movq %rax, 0x300(%rsp)
movq %rax, 0x298(%rsp)
movq %rax, 0x278(%rsp)
leal 0x1(,%r14,2), %ecx
movl $0x1, %edx
movzbl (%rdi,%rax), %esi
orq %rdx, 0x60(%rsp,%rsi,8)
addq %rdx, %rdx
incq %rax
cmpq %rax, %rcx
jne 0x9b48b
movq %rdi, 0x50(%rsp)
leaq 0x60(%rsp), %r15
movq 0x208(%r15), %rbx
movq 0x218(%r15), %r12
movq 0x2a0(%r15), %r13
movq 0x238(%r15), %rax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
movl $0x800, %edx            # imm = 0x800
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
movq %rbx, 0x208(%r15)
movq %r12, 0x218(%r15)
movq %r13, 0x2a0(%r15)
movq 0x8(%rsp), %rax
movq %rax, 0x238(%r15)
movl 0x4(%rsp), %ecx
movl %ecx, %edx
movl $0x1, %edi
movl $0x1, %eax
shlq %cl, %rax
movq %rax, 0x48(%rsp)
movq 0x18(%rsp), %rax
cmpl $0x2, %eax
jl 0x9b6b0
decl %eax
leal (%r14,%r14,2), %ecx
movl %ecx, 0x14(%rsp)
movzwl %r14w, %ecx
movq %rax, 0x30(%rsp)
movl %eax, %eax
movq %rax, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
leaq -0x1(%rcx), %r15
xorl %ebx, %ebx
movq %rdx, %r13
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %esi, %esi
movq 0x28(%rsp), %rax
movzbl (%rax,%rbx), %r8d
movq 0x60(%rsp,%r8,8), %r9
orq %rcx, %r9
movq %r9, %r8
andq %rsi, %r8
addq %rsi, %r8
xorq %rsi, %r8
orq %r9, %r8
testb $0x1, %r8b
movq %r13, 0x8(%rsp)
jne 0x9b588
cmpl 0x14(%rsp), %ebp
jge 0x9b7cd
incl %ebp
movq %r8, %r9
andq %rsi, %r9
orq %r8, %rsi
notq %rsi
movq %rcx, %r10
orq %rsi, %r10
shrq %r8
movq %r10, %rcx
andq %r8, %rcx
orq %r10, %r8
notq %r8
movq %r8, %rsi
orq %r9, %rsi
cmpl %r14d, %ebp
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl %ebp, %r11d
cmovgl %r10d, %r11d
movq %rbx, 0x58(%rsp)
movl %ebx, %r12d
cmovgl %r10d, %r12d
movl %r11d, %r9d
testw %r14w, %r14w
je 0x9b61d
xorl %r13d, %r13d
movl %ebp, %r8d
btq %r13, %rsi
adcl $0x0, %r8d
btq %r13, %rcx
sbbl $0x0, %r8d
cmpl %r14d, %r8d
setg %bl
cmpl %r9d, %r8d
seta %al
orb %bl, %al
leal (%rdi,%r13), %ebx
testb %al, %al
cmovel %r8d, %r11d
cmovel %r8d, %r9d
cmovel %ebx, %r12d
cmpq %r13, %r15
leaq 0x1(%r13), %rax
cmovel %r8d, %r10d
movq %rax, %r13
cmpq %rax, %rdx
jne 0x9b5da
cmpl $-0x1, %r11d
movq 0x58(%rsp), %rbx
movq 0x8(%rsp), %r13
je 0x9b660
movq 0x38(%rsp), %rax
addl %ebx, %eax
cmpl %r9d, %r10d
cmovnel %r12d, %eax
cmpl %r14d, %r10d
cmoval %r12d, %eax
movq 0x8a8(%rsp), %r8
movl %ebx, (%r8)
movq 0x8a0(%rsp), %r8
movl %eax, (%r8)
movq 0x20(%rsp), %rax
movl %r11d, (%rax)
shrq 0x268(%rsp)
shrq 0x278(%rsp)
shrq 0x298(%rsp)
shrq 0x300(%rsp)
movq 0x50(%rsp), %rax
movzbl 0x1(%rax,%r13), %eax
movq 0x48(%rsp), %r8
orq %r8, 0x60(%rsp,%rax,8)
incq %rbx
incq %r13
incq %rdi
cmpq 0x40(%rsp), %rbx
jne 0x9b550
movq 0x30(%rsp), %rax
jmp 0x9b6b6
xorl %esi, %esi
xorl %ecx, %ecx
xorl %eax, %eax
movq %rax, %r13
movl %eax, %eax
movq 0x28(%rsp), %rdi
movzbl (%rdi,%rax), %eax
movq 0x60(%rsp,%rax,8), %rdi
orq %rcx, %rdi
movq %rdi, %rax
andq %rsi, %rax
addq %rsi, %rax
xorq %rsi, %rax
orq %rdi, %rax
testb $0x1, %al
jne 0x9b6f0
incl %ebp
movl %ebp, %edi
subl 0x4(%rsp), %edi
cmpl %r14d, %edi
jg 0x9b7cd
cmpl %r14d, %ebp
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %ebp, %edi
cmovgl %r9d, %edi
movl %r13d, %r8d
cmovgl %r9d, %r8d
testw %r14w, %r14w
je 0x9b798
movq %rax, %r9
andq %rsi, %r9
orq %rax, %rsi
notq %rsi
orq %rsi, %rcx
shrq %rax
movq %rcx, %rsi
andq %rax, %rsi
orq %rcx, %rax
notq %rax
orq %r9, %rax
movzwl %r14w, %r10d
movq 0x18(%rsp), %rcx
cmpl $0x2, %ecx
movl $0x1, %r11d
cmovgel %ecx, %r11d
decq %r10
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movl %edi, %ecx
btq %rbx, %rax
adcl $0x0, %ebp
btq %rbx, %rsi
sbbl $0x0, %ebp
cmpl %r14d, %ebp
setg %r15b
cmpl %ecx, %ebp
seta %r12b
orb %r15b, %r12b
leal (%r11,%rbx), %r15d
testb %r12b, %r12b
cmovel %ebp, %edi
cmovel %ebp, %ecx
cmovel %r15d, %r8d
cmpq %rbx, %r10
leaq 0x1(%rbx), %r15
cmovel %ebp, %r9d
movq %r15, %rbx
cmpq %r15, %rdx
jne 0x9b754
jmp 0x9b79a
movl %edi, %ecx
cmpl $-0x1, %edi
je 0x9b7cd
leal (%r14,%r13), %eax
cmpl %ecx, %r9d
cmovnel %r8d, %eax
cmpl %r14d, %r9d
cmoval %r8d, %eax
movq 0x8a8(%rsp), %rcx
movl %r13d, (%rcx)
movq 0x8a0(%rsp), %rcx
movl %eax, (%rcx)
movq 0x20(%rsp), %rax
movl %edi, (%rax)
movq 0x8a8(%rsp), %rax
movl (%rax), %eax
addq $0x868, %rsp            # imm = 0x868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq