// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movl %r8d, %r10d
movq %rdx, -0x48(%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 %eax, 0x10(%r9)
leal -0x1(%rsi), %eax
movl %eax, 0x14(%r9)
leal -0x1(%rcx), %r11d
leal (%r10,%rcx), %r8d
leal (%r10,%rsi), %edx
cmpl %ecx, %edx
cmovgel %ecx, %edx
cmpl %esi, %r8d
movq %rcx, -0x8(%rsp)
cmovll %ecx, %edx
movq %rsi, -0x68(%rsp)
cmovgel %esi, %r8d
movq %r9, -0x70(%rsp)
movl %r11d, 0x20(%r9)
andq $0x0, -0x20(%rsp)
movq %rdi, %r9
movq %rdx, -0x10(%rsp)
leal -0x1(%rdx), %ebx
leal 0x1(%r10), %esi
cmpl %r8d, %esi
cmovgel %r8d, %esi
movabsq $0x1, %rdx
movq %rdx, -0x58(%rsp)
movl %r10d, %ecx
shlq %cl, %rdx
movdqa %xmm0, -0x30(%rsp)
movdqa %xmm0, -0x40(%rsp)
xorl %ecx, %ecx
testl %esi, %esi
cmovlel %ecx, %esi
negq %rsi
leal (%r10,%r10,2), %edi
movl %edi, -0x74(%rsp)
movl %eax, -0x78(%rsp)
cltq
movq %r9, 0x10(%rsp)
movq %rax, 0x8(%rsp)
leaq (%r9,%rax), %rdi
leaq 0x18b47f(%rip), %r12    # 0x1ebb00
movq %rdx, %r9
cmpq %rcx, %rsi
je 0x6069f
movzbl (%rdi,%rcx), %eax
movzbl (%r12,%rax), %eax
orq %r9, -0x40(%rsp,%rax,8)
addq %r9, %r9
decq %rcx
jmp 0x60684
andq $0x0, -0x20(%rsp)
leal (%r10,%r10), %edi
leal -0x1(%r8), %ebp
leal 0x1(%rdi), %ecx
movabsq $-0x1, %rax
shlq %cl, %rax
leaq -0x1(%rdx), %rsi
negq %rdx
xorq %rax, %rdx
movq %rdi, (%rsp)
movl %edi, %ecx
shlq %cl, -0x58(%rsp)
movl %r10d, %eax
subl %r8d, %eax
incl %eax
movl %eax, -0x5c(%rsp)
movq -0x68(%rsp), %rax
subl %r8d, %eax
movq %rax, -0x68(%rsp)
movslq %r10d, %r15
movslq %r8d, %rax
movq %rax, 0x18(%rsp)
movl %r11d, -0x60(%rsp)
movslq %r11d, %rax
movq %rax, 0x20(%rsp)
xorl %r14d, %r14d
testl %ebx, %ebx
movq %r10, %rax
movl $0x0, %r10d
cmovgl %ebx, %r10d
movl %eax, %r8d
negl %r8d
movl $0x7fffffff, -0x7c(%rsp) # imm = 0x7FFFFFFF
movq %rax, -0x50(%rsp)
movl %eax, %edi
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %r10, %r14
je 0x6087f
movq 0x20(%rsp), %r11
subq %r14, %r11
movq -0x48(%rsp), %rax
movzbl (%rax,%r11), %eax
movzbl (%r12,%rax), %eax
movq -0x40(%rsp,%rax,8), %rax
orq %rsi, %rax
movq %rax, %r13
andq %rdx, %r13
addq %rdx, %r13
xorq %rdx, %r13
orq %rax, %r13
testb $0x1, %r13b
jne 0x60778
cmpl -0x74(%rsp), %edi
jge 0x60950
incl %edi
movq %r13, %r12
andq %rdx, %r12
orq %r13, %rdx
notq %rdx
orq %rdx, %rsi
shrq %r13
movq %rsi, %r9
andq %r13, %r9
orq %rsi, %r13
notq %r13
movq %r13, %rdx
orq %r12, %rdx
movl -0x5c(%rsp), %r13d
addl %r14d, %r13d
js 0x607d4
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jne 0x607dd
xorl %esi, %esi
movl %edi, %ecx
leaq 0x18b347(%rip), %r12    # 0x1ebb00
leal (%r8,%rsi), %eax
cmpl %ebp, %eax
jge 0x607fb
btq %rsi, %rdx
adcl $0x0, %ecx
btq %rsi, %r9
sbbl $0x0, %ecx
incq %rsi
jmp 0x607b9
leaq 0x18b325(%rip), %r12    # 0x1ebb00
jmp 0x6081f
movq (%rsp), %rax
subl %r13d, %eax
js 0x607f4
btq %rax, %r12
sbbl $0x0, %ecx
btq %rax, %rsi
adcl $0x0, %ecx
leaq 0x18b305(%rip), %r12    # 0x1ebb00
cmpl -0x50(%rsp), %ecx
jg 0x6081f
cmpl -0x7c(%rsp), %ecx
jge 0x6081f
movq -0x70(%rsp), %rax
movl %ecx, 0x2c(%rax)
movq -0x68(%rsp), %rsi
movl %esi, 0x10(%rax)
movl %r11d, 0x1c(%rax)
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 %r15
cmpq 0x18(%rsp), %r15
jge 0x60871
movq 0x8(%rsp), %rax
subq %r15, %rax
movq 0x10(%rsp), %rsi
movzbl (%rsi,%rax), %eax
movzbl (%r12,%rax), %eax
cmpq $0x4, %rax
jae 0x60871
movq -0x58(%rsp), %rsi
orq %rsi, -0x40(%rsp,%rax,8)
incq %r14
incq %r8
movq %r9, %rsi
jmp 0x6072f
movl -0x60(%rsp), %eax
subl %r10d, %eax
cltq
movq -0x48(%rsp), %rcx
movzbl (%rcx,%rax), %eax
movzbl (%r12,%rax), %eax
movq -0x40(%rsp,%rax,8), %rcx
orq %rsi, %rcx
movq %rcx, %rax
andq %rdx, %rax
addq %rdx, %rax
xorq %rdx, %rax
orq %rcx, %rax
testb $0x1, %al
jne 0x608bd
cmpl -0x74(%rsp), %edi
jge 0x60950
incl %edi
movq %rax, %r8
andq %rdx, %r8
orq %rax, %rdx
notq %rdx
orq %rdx, %rsi
shrq %rax
movq %rsi, %rcx
andq %rax, %rcx
orq %rsi, %rax
notq %rax
orq %r8, %rax
movq -0x50(%rsp), %r9
subl %r9d, %ebx
movq -0x8(%rsp), %r8
subl -0x10(%rsp), %r8d
movq -0x70(%rsp), %rsi
cmpl %ebp, %ebx
jge 0x60930
movl %eax, %edx
andl $0x1, %edx
addl %edx, %edi
shrq %rax
movl %ecx, %edx
andl $0x1, %edx
subl %edx, %edi
shrq %rcx
incl %ebx
cmpl %r9d, %edi
jg 0x608f5
cmpl -0x7c(%rsp), %edi
jge 0x608f5
movl %edi, 0x2c(%rsi)
movl -0x78(%rsp), %edx
subl %ebx, %edx
movl %edx, 0x10(%rsi)
movl %r8d, 0x1c(%rsi)
movl %edi, -0x7c(%rsp)
jmp 0x608f5
cmpl %r9d, %edi
jg 0x60950
cmpl -0x7c(%rsp), %edi
jge 0x60950
movq -0x70(%rsp), %rax
movl %edi, 0x2c(%rax)
movl -0x78(%rsp), %ecx
subl %ebx, %ecx
movl %ecx, 0x10(%rax)
movl %r8d, 0x1c(%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq