// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %rax
movq %r9, %r10
movl %r8d, %ebx
movq %rdx, -0x68(%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)
xorps %xmm0, %xmm0
movups %xmm0, (%r9)
leal (%rbx,%rcx), %edx
leal (%rbx,%rsi), %r9d
cmpl %ecx, %r9d
cmovgel %ecx, %r9d
cmpl %esi, %edx
cmovll %ecx, %r9d
cmovll %edx, %esi
movq %r10, -0x70(%rsp)
movq %rax, 0x10(%r10)
leal (%rbx,%rbx), %eax
movl %eax, -0x78(%rsp)
movaps %xmm0, -0x40(%rsp)
movaps %xmm0, -0x50(%rsp)
leal 0x1(%rbx), %edx
cmpl %esi, %edx
cmovgel %esi, %edx
movq $0x0, -0x30(%rsp)
movl $0x1, %eax
movq %rax, -0x60(%rsp)
movl $0x1, %r10d
movl %ebx, %ecx
shlq %cl, %r10
testl %edx, %edx
jle 0x94984
movl %edx, %ecx
xorl %edx, %edx
leaq 0x1990a8(%rip), %r8     # 0x22da10
movq %r10, %r11
movzbl (%rdi,%rdx), %eax
movzbl (%r8,%rax), %eax
orq %r11, -0x50(%rsp,%rax,8)
addq %r11, %r11
incq %rdx
cmpq %rdx, %rcx
jne 0x9496b
leal -0x1(%r9), %r14d
leal (%rbx,%rbx,2), %eax
movl %eax, -0x74(%rsp)
leal -0x1(%rsi), %ebp
movq $0x0, -0x30(%rsp)
movl -0x78(%rsp), %edx
movl %edx, %ecx
orb $0x1, %cl
movq $-0x1, %rax
shlq %cl, %rax
leaq -0x1(%r10), %r13
negq %r10
xorq %rax, %r10
movl %edx, %ecx
shlq %cl, -0x60(%rsp)
cmpl $0x2, %r9d
movl %r14d, -0x7c(%rsp)
jl 0x94b50
movq %rdi, -0x10(%rsp)
movl %ebx, %edi
subl %esi, %edi
incl %edi
movslq %ebx, %rdx
movq %rsi, -0x28(%rsp)
movslq %esi, %rax
movq %rax, (%rsp)
movslq %ebp, %rax
movq %rax, -0x20(%rsp)
movl %r14d, %eax
movq %rax, -0x8(%rsp)
movl %ebx, %r8d
negl %r8d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %r15d, %r15d
movq %rdx, -0x18(%rsp)
movq %rbx, -0x58(%rsp)
movl %ebx, %r11d
movq -0x68(%rsp), %rsi
movzbl (%rsi,%r15), %eax
leaq 0x198fec(%rip), %rbx    # 0x22da10
movzbl (%rbx,%rax), %eax
movq -0x50(%rsp,%rax,8), %rax
orq %r13, %rax
movq %rax, %rbx
andq %r10, %rbx
addq %r10, %rbx
xorq %r10, %rbx
orq %rax, %rbx
testb $0x1, %bl
jne 0x94a52
cmpl -0x74(%rsp), %r11d
jge 0x94c28
incl %r11d
movq %rbx, %r12
andq %r10, %r12
orq %rbx, %r10
notq %r10
movq %r13, %r14
orq %r10, %r14
shrq %rbx
movq %r14, %r13
andq %rbx, %r13
orq %r14, %rbx
notq %rbx
movq %rbx, %r10
orq %r12, %r10
movl %edi, %ebx
addl %r15d, %ebx
js 0x94aea
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jne 0x94aba
movq %r15, %rax
subq -0x18(%rsp), %rax
movl %r11d, %ecx
cmpq -0x20(%rsp), %rax
jge 0x94ad0
xorl %ebx, %ebx
movl %r11d, %ecx
btq %rbx, %r10
adcl $0x0, %ecx
btq %rbx, %r13
sbbl $0x0, %ecx
incq %rbx
leal (%rbx,%r8), %eax
cmpl %ebp, %eax
jl 0x94a9f
jmp 0x94ad0
movl -0x78(%rsp), %eax
subl %ebx, %eax
js 0x94ad0
btq %rax, %r12
sbbl $0x0, %ecx
btq %rax, %r14
adcl $0x0, %ecx
cmpl -0x58(%rsp), %ecx
jg 0x94aea
movq -0x70(%rsp), %rax
cmpl 0x2c(%rax), %ecx
jge 0x94aea
movl %ecx, 0x2c(%rax)
movl %ebp, 0x14(%rax)
movl %r15d, 0x20(%rax)
shrq -0x50(%rsp)
shrq -0x48(%rsp)
shrq -0x40(%rsp)
shrq -0x38(%rsp)
incq %rdx
cmpq (%rsp), %rdx
jge 0x94b2b
movq -0x10(%rsp), %rax
movzbl (%rax,%rdx), %eax
leaq 0x198ef9(%rip), %rbx    # 0x22da10
movzbl (%rbx,%rax), %eax
cmpq $0x3, %rax
ja 0x94b2b
movq -0x60(%rsp), %rbx
orq %rbx, -0x50(%rsp,%rax,8)
incq %r15
incq %r8
cmpq -0x8(%rsp), %r15
jne 0x94a18
movl %r15d, %ecx
movq -0x58(%rsp), %rbx
movq -0x28(%rsp), %rsi
movl -0x7c(%rsp), %r14d
jmp 0x94b55
xorl %ecx, %ecx
movl %ebx, %r11d
movq -0x68(%rsp), %rax
movzbl (%rax,%rcx), %eax
leaq 0x198eab(%rip), %rcx    # 0x22da10
movzbl (%rcx,%rax), %eax
movq -0x50(%rsp,%rax,8), %rax
orq %r13, %rax
movq %rax, %rcx
andq %r10, %rcx
addq %r10, %rcx
xorq %r10, %rcx
orq %rax, %rcx
testb $0x1, %cl
jne 0x94b98
cmpl -0x74(%rsp), %r11d
jge 0x94c28
incl %r11d
movl -0x7c(%rsp), %r14d
movl %r14d, %edx
subl %ebx, %edx
cmpl %ebp, %edx
jge 0x94c05
movq %rcx, %rax
andq %r10, %rax
orq %rcx, %r10
notq %r10
orq %r10, %r13
shrq %rcx
movq %r13, %rdx
andq %rcx, %rdx
orq %r13, %rcx
notq %rcx
orq %rax, %rcx
subl %ebx, %r9d
movq -0x70(%rsp), %r8
movl %ecx, %eax
andl $0x1, %eax
addl %eax, %r11d
movl %edx, %eax
andl $0x1, %eax
subl %eax, %r11d
cmpl %ebx, %r11d
jg 0x94bf1
cmpl 0x2c(%r8), %r11d
jge 0x94bf1
movl %r11d, 0x2c(%r8)
movl %r9d, 0x14(%r8)
movl %r14d, 0x20(%r8)
shrq %rcx
shrq %rdx
incl %r9d
cmpl %r9d, %esi
jne 0x94bca
decl %r9d
movl %r9d, %edx
cmpl %ebx, %r11d
jg 0x94c28
movq -0x70(%rsp), %rax
cmpl 0x2c(%rax), %r11d
jge 0x94c28
movq -0x70(%rsp), %rax
movl %r11d, 0x2c(%rax)
movl %edx, 0x14(%rax)
movl -0x7c(%rsp), %ecx
movl %ecx, 0x20(%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq