// File: /workspace/github_cpp_makefile_star2_O3/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
movl %r8d, %r14d
movq %rdx, -0x48(%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 (%r14,%rcx), %edx
leal (%r14,%rsi), %r10d
cmpl %ecx, %r10d
cmovgel %ecx, %r10d
cmpl %esi, %edx
cmovll %ecx, %r10d
cmovll %edx, %esi
movq %rax, 0x10(%r9)
leal (%r14,%r14), %eax
movl %eax, -0x70(%rsp)
movdqa %xmm0, -0x30(%rsp)
movdqa %xmm0, -0x40(%rsp)
leal 0x1(%r14), %eax
cmpl %esi, %eax
cmovgel %esi, %eax
movq $0x0, -0x20(%rsp)
movl $0x1, %ecx
movq %rcx, -0x50(%rsp)
movl $0x1, %r8d
movl %r14d, %ecx
shlq %cl, %r8
testl %eax, %eax
jle 0x94b3e
movl %eax, %eax
xorl %ecx, %ecx
leaq 0x1a50fd(%rip), %rdx    # 0x239c20
movq %r8, %r11
movzbl (%rdi,%rcx), %ebx
movzbl (%rdx,%rbx), %ebx
orq %r11, -0x40(%rsp,%rbx,8)
addq %r11, %r11
incq %rcx
cmpq %rcx, %rax
jne 0x94b26
leal -0x1(%r10), %r11d
leal (%r14,%r14,2), %eax
movl %eax, -0x6c(%rsp)
leal -0x1(%rsi), %r15d
movq $0x0, -0x20(%rsp)
movl -0x70(%rsp), %edx
movl %edx, %ecx
orb $0x1, %cl
movq $-0x1, %rax
shlq %cl, %rax
leaq -0x1(%r8), %rbx
negq %r8
xorq %rax, %r8
movl %edx, %ecx
shlq %cl, -0x50(%rsp)
cmpl $0x2, %r10d
movq %r9, -0x60(%rsp)
movl %r11d, -0x74(%rsp)
jl 0x94ed2
movq %rdi, -0x8(%rsp)
movl %r14d, %eax
subl %esi, %eax
incl %eax
movl %eax, -0x54(%rsp)
movslq %r14d, %rax
movq %rsi, -0x18(%rsp)
movslq %esi, %rcx
movq %rcx, (%rsp)
movslq %r15d, %rcx
movl %r11d, %edi
addq %rax, %rcx
movq %rcx, -0x10(%rsp)
movl $0x1, %r12d
subl %r14d, %r12d
leal -0x1(%r14), %edx
movl $0x7fffffff, -0x7c(%rsp) # imm = 0x7FFFFFFF
xorl %r11d, %r11d
movaps 0x19c915(%rip), %xmm2   # 0x2314f0
movdqa 0x19c94d(%rip), %xmm3   # 0x231530
movdqa 0x19c6f5(%rip), %xmm4   # 0x2312e0
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %r14d, %ebp
movq %r14, -0x68(%rsp)
movl %r15d, -0x78(%rsp)
cmpl %r15d, %r12d
movl %r15d, %esi
cmovgl %r12d, %esi
movl %r11d, %r13d
subl %r14d, %r13d
incl %r13d
cmpl %r15d, %r13d
cmovlel %r15d, %r13d
movq -0x48(%rsp), %r9
movzbl (%r9,%r11), %r9d
leaq 0x1a4ff8(%rip), %r14    # 0x239c20
movzbl (%r14,%r9), %r9d
movq -0x40(%rsp,%r9,8), %r9
orq %rbx, %r9
movq %r9, %r14
andq %r8, %r14
addq %r8, %r14
xorq %r8, %r14
orq %r9, %r14
testb $0x1, %r14b
jne 0x94c56
cmpl -0x6c(%rsp), %ebp
jge 0x94fb2
incl %ebp
movq %r14, %r9
andq %r8, %r9
orq %r14, %r8
notq %r8
movq %rbx, %r15
orq %r8, %r15
shrq %r14
movq %r15, %rbx
andq %r14, %rbx
orq %r15, %r14
notq %r14
movq %r14, %r8
orq %r9, %r8
movl -0x54(%rsp), %r14d
addl %r11d, %r14d
js 0x94e0c
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jne 0x94e18
movl %ebp, %ecx
cmpq -0x10(%rsp), %r11
movq -0x68(%rsp), %r14
movl -0x78(%rsp), %r15d
jge 0x94e39
addl %edx, %esi
addq $0x4, %rsi
andq $-0x4, %rsi
movl %r11d, %ecx
notl %ecx
addl %r14d, %ecx
addl %ecx, %r13d
movd %ebp, %xmm5
movd %r13d, %xmm6
pshufd $0x44, %xmm6, %xmm7     # xmm7 = xmm6[0,1,0,1]
movq %r8, %xmm6
pshufd $0x44, %xmm6, %xmm1     # xmm1 = xmm6[0,1,0,1]
movq %rbx, %xmm6
pshufd $0x44, %xmm6, %xmm11    # xmm11 = xmm6[0,1,0,1]
movdqa 0x19c5e1(%rip), %xmm12  # 0x2312d0
movdqa 0x19c5c8(%rip), %xmm13  # 0x2312c0
movdqa %xmm5, %xmm6
movdqa %xmm13, %xmm9
movdqa %xmm12, %xmm8
movdqa %xmm1, %xmm14
psrlq %xmm12, %xmm14
pshufd $0xee, %xmm12, %xmm15   # xmm15 = xmm12[2,3,2,3]
movdqa %xmm1, %xmm5
psrlq %xmm15, %xmm5
movsd %xmm14, %xmm5           # xmm5 = xmm14[0],xmm5[1]
movdqa %xmm1, %xmm14
psrlq %xmm13, %xmm14
pshufd $0xee, %xmm13, %xmm0    # xmm0 = xmm13[2,3,2,3]
movdqa %xmm1, %xmm10
psrlq %xmm0, %xmm10
movsd %xmm14, %xmm10          # xmm10 = xmm14[0],xmm10[1]
movdqa %xmm11, %xmm14
psrlq %xmm12, %xmm14
shufps $0x88, %xmm10, %xmm5    # xmm5 = xmm5[0,2],xmm10[0,2]
movdqa %xmm11, %xmm10
psrlq %xmm15, %xmm10
andps %xmm2, %xmm5
movdqa %xmm11, %xmm15
psrlq %xmm0, %xmm15
paddd %xmm6, %xmm5
movdqa %xmm11, %xmm0
psrlq %xmm13, %xmm0
movsd %xmm14, %xmm10          # xmm10 = xmm14[0],xmm10[1]
movsd %xmm0, %xmm15           # xmm15 = xmm0[0],xmm15[1]
shufps $0x88, %xmm15, %xmm10   # xmm10 = xmm10[0,2],xmm15[0,2]
andps %xmm2, %xmm10
psubd %xmm10, %xmm5
paddq %xmm3, %xmm12
paddq %xmm3, %xmm13
addq $-0x4, %rsi
jne 0x94cf8
pxor %xmm4, %xmm7
pxor %xmm4, %xmm8
movdqa %xmm8, %xmm0
pcmpgtd %xmm7, %xmm0
pxor %xmm4, %xmm9
movdqa %xmm9, %xmm1
pcmpgtd %xmm7, %xmm1
movdqa %xmm0, %xmm10
shufps $0x88, %xmm1, %xmm10    # xmm10 = xmm10[0,2],xmm1[0,2]
pcmpeqd %xmm7, %xmm8
pcmpeqd %xmm7, %xmm9
shufps $0xdd, %xmm9, %xmm8     # xmm8 = xmm8[1,3],xmm9[1,3]
andps %xmm10, %xmm8
shufps $0xdd, %xmm1, %xmm0     # xmm0 = xmm0[1,3],xmm1[1,3]
orps %xmm8, %xmm0
pand %xmm0, %xmm6
pandn %xmm5, %xmm0
por %xmm6, %xmm0
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %ecx
jmp 0x94e39
movq -0x68(%rsp), %r14
movl -0x78(%rsp), %r15d
jmp 0x94e65
movl -0x70(%rsp), %esi
subl %r14d, %esi
js 0x94e2f
btq %rsi, %r9
sbbl $0x0, %ecx
btq %rsi, %r15
adcl $0x0, %ecx
movq -0x68(%rsp), %r14
movl -0x78(%rsp), %r15d
cmpl %r14d, %ecx
setle %sil
cmpl -0x7c(%rsp), %ecx
setl %r9b
andb %sil, %r9b
cmpb $0x1, %r9b
jne 0x94e65
movq -0x60(%rsp), %rsi
movl %ecx, 0x2c(%rsi)
movl %r15d, 0x14(%rsi)
movl %r11d, 0x20(%rsi)
movl %ecx, -0x7c(%rsp)
movdqa -0x40(%rsp), %xmm0
movdqa -0x30(%rsp), %xmm1
psrlq $0x1, %xmm0
movdqa %xmm0, -0x40(%rsp)
psrlq $0x1, %xmm1
movdqa %xmm1, -0x30(%rsp)
incq %rax
cmpq (%rsp), %rax
jge 0x94eb5
movq -0x8(%rsp), %rsi
movzbl (%rsi,%rax), %esi
leaq 0x1a4d80(%rip), %r9     # 0x239c20
movzbl (%r9,%rsi), %esi
cmpq $0x3, %rsi
ja 0x94eb5
movq -0x50(%rsp), %r9
orq %r9, -0x40(%rsp,%rsi,8)
incq %r11
incl %r12d
decl %edx
cmpq %rdi, %r11
jne 0x94bfd
movq -0x18(%rsp), %rsi
movl -0x74(%rsp), %r11d
jmp 0x94edf
movl $0x7fffffff, -0x7c(%rsp) # imm = 0x7FFFFFFF
xorl %edi, %edi
movl %r14d, %ebp
movq -0x48(%rsp), %rax
movzbl (%rax,%rdi), %eax
leaq 0x1a4d31(%rip), %rcx    # 0x239c20
movzbl (%rcx,%rax), %eax
movq -0x40(%rsp,%rax,8), %rcx
orq %rbx, %rcx
movq %rcx, %rax
andq %r8, %rax
addq %r8, %rax
xorq %r8, %rax
orq %rcx, %rax
testb $0x1, %al
jne 0x94f1f
cmpl -0x6c(%rsp), %ebp
jge 0x94fb2
incl %ebp
movl -0x74(%rsp), %r11d
movl %r11d, %ecx
subl %r14d, %ecx
cmpl %r15d, %ecx
jge 0x94f95
movq %rax, %rdx
andq %r8, %rdx
orq %rax, %r8
notq %r8
orq %r8, %rbx
shrq %rax
movq %rbx, %rcx
andq %rax, %rcx
orq %rbx, %rax
notq %rax
orq %rdx, %rax
subl %r14d, %r10d
movq -0x60(%rsp), %r8
movl -0x7c(%rsp), %r9d
movl %eax, %edx
andl $0x1, %edx
addl %edx, %ebp
movl %ecx, %edx
andl $0x1, %edx
subl %edx, %ebp
cmpl %r14d, %ebp
jg 0x94f7f
cmpl %r9d, %ebp
jge 0x94f7f
movl %ebp, 0x2c(%r8)
movl %r10d, 0x14(%r8)
movl %r11d, 0x20(%r8)
movl %ebp, %r9d
shrq %rax
shrq %rcx
incl %r10d
cmpl %r10d, %esi
jne 0x94f58
movl %r9d, -0x7c(%rsp)
movl %r15d, %ecx
cmpl %r14d, %ebp
jg 0x94fb2
cmpl -0x7c(%rsp), %ebp
jge 0x94fb2
movq -0x60(%rsp), %rax
movl %ebp, 0x2c(%rax)
movl %ecx, 0x14(%rax)
movl -0x74(%rsp), %ecx
movl %ecx, 0x20(%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq