// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/./Levenshtein_distance.h
// Function: gen_trace(bit_extz_t*, int, int)
inline void gen_trace(bit_extz_t *ez, int32_t ptrim, int32_t reverse)///ptrim = thre for global and extension; = 0 for semi
{
	if(ez->err > ez->thre) return;
	ez->cigar.n = 0;
	int32_t V, H, D, min, cur, tn = (ez->te+1-ez->ts), pn = tn + (ez->thre<<1), bd = (ez->thre<<1)+1;
	int32_t bs = ez->path.n/tn, bbs = bs/5, poff = ez->pe, sft = bd - (pn - ez->pe - ptrim);
	int32_t i = tn, low = bd-1, wi, wm, d = 0, pd = -1, pdn = 0; cur = ez->err;
	w_sig *D0, *VP, *VN, *HP, *HN;

	// fprintf(stderr, "\n[M::%s::] bs::%d, bbs::%d, cur::%d, poff::%d, sft::%d\n", __func__, bs, bbs, cur, poff, sft);

	while (i > 0 && cur > 0) {
		D0 = ez->path.a + ((i-1)*bs); VP = D0 + bbs; VN = VP + bbs; HP = VN + bbs; HN = HP + bbs;

		wi = (sft>>bitw); wm = sft&bitz;
		D = cur - ((~(D0[wi]>>wm))&(1ULL)); d = 0; min = D;
		H = V = INT32_MAX;
		if(sft!=low) {
            H = cur + ((HN[wi]>> wm)&(1ULL)) - ((HP[wi]>> wm)&(1ULL)); 
            if ((H+1) == cur && H <= min) {//prefer indels
                min = H; d = 3;
            }
		}
		if(sft!=0) {
			wi = ((sft-1)>>bitw); wm = (sft-1)&bitz;
			V = cur + ((VN[wi]>> wm)&(1ULL)) - ((VP[wi]>> wm)&(1ULL)); 
			if ((V+1) == cur && V <= min) {//prefer indels
				min = V; d = 2;
			}
		}
		// fprintf(stderr, "[M::%s::] cur::%d, D::%d, V::%d, H::%d, poff::%d, toff::%d, d::%d\n", __func__, cur, D, V, H, poff, i, d);
		if(d == 0) {
			if(D != cur) d = 1;
			i--; poff--;
		} else if(d == 2) {//more pstr
			sft--; poff--;
		} else if(d == 3) {///more tstr
			i--; sft++;
		}

		if(d == pd) {
			pdn++;
		} else {
			if(pdn > 0) push_trace(&(ez->cigar), pd, pdn);
			pd = d; pdn = 1;
		}
		cur = min;
	}

	if (i > 0) {
        d = 0; poff -= i;
		if(d == pd) {
			pdn += i;
		} else {
			if(pdn > 0) push_trace(&(ez->cigar), pd, pdn);
			pd = d; pdn = i;
		}
    }

	poff++;
	// fprintf(stderr, "[M::%s::] poff::%d, ez->ps::%d\n", __func__, poff, ez->ps);
	if(ez->ps < 0 || ez->ps >= ez->pl) {//ps is unavailable
		ez->ps = poff;
	} else if(poff > ez->ps){
		d = 2; i = poff - ez->ps;
		if(d == pd) {
			pdn += i;
		} else {
			if(pdn > 0) push_trace(&(ez->cigar), pd, pdn);
			pd = d; pdn = i;
		}
	}
	if(pdn > 0) push_trace(&(ez->cigar), pd, pdn);

	if(reverse) {
		uint16_t t; pdn = ez->cigar.n>>1; 
		for (i = 0; i < pdn; i++) {
			t = ez->cigar.a[i]; 
			ez->cigar.a[i] = ez->cigar.a[ez->cigar.n-i-1]; 
			ez->cigar.a[ez->cigar.n-i-1] = t;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl 0x28(%rdi), %ecx
movl 0x2c(%rdi), %r14d
cmpl %ecx, %r14d
jg 0x66258
movq %rdi, %rbx
movl %edx, 0x20(%rsp)
andq $0x0, 0xa8(%rdi)
leaq 0xa8(%rdi), %rax
movq %rax, 0x18(%rsp)
movl 0x14(%rdi), %r10d
movl 0x20(%rdi), %r9d
subl 0x1c(%rdi), %r9d
incl %r9d
addl %ecx, %ecx
movl %ecx, 0x24(%rsp)
movq 0xc0(%rdi), %rax
movslq %r9d, %rdi
xorl %ecx, %ecx
xorl %edx, %edx
divq %rdi
pushq $0x5
popq %r8
movq %rax, 0x30(%rsp)
cltd
idivl %r8d
subl %edi, %esi
movq %r10, 0x10(%rsp)
leal (%rsi,%r10), %r12d
incl %r12d
movslq %eax, %rbp
pushq $-0x1
popq %r13
movl %r13d, 0xc(%rsp)
negl %ecx
movl %ecx, %edx
movl %r14d, %edi
testl %r9d, %r9d
jle 0x6618c
testl %edi, %edi
jle 0x6618c
movq %r9, 0x28(%rsp)
leal -0x1(%r9), %eax
movl %eax, %ecx
imull 0x30(%rsp), %ecx
movslq %ecx, %r8
shlq $0x3, %r8
movq %rbx, %r15
addq 0xd0(%rbx), %r8
leaq (%r8,%rbp,8), %r9
movl %r12d, %esi
sarl $0x6, %esi
movl %r12d, %ecx
andl $0x3f, %ecx
movslq %esi, %rsi
movq (%r8,%rsi,8), %r13
shrq %cl, %r13
leaq (%r9,%rbp,8), %r10
movl %edi, %ebx
notl %r13d
andl $0x1, %r13d
movl %edi, %r14d
subl %r13d, %r14d
xorl %r11d, %r11d
cmpl 0x24(%rsp), %r12d
je 0x660bc
leaq (%r10,%rbp,8), %r11
leaq (%r11,%rsi,8), %rsi
movq (%rsi,%rbp,8), %r11
btq %rcx, %r11
movq %rbx, %r8
adcq $0x0, %r8
movq (%rsi), %rsi
btq %rcx, %rsi
sbbq $0x0, %r8
leal 0x1(%r8), %ecx
cmpl %edi, %ecx
setne %cl
cmpl %r8d, %r14d
setl %sil
orb %cl, %sil
movl %esi, %ecx
xorb $0x1, %cl
movzbl %cl, %ecx
leal (%rcx,%rcx,2), %r11d
testb %sil, %sil
cmovnel %r14d, %r8d
movl %r8d, %r14d
testl %r12d, %r12d
je 0x660f0
leal -0x1(%r12), %ecx
movl %ecx, %esi
sarl $0x6, %esi
movslq %esi, %rsi
movq (%r10,%rsi,8), %r8
btq %rcx, %r8
adcl $0x0, %ebx
movq (%r9,%rsi,8), %rsi
btq %rcx, %rsi
sbbl $0x0, %ebx
leal 0x1(%rbx), %esi
cmpl %edi, %esi
jne 0x660f0
cmpl %ebx, %r14d
jge 0x6611d
movl %r11d, %ecx
leaq 0x17d57e(%rip), %rsi    # 0x1e3678
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
movq 0x10(%rsp), %rcx
decl %ecx
movq %rcx, 0x10(%rsp)
movl %eax, %r9d
jmp 0x6614d
decl %r12d
movl %r12d, %ecx
movl %r14d, %ebx
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x10(%rsp)
movl %ebx, %r14d
pushq $0x2
popq %rax
movl %eax, %r13d
movl %ecx, %r12d
jmp 0x66148
incl %r12d
movl %eax, %r9d
pushq $0x3
popq %rax
movl %eax, %r13d
jmp 0x6614d
movl %r11d, %r13d
movq 0x28(%rsp), %r9
cmpl 0xc(%rsp), %r13d
movq %r15, %rbx
jne 0x6615e
decl %edx
jmp 0x66005
pushq $0x1
popq %rax
movl %eax, %ecx
testl %edx, %edx
je 0x65ffc
negl %edx
movzwl 0xc(%rsp), %esi
movq 0x18(%rsp), %rdi
movq %r9, %r15
callq 0x62885
movq %r15, %r9
pushq $0x1
popq %rax
movl %eax, %ecx
jmp 0x65ffc
movl %edx, %eax
negl %eax
testl %r9d, %r9d
jle 0x661c3
movq 0x10(%rsp), %r15
subl %r9d, %r15d
cmpl $0x0, 0xc(%rsp)
je 0x661d1
xorl %ebp, %ebp
testl %edx, %edx
je 0x661d6
movzwl 0xc(%rsp), %esi
movq 0x18(%rsp), %rdi
movl %eax, %edx
movq %r9, %r14
callq 0x62885
movq %r14, %r9
jmp 0x661d6
movl 0xc(%rsp), %ebp
movl %eax, %r9d
movq 0x10(%rsp), %r15
jmp 0x661d6
subl %edx, %r9d
xorl %ebp, %ebp
leal 0x1(%r15), %r14d
movl 0x10(%rbx), %eax
testl %eax, %eax
js 0x661fe
cmpl 0x18(%rbx), %eax
jge 0x661fe
cmpl %eax, %r15d
jl 0x66202
subl %eax, %r14d
cmpl $0x2, %ebp
jne 0x66267
addl %r9d, %r14d
pushq $0x2
popq %rbp
jmp 0x66283
movl %r14d, 0x10(%rbx)
testl %r9d, %r9d
jle 0x66217
movzwl %bp, %esi
movq 0x18(%rsp), %rdi
movl %r9d, %edx
callq 0x62885
cmpl $0x0, 0x20(%rsp)
je 0x66258
movq 0xa8(%rbx), %rax
movq %rax, %rcx
shrq %rcx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
decq %rax
cmpq %rdx, %rcx
je 0x66258
movq 0xb8(%rbx), %rsi
movzwl (%rsi,%rdx,2), %edi
movzwl (%rsi,%rax,2), %r8d
movw %r8w, (%rsi,%rdx,2)
movw %di, (%rsi,%rax,2)
incq %rdx
jmp 0x66232
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x2
popq %r12
testl %r9d, %r9d
jle 0x66280
movzwl %bp, %esi
movq 0x18(%rsp), %rdi
movl %r9d, %edx
callq 0x62885
movl %r12d, %ebp
movl %r14d, %r9d
jmp 0x66202
nop