// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/./Levenshtein_distance.h
// Function: Reserve_Banded_BPM_PATH(char*, int, char*, int, unsigned short, unsigned int*, int*, int*, unsigned long*, char*, int, int)
inline int Reserve_Banded_BPM_PATH
(char *pattern, int p_length, char *text, int t_length, unsigned short errthold, 
		unsigned int* return_err, int* return_start_site, int* return_path_length, Word* matrix_bit, char* path, 
		int old_error, int old_end_site)
{
	if (old_error != -1 && old_end_site != -1)
	{
		if (old_error == 0)
		{
			(*return_err) = old_error;
			(*return_start_site) = old_end_site - t_length + 1;
			return old_end_site;
		}

		if (try_cigar(pattern, p_length, text, t_length, old_end_site, path,
		old_error, return_start_site, return_path_length))
		{
			(*return_err) = old_error;
			return old_end_site;
		}
		
	}
	
	(*return_err) = (unsigned int)-1;

	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;

	Word column_start;

	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)
	{
		///pattern[0]ÔÚPeq[2k], ¶øpattern[2k]ÔÚPeq[0]
		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;


		///Peq[(uint8_t)'T'] = Peq[(uint8_t)'T'] | Peq[(uint8_t)'C'];

		column_start = i << 3;
		matrix_bit[column_start] = D0;
		matrix_bit[column_start + 1] = VP;
		matrix_bit[column_start + 2] = VN;
		matrix_bit[column_start + 3] = HP;
		matrix_bit[column_start + 4] = HN;
	}





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


	column_start = (i + 1) << 3;
	matrix_bit[column_start] = D0;
	matrix_bit[column_start + 1] = VP;
	matrix_bit[column_start + 2] = VN;
	matrix_bit[column_start + 3] = HP;
	matrix_bit[column_start + 4] = HN;


	////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;

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

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

	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********************************/
	}

	


	if ((*return_err) == (unsigned int)-1)
	{
		return return_site;
	}


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

	///need to correct p_length here, since p_length might be smaller than t_length + 2* err_threashlod
	p_length = t_length + 2 * errthold;
	///end_site is always correct
	int end_site = return_site;
	int start_site = end_site;
	int back_track_site = band_length - (p_length - end_site);

	Word v_value, h_value, delta_value, min_value, current_value;
	///Word direction; ///0 is match, 1 is mismatch, 2 is up, 3 is left
	Word direction = 0; ///0 is match, 1 is mismatch, 2 is up, 3 is left
	i = t_length;
	int path_length = 0;
	current_value = *return_err;


	int low_bound = band_length - 1;


	while (i>0)
	{
		if (current_value == 0)
		{
			break;
		}

		column_start = i << 3;

		delta_value = current_value -
			((~(matrix_bit[column_start] >> back_track_site))&err_mask);


		if (back_track_site == 0)
		{
			///HP
			h_value = current_value - ((matrix_bit[column_start + 3] >> back_track_site)&err_mask);
			//HN
			h_value = h_value + ((matrix_bit[column_start + 4] >> back_track_site)&err_mask);


			min_value = delta_value;
			direction = 0;

			if (h_value < min_value)
			{
				min_value = h_value;
				direction = 3;
			}

		}
		else if (back_track_site == low_bound)
		{
			v_value = current_value - ((matrix_bit[column_start + 1] >> (back_track_site - 1))&err_mask);
			v_value = v_value + ((matrix_bit[column_start + 2] >> (back_track_site - 1))&err_mask);

			min_value = delta_value;
			direction = 0;

			if (v_value < min_value)
			{
				min_value = v_value;
				direction = 2;
			}

		}
		else
		{

			h_value = current_value - ((matrix_bit[column_start + 3] >> back_track_site)&(Word)1);


			h_value = h_value + ((matrix_bit[column_start + 4] >> back_track_site)&(Word)1);


			v_value = current_value - ((matrix_bit[column_start + 1] >> (back_track_site - 1))&err_mask);
			v_value = v_value + ((matrix_bit[column_start + 2] >> (back_track_site - 1))&err_mask);


			min_value = delta_value;
			direction = 0;

			if (v_value < min_value)
			{
				min_value = v_value;
				direction = 2;
			}


			if (h_value < min_value)
			{
				min_value = h_value;
				direction = 3;
			}

		}


		if (direction == 0)
		{

			if (delta_value != current_value)
			{
				direction = 1;
			}

			i--;

			start_site--;

		}
		if (direction == 2)///ru guo xiang shang yi dong, bing bu huan lie
		{
			back_track_site--;
			start_site--;
		}
		else if (direction == 3)///ru guo xiang zuo yi dong
		{
			i--;
			back_track_site++;
		}


		path[path_length++] = direction;


		current_value = min_value;

	}


	if (i > 0)
	{
		memset(path + path_length, 0, i);
		start_site = start_site - i;
		direction = 0;
		path_length = path_length + i;
	}

	if (direction != 3)
	{
		start_site++;
	}
	(*return_start_site) = start_site;
	(*return_path_length) = path_length;

	return return_site;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x858, %rsp            # imm = 0x858
movq %r9, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %r13
movl %esi, 0x34(%rsp)
movq %rdi, %r14
movl 0x8b8(%rsp), %ebp
movl 0x8b0(%rsp), %eax
movq 0x898(%rsp), %r15
movq 0x890(%rsp), %rbx
cmpl $-0x1, %eax
sete %cl
cmpl $-0x1, %ebp
sete %dl
orb %cl, %dl
jne 0x8616d
testl %eax, %eax
je 0x86135
movl %ebp, %ecx
subl 0x10(%rsp), %ecx
cmpl $-0x1, %ecx
jl 0x8616d
movslq %ecx, %rcx
incq %rcx
movq 0x10(%rsp), %rdx
testl %edx, %edx
jle 0x8614f
movl %edx, %edx
leaq (%r14,%rcx), %rsi
movq 0x8a8(%rsp), %rdi
decq %rdi
xorl %r10d, %r10d
xorl %r9d, %r9d
movb $0x0, (%rdi,%rdx)
movb (%r13,%r10), %r11b
cmpb (%rsi,%r10), %r11b
je 0x86128
movb $0x1, (%rdi,%rdx)
cmpl %eax, %r9d
jge 0x8616d
incl %r9d
incq %r10
decq %rdi
cmpq %r10, %rdx
jne 0x8610d
jmp 0x86152
movq 0x20(%rsp), %rax
movl $0x0, (%rax)
movl %ebp, %eax
subl 0x10(%rsp), %eax
incl %eax
movl %eax, (%rbx)
jmp 0x86628
xorl %r9d, %r9d
cmpl %eax, %r9d
jne 0x8616d
movq 0x10(%rsp), %rdx
movl %edx, (%r15)
movl %ecx, (%rbx)
movq 0x20(%rsp), %rcx
movl %eax, (%rcx)
jmp 0x86628
movq 0x8a0(%rsp), %r15
movq 0x20(%rsp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movzwl %r8w, %eax
movq %rax, 0x28(%rsp)
leal (%rax,%rax), %ecx
xorl %eax, %eax
movq %rax, 0x258(%rsp)
movq %rax, 0x2f0(%rsp)
movq %rax, 0x288(%rsp)
movq %rax, 0x268(%rsp)
movq %rcx, 0x8(%rsp)
leaq 0x1(%rcx), %rbx
movl $0x1, %ecx
movzbl (%r14,%rax), %edx
orq %rcx, 0x50(%rsp,%rdx,8)
addq %rcx, %rcx
incq %rax
cmpq %rax, %rbx
jne 0x861bc
leaq 0x50(%rsp), %rbp
movq 0x208(%rbp), %rax
movq %rax, 0x40(%rsp)
movq 0x218(%rbp), %rax
movq %rax, 0x38(%rsp)
movq 0x2a0(%rbp), %rax
movq %rax, 0x18(%rsp)
movq 0x238(%rbp), %rax
movq %rax, 0x48(%rsp)
xorl %r12d, %r12d
movl $0x800, %edx            # imm = 0x800
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x40(%rsp), %rax
movq %rax, 0x208(%rbp)
movq 0x38(%rsp), %rax
movq %rax, 0x218(%rbp)
movl $0x1, %edi
movq 0x8(%rsp), %rcx
shlq %cl, %rdi
movq 0x18(%rsp), %rax
movq %rax, 0x2a0(%rbp)
movq 0x48(%rsp), %rax
movq %rax, 0x238(%rbp)
movq 0x10(%rsp), %rax
leal -0x1(%rax), %r9d
cmpl $0x2, %eax
movl %r9d, 0x18(%rsp)
jl 0x86341
movq 0x28(%rsp), %rax
leal (%rax,%rax,2), %r8d
movl %r9d, %r9d
addq %rbx, %r14
xorl %ecx, %ecx
movl $0x8, %r10d
xorl %esi, %esi
xorl %edx, %edx
xorl %r12d, %r12d
movzbl (%r13,%rcx), %r11d
movq 0x50(%rsp,%r11,8), %rbx
orq %rdx, %rbx
movq %rbx, %r11
andq %rsi, %r11
addq %rsi, %r11
xorq %rsi, %r11
orq %rbx, %r11
testb $0x1, %r11b
jne 0x862b9
cmpl %r8d, %r12d
jge 0x8661c
incl %r12d
movq %r11, %rbx
andq %rsi, %rbx
orq %r11, %rsi
notq %rsi
movq %rdx, %rbp
orq %rsi, %rbp
movq %r11, %rsi
shrq %rsi
movq %rbp, %rdx
andq %rsi, %rdx
shrq 0x258(%rsp)
shrq 0x268(%rsp)
shrq 0x288(%rsp)
shrq 0x2f0(%rsp)
orq %rbp, %rsi
notq %rsi
orq %rbx, %rsi
movzbl (%r14,%rcx), %eax
incq %rcx
orq %rdi, 0x50(%rsp,%rax,8)
movslq %r10d, %r10
movq %r11, (%r15,%r10,8)
movq %rsi, 0x8(%r15,%r10,8)
movq %rdx, 0x10(%r15,%r10,8)
movq %rbp, 0x18(%r15,%r10,8)
movq %rbx, 0x20(%r15,%r10,8)
addl $0x8, %r10d
cmpq %rcx, %r9
jne 0x8628a
movq 0x8(%rsp), %rbp
movl 0x18(%rsp), %r9d
jmp 0x8634c
xorl %edx, %edx
xorl %esi, %esi
xorl %ecx, %ecx
movq 0x8(%rsp), %rbp
movl %ecx, %eax
movzbl (%r13,%rax), %eax
movq 0x50(%rsp,%rax,8), %rax
orq %rdx, %rax
movq %rax, %r8
andq %rsi, %r8
addq %rsi, %r8
xorq %rsi, %r8
orq %rax, %r8
testb $0x1, %r8b
movq 0x10(%rsp), %rbx
movq 0x20(%rsp), %r14
jne 0x8639a
incl %r12d
movl %r12d, %eax
subl %ebp, %eax
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movq 0x28(%rsp), %r13
cmpl %r13d, %eax
jle 0x8639f
jmp 0x86624
movq 0x28(%rsp), %r13
movq %r8, %rax
andq %rsi, %rax
orq %r8, %rsi
notq %rsi
orq %rsi, %rdx
movq %r8, %rsi
shrq %rsi
movq %rdx, %rdi
andq %rsi, %rdi
orq %rdx, %rsi
notq %rsi
orq %rax, %rsi
leal 0x8(,%rcx,8), %ecx
movslq %ecx, %rcx
movq %r8, (%r15,%rcx,8)
movq %rsi, 0x8(%r15,%rcx,8)
movq %rdi, 0x10(%r15,%rcx,8)
movq %rdx, 0x18(%r15,%rcx,8)
movq %rax, 0x20(%r15,%rcx,8)
movl 0x34(%rsp), %eax
subl %ebx, %eax
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl %r13d, %r12d
jg 0x86401
movl %r12d, (%r14)
movl %r9d, %edx
testl %eax, %eax
jle 0x86445
movl %eax, %ecx
movl %ebx, %r9d
leaq -0x1(%r13), %r10
xorl %r11d, %r11d
btq %r11, %rsi
adcl $0x0, %r12d
btq %r11, %rdi
sbbl $0x0, %r12d
cmpl %r13d, %r12d
jg 0x86432
cmpl (%r14), %r12d
ja 0x86432
movl %r12d, (%r14)
leal (%r9,%r11), %edx
cmpq %r11, %r10
leaq 0x1(%r11), %rax
cmovel %r12d, %r8d
movq %rax, %r11
cmpq %rax, %rcx
jne 0x86411
movl (%r14), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rcx
je 0x8648f
movl 0x18(%rsp), %r12d
addl %r13d, %r12d
cmpl %ecx, %r8d
cmovnel %edx, %r12d
cmpl %r13d, %r8d
cmoval %edx, %r12d
testl %ebx, %ebx
setle %al
testq %rcx, %rcx
sete %dl
orb %al, %dl
movl %r12d, 0x8(%rsp)
je 0x86498
xorl %r13d, %r13d
xorl %r14d, %r14d
movq 0x8a8(%rsp), %rdi
jmp 0x865da
movl %edx, 0x8(%rsp)
jmp 0x86624
movl %r12d, %eax
subl %ebx, %eax
incl %eax
xorl %r14d, %r14d
movl $0x3, %edx
movq 0x8a8(%rsp), %rdi
leal (,%rbx,8), %esi
movslq %esi, %r11
movq (%r15,%r11,8), %rsi
xorl %r13d, %r13d
btq %rax, %rsi
setb %r13b
xorq $0x1, %r13
movq %rcx, %rsi
subq %r13, %rsi
testl %eax, %eax
je 0x8650e
cmpl %ebp, %eax
jne 0x8653b
leal -0x1(%rax), %r8d
movq 0x8(%r15,%r11,8), %r9
movq 0x10(%r15,%r11,8), %r10
btq %r8, %r9
sbbq $0x0, %rcx
btq %r8, %r10
adcq $0x0, %rcx
xorl %r8d, %r8d
cmpq %rsi, %rcx
setb %r8b
cmovaeq %rsi, %rcx
addl %r8d, %r8d
jmp 0x8659d
movq 0x18(%r15,%r11,8), %r8
movq 0x20(%r15,%r11,8), %r9
andl $0x1, %r8d
subq %r8, %rcx
andl $0x1, %r9d
addq %rcx, %r9
xorl %ecx, %ecx
cmpq %rsi, %r9
setb %cl
cmovaeq %rsi, %r9
leaq (%rcx,%rcx,2), %r8
movq %r9, %rcx
jmp 0x8659d
movl %eax, %r8d
movq 0x18(%r15,%r11,8), %r9
btq %r8, %r9
movq %rcx, %r9
sbbq $0x0, %r9
movq 0x20(%r15,%r11,8), %r10
btq %r8, %r10
adcq $0x0, %r9
leal -0x1(%rax), %r8d
movq 0x8(%r15,%r11,8), %r10
btq %r8, %r10
movq 0x8a8(%rsp), %rdi
sbbq $0x0, %rcx
movq 0x10(%r15,%r11,8), %r10
btq %r8, %r10
adcq $0x0, %rcx
xorl %r8d, %r8d
cmpq %rsi, %rcx
setb %r8b
cmovaeq %rsi, %rcx
addl %r8d, %r8d
cmpq %rcx, %r9
cmovbq %r9, %rcx
cmovbq %rdx, %r8
xorl %esi, %esi
testq %r8, %r8
cmovneq %r8, %r13
sete %sil
subl %esi, %ebx
subl %esi, %r12d
cmpq $0x3, %r13
je 0x865c2
cmpl $0x2, %r13d
jne 0x865c6
decl %eax
decl %r12d
jmp 0x865c6
decl %ebx
incl %eax
movb %r13b, (%rdi,%r14)
incq %r14
testl %ebx, %ebx
jle 0x865da
testq %rcx, %rcx
jne 0x864af
testl %ebx, %ebx
movq %rbx, %rbp
movq 0x890(%rsp), %rbx
movq 0x898(%rsp), %r15
jle 0x86609
movl %r14d, %eax
addq %rax, %rdi
movl %ebp, %edx
xorl %r13d, %r13d
xorl %esi, %esi
callq 0x20e0
subl %ebp, %r12d
addl %ebp, %r14d
xorl %eax, %eax
cmpl $0x3, %r13d
setne %al
addl %r12d, %eax
movl %eax, (%rbx)
movl %r14d, (%r15)
jmp 0x86624
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movl 0x8(%rsp), %ebp
movl %ebp, %eax
addq $0x858, %rsp            # imm = 0x858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq