// File: /workspace/github_cpp_makefile_star2_O3/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), %eax
movl 0x2c(%rdi), %r12d
movl %eax, 0x1c(%rsp)
cmpl %eax, %r12d
jg 0x9d175
movl %edx, %r13d
movq %rdi, %rbx
movq $0x0, 0xa8(%rdi)
movl 0x14(%rdi), %r15d
movl 0x20(%rdi), %r8d
subl 0x1c(%rdi), %r8d
incl %r8d
movq 0xc0(%rdi), %rax
movslq %r8d, %rcx
xorl %edi, %edi
xorl %edx, %edx
divq %rcx
movq %rax, %rdx
testl %ecx, %ecx
setle %al
testl %r12d, %r12d
setle %cl
orb %al, %cl
jne 0x9cfbf
movl %r13d, 0x4(%rsp)
movl 0x1c(%rsp), %eax
addl %eax, %eax
movl %eax, 0x1c(%rsp)
subl %r8d, %esi
movq %r15, 0x8(%rsp)
leal (%rsi,%r15), %eax
incl %eax
movq %rax, 0x10(%rsp)
movq %rdx, 0x30(%rsp)
movslq %edx, %rax
imulq $0x66666667, %rax, %rax # imm = 0x66666667
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x21, %rax
addl %ecx, %eax
movslq %eax, %r11
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %edi, %edi
xorl %r15d, %r15d
movq %r11, 0x28(%rsp)
movq %r8, 0x20(%rsp)
leal -0x1(%r8), %eax
movl %eax, %ecx
imull 0x30(%rsp), %ecx
movslq %ecx, %r10
shlq $0x3, %r10
addq 0xd0(%rbx), %r10
leaq (%r10,%r11,8), %rdx
leaq (%rdx,%r11,8), %r8
movq 0x10(%rsp), %r14
movl %r14d, %r9d
sarl $0x6, %r9d
movl %r14d, %ecx
andl $0x3f, %ecx
movl %r12d, %esi
movslq %r9d, %r9
movq (%r10,%r9,8), %r13
shrq %cl, %r13
notl %r13d
andl $0x1, %r13d
subl %r13d, %r12d
cmpl 0x1c(%rsp), %r14d
jne 0x9cc99
xorl %ecx, %ecx
jmp 0x9cce4
leaq (%r8,%r11,8), %r10
leaq (%r10,%r9,8), %r9
movq (%r9,%r11,8), %r10
btq %rcx, %r10
movq %rsi, %r10
adcq $0x0, %r10
movq (%r9), %r9
btq %rcx, %r9
sbbq $0x0, %r10
leal 0x1(%r10), %ecx
cmpl %esi, %ecx
setne %cl
cmpl %r10d, %r12d
setl %r9b
orb %cl, %r9b
movl %r9d, %ecx
xorb $0x1, %cl
movzbl %cl, %ecx
leal (%rcx,%rcx,2), %ecx
testb %r9b, %r9b
cmovnel %r12d, %r10d
movl %r10d, %r12d
cmpl $0x0, 0x10(%rsp)
je 0x9cd25
movq 0x10(%rsp), %r9
decl %r9d
movl %r9d, %r10d
sarl $0x6, %r10d
movslq %r10d, %r10
movq (%r8,%r10,8), %r8
btq %r9, %r8
movl %esi, %r8d
adcl $0x0, %r8d
movq (%rdx,%r10,8), %rdx
btq %r9, %rdx
sbbl $0x0, %r8d
leal 0x1(%r8), %edx
cmpl %esi, %edx
jne 0x9cd25
cmpl %r8d, %r12d
jge 0x9cd50
movl %ecx, %edx
leaq 0x1949fa(%rip), %rsi    # 0x231728
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
movq 0x8(%rsp), %rcx
decl %ecx
movq %rcx, 0x8(%rsp)
jmp 0x9cd7e
movq 0x10(%rsp), %r9
decl %r9d
movl %r12d, %r8d
movq 0x8(%rsp), %rax
decl %eax
movq %rax, 0x8(%rsp)
movl $0x2, %r13d
movl %r8d, %r12d
movq %r9, 0x10(%rsp)
jmp 0x9cd86
movq 0x10(%rsp), %rcx
incl %ecx
movq %rcx, 0x10(%rsp)
movl $0x3, %r13d
movl %eax, %r8d
jmp 0x9cd8b
movl %ecx, %r13d
movq 0x20(%rsp), %r8
cmpl %ebp, %r13d
jne 0x9cd9e
incl %r15d
movl %ebp, %r13d
movl %r15d, %edx
jmp 0x9ceac
movl $0x1, %edx
testl %r15d, %r15d
jle 0x9ceac
shll $0xe, %ebp
cmpl $0x3fff, %r15d          # imm = 0x3FFF
jb 0x9ce41
movq %r8, 0x20(%rsp)
movl %ebp, %r14d
orl $0x3fff, %r14d          # imm = 0x3FFF
movq 0xb8(%rbx), %rax
cmpq 0xb0(%rbx), %rdi
jne 0x9ce10
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0xb0(%rbx)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %r11
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rdi
movq %rdi, %rcx
incq %rdi
movq %rdi, 0xa8(%rbx)
movw %r14w, (%rax,%rcx,2)
addl $0xffffc001, %r15d      # imm = 0xFFFFC001
cmpl $0x3ffe, %r15d          # imm = 0x3FFE
ja 0x9cdd2
testl %r15d, %r15d
movq 0x20(%rsp), %r8
movl $0x1, %edx
je 0x9ceac
cmpq 0xb0(%rbx), %rdi
jne 0x9ce90
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
movq %r8, %r14
callq 0x2370
movq %r14, %r8
movl $0x1, %edx
movq 0x28(%rsp), %r11
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rdi
jmp 0x9ce97
movq 0xb8(%rbx), %rax
orl %r15d, %ebp
leaq 0x1(%rdi), %rcx
movq %rcx, 0xa8(%rbx)
movw %bp, (%rax,%rdi,2)
movq %rcx, %rdi
testl %r8d, %r8d
jle 0x9cec0
movl %edx, %r15d
movl %r13d, %ebp
testl %r12d, %r12d
jg 0x9cc3f
testl %r8d, %r8d
jle 0x9cfd1
movq 0x8(%rsp), %r15
subl %r8d, %r15d
testl %r13d, %r13d
je 0x9cfee
movq %r8, %r12
xorl %r14d, %r14d
testl %edx, %edx
jle 0x9d016
shll $0xe, %r13d
movl %edx, %r14d
cmpl $0x3fff, %edx           # imm = 0x3FFF
jb 0x9cf7d
movq %r15, 0x8(%rsp)
movl %r13d, %ebp
orl $0x3fff, %ebp           # imm = 0x3FFF
movq 0xb8(%rbx), %rax
movl $0x2, %r15d
cmpq 0xb0(%rbx), %rdi
jne 0x9cf4a
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
cmoveq %r15, %rsi
movq %rsi, 0xb0(%rbx)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rdi
movq %rdi, %rcx
incq %rdi
movq %rdi, 0xa8(%rbx)
movw %bp, (%rax,%rcx,2)
movl %r14d, %ecx
addl $0xffffc001, %ecx       # imm = 0xFFFFC001
movl %ecx, %r14d
cmpl $0x3ffe, %ecx           # imm = 0x3FFE
ja 0x9cf16
testl %r14d, %r14d
movq 0x8(%rsp), %r15
je 0x9d251
orl %r14d, %r13d
cmpq 0xb0(%rbx), %rdi
jne 0x9cff9
leaq (%rdi,%rdi), %rax
testq %rdi, %rdi
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rdi
jmp 0x9d000
testl %r8d, %r8d
jle 0x9cfe3
movq %r8, %r12
subl %r8d, %r15d
xorl %edi, %edi
xorl %r14d, %r14d
jmp 0x9d01b
movl %r13d, %r14d
movl %edx, %r12d
movl 0x4(%rsp), %r13d
movq 0x8(%rsp), %r15
jmp 0x9d01b
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
xorl %r12d, %r12d
jmp 0x9d01b
addl %r8d, %edx
xorl %r14d, %r14d
movl %edx, %r12d
jmp 0x9d016
movq 0xb8(%rbx), %rax
xorl %r14d, %r14d
leaq 0x1(%rdi), %rcx
movq %rcx, 0xa8(%rbx)
movw %r13w, (%rax,%rdi,2)
movq %rcx, %rdi
movl 0x4(%rsp), %r13d
leal 0x1(%r15), %ebp
movl 0x10(%rbx), %eax
testl %eax, %eax
js 0x9d044
cmpl 0x18(%rbx), %eax
jge 0x9d044
cmpl %eax, %r15d
jl 0x9d047
subl %eax, %ebp
cmpl $0x2, %r14d
jne 0x9d184
addl %r12d, %ebp
jmp 0x9d275
movl %ebp, 0x10(%rbx)
movq %r12, %rax
testl %eax, %eax
jle 0x9d12a
shll $0xe, %r14d
cmpl $0x3fff, %eax           # imm = 0x3FFF
jb 0x9d0d1
movl %r14d, %ebp
orl $0x3fff, %ebp           # imm = 0x3FFF
movq 0xb8(%rbx), %rax
movl $0x2, %r15d
cmpq 0xb0(%rbx), %rdi
jne 0x9d0a7
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
cmoveq %r15, %rsi
movq %rsi, 0xb0(%rbx)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rdi
movq %rdi, %rcx
incq %rdi
movq %rdi, 0xa8(%rbx)
movw %bp, (%rax,%rcx,2)
movq %r12, %rcx
addl $0xffffc001, %ecx       # imm = 0xFFFFC001
movq %rcx, %r12
cmpl $0x3ffe, %ecx           # imm = 0x3FFE
ja 0x9d073
testl %r12d, %r12d
je 0x9d12a
orl %r12d, %r14d
cmpq 0xb0(%rbx), %rdi
jne 0x9d113
leaq (%rdi,%rdi), %rax
testq %rdi, %rdi
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rdi
jmp 0x9d11a
movq 0xb8(%rbx), %rax
leaq 0x1(%rdi), %rcx
movq %rcx, 0xa8(%rbx)
movw %r14w, (%rax,%rdi,2)
testl %r13d, %r13d
je 0x9d175
movq 0xa8(%rbx), %rdx
movq %rdx, %rax
shrq %rax
testl %eax, %eax
jle 0x9d175
movq 0xb8(%rbx), %rcx
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rcx,%rdx,2), %rdx
addq $-0x2, %rdx
negq %rax
xorl %esi, %esi
movzwl (%rcx), %edi
movzwl (%rdx,%rsi,2), %r8d
movw %r8w, (%rcx)
movw %di, (%rdx,%rsi,2)
addq $0x2, %rcx
decq %rsi
cmpq %rsi, %rax
jne 0x9d159
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rax
testl %eax, %eax
jle 0x9d275
shll $0xe, %r14d
cmpl $0x3fff, %eax           # imm = 0x3FFF
jb 0x9d20f
movl %r14d, %r15d
orl $0x3fff, %r15d          # imm = 0x3FFF
movq 0xb8(%rbx), %rax
cmpq 0xb0(%rbx), %rdi
jne 0x9d1e4
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0xb0(%rbx)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rdi
movq %rdi, %rcx
incq %rdi
movq %rdi, 0xa8(%rbx)
movw %r15w, (%rax,%rcx,2)
movq %r12, %rcx
addl $0xffffc001, %ecx       # imm = 0xFFFFC001
movq %rcx, %r12
cmpl $0x3ffe, %ecx           # imm = 0x3FFE
ja 0x9d1ab
testl %r12d, %r12d
je 0x9d275
orl %r12d, %r14d
cmpq 0xb0(%rbx), %rdi
jne 0x9d25b
leaq (%rdi,%rdi), %rax
testq %rdi, %rdi
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rdi
jmp 0x9d262
movl 0x4(%rsp), %r13d
jmp 0x9cfcc
movq 0xb8(%rbx), %rax
leaq 0x1(%rdi), %rcx
movq %rcx, 0xa8(%rbx)
movw %r14w, (%rax,%rdi,2)
movq %rcx, %rdi
movl $0x2, %r14d
movl %ebp, %r12d
jmp 0x9d047
nop