// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rdx, -0x38(%rsp)
movq %rdi, -0x40(%rsp)
movl %r8d, 0x28(%r9)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, 0x2c(%r9)
movl %esi, 0x18(%r9)
movl %ecx, 0x24(%r9)
xorps %xmm0, %xmm0
movups %xmm0, (%r9)
movl %eax, 0x1c(%r9)
movl %eax, 0x10(%r9)
movslq %esi, %rax
leaq -0x1(%rax), %r11
movl %r11d, 0x14(%r9)
leal -0x1(%rcx), %edi
leal (%r8,%rcx), %ebx
leal (%r8,%rsi), %edx
cmpl %ecx, %edx
movl %ecx, %r14d
movl %edx, -0x60(%rsp)
cmovll %edx, %r14d
cmpl %eax, %ebx
movq %rcx, -0x50(%rsp)
cmovll %ecx, %r14d
movq %rsi, -0x68(%rsp)
cmovgel %esi, %ebx
movl %edi, -0x7c(%rsp)
movl %edi, 0x20(%r9)
leal (%r8,%r8), %eax
movl %eax, -0x78(%rsp)
movaps %xmm0, -0x20(%rsp)
movaps %xmm0, -0x30(%rsp)
leal 0x1(%r8), %eax
cmpl %ebx, %eax
cmovgel %ebx, %eax
movq $0x0, -0x10(%rsp)
movl $0x1, %ecx
movq %rcx, -0x48(%rsp)
movl $0x1, %esi
movq %r8, -0x70(%rsp)
movl %r8d, %ecx
shlq %cl, %rsi
testl %eax, %eax
jle 0x95cc8
movl %eax, %eax
movq %r11, %rcx
addq -0x40(%rsp), %rcx
negq %rax
xorl %edx, %edx
leaq 0x197d65(%rip), %rdi    # 0x22da10
movq %rsi, %r8
movzbl (%rcx,%rdx), %r10d
movzbl (%rdi,%r10), %r10d
orq %r8, -0x30(%rsp,%r10,8)
addq %r8, %r8
decq %rdx
cmpq %rdx, %rax
jne 0x95cae
leal -0x1(%r14), %r15d
movq -0x70(%rsp), %r8
leal (%r8,%r8,2), %eax
movl %eax, -0x74(%rsp)
leal -0x1(%rbx), %ebp
movq $0x0, -0x10(%rsp)
movq %rbx, %rdx
leaq -0x1(%rsi), %r13
movl -0x78(%rsp), %edi
movl %edi, %ecx
orb $0x1, %cl
movq $-0x1, %rax
shlq %cl, %rax
movl %edi, %ecx
shlq %cl, -0x48(%rsp)
negq %rsi
xorq %rax, %rsi
xorl %ebx, %ebx
movq %r14, -0x58(%rsp)
cmpl $0x2, %r14d
movq %r11, -0x8(%rsp)
movq %rdx, (%rsp)
jl 0x95ecb
movl %r8d, %eax
subl %edx, %eax
incl %eax
movl %eax, -0x5c(%rsp)
movq -0x68(%rsp), %rax
subl %edx, %eax
movq %rax, -0x68(%rsp)
movslq -0x7c(%rsp), %rax
movq %rax, 0x30(%rsp)
movslq %r8d, %rdi
movslq %ebp, %rax
movq %rax, 0x8(%rsp)
movslq %edx, %rax
movq %rax, 0x28(%rsp)
movslq %r11d, %rax
movq %rax, 0x18(%rsp)
movl %r15d, %eax
movq %rax, 0x20(%rsp)
movq %r8, %rax
negl %r8d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %ebx, %ebx
movq %rdi, 0x10(%rsp)
movq %rdi, %rdx
movl %eax, %r14d
movq 0x30(%rsp), %r10
subq %rbx, %r10
movq -0x38(%rsp), %rax
movzbl (%rax,%r10), %eax
leaq 0x197c75(%rip), %rdi    # 0x22da10
movzbl (%rdi,%rax), %eax
movq -0x30(%rsp,%rax,8), %rax
orq %r13, %rax
movq %rax, %r12
andq %rsi, %r12
addq %rsi, %r12
xorq %rsi, %r12
orq %rax, %r12
testb $0x1, %r12b
jne 0x95dca
cmpl -0x74(%rsp), %r14d
jge 0x95fbd
incl %r14d
movq %r12, %rdi
andq %rsi, %rdi
orq %r12, %rsi
notq %rsi
movq %r13, %r11
orq %rsi, %r11
shrq %r12
movq %r11, %r13
andq %r12, %r13
orq %r11, %r12
notq %r12
movq %r12, %rsi
orq %rdi, %rsi
movl -0x5c(%rsp), %r12d
addl %ebx, %r12d
js 0x95e69
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jne 0x95e35
movq %rbx, %rax
subq 0x10(%rsp), %rax
movl %r14d, %ecx
cmpq 0x8(%rsp), %rax
jge 0x95e4c
xorl %edi, %edi
movl %r14d, %ecx
btq %rdi, %rsi
adcl $0x0, %ecx
btq %rdi, %r13
sbbl $0x0, %ecx
incq %rdi
leal (%rdi,%r8), %eax
cmpl %ebp, %eax
jl 0x95e1a
jmp 0x95e4c
movl -0x78(%rsp), %eax
subl %r12d, %eax
js 0x95e4c
btq %rax, %rdi
sbbl $0x0, %ecx
btq %rax, %r11
adcl $0x0, %ecx
cmpl -0x70(%rsp), %ecx
jg 0x95e69
cmpl 0x2c(%r9), %ecx
jge 0x95e69
movl %ecx, 0x2c(%r9)
movq -0x68(%rsp), %rax
movl %eax, 0x10(%r9)
movl %r10d, 0x1c(%r9)
shrq -0x30(%rsp)
shrq -0x28(%rsp)
shrq -0x20(%rsp)
shrq -0x18(%rsp)
incq %rdx
cmpq 0x28(%rsp), %rdx
jge 0x95eb3
movq 0x18(%rsp), %rax
subq %rdx, %rax
movq -0x40(%rsp), %rdi
movzbl (%rdi,%rax), %eax
leaq 0x197b71(%rip), %rdi    # 0x22da10
movzbl (%rdi,%rax), %eax
cmpq $0x3, %rax
ja 0x95eb3
movq -0x48(%rsp), %rdi
orq %rdi, -0x30(%rsp,%rax,8)
incq %rbx
incq %r8
cmpq 0x20(%rsp), %rbx
jne 0x95d82
movq -0x70(%rsp), %r8
jmp 0x95ece
movl %r8d, %r14d
movl -0x7c(%rsp), %eax
subl %ebx, %eax
cltq
movq -0x38(%rsp), %rcx
movzbl (%rcx,%rax), %eax
leaq 0x197b2a(%rip), %rcx    # 0x22da10
movzbl (%rcx,%rax), %eax
movq -0x30(%rsp,%rax,8), %rcx
orq %r13, %rcx
movq %rcx, %rax
andq %rsi, %rax
addq %rsi, %rax
xorq %rsi, %rax
orq %rcx, %rax
testb $0x1, %al
jne 0x95f18
cmpl -0x74(%rsp), %r14d
jge 0x95fbd
incl %r14d
movq -0x70(%rsp), %r8
subl %r8d, %r15d
cmpl %ebp, %r15d
jge 0x95f95
movq %rax, %rdx
andq %rsi, %rdx
orq %rax, %rsi
notq %rsi
orq %rsi, %r13
shrq %rax
movq %r13, %rcx
andq %rax, %rcx
orq %r13, %rax
notq %rax
orq %rdx, %rax
movq -0x50(%rsp), %rdx
movq -0x58(%rsp), %rsi
subl %esi, %edx
movq (%rsp), %r10
negl %r10d
notl %esi
addl -0x60(%rsp), %esi
movl %eax, %edi
andl $0x1, %edi
addl %edi, %r14d
movl %ecx, %edi
andl $0x1, %edi
subl %edi, %r14d
cmpl %r8d, %r14d
jg 0x95f81
cmpl 0x2c(%r9), %r14d
jge 0x95f81
movl %r14d, 0x2c(%r9)
movl %esi, 0x10(%r9)
movl %edx, 0x1c(%r9)
shrq %rax
shrq %rcx
incl %r15d
leal (%r10,%r15), %edi
decl %esi
cmpl $-0x1, %edi
jne 0x95f5a
cmpl %r8d, %r14d
jg 0x95fbd
cmpl 0x2c(%r9), %r14d
jge 0x95fbd
movl %r14d, 0x2c(%r9)
movq -0x8(%rsp), %rax
subl %r15d, %eax
movl %eax, 0x10(%r9)
movq -0x50(%rsp), %rax
subl -0x58(%rsp), %eax
movl %eax, 0x1c(%r9)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq