// File: chhylp123[P]hifiasm/./Levenshtein_distance.h
// Function: get_error(int, int, int, unsigned long, unsigned long, unsigned int*, int*)
inline void get_error(int t_length, int errthold, int init_err, Word VP, Word VN, 
unsigned int* return_err, int* back_site)
{
	(*return_err) = (unsigned int)-1;
	int site = t_length - 1;
	int return_site = -1;
	///in most cases, p_length should be t_length + 2 * errthold
	///int available_i = p_length - t_length;
	int available_i = 2 * errthold;
	

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


	int i = 0;
	unsigned int ungap_error = (unsigned int)-1;

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

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

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

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

	(*back_site) = return_site;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq 0x10(%rbp), %rax
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movl %edx, -0xc(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
movl -0x8(%rbp), %eax
shll %eax
movl %eax, -0x34(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x8(%rbp), %eax
jg 0xc5ba1
movl -0xc(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
ja 0xc5ba1
movl -0xc(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0xc5c2b
movslq -0xc(%rbp), %rax
movq -0x18(%rbp), %rdx
movl -0x38(%rbp), %ecx
shrq %cl, %rdx
movq %rdx, %rcx
andq $0x1, %rcx
addq %rcx, %rax
movl %eax, -0xc(%rbp)
movslq -0xc(%rbp), %rax
movq -0x20(%rbp), %rdx
movl -0x38(%rbp), %ecx
shrq %cl, %rdx
movq %rdx, %rcx
andq $0x1, %rcx
subq %rcx, %rax
movl %eax, -0xc(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x8(%rbp), %eax
jg 0xc5c1b
movl -0xc(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
ja 0xc5c1b
movl -0xc(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl -0x2c(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x8(%rbp), %eax
jne 0xc5c29
movl -0xc(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0xc5baf
movl -0x3c(%rbp), %eax
cmpl -0x8(%rbp), %eax
ja 0xc5c47
movl -0x3c(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
jne 0xc5c47
movl -0x2c(%rbp), %eax
addl -0x8(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)