// File: /workspace/github_cpp_makefile_star2_O3/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, 0x18(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, %r13
movl %esi, 0x34(%rsp)
movq %rdi, %r15
movl 0x8b8(%rsp), %ebp
movl 0x8b0(%rsp), %eax
movq 0x8a8(%rsp), %r14
movq 0x898(%rsp), %rbx
movq 0x890(%rsp), %r11
cmpl $-0x1, %eax
sete %cl
cmpl $-0x1, %ebp
sete %dl
orb %cl, %dl
jne 0x85809
testl %eax, %eax
je 0x857d2
movl %ebp, %ecx
subl 0x8(%rsp), %ecx
cmpl $-0x1, %ecx
jl 0x85809
incl %ecx
movq 0x8(%rsp), %rdx
testl %edx, %edx
jle 0x857ed
movl %edx, %edx
movl %ecx, %esi
addq %r15, %rsi
xorl %r9d, %r9d
xorl %edi, %edi
movb $0x0, -0x1(%r14,%rdx)
movb (%r13,%r9), %r10b
cmpb (%rsi,%r9), %r10b
je 0x857c8
movb $0x1, -0x1(%r14,%rdx)
cmpl %eax, %edi
jge 0x85809
incl %edi
incq %r9
decq %rdx
jne 0x857ab
jmp 0x857ef
movq 0x18(%rsp), %rax
movl $0x0, (%rax)
movl %ebp, %eax
subl 0x8(%rsp), %eax
incl %eax
movl %eax, (%r11)
jmp 0x85cbe
xorl %edi, %edi
cmpl %eax, %edi
jne 0x85809
movq 0x8(%rsp), %rdx
movl %edx, (%rbx)
movl %ecx, (%r11)
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
jmp 0x85cbe
movq 0x8a0(%rsp), %r14
movq 0x18(%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, 0x20(%rsp)
leaq 0x1(%rcx), %rbx
movl $0x1, %ecx
movzbl (%r15,%rax), %edx
orq %rcx, 0x50(%rsp,%rdx,8)
addq %rcx, %rcx
incq %rax
cmpq %rax, %rbx
jne 0x85858
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, 0x10(%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, %esi
movq 0x20(%rsp), %rcx
shlq %cl, %rsi
movq 0x10(%rsp), %rax
movq %rax, 0x2a0(%rbp)
movq 0x48(%rsp), %rax
movq %rax, 0x238(%rbp)
movq 0x8(%rsp), %rax
leal -0x1(%rax), %r10d
cmpl $0x2, %eax
movl %r10d, 0x10(%rsp)
jl 0x859db
movq 0x28(%rsp), %rax
leal (%rax,%rax,2), %edi
movl %r10d, %r8d
addq %rbx, %r15
xorl %r9d, %r9d
movl $0x8, %r10d
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r12d, %r12d
movzbl (%r13,%r9), %r11d
movq 0x50(%rsp,%r11,8), %rbx
orq %rcx, %rbx
movq %rbx, %r11
andq %rdx, %r11
addq %rdx, %r11
xorq %rdx, %r11
orq %rbx, %r11
testb $0x1, %r11b
jne 0x85955
cmpl %edi, %r12d
jge 0x85ca6
incl %r12d
movq %r11, %rbx
andq %rdx, %rbx
orq %r11, %rdx
notq %rdx
movq %rcx, %rbp
orq %rdx, %rbp
movq %r11, %rdx
shrq %rdx
movq %rbp, %rcx
andq %rdx, %rcx
shrq 0x258(%rsp)
shrq 0x268(%rsp)
shrq 0x288(%rsp)
shrq 0x2f0(%rsp)
orq %rbp, %rdx
notq %rdx
orq %rbx, %rdx
movzbl (%r15,%r9), %eax
incq %r9
orq %rsi, 0x50(%rsp,%rax,8)
movslq %r10d, %r10
movq %r11, (%r14,%r10,8)
movq %rdx, 0x8(%r14,%r10,8)
movq %rcx, 0x10(%r14,%r10,8)
movq %rbp, 0x18(%r14,%r10,8)
movq %rbx, 0x20(%r14,%r10,8)
addl $0x8, %r10d
cmpq %r9, %r8
jne 0x85926
movl 0x10(%rsp), %r10d
movl %r10d, %edi
jmp 0x859e1
xorl %ecx, %ecx
xorl %edx, %edx
xorl %edi, %edi
movl %edi, %eax
movzbl (%r13,%rax), %eax
movq 0x50(%rsp,%rax,8), %rax
orq %rcx, %rax
movq %rax, %r8
andq %rdx, %r8
addq %rdx, %r8
xorq %rdx, %r8
orq %rax, %r8
testb $0x1, %r8b
jne 0x85a29
incl %r12d
movl %r12d, %eax
subl 0x20(%rsp), %eax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x28(%rsp), %r9
cmpl %r9d, %eax
movq 0x8(%rsp), %r13
jle 0x85a33
jmp 0x85cbe
movq 0x8(%rsp), %r13
movq 0x28(%rsp), %r9
movq %r8, %rax
andq %rdx, %rax
orq %r8, %rdx
notq %rdx
orq %rdx, %rcx
movq %r8, %rdx
shrq %rdx
movq %rcx, %rsi
andq %rdx, %rsi
orq %rcx, %rdx
notq %rdx
orq %rax, %rdx
leal 0x8(,%rdi,8), %edi
movslq %edi, %rdi
movq %r8, (%r14,%rdi,8)
movq %rdx, 0x8(%r14,%rdi,8)
movq %rsi, 0x10(%r14,%rdi,8)
movq %rcx, 0x18(%r14,%rdi,8)
movq %rax, 0x20(%r14,%rdi,8)
movl 0x34(%rsp), %eax
subl %r13d, %eax
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
cmpl %r9d, %r12d
movq 0x18(%rsp), %r15
movq %r9, %rbp
jg 0x85aa6
movl %r12d, (%r15)
movl %r12d, %ecx
movl %r10d, %r8d
testl %eax, %eax
jle 0x85aed
movl %eax, %r9d
movl %r13d, %r10d
leaq -0x1(%rbp), %r11
xorl %ebx, %ebx
btq %rbx, %rdx
adcl $0x0, %r12d
btq %rbx, %rsi
sbbl $0x0, %r12d
cmpl %ebp, %r12d
jg 0x85ada
cmpl %ecx, %r12d
ja 0x85ada
movl %r12d, (%r15)
leal (%r10,%rbx), %r8d
movl %r12d, %ecx
cmpq %rbx, %r11
leaq 0x1(%rbx), %rax
cmovel %r12d, %edi
movq %rax, %rbx
cmpq %rax, %r9
jne 0x85ab6
cmpl $-0x1, %ecx
je 0x85c6e
movl 0x10(%rsp), %eax
addl %ebp, %eax
cmpl %ecx, %edi
cmovnel %r8d, %eax
cmpl %ebp, %edi
movl %eax, %ebp
cmoval %r8d, %ebp
xorl %r15d, %r15d
movl $0x1, %eax
testl %r13d, %r13d
jle 0x85c73
testl %ecx, %ecx
je 0x85c73
movl %ecx, %edx
movl %ebp, %ecx
subl %r13d, %ecx
incl %ecx
movq 0x20(%rsp), %rax
decl %eax
xorl %r15d, %r15d
movl $0x3, %esi
movl %ebp, %r12d
leal (,%r13,8), %edi
movslq %edi, %r9
movq (%r14,%r9,8), %rdi
shrq %cl, %rdi
notl %edi
andl $0x1, %edi
movq %rdx, %r8
subq %rdi, %r8
testl %ecx, %ecx
je 0x85b95
cmpl 0x20(%rsp), %ecx
jne 0x85bc2
movq 0x8(%r14,%r9,8), %r10
movq 0x10(%r14,%r9,8), %r9
btq %rax, %r10
sbbq $0x0, %rdx
btq %rax, %r9
adcq $0x0, %rdx
xorl %r9d, %r9d
cmpq %r8, %rdx
setb %r9b
cmovaeq %r8, %rdx
addl %r9d, %r9d
jmp 0x85c1c
movq 0x18(%r14,%r9,8), %r10
movq 0x20(%r14,%r9,8), %r11
andl $0x1, %r10d
subq %r10, %rdx
andl $0x1, %r11d
addq %rdx, %r11
xorl %edx, %edx
cmpq %r8, %r11
setb %dl
cmovaeq %r8, %r11
leaq (%rdx,%rdx,2), %r9
movq %r11, %rdx
jmp 0x85c1c
movl %ecx, %r10d
movq 0x18(%r14,%r9,8), %r11
btq %r10, %r11
movq %rdx, %r11
sbbq $0x0, %r11
movq 0x20(%r14,%r9,8), %rbx
btq %r10, %rbx
adcq $0x0, %r11
leal -0x1(%rcx), %r10d
movq 0x8(%r14,%r9,8), %rbx
btq %r10, %rbx
sbbq $0x0, %rdx
movq 0x10(%r14,%r9,8), %r9
btq %r10, %r9
adcq $0x0, %rdx
xorl %r9d, %r9d
cmpq %r8, %rdx
setb %r9b
cmovaeq %r8, %rdx
addl %r9d, %r9d
cmpq %rdx, %r11
cmovbq %r11, %rdx
cmovbq %rsi, %r9
xorl %r8d, %r8d
testq %r9, %r9
cmovneq %r9, %rdi
sete %r8b
subl %r8d, %r13d
subl %r8d, %r12d
cmpq $0x3, %rdi
je 0x85c42
cmpl $0x2, %edi
jne 0x85c47
decl %ecx
decl %r12d
jmp 0x85c47
decl %r13d
incl %ecx
movq 0x8a8(%rsp), %r8
movb %dil, (%r8,%r15)
incq %r15
testl %r13d, %r13d
jle 0x85c64
testq %rdx, %rdx
jne 0x85b3e
xorl %eax, %eax
cmpl $0x3, %edi
setne %al
jmp 0x85c76
movl %r8d, %ebp
jmp 0x85cbe
movl %ebp, %r12d
testl %r13d, %r13d
movq 0x898(%rsp), %rbx
jle 0x85cad
movl %r15d, %eax
movq 0x8a8(%rsp), %rdi
addq %rax, %rdi
movl %r13d, %edx
xorl %esi, %esi
callq 0x20e0
addl %r13d, %r15d
subl %r13d, %r12d
incl %r12d
jmp 0x85cb0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x85cbe
addl %eax, %r12d
movq 0x890(%rsp), %rax
movl %r12d, (%rax)
movl %r15d, (%rbx)
movl %ebp, %eax
addq $0x858, %rsp            # imm = 0x858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq