// File: chhylp123[P]hifiasm/./Levenshtein_distance.h
// Function: Reserve_Banded_BPM(char*, int, char*, int, unsigned short, unsigned int*)
inline int Reserve_Banded_BPM
(char *pattern, int p_length, char *text, int t_length, unsigned short errthold, unsigned int* return_err)
{
	(*return_err) = (unsigned int)-1;

	// bit_extz_t exz; ed_band_cal_semi_128bit(pattern, p_length, text, t_length, errthold, &exz);
	// if(exz.err <= exz.thre) (*return_err) = exz.err;
	// return exz.pe;

	Word Peq[256];

	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[(uint8_t)'T'] = Peq[(uint8_t)'T'] | Peq[(uint8_t)'C'];

	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;


	///band_down = 2k
	///i_bd = 2k
	///int i_bd = i + band_down;
	int i_bd = (errthold << 1);


	int last_high = (errthold << 1);


	/// t_length_1 = SEQ_LENGTH - 1
	int t_length_1 = t_length - 1;
	//while(i<t_length)

	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)>(int)errthold)
			{
				return -1;
			}
				
		}


		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 -1;
	}


	////fprintf(stderr, "sucess(2)\n");

	/// last_high = 2k
	/// site = (SEQ_LENGTH + 2k) - 2k -1
	/// site = SEQ_LENGTH - 1
	///int site = p_length - last_high - 1;
	int site = t_length - 1;
	int return_site = -1;
	///in most cases, p_lengthshould be t_length + 2 * errthold
	int available_i = p_length - t_length;
	if ((err <= errthold) && ((unsigned int)err<=*return_err))
	{
		*return_err = err;
		return_site = site;
	}
	i = 0;

	/****************************may have bugs********************************/
	unsigned int ungap_error = (unsigned int)-1;
	/****************************may have bugs********************************/

	while (i < available_i)
	{
		err = err + ((VP >> i)&(Word)1);
		err = err - ((VN >> i)&(Word)1);
		++i;

		if ((err <= (int)errthold) && ((unsigned int)err <= *return_err))
		{
			*return_err = err;
			return_site = site + i;
		}

		/****************************may have bugs********************************/
		if(i == (int)errthold)
		{
			ungap_error = err;
		}
		/****************************may have bugs********************************/
	}

	/****************************may have bugs********************************/
	if((ungap_error<=errthold) && (ungap_error == (*return_err)))
	{
		return_site = site + errthold;
	}
	/****************************may have bugs********************************/

	return return_site;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x8d0, %rsp            # imm = 0x8D0
movw %r8w, %ax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movw %ax, -0x26(%rbp)
movq %r9, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movzwl -0x26(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x834(%rbp)
movl $0x0, -0x838(%rbp)
movq $0x1, -0x840(%rbp)
movq $0x0, -0x628(%rbp)
movq $0x0, -0x590(%rbp)
movq $0x0, -0x5f8(%rbp)
movq $0x0, -0x618(%rbp)
movl $0x0, -0x838(%rbp)
movl -0x838(%rbp), %eax
cmpl -0x834(%rbp), %eax
jge 0x9c39e
movq -0x10(%rbp), %rax
movslq -0x838(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq -0x830(%rbp,%rax,8), %rcx
orq -0x840(%rbp), %rcx
movq -0x10(%rbp), %rax
movslq -0x838(%rbp), %rdx
movzbl (%rax,%rdx), %eax
movq %rcx, -0x830(%rbp,%rax,8)
movq -0x840(%rbp), %rax
shlq %rax
movq %rax, -0x840(%rbp)
movl -0x838(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x838(%rbp)
jmp 0x9c339
movq -0x628(%rbp), %rax
movq %rax, -0x848(%rbp)
movq -0x618(%rbp), %rax
movq %rax, -0x858(%rbp)
movq -0x590(%rbp), %rax
movq %rax, -0x850(%rbp)
movq -0x5f8(%rbp), %rax
movq %rax, -0x860(%rbp)
leaq -0x830(%rbp), %rdi
xorl %esi, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x2100
movq -0x848(%rbp), %rax
movq %rax, -0x628(%rbp)
movq -0x858(%rbp), %rax
movq %rax, -0x618(%rbp)
movq -0x850(%rbp), %rax
movq %rax, -0x590(%rbp)
movq -0x860(%rbp), %rax
movq %rax, -0x5f8(%rbp)
movzwl -0x26(%rbp), %eax
shll %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x868(%rbp)
movq $0x0, -0x870(%rbp)
movq $0x0, -0x878(%rbp)
movq $0x0, -0x880(%rbp)
movq $0x0, -0x888(%rbp)
movq $0x0, -0x890(%rbp)
movq $0x0, -0x898(%rbp)
movl $0x0, -0x838(%rbp)
movl $0x0, -0x89c(%rbp)
movq $0x1, -0x8a8(%rbp)
movzwl -0x26(%rbp), %eax
shll %eax
movl %eax, -0x8ac(%rbp)
movzwl -0x26(%rbp), %eax
shll %eax
movl %eax, -0x8b0(%rbp)
movl -0x24(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x8b4(%rbp)
movl -0x838(%rbp), %eax
cmpl -0x8b4(%rbp), %eax
jge 0x9c685
movq -0x20(%rbp), %rax
movslq -0x838(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq -0x830(%rbp,%rax,8), %rax
orq -0x878(%rbp), %rax
movq %rax, -0x880(%rbp)
movq -0x870(%rbp), %rax
movq -0x880(%rbp), %rcx
andq -0x870(%rbp), %rcx
addq %rcx, %rax
xorq -0x870(%rbp), %rax
orq -0x880(%rbp), %rax
movq %rax, -0x888(%rbp)
movq -0x870(%rbp), %rax
andq -0x888(%rbp), %rax
movq %rax, -0x890(%rbp)
movq -0x878(%rbp), %rax
movq -0x870(%rbp), %rcx
orq -0x888(%rbp), %rcx
xorq $-0x1, %rcx
orq %rcx, %rax
movq %rax, -0x898(%rbp)
movq -0x888(%rbp), %rax
shrq %rax
movq %rax, -0x880(%rbp)
movq -0x880(%rbp), %rax
andq -0x898(%rbp), %rax
movq %rax, -0x878(%rbp)
movq -0x890(%rbp), %rax
movq -0x880(%rbp), %rcx
orq -0x898(%rbp), %rcx
xorq $-0x1, %rcx
orq %rcx, %rax
movq %rax, -0x870(%rbp)
movq -0x888(%rbp), %rax
andq -0x8a8(%rbp), %rax
cmpq $0x0, %rax
jne 0x9c5e9
movl -0x89c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x89c(%rbp)
movl -0x89c(%rbp), %eax
subl -0x8b0(%rbp), %eax
movzwl -0x26(%rbp), %ecx
cmpl %ecx, %eax
jle 0x9c5e7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9c906
jmp 0x9c5e9
movq -0x628(%rbp), %rax
shrq %rax
movq %rax, -0x628(%rbp)
movq -0x618(%rbp), %rax
shrq %rax
movq %rax, -0x618(%rbp)
movq -0x5f8(%rbp), %rax
shrq %rax
movq %rax, -0x5f8(%rbp)
movq -0x590(%rbp), %rax
shrq %rax
movq %rax, -0x590(%rbp)
movl -0x838(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x838(%rbp)
movl -0x8ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8ac(%rbp)
movq -0x10(%rbp), %rax
movslq -0x8ac(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq -0x830(%rbp,%rax,8), %rcx
orq -0x868(%rbp), %rcx
movq -0x10(%rbp), %rax
movslq -0x8ac(%rbp), %rdx
movzbl (%rax,%rdx), %eax
movq %rcx, -0x830(%rbp,%rax,8)
jmp 0x9c4bf
movq -0x20(%rbp), %rax
movslq -0x838(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq -0x830(%rbp,%rax,8), %rax
orq -0x878(%rbp), %rax
movq %rax, -0x880(%rbp)
movq -0x870(%rbp), %rax
movq -0x880(%rbp), %rcx
andq -0x870(%rbp), %rcx
addq %rcx, %rax
xorq -0x870(%rbp), %rax
orq -0x880(%rbp), %rax
movq %rax, -0x888(%rbp)
movq -0x870(%rbp), %rax
andq -0x888(%rbp), %rax
movq %rax, -0x890(%rbp)
movq -0x878(%rbp), %rax
movq -0x870(%rbp), %rcx
orq -0x888(%rbp), %rcx
xorq $-0x1, %rcx
orq %rcx, %rax
movq %rax, -0x898(%rbp)
movq -0x888(%rbp), %rax
shrq %rax
movq %rax, -0x880(%rbp)
movq -0x880(%rbp), %rax
andq -0x898(%rbp), %rax
movq %rax, -0x878(%rbp)
movq -0x890(%rbp), %rax
movq -0x880(%rbp), %rcx
orq -0x898(%rbp), %rcx
xorq $-0x1, %rcx
orq %rcx, %rax
movq %rax, -0x870(%rbp)
movq -0x888(%rbp), %rax
andq -0x8a8(%rbp), %rax
cmpq $0x0, %rax
jne 0x9c79d
movl -0x89c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x89c(%rbp)
movl -0x89c(%rbp), %eax
subl -0x8b0(%rbp), %eax
movzwl -0x26(%rbp), %ecx
cmpl %ecx, %eax
jle 0x9c79b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9c906
jmp 0x9c79d
movl -0x24(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x8b8(%rbp)
movl $0xffffffff, -0x8bc(%rbp) # imm = 0xFFFFFFFF
movl -0x14(%rbp), %eax
subl -0x24(%rbp), %eax
movl %eax, -0x8c0(%rbp)
movl -0x89c(%rbp), %eax
movzwl -0x26(%rbp), %ecx
cmpl %ecx, %eax
jg 0x9c7f3
movl -0x89c(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl (%rcx), %eax
ja 0x9c7f3
movl -0x89c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movl -0x8b8(%rbp), %eax
movl %eax, -0x8bc(%rbp)
movl $0x0, -0x838(%rbp)
movl $0xffffffff, -0x8c4(%rbp) # imm = 0xFFFFFFFF
movl -0x838(%rbp), %eax
cmpl -0x8c0(%rbp), %eax
jge 0x9c8cf
movslq -0x89c(%rbp), %rax
movq -0x870(%rbp), %rdx
movl -0x838(%rbp), %ecx
shrq %cl, %rdx
movq %rdx, %rcx
andq $0x1, %rcx
addq %rcx, %rax
movl %eax, -0x89c(%rbp)
movslq -0x89c(%rbp), %rax
movq -0x878(%rbp), %rdx
movl -0x838(%rbp), %ecx
shrq %cl, %rdx
movq %rdx, %rcx
andq $0x1, %rcx
subq %rcx, %rax
movl %eax, -0x89c(%rbp)
movl -0x838(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x838(%rbp)
movl -0x89c(%rbp), %eax
movzwl -0x26(%rbp), %ecx
cmpl %ecx, %eax
jg 0x9c8b0
movl -0x89c(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl (%rcx), %eax
ja 0x9c8b0
movl -0x89c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movl -0x8b8(%rbp), %eax
addl -0x838(%rbp), %eax
movl %eax, -0x8bc(%rbp)
movl -0x838(%rbp), %eax
movzwl -0x26(%rbp), %ecx
cmpl %ecx, %eax
jne 0x9c8ca
movl -0x89c(%rbp), %eax
movl %eax, -0x8c4(%rbp)
jmp 0x9c807
movl -0x8c4(%rbp), %eax
movzwl -0x26(%rbp), %ecx
cmpl %ecx, %eax
ja 0x9c8fd
movl -0x8c4(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x9c8fd
movl -0x8b8(%rbp), %eax
movzwl -0x26(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x8bc(%rbp)
movl -0x8bc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x8d0, %rsp            # imm = 0x8D0
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)