// File: /workspace/github_cpp_makefile_star2_O2/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 $0x858, %rsp            # imm = 0x858
movq %r9, 0x40(%rsp)
orl $-0x1, (%r9)
movq 0x890(%rsp), %rax
orl $-0x1, (%rax)
movq 0x898(%rsp), %rax
orl $-0x1, (%rax)
orl $-0x1, 0x14(%rsp)
andq $0x0, 0x258(%rsp)
movl %ecx, 0x18(%rsp)
andq $0x0, 0x2f0(%rsp)
movq %rdx, 0x48(%rsp)
andq $0x0, 0x288(%rsp)
movq %rdi, %r13
andq $0x0, 0x268(%rsp)
movq %r8, 0x20(%rsp)
leal (%r8,%r8), %eax
movq %rax, 0x38(%rsp)
leaq 0x1(%rax), %rbp
pushq $0x1
popq %rax
xorl %ecx, %ecx
cmpq %rcx, %rbp
je 0x64b98
movzbl (%r13,%rcx), %edx
orq %rax, 0x50(%rsp,%rdx,8)
addq %rax, %rax
incq %rcx
jmp 0x64b80
leaq 0x50(%rsp), %r15
movq 0x208(%r15), %r14
movq 0x218(%r15), %rbx
movq 0x2a0(%r15), %rax
movq %rax, 0x30(%rsp)
movq 0x238(%r15), %rax
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
movl $0x800, %edx            # imm = 0x800
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r14, 0x208(%r15)
movq %rbx, 0x218(%r15)
movq 0x30(%rsp), %rax
movq %rax, 0x2a0(%r15)
movq 0x28(%rsp), %rax
movq %rax, 0x238(%r15)
pushq $0x1
popq %r14
movq 0x38(%rsp), %rcx
shlq %cl, %r14
movl 0x18(%rsp), %eax
cmpl $0x2, %eax
pushq $0x1
popq %rcx
cmovgel %eax, %ecx
movq 0x20(%rsp), %rax
leal (%rax,%rax,2), %eax
movl %eax, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
leal -0x1(%rcx), %eax
movq %rax, 0x18(%rsp)
addq %rbp, %r13
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r15d, %r15d
cmpq %r12, 0x18(%rsp)
je 0x64d1e
movq 0x48(%rsp), %rax
movzbl (%rax,%r12), %eax
movq 0x50(%rsp,%rax,8), %rcx
orq %rbp, %rcx
movq %rcx, %rax
andq %rbx, %rax
addq %rbx, %rax
xorq %rbx, %rax
orq %rcx, %rax
testb $0x1, %al
jne 0x64c75
cmpl 0x30(%rsp), %r15d
jge 0x64dc7
incl %r15d
movq %rax, %rcx
andq %rbx, %rcx
orq %rax, %rbx
notq %rbx
movq %rbp, %rdx
orq %rbx, %rdx
shrq %rax
movq %rdx, %rbp
andq %rax, %rbp
orq %rdx, %rax
notq %rax
movq %rax, %rbx
orq %rcx, %rbx
leal 0x1(%r12), %edi
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rsi
movl %r15d, %edx
movq %rbx, %rcx
movq %rbp, %r8
leaq 0x14(%rsp), %r9
callq 0x64e10
movl 0x14(%rsp), %eax
cmpl $-0x1, %eax
je 0x64ceb
movq 0x898(%rsp), %rcx
movl %r12d, (%rcx)
movl 0x10(%rsp), %ecx
movq 0x890(%rsp), %rdx
movl %ecx, (%rdx)
movq 0x40(%rsp), %rcx
movl %eax, (%rcx)
shrq 0x258(%rsp)
shrq 0x268(%rsp)
shrq 0x288(%rsp)
shrq 0x2f0(%rsp)
movzbl (%r13,%r12), %eax
orq %r14, 0x50(%rsp,%rax,8)
incq %r12
jmp 0x64c37
movq 0x18(%rsp), %rax
movq 0x48(%rsp), %rcx
movzbl (%rcx,%rax), %eax
movq 0x50(%rsp,%rax,8), %rax
orq %rbp, %rax
movq %rax, %rcx
andq %rbx, %rcx
addq %rbx, %rcx
xorq %rbx, %rcx
orq %rax, %rcx
testb $0x1, %cl
jne 0x64d58
incl %r15d
movl %r15d, %eax
subl 0x38(%rsp), %eax
cmpl 0x20(%rsp), %eax
jg 0x64dc7
movq %rcx, %rax
andq %rbx, %rax
orq %rcx, %rbx
notq %rbx
orq %rbx, %rbp
shrq %rcx
movq %rbp, %r8
andq %rcx, %r8
orq %rbp, %rcx
notq %rcx
orq %rax, %rcx
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
leaq 0x14(%rsp), %rbx
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl %r15d, %edx
movq %rbx, %r9
callq 0x64e10
movl (%rbx), %eax
cmpl $-0x1, %eax
je 0x64dc7
movq 0x898(%rsp), %rcx
movq 0x18(%rsp), %rdx
movl %edx, (%rcx)
movl 0x10(%rsp), %ecx
movq 0x890(%rsp), %rdx
movl %ecx, (%rdx)
movq 0x40(%rsp), %rcx
movl %eax, (%rcx)
movq 0x898(%rsp), %rax
movl (%rax), %eax
addq $0x858, %rsp            # imm = 0x858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq