// File: /workspace/github_c_makefile_star2_O2/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 $0xb8, %rsp
movq %r8, 0xa8(%rsp)
movq %rdx, %r12
movl %esi, %ebx
movq %rdi, %r15
movq 0x120(%rsp), %rsi
movl 0x118(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x110(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x48(%rsp)
movl 0x108(%rsp), %eax
movsbl 0xf8(%rsp), %edx
movq %rdx, 0x50(%rsp)
movsbl 0x100(%rsp), %r14d
movaps 0x3c5494(%rip), %xmm0   # 0x568150
movups %xmm0, 0x10(%rsi)
andl $0x0, 0x24(%rsi)
cmpl %ebx, %ecx
movl %ebx, %edx
movq %rcx, 0x40(%rsp)
cmovgl %ecx, %edx
movaps 0x3c5489(%rip), %xmm0   # 0x568160
testl %eax, %eax
cmovnsl %eax, %edx
movq %rdx, 0x58(%rsp)
leal 0x1(,%rdx,2), %r13d
cmpl %ebx, %r13d
cmovgel %ebx, %r13d
movq %rsi, 0x28(%rsp)
movups %xmm0, (%rsi)
movq %r9, 0x10(%rsp)
movl %r9d, %eax
imull %ebx, %eax
movslq %eax, %rsi
callq 0x19e512
movq %rax, %rbp
movq %rbx, 0x30(%rsp)
leal 0x1(%rbx), %eax
movslq %eax, %rsi
pushq $0x8
popq %rdx
movq %r15, 0x70(%rsp)
movq %r15, %rdi
callq 0x19e66e
movq %rax, %rbx
xorl %r15d, %r15d
testb $0x1, 0x4(%rsp)
movl %r13d, 0x38(%rsp)
jne 0x1a2d75
movslq %r13d, %rsi
movslq 0x40(%rsp), %rax
movq %rax, 0x8(%rsp)
imulq %rax, %rsi
movq 0x70(%rsp), %r13
movq %r13, %rdi
callq 0x19e512
movq %rax, 0x18(%rsp)
pushq $0x4
popq %rdx
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x19e66e
movq %rax, 0x20(%rsp)
jmp 0x1a2d87
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x50(%rsp), %r11
leal (%r14,%r11), %eax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %rax
addq $0x24, %rax
movq %rax, 0x68(%rsp)
movq 0x58(%rsp), %rax
leal (%rax,%rax), %ecx
movq 0x30(%rsp), %rax
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
movq 0x10(%rsp), %rax
movsbq %al, %rsi
testl %eax, %eax
cmovlel %r15d, %eax
xorl %edi, %edi
movq 0x48(%rsp), %r13
cmpq %rax, %r15
je 0x1a2e05
movq %r15, %r8
imulq %rsi, %r8
addq %r13, %r8
movslq %edi, %rdi
xorl %r9d, %r9d
cmpq %r9, %rdx
je 0x1a2e00
movzbl (%r12,%r9), %r10d
movb (%r8,%r10), %r10b
movb %r10b, (%rbp,%rdi)
incq %rdi
incq %r9
jmp 0x1a2de5
incq %r15
jmp 0x1a2dd0
andl $0x0, (%rbx)
movq 0x8(%rsp), %r9
leal (%r9,%r9), %edx
negl %edx
movq 0x30(%rsp), %rax
movq 0x58(%rsp), %r10
cmpl %r10d, %eax
movl %r10d, %esi
cmovll %eax, %esi
movl %edx, 0x64(%rsp)
movl %edx, 0x4(%rbx)
movl %r14d, %eax
movq %rax, 0xa0(%rsp)
xorl %edi, %edi
testl %esi, %esi
cmovlel %edi, %esi
leal 0x1(%rsi), %edx
leal (%r14,%r14,2), %r8d
leal (%r8,%r11,2), %r8d
negl %r8d
negl %r9d
movl 0x4(%rsp), %r15d
cmpq %rdi, %rsi
je 0x1a2e6f
movl %r9d, 0x8(%rbx,%rdi,8)
movl %r8d, 0xc(%rbx,%rdi,8)
subl %r14d, %r8d
subl %r14d, %r9d
incq %rdi
jmp 0x1a2e55
movslq 0x30(%rsp), %rdi
movabsq $-0x3fffffff40000000, %rsi # imm = 0xC0000000C0000000
movq 0x28(%rsp), %r9
cmpq %rdi, %rdx
jg 0x1a2e91
movq %rsi, (%rbx,%rdx,8)
incq %rdx
jmp 0x1a2e83
movq 0x30(%rsp), %rax
decl %eax
movq 0x40(%rsp), %rsi
leal -0x1(%rsi), %r12d
movslq %r10d, %rdx
movq %rdx, 0x90(%rsp)
movq %rax, 0x80(%rsp)
cltq
movq %rax, 0x48(%rsp)
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
movslq %ecx, %rcx
incq %rcx
cmpq %rdi, %rcx
movq %rdi, 0x78(%rsp)
cmovgeq %rdi, %rcx
movq %rcx, 0x88(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x10(%rsp)
xorl %r8d, %r8d
xorl %r11d, %r11d
movq %rsi, 0x40(%rsp)
movq %r12, 0x98(%rsp)
cmpl %r8d, %r10d
movl %r8d, %edx
cmovgl %r10d, %edx
cmpq %rsi, %r8
je 0x1a3242
subl %r10d, %edx
movq %r10, %rcx
movslq %edx, %r12
movq 0xa8(%rsp), %rax
movzbl (%rax,%r8), %edx
imull 0x30(%rsp), %edx
movslq %edx, %rdi
addq %rbp, %rdi
movl %r8d, %r10d
subl %ecx, %r10d
movq 0x90(%rsp), %rax
cmpq %rax, %r8
movl $0x0, %ecx
cmovlel %ecx, %r10d
leaq (%r8,%rax), %rcx
movq 0x80(%rsp), %rax
cmpl %eax, %ecx
movl %eax, %edx
movq %rcx, 0x50(%rsp)
cmovll %ecx, %edx
movq %r8, %rcx
imull 0xa0(%rsp), %r8d
movl 0x64(%rsp), %esi
subl %r8d, %esi
movq 0x8(%rsp), %rax
addl %eax, %r8d
negl %r8d
testl %r10d, %r10d
movl $0xc0000000, %eax       # imm = 0xC0000000
cmovgl %eax, %r8d
cmovgl %eax, %esi
movslq %edx, %rdx
testb $0x1, %r15b
movq %rcx, 0xb0(%rsp)
jne 0x1a3049
movq 0x20(%rsp), %rax
movl %r10d, (%rax,%rcx,4)
movslq %r10d, %r10
testb $0x2, %r15b
jne 0x1a30ad
movq 0x10(%rsp), %r13
subq %r10, %r13
movl $0xc0000000, %r15d      # imm = 0xC0000000
cmpq %rdx, %r12
jg 0x1a3135
movl (%rbx,%r12,8), %r9d
movl 0x4(%rbx,%r12,8), %r10d
movl %r8d, (%rbx,%r12,8)
movsbl (%rdi,%r12), %r8d
addl %r9d, %r8d
xorl %r9d, %r9d
cmpl %r10d, %r8d
cmovlel %r10d, %r8d
setl %r9b
cmpl %esi, %r8d
cmovlel %esi, %r8d
pushq $0x2
popq %rax
cmovll %eax, %r9d
cmpl %r8d, %r15d
cmovlel %r12d, %r11d
cmovlel %r8d, %r15d
movl %r8d, %ecx
subl 0x8(%rsp), %ecx
subl %r14d, %r10d
cmpl %ecx, %r10d
setg %al
cmovlel %ecx, %r10d
shlb $0x3, %al
orb %r9b, %al
movl %r10d, 0x4(%rbx,%r12,8)
subl %r14d, %esi
cmpl %ecx, %esi
setg %r9b
cmovlel %ecx, %esi
shlb $0x4, %r9b
orb %al, %r9b
movb %r9b, (%r13,%r12)
incq %r12
jmp 0x1a2fc5
movl $0xc0000000, %r15d      # imm = 0xC0000000
cmpq %rdx, %r12
jg 0x1a313a
movl (%rbx,%r12,8), %r13d
movl 0x4(%rbx,%r12,8), %r10d
movl %r8d, (%rbx,%r12,8)
movsbl (%rdi,%r12), %r8d
addl %r13d, %r8d
cmpl %r10d, %r8d
cmovlel %r10d, %r8d
cmpl %esi, %r8d
cmovlel %esi, %r8d
cmpl %r8d, %r15d
cmovlel %r12d, %r11d
cmovlel %r8d, %r15d
movl %r8d, %r13d
subl 0x8(%rsp), %r13d
subl %r14d, %r10d
cmpl %r13d, %r10d
jg 0x1a3099
movl %r13d, %r10d
movl %r10d, 0x4(%rbx,%r12,8)
subl %r14d, %esi
cmpl %r13d, %esi
cmovlel %r13d, %esi
incq %r12
jmp 0x1a304f
movq 0x10(%rsp), %r13
subq %r10, %r13
movl $0xc0000000, %r15d      # imm = 0xC0000000
cmpq %rdx, %r12
jg 0x1a3135
movl (%rbx,%r12,8), %eax
movl 0x4(%rbx,%r12,8), %r10d
movl %r8d, (%rbx,%r12,8)
movsbl (%rdi,%r12), %r8d
addl %eax, %r8d
xorl %eax, %eax
cmpl %r10d, %r8d
cmovlel %r10d, %r8d
setle %al
cmpl %esi, %r8d
cmovlel %esi, %r8d
pushq $0x2
popq %rcx
cmovlel %ecx, %eax
cmpl %r8d, %r15d
cmovll %r12d, %r11d
cmovlel %r8d, %r15d
movl %r8d, %ecx
subl 0x8(%rsp), %ecx
subl %r14d, %r10d
cmpl %ecx, %r10d
setge %r9b
cmovlel %ecx, %r10d
shlb $0x3, %r9b
orb %al, %r9b
movl %r10d, 0x4(%rbx,%r12,8)
subl %r14d, %esi
cmpl %ecx, %esi
setge %al
cmovlel %ecx, %esi
shlb $0x4, %al
orb %r9b, %al
movb %al, (%r13,%r12)
incq %r12
jmp 0x1a30bb
movq 0x28(%rsp), %r9
movslq %r12d, %rax
movl %r8d, (%rbx,%rax,8)
movl $0xc0000000, 0x4(%rbx,%rax,8) # imm = 0xC0000000
movq 0x50(%rsp), %rax
cmpq 0x48(%rsp), %rax
movq 0xb0(%rsp), %r13
jl 0x1a3173
movq 0x78(%rsp), %rax
movl (%rbx,%rax,8), %edx
cmpl 0xc(%r9), %edx
jle 0x1a3173
movl %edx, 0xc(%r9)
movl %r13d, 0x10(%r9)
movq 0x98(%rsp), %r12
cmpq %r12, %r13
movq 0x58(%rsp), %r10
jne 0x1a318d
movl %r15d, 0x14(%r9)
movl %r11d, 0x18(%r9)
movl (%r9), %edx
movl %edx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
subl %r15d, %esi
jge 0x1a31b8
andl $0x80000000, %edx       # imm = 0x80000000
orl %r15d, %edx
movl %edx, (%r9)
movl %r13d, 0x8(%r9)
movl %r11d, 0x4(%r9)
movl 0x4(%rsp), %r15d
jmp 0x1a31f4
movslq 0x8(%r9), %rdi
cmpq %rdi, %r13
movl 0x4(%rsp), %r15d
jl 0x1a31f4
cmpl $0x0, 0x3c(%rsp)
js 0x1a31f4
movl 0x4(%r9), %r8d
cmpl %r11d, %r8d
jg 0x1a31f4
addl %r11d, %edi
movl %r13d, %eax
subl %edi, %eax
addl %r8d, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
imull %r14d, %ecx
addl 0x3c(%rsp), %ecx
cmpl %ecx, %esi
jg 0x1a3239
cmpq %r12, %r13
setne %al
movq 0x50(%rsp), %rcx
cmpq 0x48(%rsp), %rcx
setl %cl
orb %al, %cl
movq %r13, %r8
jne 0x1a321a
movq 0x78(%rsp), %rax
movl (%rbx,%rax,8), %eax
movl %eax, 0x1c(%r9)
incq %r8
movq 0x10(%rsp), %rax
addq 0x88(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x40(%rsp), %rsi
jmp 0x1a2ef9
orl $0x80000000, %edx       # imm = 0x80000000
movl %edx, (%r9)
movq 0x70(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x19e38c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x19e38c
testb $0x1, %r15b
jne 0x1a32aa
movl %r15d, %esi
shrl $0x7, %esi
andl $0x1, %esi
testb $0x40, %r15b
movq 0x28(%rsp), %r11
jne 0x1a32bc
cmpl $0x0, (%r11)
js 0x1a32bc
leaq 0x20(%r11), %rax
addq $0x28, %r11
movq %r14, %rdi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl 0x38(%rsp), %r8d
movl %r12d, %r9d
pushq %r11
pushq 0x70(%rsp)
pushq %rax
pushq 0x98(%rsp)
jmp 0x1a32f0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x8(%r11), %r9d
testl %r9d, %r9d
movl 0x38(%rsp), %r8d
js 0x1a32f9
movl 0x4(%r11), %eax
testl %eax, %eax
js 0x1a32f9
leaq 0x20(%r11), %r10
addq $0x28, %r11
movq %r14, %rdi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
pushq %r11
pushq 0x70(%rsp)
pushq %r10
pushq %rax
callq 0x1a3324
addq $0x20, %rsp
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x19e38c
movq %r14, %rdi
movq 0x20(%rsp), %rsi
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x19e38c