// File: /workspace/github_cpp_makefile_star2_O1/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
movl %r8d, %r14d
movl %ecx, %r13d
movq %rdx, 0x30(%rsp)
movq 0x898(%rsp), %rcx
movq 0x890(%rsp), %rdx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %r9, 0x28(%rsp)
movl %eax, (%r9)
movl %eax, (%rdx)
movl %eax, (%rcx)
leal (%r14,%r14), %eax
movl %eax, 0xc(%rsp)
xorl %eax, %eax
movq %rax, 0x258(%rsp)
movq %rax, 0x2f0(%rsp)
movq %rax, 0x288(%rsp)
movq %rax, 0x268(%rsp)
leal 0x1(,%r14,2), %ecx
movl $0x1, %edx
movzbl (%rdi,%rax), %esi
orq %rdx, 0x50(%rsp,%rsi,8)
addq %rdx, %rdx
incq %rax
cmpq %rax, %rcx
jne 0x9ad61
movq %rdi, 0x48(%rsp)
leaq 0x50(%rsp), %rbp
movq 0x208(%rbp), %r15
movq 0x218(%rbp), %r12
movq 0x2a0(%rbp), %rax
movq %rax, 0x18(%rsp)
movq 0x238(%rbp), %rax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
movl $0x800, %edx            # imm = 0x800
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r15, 0x208(%rbp)
movq %r12, 0x218(%rbp)
movq 0x18(%rsp), %rax
movq %rax, 0x2a0(%rbp)
movq 0x10(%rsp), %rax
movq %rax, 0x238(%rbp)
movl $0x1, %esi
movl $0x1, %eax
movl 0xc(%rsp), %ecx
shlq %cl, %rax
movq %rax, 0x10(%rsp)
cmpl $0x2, %r13d
jl 0x9af85
leal (%r14,%r14,2), %eax
movl %eax, 0x24(%rsp)
decl %r13d
cmpl $0x2, %ecx
movl $0x1, %r9d
cmovgel %ecx, %r9d
movzwl %r14w, %eax
movl %eax, %ecx
addl %ecx, %ecx
movq %rax, 0x38(%rsp)
leaq -0x1(%rax), %rbp
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %r11d, %r11d
xorl %edx, %edx
movq %r13, 0x40(%rsp)
movq 0x30(%rsp), %rax
movzbl (%rax,%r12), %edi
movq 0x50(%rsp,%rdi,8), %r8
orq %r11, %r8
movq %r8, %rdi
andq %rdx, %rdi
addq %rdx, %rdi
xorq %rdx, %rdi
orq %r8, %rdi
testb $0x1, %dil
jne 0x9ae67
cmpl 0x24(%rsp), %ebx
jge 0x9b096
incl %ebx
movq %rdi, %r8
andq %rdx, %r8
orq %rdi, %rdx
notq %rdx
movq %r11, %r10
orq %rdx, %r10
shrq %rdi
movq %r10, %r11
andq %rdi, %r11
orq %r10, %rdi
notq %rdi
movq %rdi, %rdx
orq %r8, %rdx
cmpl %r14d, %ebx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl %ebx, %r8d
cmovgl %r10d, %r8d
movq %r12, 0x18(%rsp)
movl %r12d, %r15d
cmovgl %r10d, %r15d
testw %r14w, %r14w
je 0x9aef6
xorl %edi, %edi
movl %ebx, %r12d
btq %rdi, %rdx
adcl $0x0, %r12d
btq %rdi, %r11
sbbl $0x0, %r12d
cmpl %r14d, %r12d
setg %r13b
cmpl %r8d, %r12d
seta %al
orb %r13b, %al
leal (%rsi,%rdi), %r13d
testb %al, %al
cmovel %r12d, %r8d
cmovel %r13d, %r15d
cmpq %rdi, %rbp
leaq 0x1(%rdi), %rax
cmovel %r12d, %r10d
movq %rax, %rdi
cmpq %rax, %r9
jne 0x9aeb5
cmpl $-0x1, %r8d
movq 0x18(%rsp), %r12
je 0x9af34
movq 0x38(%rsp), %rax
addl %r12d, %eax
cmpl %r8d, %r10d
cmovnel %r15d, %eax
cmpl %r14d, %r10d
cmoval %r15d, %eax
movq 0x898(%rsp), %rdi
movl %r12d, (%rdi)
movq 0x890(%rsp), %rdi
movl %eax, (%rdi)
movq 0x28(%rsp), %rax
movl %r8d, (%rax)
shrq 0x258(%rsp)
shrq 0x268(%rsp)
shrq 0x288(%rsp)
shrq 0x2f0(%rsp)
movq 0x48(%rsp), %rax
movzbl 0x1(%rax,%rcx), %eax
movq 0x10(%rsp), %rdi
orq %rdi, 0x50(%rsp,%rax,8)
incq %r12
incq %rcx
incq %rsi
movq 0x40(%rsp), %r13
cmpq %r13, %r12
jne 0x9ae34
movl 0xc(%rsp), %ecx
jmp 0x9af8d
xorl %edx, %edx
xorl %r11d, %r11d
xorl %r12d, %r12d
movl %r12d, %esi
movq 0x30(%rsp), %rax
movzbl (%rax,%rsi), %eax
movq 0x50(%rsp,%rax,8), %rax
orq %r11, %rax
movq %rax, %rdi
andq %rdx, %rdi
addq %rdx, %rdi
xorq %rdx, %rdi
orq %rax, %rdi
testb $0x1, %dil
jne 0x9afc5
incl %ebx
movl %ebx, %eax
subl %ecx, %eax
cmpl %r14d, %eax
jg 0x9b096
cmpl %r14d, %ebx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl %ebx, %r8d
cmovgl %r10d, %r8d
movl %r12d, %r9d
cmovgl %r10d, %r9d
testw %r14w, %r14w
je 0x9b061
movq %rdi, %rax
andq %rdx, %rax
orq %rdi, %rdx
notq %rdx
orq %rdx, %r11
shrq %rdi
movq %r11, %rdx
andq %rdi, %rdx
orq %r11, %rdi
notq %rdi
orq %rax, %rdi
movzwl %r14w, %ecx
movl 0xc(%rsp), %eax
cmpl $0x2, %eax
movl $0x1, %r11d
cmovgel %eax, %r11d
incq %rsi
decq %rcx
xorl %r15d, %r15d
btq %r15, %rdi
adcl $0x0, %ebx
btq %r15, %rdx
sbbl $0x0, %ebx
cmpl %r14d, %ebx
setg %al
cmpl %r8d, %ebx
seta %bpl
orb %al, %bpl
leal (%rsi,%r15), %eax
testb %bpl, %bpl
cmovel %ebx, %r8d
cmovel %eax, %r9d
cmpq %r15, %rcx
leaq 0x1(%r15), %rax
cmovel %ebx, %r10d
movq %rax, %r15
cmpq %rax, %r11
jne 0x9b021
cmpl $-0x1, %r8d
je 0x9b096
leal (%r12,%r14), %eax
cmpl %r8d, %r10d
cmovnel %r9d, %eax
cmpl %r14d, %r10d
cmoval %r9d, %eax
movq 0x898(%rsp), %rcx
movl %r12d, (%rcx)
movq 0x890(%rsp), %rcx
movl %eax, (%rcx)
movq 0x28(%rsp), %rax
movl %r8d, (%rax)
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