// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/./Levenshtein_distance.h
// Function: ed_band_cal_extension_64_0_w(char*, int, char*, int, int, bit_extz_t*)
inline void ed_band_cal_extension_64_0_w(char *pstr, int32_t pn, char *tstr, int32_t tn, int32_t thre, bit_extz_t *ez)
{
	init_base_ed(*ez, thre, pn, tn); ez->ps = ez->ts = 0; ez->pe = ez->te = -1;
	if(pn > tn + thre) pn = tn + thre;
    else if(tn > pn + thre) tn = pn + thre;
	int32_t i, err, tn0 = tn - 1, cut = thre+(thre<<1), bd, i_bd, poff, pe = pn-1, tmp_e = INT32_MAX, k;
	Word c, Peq[5] = {0}, VP, VN, X, D0, HN, HP, mm;

	bd = thre+1; bd = ((bd<=pn)?bd:pn); i_bd = thre;
	for (i = 0, mm = (((Word)1)<<i_bd); i < bd; i++) {
        Peq[seq_nt4_table[(uint8_t)pstr[i]]] |= mm; mm <<= 1;
    }	
	bd = thre+1, i_bd = thre; Peq[4] = 0; err = thre;
	VN = (((Word)1)<<(thre))-1; VP = (((Word)1)<<((thre<<1)+1))-1; VP ^= VN;

	i = 0; mm = ((Word)1 << (thre<<1));
	// X = D0 = HN = HP = mm = 0;//for debug
	while (i < tn0) {
		// fprintf(stderr, "i::%d\n", i);
		// prt_vector(Peq, VP, VN, X, D0, HN, HP, ((thre<<1)+1));
		ed_core_64(Peq, VP, VN, X, D0, HN, HP, (uint8_t)tstr[i]);
		if (!(D0&(1ULL))) {
			++err; if (err>cut) return;
		}

		{
			poff = i-thre; k = i+thre-pe;/**poff:[i-thre, i+thre]**/
            if(k >= 0) {
                if(tmp_e == INT32_MAX) {
                    tmp_e = err;
                    for (k = 0; poff < pe; poff++, (k)++) {
                        tmp_e += ((VP>>k)&(1ULL)); tmp_e -= ((VN>>k)&(1ULL));
                    } 
                } else {
                    k = (thre<<1) - k; 
                    if(k >= 0) {
                        tmp_e += ((HP>>k)&(1ULL)); tmp_e -= ((HN>>k)&(1ULL)); 
                    }
                }
                if(tmp_e <= (*ez).thre && tmp_e < (*ez).err) {
					(*ez).err = tmp_e; (*ez).pe = pe; (*ez).te = i;
                }
            }
        }
		// fprintf(stderr, "i::%d, tmp_e::%d, err::%d\n", i, tmp_e, err);

		Peq[0] >>= 1; Peq[1] >>= 1; Peq[2] >>= 1; Peq[3] >>= 1;
		++i; ++i_bd;
        if(i_bd < pn) {
            c = seq_nt4_table[(uint8_t)pstr[i_bd]]; 
            if(c < 4) Peq[c] |= mm;
        }
	}
	ed_core_64(Peq, VP, VN, X, D0, HN, HP, (uint8_t)tstr[i]);
	if (!(D0&(1ULL))) {
		++err; if (err>cut) return;
	}

	int32_t site = tn - 1 - thre;/**up bound; site:[tn - 1 - thre, tn - 1 + thre]**/\
    for (cut = pn - 1; site < cut; ) {
		err += VP&(1ULL); VP >>= 1; err -= VN&(1ULL); VN >>= 1; 
        site++;
        if(err <= thre && err < ez->err) {
            ez->err = err; ez->pe = site; ez->te = tn-1;
        }
    }
    if(err <= thre && err < ez->err) {
        ez->err = err; ez->pe = site; ez->te = tn-1;
    }
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %r8d, %ebp
movq %rdx, -0x58(%rsp)
movl %r8d, 0x28(%r9)
movl $0x7fffffff, 0x2c(%r9)  # imm = 0x7FFFFFFF
movl %esi, 0x18(%r9)
movl %ecx, 0x24(%r9)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x1c(%r9)
pxor %xmm0, %xmm0
movdqu %xmm0, (%r9)
leal (%rcx,%rbp), %edx
leal (%rsi,%rbp), %r8d
cmpl %ecx, %r8d
cmovgel %ecx, %r8d
cmpl %esi, %edx
cmovll %ecx, %r8d
cmovll %edx, %esi
movq %r9, -0x60(%rsp)
movq %rax, 0x10(%r9)
andq $0x0, -0x18(%rsp)
decl %r8d
movl %r8d, -0x68(%rsp)
leal (%rbp,%rbp,2), %eax
movl %eax, -0x64(%rsp)
leal 0x1(%rbp), %eax
cmpl %esi, %eax
cmovgel %esi, %eax
movabsq $0x1, %r10
movq %r10, -0x48(%rsp)
movl %ebp, %ecx
shlq %cl, %r10
movdqa %xmm0, -0x28(%rsp)
movdqa %xmm0, -0x38(%rsp)
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
leaq 0x18c61e(%rip), %r12    # 0x1ebb00
movq %r10, %rdx
cmpq %rcx, %rax
je 0x5f501
movzbl (%rdi,%rcx), %r8d
movzbl (%r12,%r8), %r8d
orq %rdx, -0x38(%rsp,%r8,8)
addq %rdx, %rdx
incq %rcx
jmp 0x5f4e5
leal (,%rbp,2), %edx
leal -0x1(%rsi), %r11d
andq $0x0, -0x18(%rsp)
leaq -0x1(%r10), %r9
leal 0x1(%rdx), %ecx
movabsq $-0x1, %rax
shlq %cl, %rax
negq %r10
movq %rdx, -0x10(%rsp)
movl %edx, %ecx
shlq %cl, -0x48(%rsp)
xorq %rax, %r10
movl %ebp, %eax
subl %esi, %eax
incl %eax
movl %eax, -0x4c(%rsp)
movslq %ebp, %r13
movslq %esi, %rax
movq %rax, -0x8(%rsp)
xorl %r15d, %r15d
movl -0x68(%rsp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, -0x40(%rsp)
movl %ebp, %r8d
negl %r8d
movl $0x7fffffff, -0x6c(%rsp) # imm = 0x7FFFFFFF
movl %ebp, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq -0x58(%rsp), %rsi
cmpq -0x40(%rsp), %r15
je 0x5f6a6
movzbl (%rsi,%r15), %edx
movzbl (%r12,%rdx), %edx
movq -0x38(%rsp,%rdx,8), %rdx
orq %r9, %rdx
movq %rdx, %r14
andq %r10, %r14
addq %r10, %r14
xorq %r10, %r14
orq %rdx, %r14
testb $0x1, %r14b
jne 0x5f5bb
cmpl -0x64(%rsp), %eax
jge 0x5f75e
incl %eax
movq %r14, %rdx
andq %r10, %rdx
orq %r14, %r10
notq %r10
orq %r10, %r9
shrq %r14
movq %r9, %rbx
andq %r14, %rbx
orq %r9, %r14
notq %r14
movq %r14, %r10
orq %rdx, %r10
movl -0x4c(%rsp), %r14d
addl %r15d, %r14d
js 0x5f652
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jne 0x5f611
xorl %edx, %edx
movl %eax, %ecx
leal (%r8,%rdx), %r9d
cmpl %r11d, %r9d
jge 0x5f634
btq %rdx, %r10
adcl $0x0, %ecx
btq %rdx, %rbx
sbbl $0x0, %ecx
incq %rdx
jmp 0x5f5f5
movq %rbp, %rsi
movq -0x10(%rsp), %rbp
subl %r14d, %ebp
js 0x5f62c
btq %rbp, %rdx
sbbl $0x0, %ecx
btq %rbp, %r9
adcl $0x0, %ecx
movq %rsi, %rbp
movq -0x58(%rsp), %rsi
cmpl %ebp, %ecx
jg 0x5f652
cmpl -0x6c(%rsp), %ecx
jge 0x5f652
movq -0x60(%rsp), %rdx
movl %ecx, 0x2c(%rdx)
movl %r11d, 0x14(%rdx)
movl %r15d, 0x20(%rdx)
movl %ecx, -0x6c(%rsp)
movdqa -0x38(%rsp), %xmm0
movdqa -0x28(%rsp), %xmm1
psrlq $0x1, %xmm0
movdqa %xmm0, -0x38(%rsp)
psrlq $0x1, %xmm1
movdqa %xmm1, -0x28(%rsp)
incq %r13
cmpq -0x8(%rsp), %r13
jge 0x5f698
movzbl (%rdi,%r13), %edx
movzbl (%r12,%rdx), %edx
cmpq $0x4, %rdx
jae 0x5f698
movq -0x48(%rsp), %r9
orq %r9, -0x38(%rsp,%rdx,8)
incq %r15
incq %r8
movq %rbx, %r9
jmp 0x5f57d
movq -0x40(%rsp), %rcx
movzbl (%rsi,%rcx), %ecx
movzbl (%r12,%rcx), %ecx
movq -0x38(%rsp,%rcx,8), %rdx
orq %r9, %rdx
movq %rdx, %rcx
andq %r10, %rcx
addq %r10, %rcx
xorq %r10, %rcx
orq %rdx, %rcx
testb $0x1, %cl
jne 0x5f6dc
cmpl -0x64(%rsp), %eax
jge 0x5f75e
incl %eax
movq %rcx, %rdx
andq %r10, %rdx
orq %rcx, %r10
notq %r10
orq %r10, %r9
shrq %rcx
movq %r9, %rsi
andq %rcx, %rsi
orq %r9, %rcx
notq %rcx
orq %rdx, %rcx
movl -0x68(%rsp), %edx
subl %ebp, %edx
movl -0x68(%rsp), %r8d
cmpl %r11d, %edx
jge 0x5f742
movl %ecx, %edi
andl $0x1, %edi
addl %edi, %eax
shrq %rcx
movl %esi, %edi
andl $0x1, %edi
subl %edi, %eax
shrq %rsi
incl %edx
cmpl %ebp, %eax
jg 0x5f708
cmpl -0x6c(%rsp), %eax
jge 0x5f708
movq -0x60(%rsp), %rdi
movl %eax, 0x2c(%rdi)
movl %edx, 0x14(%rdi)
movl %r8d, 0x20(%rdi)
movl %eax, -0x6c(%rsp)
jmp 0x5f708
cmpl %ebp, %eax
jg 0x5f75e
cmpl -0x6c(%rsp), %eax
jge 0x5f75e
movq -0x60(%rsp), %rcx
movl %eax, 0x2c(%rcx)
movl %edx, 0x14(%rcx)
movl -0x68(%rsp), %eax
movl %eax, 0x20(%rcx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq