// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/./Levenshtein_distance.h
// Function: ed_band_cal_extension_64_1_w(char*, int, char*, int, int, bit_extz_t*)
inline void ed_band_cal_extension_64_1_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 = INT32_MAX; ez->pe = pn-1; ez->te = tn-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;
	int32_t poff, pe = pn-1, tmp_e = INT32_MAX, k, pidx = ez->pe, tidx = ez->te;
	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[pidx-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[tidx-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).ps = pidx - pe; (*ez).ts = tidx-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[pidx-i_bd]]; 
            if(c < 4) Peq[c] |= mm;
        }
	}
	ed_core_64(Peq, VP, VN, X, D0, HN, HP, (uint8_t)tstr[tidx-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->ps = pidx-site; ez->ts = tidx+1-tn;
        }
    }
    if(err <= thre && err < ez->err) {
        ez->err = err; ez->ps = pidx-site; ez->ts = tidx+1-tn;
    }
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r8d, %r11d
movq %rdx, -0x28(%rsp)
movq %rdi, -0x30(%rsp)
movl %r8d, 0x28(%r9)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, 0x2c(%r9)
movl %esi, 0x18(%r9)
movl %ecx, 0x24(%r9)
pxor %xmm0, %xmm0
movdqu %xmm0, (%r9)
movl %eax, 0x1c(%r9)
movl $0x7fffffff, -0x7c(%rsp) # imm = 0x7FFFFFFF
movl %eax, 0x10(%r9)
movslq %esi, %rax
leaq -0x1(%rax), %rbx
movl %ebx, 0x14(%r9)
leal -0x1(%rcx), %ebp
leal (%r11,%rcx), %r14d
leal (%r11,%rsi), %edx
cmpl %ecx, %edx
movl %ecx, %r15d
movl %edx, -0x54(%rsp)
cmovll %edx, %r15d
cmpl %eax, %r14d
movq %rcx, -0x40(%rsp)
cmovll %ecx, %r15d
movq %rsi, -0x60(%rsp)
cmovgel %esi, %r14d
movl %ebp, 0x20(%r9)
leal (%r11,%r11), %eax
movl %eax, -0x78(%rsp)
movdqa %xmm0, -0x10(%rsp)
leal 0x1(%r11), %eax
cmpl %r14d, %eax
cmovgel %r14d, %eax
movl $0x1, %r8d
movl %r11d, %ecx
shlq %cl, %r8
movdqa %xmm0, -0x20(%rsp)
movq $0x0, (%rsp)
movl $0x1, %ecx
movq %rcx, -0x38(%rsp)
testl %eax, %eax
jle 0x96378
movl %eax, %eax
movq %rbx, %rcx
addq -0x30(%rsp), %rcx
negq %rax
xorl %edx, %edx
leaq 0x1a38c5(%rip), %rsi    # 0x239c20
movq %r8, %rdi
movzbl (%rcx,%rdx), %r10d
movzbl (%rsi,%r10), %r10d
orq %rdi, -0x20(%rsp,%r10,8)
addq %rdi, %rdi
decq %rdx
cmpq %rdx, %rax
jne 0x9635e
leal -0x1(%r15), %edx
leal (%r11,%r11,2), %eax
movl %eax, -0x74(%rsp)
leal -0x1(%r14), %r13d
movq $0x0, (%rsp)
leaq -0x1(%r8), %r12
movl -0x78(%rsp), %esi
movl %esi, %ecx
orb $0x1, %cl
movq $-0x1, %rax
shlq %cl, %rax
movl %esi, %ecx
shlq %cl, -0x38(%rsp)
negq %r8
xorq %rax, %r8
xorl %eax, %eax
movq %r15, -0x48(%rsp)
cmpl $0x2, %r15d
movq %r9, -0x68(%rsp)
movq %rbx, 0x18(%rsp)
movl %ebp, -0x50(%rsp)
movq %r14, 0x10(%rsp)
jl 0x96743
movl %r11d, %eax
subl %r14d, %eax
incl %eax
movl %eax, -0x4c(%rsp)
movq -0x60(%rsp), %rax
subl %r14d, %eax
movq %rax, -0x60(%rsp)
movslq %ebp, %rax
movq %rax, 0x40(%rsp)
movslq %r11d, %r9
movslq %r13d, %rax
movslq %r14d, %rcx
movq %rcx, 0x38(%rsp)
movslq %ebx, %rcx
movq %rcx, 0x28(%rsp)
movl %edx, -0x58(%rsp)
movl %edx, %ecx
movq %rcx, 0x30(%rsp)
addq %r9, %rax
movq %rax, 0x20(%rsp)
movl $0x1, %r15d
subl %r11d, %r15d
leal -0x1(%r11), %esi
movl $0x7fffffff, -0x7c(%rsp) # imm = 0x7FFFFFFF
xorl %edi, %edi
movaps 0x19b0b0(%rip), %xmm2   # 0x2314f0
movdqa 0x19b0e8(%rip), %xmm3   # 0x231530
movdqa 0x19ae90(%rip), %xmm4   # 0x2312e0
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %r11d, %ebp
movq %r11, -0x70(%rsp)
movq %r9, %r14
cmpl %r13d, %r15d
movl %r13d, %eax
cmovgl %r15d, %eax
movl %edi, %edx
subl %r11d, %edx
incl %edx
cmpl %r13d, %edx
movl %r13d, %r10d
cmovlel %r13d, %edx
movq 0x40(%rsp), %rbx
subq %rdi, %rbx
movq -0x28(%rsp), %r9
movzbl (%r9,%rbx), %r9d
leaq 0x1a378c(%rip), %r11    # 0x239c20
movzbl (%r11,%r9), %r9d
movq -0x20(%rsp,%r9,8), %r9
orq %r12, %r9
movq %r9, %r13
andq %r8, %r13
addq %r8, %r13
xorq %r8, %r13
orq %r9, %r13
testb $0x1, %r13b
jne 0x964c2
cmpl -0x74(%rsp), %ebp
jge 0x96846
incl %ebp
movq %r13, %r9
andq %r8, %r9
orq %r13, %r8
notq %r8
movq %r12, %r11
orq %r8, %r11
shrq %r13
movq %r11, %r12
andq %r13, %r12
orq %r11, %r13
notq %r13
movq %r13, %r8
orq %r9, %r8
movl -0x4c(%rsp), %r13d
addl %edi, %r13d
js 0x96676
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jne 0x96683
movl %ebp, %ecx
cmpq 0x20(%rsp), %rdi
movq -0x70(%rsp), %r11
movl %r10d, %r13d
movq %r14, %r9
jge 0x966a5
addl %esi, %eax
addq $0x4, %rax
andq $-0x4, %rax
movl %edi, %ecx
notl %ecx
addl %r11d, %ecx
addl %ecx, %edx
movd %ebp, %xmm5
movd %edx, %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 %r12, %xmm6
pshufd $0x44, %xmm6, %xmm11    # xmm11 = xmm6[0,1,0,1]
movdqa 0x19ad77(%rip), %xmm12  # 0x2312d0
movdqa 0x19ad5e(%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, %rax
jne 0x96562
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 0x966a5
movq -0x70(%rsp), %r11
movl %r10d, %r13d
movq %r14, %r9
jmp 0x966d0
movl -0x78(%rsp), %eax
subl %r13d, %eax
js 0x9669a
btq %rax, %r9
sbbl $0x0, %ecx
btq %rax, %r11
adcl $0x0, %ecx
movq -0x70(%rsp), %r11
movl %r10d, %r13d
movq %r14, %r9
cmpl %r11d, %ecx
setle %al
cmpl -0x7c(%rsp), %ecx
setl %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x966d0
movq -0x68(%rsp), %rax
movl %ecx, 0x2c(%rax)
movq -0x60(%rsp), %rdx
movl %edx, 0x10(%rax)
movl %ebx, 0x1c(%rax)
movl %ecx, -0x7c(%rsp)
movdqa -0x20(%rsp), %xmm0
movdqa -0x10(%rsp), %xmm1
psrlq $0x1, %xmm0
movdqa %xmm0, -0x20(%rsp)
psrlq $0x1, %xmm1
movdqa %xmm1, -0x10(%rsp)
incq %r9
cmpq 0x38(%rsp), %r9
jge 0x96728
movq 0x28(%rsp), %rax
subq %r9, %rax
movq -0x30(%rsp), %rdx
movzbl (%rdx,%rax), %eax
leaq 0x1a350c(%rip), %rdx    # 0x239c20
movzbl (%rdx,%rax), %eax
cmpq $0x3, %rax
ja 0x96728
movq -0x38(%rsp), %rdx
orq %rdx, -0x20(%rsp,%rax,8)
incq %rdi
incl %r15d
decl %esi
cmpq 0x30(%rsp), %rdi
jne 0x9645d
movl -0x58(%rsp), %edx
movl %edx, %eax
jmp 0x96746
movl %r11d, %ebp
movl -0x50(%rsp), %ecx
subl %eax, %ecx
movslq %ecx, %rax
movq -0x28(%rsp), %rcx
movzbl (%rcx,%rax), %eax
leaq 0x1a34c1(%rip), %rcx    # 0x239c20
movzbl (%rcx,%rax), %eax
movq -0x20(%rsp,%rax,8), %rcx
orq %r12, %rcx
movq %rcx, %rax
andq %r8, %rax
addq %r8, %rax
xorq %r8, %rax
orq %rcx, %rax
testb $0x1, %al
jne 0x9678a
cmpl -0x74(%rsp), %ebp
jge 0x96846
incl %ebp
subl %r11d, %edx
cmpl %r13d, %edx
jge 0x96819
movq %rax, %rdx
andq %r8, %rdx
orq %rax, %r8
notq %r8
orq %r8, %r12
shrq %rax
movq %r12, %rcx
andq %rax, %rcx
orq %r12, %rax
notq %rax
orq %rdx, %rax
movq -0x40(%rsp), %rdx
movq -0x48(%rsp), %rsi
subl %esi, %edx
movq 0x10(%rsp), %r10
addl %r11d, %r10d
subl %esi, %r10d
notl %esi
addl -0x54(%rsp), %esi
movq -0x68(%rsp), %r8
movl -0x7c(%rsp), %r9d
movl %eax, %edi
andl $0x1, %edi
addl %edi, %ebp
movl %ecx, %edi
andl $0x1, %edi
subl %edi, %ebp
cmpl %r11d, %ebp
jg 0x96805
cmpl %r9d, %ebp
jge 0x96805
movl %ebp, 0x2c(%r8)
movl %esi, 0x10(%r8)
movl %edx, 0x1c(%r8)
movl %ebp, %r9d
shrq %rax
shrq %rcx
decl %esi
decl %r10d
jne 0x967de
movl %r9d, -0x7c(%rsp)
jmp 0x9681c
movl %edx, %r13d
cmpl %r11d, %ebp
jg 0x96846
cmpl -0x7c(%rsp), %ebp
jge 0x96846
movq -0x68(%rsp), %rax
movl %ebp, 0x2c(%rax)
movq 0x18(%rsp), %rcx
subl %r13d, %ecx
movl %ecx, 0x10(%rax)
movq -0x40(%rsp), %rcx
subl -0x48(%rsp), %ecx
movl %ecx, 0x1c(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq