// File: /workspace/github_c_makefile_star2_O1/nh13[P]ksw/src/ksw2/ksw2_extz.c
// Function: ksw_extz
void ksw_extz(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, int8_t gapo, int8_t gape, int w, int zdrop, int flag, ksw_extz_t *ez)
{
	eh_t *eh;
	int8_t *qp; // query profile
	int32_t i, j, k, max_j = 0, gapoe = gapo + gape, n_col, *off = 0, with_cigar = !(flag&KSW_EZ_SCORE_ONLY);
	uint8_t *z = 0; // backtrack matrix; in each cell: f<<4|e<<2|h; in principle, we can halve the memory, but backtrack will be more complex

	ksw_reset_extz(ez);

	// allocate memory
	if (w < 0) w = tlen > qlen? tlen : qlen;
	n_col = qlen < 2*w+1? qlen : 2*w+1; // maximum #columns of the backtrack matrix
	qp = (int8_t*)kmalloc(km, qlen * m);
	eh = (eh_t*)kcalloc(km, qlen + 1, 8);
	if (with_cigar) {
		z = (uint8_t*)kmalloc(km, (size_t)n_col * tlen);
		off = (int32_t*)kcalloc(km, tlen, 4);
	}

	// generate the query profile
	for (k = i = 0; k < m; ++k) {
		const int8_t *p = &mat[k * m];
		for (j = 0; j < qlen; ++j) qp[i++] = p[query[j]];
	}

	// fill the first row
	eh[0].h = 0, eh[0].e = -gapoe - gapoe;
	for (j = 1; j <= qlen && j <= w; ++j)
		eh[j].h = -(gapoe + gape * (j - 1)), eh[j].e = -(gapoe + gapoe + gape * j);
	for (; j <= qlen; ++j) eh[j].h = eh[j].e = KSW_NEG_INF; // everything is -inf outside the band

	// DP loop
	for (i = 0; i < tlen; ++i) { // target sequence is in the outer loop
		int32_t f, h1, st, en, max = KSW_NEG_INF;
		int8_t *q = &qp[target[i] * qlen];
		st = i > w? i - w : 0;
		en = i + w < qlen - 1? i + w : qlen - 1;
		h1 = st > 0? KSW_NEG_INF : -(gapoe + gape * i);
		f  = st > 0? KSW_NEG_INF : -(gapoe + gapoe + gape * i);
		if (!with_cigar) {
			for (j = st; j <= en; ++j) {
				eh_t *p = &eh[j];
				int32_t h = p->h, e = p->e;
				p->h = h1;
				h += q[j];
				h = h >= e? h : e;
				h = h >= f? h : f;
				h1 = h;
				max_j = max > h? max_j : j;
				max   = max > h? max   : h;
				h -= gapoe;
				e -= gape;
				e  = e > h? e : h;
				p->e = e;
				f -= gape;
				f  = f > h? f : h;
			}
		} else if (!(flag&KSW_EZ_RIGHT)) {
			uint8_t *zi = &z[(long)i * n_col];
			off[i] = st;
			for (j = st; j <= en; ++j) {
				eh_t *p = &eh[j];
				int32_t h = p->h, e = p->e;
				uint8_t d; // direction
				p->h = h1;
				h += q[j];
				d = h >= e? 0 : 1;
				h = h >= e? h : e;
				d = h >= f? d : 2;
				h = h >= f? h : f;
				h1 = h;
				max_j = max > h? max_j : j;
				max   = max > h? max   : h;
				h -= gapoe;
				e -= gape;
				d |= e > h? 0x08 : 0;
				e  = e > h? e    : h;
				p->e = e;
				f -= gape;
				d |= f > h? 0x10 : 0; // if we want to halve the memory, use one bit only, instead of two
				f  = f > h? f    : h;
				zi[j - st] = d; // z[i,j] keeps h for the current cell and e/f for the next cell
			}
		} else {
			uint8_t *zi = &z[(long)i * n_col];
			off[i] = st;
			for (j = st; j <= en; ++j) {
				eh_t *p = &eh[j];
				int32_t h = p->h, e = p->e;
				uint8_t d; // direction
				p->h = h1;
				h += q[j];
				d = h > e? 0 : 1;
				h = h > e? h : e;
				d = h > f? d : 2;
				h = h > f? h : f;
				h1 = h;
				max_j = max >= h? max_j : j;
				max   = max >= h? max   : h;
				h -= gapoe;
				e -= gape;
				d |= e >= h? 0x08 : 0;
				e  = e >= h? e    : h;
				p->e = e;
				f -= gape;
				d |= f >= h? 0x10 : 0; // if we want to halve the memory, use one bit only, instead of two
				f  = f >= h? f    : h;
				zi[j - st] = d; // z[i,j] keeps h for the current cell and e/f for the next cell
			}
		}
		eh[j].h = h1, eh[j].e = KSW_NEG_INF;
		// update ez
		if (en == qlen - 1 && eh[qlen].h > ez->mqe)
			ez->mqe = eh[qlen].h, ez->mqe_t = i;
		if (i == tlen - 1)
			ez->mte = max, ez->mte_q = max_j;
		if (ksw_apply_zdrop(ez, 0, max, i, max_j, zdrop, gape)) break;
		if (i == tlen - 1 && en == qlen - 1)
			ez->score = eh[qlen].h;
	}
	kfree(km, qp); kfree(km, eh);
	if (with_cigar) {
		int rev_cigar = !!(flag & KSW_EZ_REV_CIGAR);
		if (!ez->zdropped && !(flag&KSW_EZ_EXTZ_ONLY))
			ksw_backtrack(km, 0, rev_cigar, 0, z, off, 0, n_col, tlen-1, qlen-1, &ez->m_cigar, &ez->n_cigar, &ez->cigar);
		else if (ez->max_t >= 0 && ez->max_q >= 0)
			ksw_backtrack(km, 0, rev_cigar, 0, z, off, 0, n_col, ez->max_t, ez->max_q, &ez->m_cigar, &ez->n_cigar, &ez->cigar);
		kfree(km, z); kfree(km, off);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r8, 0xb8(%rsp)
movl %ecx, %r8d
movq %rdx, %rbx
movl %esi, %r15d
movq %rdi, 0x38(%rsp)
movq 0x130(%rsp), %r13
movl 0x128(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0x120(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x100(%rsp), %rbp
movl 0x118(%rsp), %eax
movsbl 0x108(%rsp), %ecx
movq %rcx, 0x10(%rsp)
movsbl 0x110(%rsp), %r12d
movabsq $-0x3fffffff00000001, %rcx # imm = 0xC0000000FFFFFFFF
movq %rcx, 0x18(%r13)
movq %rcx, 0x10(%r13)
movq %rcx, 0x8(%r13)
movl $0x0, 0x24(%r13)
movq %r8, 0x20(%rsp)
cmpl %esi, %r8d
movl %esi, %edx
cmovgl %r8d, %edx
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
testl %eax, %eax
cmovnsl %eax, %edx
movq %rdx, 0x50(%rsp)
leal 0x1(,%rdx,2), %r14d
cmpl %esi, %r14d
cmovgel %esi, %r14d
movq %rcx, (%r13)
movl %r9d, 0x60(%rsp)
movl %r9d, %eax
imull %esi, %eax
movslq %eax, %rsi
callq 0x1fbb36
movq %rax, 0x58(%rsp)
movq %r15, 0x48(%rsp)
leal 0x1(%r15), %eax
movslq %eax, %rsi
movl $0x8, %edx
movq 0x38(%rsp), %rdi
callq 0x1fbc8d
movq %rax, %r15
testb $0x1, 0xc(%rsp)
movq 0x20(%rsp), %rax
movl %r14d, 0x30(%rsp)
jne 0x20095b
movslq %r14d, %rsi
cltq
movq %rax, (%rsp)
imulq %rax, %rsi
movq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x1fbb36
movq %rax, 0x18(%rsp)
movl $0x4, %edx
movq %r14, %rdi
movq (%rsp), %rsi
callq 0x1fbc8d
movq %rax, 0x28(%rsp)
jmp 0x20096d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x10(%rsp), %r14
leal (%r12,%r14), %eax
movq %rax, (%rsp)
movl 0x60(%rsp), %ecx
testb %cl, %cl
movq 0x48(%rsp), %rax
jle 0x2009c8
movl %ecx, %ecx
movl %eax, %edx
xorl %esi, %esi
xorl %edi, %edi
testl %eax, %eax
jle 0x2009c0
movq %rsi, %r8
imulq %rcx, %r8
addq %rbp, %r8
movslq %edi, %r9
addq 0x58(%rsp), %r9
xorl %r10d, %r10d
movzbl (%rbx,%r10), %r11d
movb (%r8,%r11), %r11b
movb %r11b, (%r9,%r10)
incq %r10
cmpq %r10, %rdx
jne 0x2009a8
addl %r10d, %edi
incq %rsi
cmpq %rcx, %rsi
jne 0x20098f
movl $0x0, (%r15)
movq (%rsp), %rdi
leal (%rdi,%rdi), %ecx
negl %ecx
movl %ecx, 0x74(%rsp)
movl %ecx, 0x4(%r15)
movq 0x50(%rsp), %rdx
cmpl %edx, %eax
cmovll %eax, %edx
testl %edx, %edx
jle 0x200a1f
incl %edx
leal (%r12,%r12,2), %ecx
leal (%rcx,%r14,2), %esi
negl %esi
negl %edi
movl $0x1, %ecx
movq 0x20(%rsp), %r8
movl %edi, (%r15,%rcx,8)
movl %esi, 0x4(%r15,%rcx,8)
incq %rcx
subl %r12d, %esi
subl %r12d, %edi
cmpq %rcx, %rdx
jne 0x200a06
jmp 0x200a29
movl $0x1, %ecx
movq 0x20(%rsp), %r8
cmpl %eax, %ecx
jg 0x200a51
movl %ecx, %edx
decl %ecx
leaq (%r15,%rdx,8), %rdx
addq $0x4, %rdx
movabsq $-0x3fffffff40000000, %rsi # imm = 0xC0000000C0000000
movq %rsi, -0x4(%rdx)
incl %ecx
addq $0x8, %rdx
cmpl %eax, %ecx
jl 0x200a43
testl %r8d, %r8d
jle 0x200e97
movq 0x50(%rsp), %rsi
leal (%rsi,%rsi), %ecx
leal -0x1(%rax), %edi
movslq %eax, %r8
movq 0x20(%rsp), %rdx
leal -0x1(%rdx), %r10d
movl %esi, %r9d
movq %r9, 0xa0(%rsp)
movl %edi, 0x70(%rsp)
movslq %edi, %r11
movl %r12d, %edi
movq %rdi, 0x90(%rsp)
movl %edx, %edx
movq %rdx, 0x88(%rsp)
movslq %esi, %rdx
notq %rdx
movq %rdx, 0x40(%rsp)
movl %esi, %edx
movq (%rsp), %rsi
negl %edx
movl %edx, 0x6c(%rsp)
movslq %ecx, %rdx
incq %rdx
cmpq %r8, %rdx
movq %r8, 0x78(%rsp)
cmovgeq %r8, %rdx
movq %rdx, 0x80(%rsp)
movq 0x18(%rsp), %rcx
movq %rcx, 0x10(%rsp)
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %r13, 0xb0(%rsp)
movq %r10, 0xa8(%rsp)
movq %r11, 0x98(%rsp)
movq 0x50(%rsp), %rcx
movl %ecx, %r10d
subl %r14d, %r10d
movq %r14, %rdi
cmovgl %ecx, %r14d
negl %r10d
movl %r14d, %ebp
addl 0x6c(%rsp), %ebp
movq 0xb8(%rsp), %rcx
movzbl (%rcx,%rdi), %ecx
imull %eax, %ecx
movslq %ecx, %r9
addq 0x58(%rsp), %r9
movq 0xa0(%rsp), %rax
cmpq %rax, %rdi
movl $0x0, %ecx
cmovbel %ecx, %r10d
addq %rdi, %rax
movl 0x70(%rsp), %r13d
cmpl %r13d, %eax
movq %rax, 0xc0(%rsp)
cmovll %eax, %r13d
movl %edi, %ecx
imull 0x90(%rsp), %ecx
movl 0x74(%rsp), %edx
subl %ecx, %edx
leal (%rsi,%rcx), %r8d
negl %r8d
testl %r10d, %r10d
movl $0xc0000000, %eax       # imm = 0xC0000000
cmovgl %eax, %r8d
cmovgl %eax, %edx
movl 0xc(%rsp), %eax
testb $0x1, %al
movq %rdi, 0x60(%rsp)
jne 0x200c41
movq 0x28(%rsp), %rcx
movl %r10d, (%rcx,%rdi,4)
movl $0xc0000000, %edi       # imm = 0xC0000000
testb $0x2, %al
jne 0x200cb3
cmpl %r13d, %r10d
jg 0x200cbc
movq 0x40(%rsp), %rax
addq %rax, %r14
movslq %r10d, %rcx
movslq %r13d, %r13
movq 0x10(%rsp), %r10
subq %rcx, %r10
movl 0x8(%r15,%r14,8), %r11d
movl 0xc(%r15,%r14,8), %ecx
movl %r8d, 0x8(%r15,%r14,8)
movsbl 0x1(%r9,%r14), %r8d
addl %r11d, %r8d
xorl %r11d, %r11d
cmpl %ecx, %r8d
setl %r11b
cmovlel %ecx, %r8d
cmpl %edx, %r8d
cmovlel %edx, %r8d
movl $0x2, %eax
cmovll %eax, %r11d
cmpl %r8d, %edi
cmovlel %ebp, %ebx
cmovlel %r8d, %edi
movl %r8d, %eax
subl %esi, %eax
subl %r12d, %ecx
cmpl %eax, %ecx
setg %sil
cmovlel %eax, %ecx
shlb $0x3, %sil
orb %r11b, %sil
subl %r12d, %edx
cmpl %eax, %edx
cmovlel %eax, %edx
movl %ecx, 0xc(%r15,%r14,8)
setg %al
shlb $0x4, %al
orb %sil, %al
movq (%rsp), %rsi
movb %al, 0x1(%r10,%r14)
incq %r14
incl %ebp
cmpq %r13, %r14
jl 0x200bba
jmp 0x200cbf
movl $0xc0000000, %edi       # imm = 0xC0000000
cmpl %r13d, %r10d
jg 0x200cbc
movq 0x40(%rsp), %rax
leaq (%rax,%r14), %r10
movslq %r13d, %r14
movl 0x8(%r15,%r10,8), %r13d
movl 0xc(%r15,%r10,8), %ecx
movl %r8d, 0x8(%r15,%r10,8)
movsbl 0x1(%r9,%r10), %r8d
addl %r13d, %r8d
cmpl %ecx, %r8d
cmovlel %ecx, %r8d
cmpl %edx, %r8d
cmovlel %edx, %r8d
cmpl %r8d, %edi
cmovlel %ebp, %ebx
cmovlel %r8d, %edi
movl %r8d, %r13d
subl %esi, %r13d
subl %r12d, %ecx
cmpl %r13d, %ecx
jg 0x200c98
movl %r13d, %ecx
subl %r12d, %edx
cmpl %r13d, %edx
cmovlel %r13d, %edx
movl %ecx, 0xc(%r15,%r10,8)
incq %r10
incl %ebp
cmpq %r14, %r10
jl 0x200c57
jmp 0x200cbf
cmpl %r13d, %r10d
jle 0x200df2
movl %r10d, %ebp
movslq %ebp, %rax
movl %r8d, (%r15,%rax,8)
movl $0xc0000000, 0x4(%r15,%rax,8) # imm = 0xC0000000
movq 0x98(%rsp), %r11
movq 0xc0(%rsp), %rbp
cmpq %r11, %rbp
movq 0xb0(%rsp), %r13
movq 0x60(%rsp), %r14
jl 0x200d08
movq 0x78(%rsp), %rax
movl (%r15,%rax,8), %ecx
cmpl 0xc(%r13), %ecx
jle 0x200d08
movl %ecx, 0xc(%r13)
movl %r14d, 0x10(%r13)
movq 0xa8(%rsp), %r10
cmpq %r10, %r14
jne 0x200d1d
movl %edi, 0x14(%r13)
movl %ebx, 0x18(%r13)
movl (%r13), %edx
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %edi, %ecx
jge 0x200d49
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orl %edi, %edx
movl %edx, (%r13)
movl %r14d, 0x8(%r13)
movl %ebx, 0x4(%r13)
jmp 0x200d52
movslq 0x8(%r13), %r8
cmpq %r8, %r14
jge 0x200dab
xorl %edi, %edi
cmpq %r10, %r14
setne %al
cmpq %r11, %rbp
setl %cl
orb %dil, %cl
orb %al, %cl
jne 0x200d74
movq 0x78(%rsp), %rax
movl (%r15,%rax,8), %eax
movl %eax, 0x1c(%r13)
incq %r14
cmpq 0x88(%rsp), %r14
sete %al
orb %al, %dil
movq 0x10(%rsp), %rax
addq 0x80(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpb $0x1, %dil
movq 0x48(%rsp), %rax
jne 0x200af0
jmp 0x200e97
xorl %edi, %edi
cmpl $0x0, 0x34(%rsp)
js 0x200d54
movl 0x4(%r13), %r9d
cmpl %ebx, %r9d
jg 0x200d54
addl %ebx, %r8d
movl %r14d, %eax
subl %r8d, %eax
addl %r9d, %eax
movl %eax, %esi
negl %esi
cmovsl %eax, %esi
imull %r12d, %esi
addl 0x34(%rsp), %esi
cmpl %esi, %ecx
jle 0x200e8c
orl $0x80000000, %edx       # imm = 0x80000000
movl %edx, (%r13)
movb $0x1, %dil
jmp 0x200e8e
movq 0x40(%rsp), %rax
addq %rax, %r14
movslq %r10d, %rax
movslq %r13d, %r13
movq 0x10(%rsp), %r10
subq %rax, %r10
movl 0x8(%r15,%r14,8), %eax
movl 0xc(%r15,%r14,8), %ecx
movl %r8d, 0x8(%r15,%r14,8)
movsbl 0x1(%r9,%r14), %r8d
addl %eax, %r8d
xorl %eax, %eax
cmpl %ecx, %r8d
setle %al
cmovlel %ecx, %r8d
cmpl %edx, %r8d
cmovlel %edx, %r8d
movl $0x2, %esi
cmovlel %esi, %eax
cmpl %r8d, %edi
cmovll %ebp, %ebx
cmovlel %r8d, %edi
movl %r8d, %esi
subl (%rsp), %esi
subl %r12d, %ecx
cmpl %esi, %ecx
setge %r11b
cmovlel %esi, %ecx
shlb $0x3, %r11b
orb %al, %r11b
subl %r12d, %edx
cmpl %esi, %edx
cmovlel %esi, %edx
movq (%rsp), %rsi
movl %ecx, 0xc(%r15,%r14,8)
setge %al
shlb $0x4, %al
orb %r11b, %al
movb %al, 0x1(%r10,%r14)
incq %r14
incl %ebp
cmpq %r13, %r14
jl 0x200e08
jmp 0x200cbf
xorl %edi, %edi
movq (%rsp), %rsi
jmp 0x200d54
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
callq 0x1fb9e5
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1fb9e5
movl 0xc(%rsp), %ecx
testb $0x1, %cl
jne 0x200f08
leaq 0x24(%r13), %rax
movl %ecx, %esi
shrl $0x7, %esi
andl $0x1, %esi
testb $0x40, %cl
jne 0x200f1a
cmpl $0x0, (%r13)
js 0x200f1a
movq 0x20(%rsp), %r9
decl %r9d
movq 0x48(%rsp), %r11
decl %r11d
leaq 0x20(%r13), %r10
addq $0x28, %r13
movq %rbx, %rdi
movq 0x18(%rsp), %rdx
movq 0x28(%rsp), %rcx
movl 0x30(%rsp), %r8d
pushq %r13
pushq %rax
pushq %r10
pushq %r11
jmp 0x200f4d
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x8(%r13), %r9d
testl %r9d, %r9d
movl 0x30(%rsp), %r8d
js 0x200f56
movl 0x4(%r13), %r10d
testl %r10d, %r10d
js 0x200f56
leaq 0x20(%r13), %r11
addq $0x28, %r13
movq %rbx, %rdi
movq 0x18(%rsp), %rdx
movq 0x28(%rsp), %rcx
pushq %r13
pushq %rax
pushq %r11
pushq %r10
callq 0x200f81
addq $0x20, %rsp
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x1fb9e5
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1fb9e5