// File: /workspace/github_cpp_makefile_star2_O1/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, 0x14(%rsp)
cmpl %eax, %r12d
jg 0x9ca42
movq %rdi, %rbx
movl %edx, 0x18(%rsp)
movq $0x0, 0xa8(%rdi)
movl 0x20(%rdi), %ebp
subl 0x1c(%rdi), %ebp
movl 0x14(%rdi), %edi
incl %ebp
movq 0xc0(%rbx), %rax
movslq %ebp, %rcx
xorl %r9d, %r9d
xorl %edx, %edx
divq %rcx
movq %rax, %r8
testl %ecx, %ecx
setg %al
setle %cl
testl %r12d, %r12d
setle %dl
orb %cl, %dl
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %rdi, 0x8(%rsp)
jne 0x9c798
movl 0x14(%rsp), %eax
addl %eax, %eax
movl %eax, 0x14(%rsp)
subl %ebp, %esi
leal (%rsi,%rdi), %r10d
incl %r10d
movq %r8, 0x30(%rsp)
movslq %r8d, %rax
imulq $0x66666667, %rax, %rax # imm = 0x66666667
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x21, %rax
addl %ecx, %eax
movslq %eax, %r11
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
xorl %r15d, %r15d
movq %r11, 0x20(%rsp)
leal -0x1(%rbp), %eax
movl %eax, %ecx
imull 0x30(%rsp), %ecx
movslq %ecx, %r8
shlq $0x3, %r8
addq 0xd0(%rbx), %r8
leaq (%r8,%r11,8), %rdx
leaq (%rdx,%r11,8), %rsi
movl %r10d, %edi
sarl $0x6, %edi
movl %r12d, %ecx
movslq %edi, %rdi
movq (%r8,%rdi,8), %r8
xorl %r13d, %r13d
btq %r10, %r8
setb %r13b
xorl $0x1, %r13d
subl %r13d, %r12d
cmpl 0x14(%rsp), %r10d
jne 0x9c56b
xorl %edi, %edi
jmp 0x9c5c0
movl %r10d, %r8d
andl $0x3f, %r8d
leaq (%rsi,%r11,8), %r9
leaq (%r9,%rdi,8), %rdi
movq (%rdi,%r11,8), %r9
btq %r8, %r9
movq %rcx, %r9
adcq $0x0, %r9
movq (%rdi), %rdi
btq %r8, %rdi
sbbq $0x0, %r9
leal 0x1(%r9), %edi
cmpl %ecx, %edi
setne %dil
cmpl %r9d, %r12d
setl %r8b
orb %dil, %r8b
movl %r8d, %edi
xorb $0x1, %dil
movzbl %dil, %edi
leal (%rdi,%rdi,2), %edi
testb %r8b, %r8b
cmovnel %r12d, %r9d
movl %r9d, %r12d
testl %r10d, %r10d
je 0x9c60d
leal -0x1(%r10), %r8d
movl %r8d, %r9d
sarl $0x6, %r9d
movslq %r9d, %r9
movq (%rsi,%r9,8), %rsi
btq %r8, %rsi
movq %rcx, %rsi
adcq $0x0, %rsi
movq (%rdx,%r9,8), %rdx
btq %r8, %rdx
sbbq $0x0, %rsi
leal 0x1(%rsi), %edx
cmpl %ecx, %edx
setne %cl
cmpl %esi, %r12d
setl %dl
orb %cl, %dl
movl $0x2, %ecx
cmovel %ecx, %edi
cmovnel %r12d, %esi
movl %esi, %r12d
movl %r14d, %edx
cmpl $0x3, %edi
je 0x9c643
cmpl $0x2, %edi
je 0x9c62c
testl %edi, %edi
jne 0x9c650
movq 0x8(%rsp), %rcx
decl %ecx
movq %rcx, 0x8(%rsp)
jmp 0x9c64c
decl %r10d
movq 0x8(%rsp), %rax
decl %eax
movq %rax, 0x8(%rsp)
movl $0x2, %r13d
jmp 0x9c653
incl %r10d
movl $0x3, %r13d
movl %eax, %ebp
jmp 0x9c653
movl %edi, %r13d
cmpl %edx, %r13d
jne 0x9c666
incl %r15d
movl %edx, %r13d
movl %r15d, %r9d
jmp 0x9c782
movl $0x1, %r9d
testl %r15d, %r15d
jle 0x9c782
movq %rbp, 0x28(%rsp)
shll $0xe, %edx
cmpl $0x3fff, %r15d          # imm = 0x3FFF
movq %r10, %r14
movl %edx, 0x1c(%rsp)
jb 0x9c702
movl %edx, %ebp
orl $0x3fff, %ebp           # imm = 0x3FFF
movq 0xb0(%rbx), %rax
cmpq %rax, 0xa8(%rbx)
jne 0x9c6d5
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %r14, %r10
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rax
movq 0xb8(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xa8(%rbx)
movw %bp, (%rcx,%rax,2)
addl $0xffffc001, %r15d      # imm = 0xFFFFC001
cmpl $0x3ffe, %r15d          # imm = 0x3FFE
ja 0x9c695
testl %r15d, %r15d
je 0x9c772
movq 0xb0(%rbx), %rax
cmpq %rax, 0xa8(%rbx)
movq 0x28(%rsp), %rbp
jne 0x9c74c
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %r14, %r10
movq %rax, 0xb8(%rbx)
movl 0x1c(%rsp), %esi
orl %r15d, %esi
movq 0xa8(%rbx), %rax
movq 0xb8(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xa8(%rbx)
movw %si, (%rcx,%rax,2)
jmp 0x9c777
movq 0x28(%rsp), %rbp
movq 0x20(%rsp), %r11
movl $0x1, %r9d
testl %ebp, %ebp
setg %al
jle 0x9c798
movl %r9d, %r15d
movl %r13d, %r14d
testl %r12d, %r12d
jg 0x9c51e
testb %al, %al
je 0x9c8b4
movq 0x8(%rsp), %r15
subl %ebp, %r15d
testl %r13d, %r13d
je 0x9c8c1
xorl %r12d, %r12d
testl %r9d, %r9d
jle 0x9c8d4
movq %r15, 0x8(%rsp)
shll $0xe, %r13d
movq %r9, %r12
cmpl $0x3fff, %r9d           # imm = 0x3FFF
jb 0x9c84a
movl %r13d, %r14d
orl $0x3fff, %r14d          # imm = 0x3FFF
movl $0x2, %r15d
movq 0xb0(%rbx), %rax
cmpq %rax, 0xa8(%rbx)
jne 0x9c81a
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r15, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rax
movq 0xb8(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xa8(%rbx)
movw %r14w, (%rcx,%rax,2)
movq %r12, %rax
addl $0xffffc001, %eax       # imm = 0xFFFFC001
movq %rax, %r12
cmpl $0x3ffe, %eax           # imm = 0x3FFE
ja 0x9c7e2
testl %r12d, %r12d
je 0x9c8cc
orl %r12d, %r13d
movq 0xb0(%rbx), %rax
cmpq %rax, 0xa8(%rbx)
movq 0x8(%rsp), %r15
jne 0x9c894
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rax
movq 0xb8(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xa8(%rbx)
movw %r13w, (%rcx,%rax,2)
jmp 0x9c8d1
movl %r13d, %r12d
movl %r9d, %ebp
movq 0x8(%rsp), %r15
jmp 0x9c8d4
addl %ebp, %r9d
xorl %r12d, %r12d
movl %r9d, %ebp
jmp 0x9c8d4
movq 0x8(%rsp), %r15
xorl %r12d, %r12d
leal 0x1(%r15), %r14d
movl 0x10(%rbx), %eax
testl %eax, %eax
movl 0x18(%rsp), %r13d
js 0x9c903
cmpl 0x18(%rbx), %eax
jge 0x9c903
cmpl %eax, %r15d
jl 0x9c907
subl %eax, %r14d
cmpl $0x2, %r12d
jne 0x9ca51
addl %ebp, %r14d
jmp 0x9cb40
movl %r14d, 0x10(%rbx)
movq %rbp, %rax
testl %eax, %eax
jle 0x9c9f7
shll $0xe, %r12d
cmpl $0x3fff, %eax           # imm = 0x3FFF
jb 0x9c995
movl %r12d, %r14d
orl $0x3fff, %r14d          # imm = 0x3FFF
movl $0x2, %r15d
movq 0xb0(%rbx), %rax
cmpq %rax, 0xa8(%rbx)
jne 0x9c965
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r15, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rax
movq 0xb8(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xa8(%rbx)
movw %r14w, (%rcx,%rax,2)
movq %rbp, %rax
addl $0xffffc001, %eax       # imm = 0xFFFFC001
movq %rax, %rbp
cmpl $0x3ffe, %eax           # imm = 0x3FFE
ja 0x9c92d
testl %ebp, %ebp
je 0x9c9f7
orl %ebp, %r12d
movq 0xb0(%rbx), %rax
cmpq %rax, 0xa8(%rbx)
jne 0x9c9d9
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rax
movq 0xb8(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xa8(%rbx)
movw %r12w, (%rcx,%rax,2)
testl %r13d, %r13d
je 0x9ca42
movq 0xa8(%rbx), %rdx
movq %rdx, %rax
shrq %rax
testl %eax, %eax
jle 0x9ca42
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 0x9ca26
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rax
testl %eax, %eax
jle 0x9cb40
shll $0xe, %r12d
cmpl $0x3fff, %eax           # imm = 0x3FFF
jb 0x9cade
movl %r12d, %r15d
orl $0x3fff, %r15d          # imm = 0x3FFF
movq 0xb0(%rbx), %rax
cmpq %rax, 0xa8(%rbx)
jne 0x9caae
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rax
movq 0xb8(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xa8(%rbx)
movw %r15w, (%rcx,%rax,2)
movq %rbp, %rax
addl $0xffffc001, %eax       # imm = 0xFFFFC001
movq %rax, %rbp
cmpl $0x3ffe, %eax           # imm = 0x3FFE
ja 0x9ca71
testl %ebp, %ebp
je 0x9cb40
orl %ebp, %r12d
movq 0xb0(%rbx), %rax
cmpq %rax, 0xa8(%rbx)
jne 0x9cb22
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rax
movq 0xb8(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xa8(%rbx)
movw %r12w, (%rcx,%rax,2)
movl $0x2, %r12d
movl %r14d, %ebp
jmp 0x9c907
nop