// File: /workspace/github_c_makefile_star2_O3/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)
movq %rdx, %rbx
movl %esi, %r15d
movq %rdi, %r14
movq 0x130(%rsp), %rsi
movl 0x128(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0x120(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x100(%rsp), %rbp
movl 0x118(%rsp), %eax
movsbl 0x108(%rsp), %edx
movq %rdx, 0x38(%rsp)
movsbl 0x110(%rsp), %r12d
movaps 0x3c9804(%rip), %xmm0   # 0x5ca150
movups %xmm0, 0x10(%rsi)
movl $0x0, 0x24(%rsi)
movq %rcx, 0x30(%rsp)
cmpl %r15d, %ecx
movl %r15d, %edx
cmovgl %ecx, %edx
movaps 0x3c97f4(%rip), %xmm0   # 0x5ca160
testl %eax, %eax
cmovnsl %eax, %edx
movq %rdx, 0x48(%rsp)
leal 0x1(,%rdx,2), %r13d
cmpl %r15d, %r13d
cmovgel %r15d, %r13d
movq %rsi, 0x80(%rsp)
movups %xmm0, (%rsi)
movl %r9d, 0x10(%rsp)
movl %r9d, %eax
imull %r15d, %eax
movslq %eax, %rsi
callq 0x1fbb34
movq %rax, 0x50(%rsp)
movq %r15, 0x88(%rsp)
leal 0x1(%r15), %eax
movslq %eax, %rsi
movl $0x8, %edx
movq %r14, 0x70(%rsp)
movq %r14, %rdi
callq 0x1fbc89
movq 0x30(%rsp), %rcx
movq %rax, %r15
testb $0x1, 0xc(%rsp)
movl %r13d, 0x28(%rsp)
jne 0x200a11
movslq %r13d, %rsi
movslq %ecx, %r13
imulq %r13, %rsi
movq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x1fbb34
movq %rax, 0x18(%rsp)
movl $0x4, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x1fbc89
movq %rax, 0x20(%rsp)
jmp 0x200a23
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x38(%rsp), %r14
leal (%r12,%r14), %r10d
movl 0x10(%rsp), %eax
testb %al, %al
movq 0x88(%rsp), %r13
movq %r10, 0x58(%rsp)
jle 0x200a89
movl %eax, %ecx
movl %r13d, %edx
xorl %esi, %esi
xorl %edi, %edi
testl %r13d, %r13d
jle 0x200a81
movq %rsi, %r8
imulq %rcx, %r8
addq %rbp, %r8
movslq %edi, %r9
addq 0x50(%rsp), %r9
xorl %r10d, %r10d
movzbl (%rbx,%r10), %r11d
movb (%r8,%r11), %r11b
movb %r11b, (%r9,%r10)
incq %r10
cmpq %r10, %rdx
jne 0x200a64
addl %r10d, %edi
movq 0x58(%rsp), %r10
incq %rsi
cmpq %rcx, %rsi
jne 0x200a4a
movl $0x0, (%r15)
leal (%r10,%r10), %eax
negl %eax
movl %eax, 0x6c(%rsp)
movl %eax, 0x4(%r15)
movq 0x48(%rsp), %rcx
cmpl %ecx, %r13d
cmovll %r13d, %ecx
testl %ecx, %ecx
jle 0x200ae4
leal 0x1(%rcx), %edx
movl %ecx, %esi
leal (%r12,%r12,2), %edi
leal (%rdi,%r14,2), %edi
negl %edi
movl %r10d, %r8d
negl %r8d
xorl %r9d, %r9d
movl 0xc(%rsp), %ebp
movl %r8d, 0x8(%r15,%r9,8)
movl %edi, 0xc(%r15,%r9,8)
subl %r12d, %edi
subl %r12d, %r8d
incq %r9
cmpq %r9, %rsi
jne 0x200aca
jmp 0x200aed
movl $0x1, %edx
movl 0xc(%rsp), %ebp
cmpl %r13d, %edx
movq 0x30(%rsp), %rax
jg 0x200b1f
xorl %edx, %edx
testl %ecx, %ecx
cmovgl %ecx, %edx
leaq (%r15,%rdx,8), %rcx
addq $0xc, %rcx
movabsq $-0x3fffffff40000000, %rsi # imm = 0xC0000000C0000000
movq %rsi, -0x4(%rcx)
incl %edx
addq $0x8, %rcx
cmpl %r13d, %edx
jl 0x200b10
testl %eax, %eax
jle 0x200f3f
movq 0x48(%rsp), %rdx
leal (%rdx,%rdx), %ecx
leal -0x1(%r13), %r11d
movslq %r13d, %rsi
movq 0x30(%rsp), %rax
leal -0x1(%rax), %ebx
movl %edx, %edi
movq %rdi, 0xa8(%rsp)
movl %r12d, %edi
movq %rdi, 0xa0(%rsp)
movl %eax, %eax
movq %rax, 0x98(%rsp)
movslq %edx, %rax
notq %rax
movq %rax, 0x40(%rsp)
movl %edx, %eax
negl %eax
movl %eax, 0x64(%rsp)
movslq %ecx, %rcx
incq %rcx
cmpq %rsi, %rcx
movq %rsi, 0x78(%rsp)
cmovgeq %rsi, %rcx
movq %rcx, 0x90(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x10(%rsp)
xorl %r14d, %r14d
xorl %ecx, %ecx
movl %r11d, 0x68(%rsp)
movq %rbx, 0xb0(%rsp)
movq 0x48(%rsp), %r8
movl %r8d, %edx
subl %r14d, %edx
movl %r14d, %r9d
cmovgl %r8d, %r9d
negl %edx
movl %r9d, %ebx
addl 0x64(%rsp), %ebx
movq 0xb8(%rsp), %rax
movzbl (%rax,%r14), %esi
imull %r13d, %esi
movslq %esi, %rdi
addq 0x50(%rsp), %rdi
cmpq 0xa8(%rsp), %r14
movl $0x0, %eax
cmovbel %eax, %edx
leal (%r8,%r14), %eax
cmpl %r11d, %eax
movl %r11d, %esi
movl %eax, 0x38(%rsp)
cmovll %eax, %esi
movl %r14d, %r11d
imull 0xa0(%rsp), %r11d
movq %r14, %r8
movl 0x6c(%rsp), %r14d
subl %r11d, %r14d
addl %r10d, %r11d
negl %r11d
testl %edx, %edx
movl $0xc0000000, %eax       # imm = 0xC0000000
cmovgl %eax, %r11d
cmovgl %eax, %r14d
testb $0x1, %bpl
movq %r8, 0xc0(%rsp)
jne 0x200d03
movq 0x20(%rsp), %rax
movl %edx, (%rax,%r8,4)
testb $0x2, %bpl
jne 0x200d74
movl $0xc0000000, %ebp       # imm = 0xC0000000
cmpl %esi, %edx
jg 0x200d81
movq 0x40(%rsp), %rax
leaq (%rax,%r9), %r13
movslq %edx, %rbp
movslq %esi, %rdx
movq 0x10(%rsp), %r9
subq %rbp, %r9
movl $0xc0000000, %ebp       # imm = 0xC0000000
movl 0x8(%r15,%r13,8), %r8d
movl 0xc(%r15,%r13,8), %esi
movl %r11d, 0x8(%r15,%r13,8)
movsbl 0x1(%rdi,%r13), %r11d
addl %r8d, %r11d
xorl %r8d, %r8d
cmpl %esi, %r11d
setl %r8b
cmovlel %esi, %r11d
cmpl %r14d, %r11d
cmovlel %r14d, %r11d
movl $0x2, %eax
cmovll %eax, %r8d
cmpl %r11d, %ebp
cmovlel %ebx, %ecx
cmovlel %r11d, %ebp
movl %r11d, %eax
subl %r10d, %eax
subl %r12d, %esi
cmpl %eax, %esi
setg %r10b
cmovlel %eax, %esi
shlb $0x3, %r10b
orb %r8b, %r10b
subl %r12d, %r14d
cmpl %eax, %r14d
cmovlel %eax, %r14d
movl %esi, 0xc(%r15,%r13,8)
setg %al
shlb $0x4, %al
orb %r10b, %al
movq 0x58(%rsp), %r10
movb %al, 0x1(%r9,%r13)
incq %r13
incl %ebx
cmpq %rdx, %r13
jl 0x200c75
jmp 0x200d83
movl $0xc0000000, %ebp       # imm = 0xC0000000
cmpl %esi, %edx
jg 0x200d81
movq 0x40(%rsp), %rax
leaq (%rax,%r9), %rdx
movslq %esi, %r9
movl 0x8(%r15,%rdx,8), %r13d
movl 0xc(%r15,%rdx,8), %esi
movl %r11d, 0x8(%r15,%rdx,8)
movsbl 0x1(%rdi,%rdx), %r11d
addl %r13d, %r11d
cmpl %esi, %r11d
cmovlel %esi, %r11d
cmpl %r14d, %r11d
cmovlel %r14d, %r11d
cmpl %r11d, %ebp
cmovlel %ebx, %ecx
cmovlel %r11d, %ebp
movl %r11d, %r13d
subl %r10d, %r13d
subl %r12d, %esi
cmpl %r13d, %esi
jg 0x200d59
movl %r13d, %esi
subl %r12d, %r14d
cmpl %r13d, %r14d
cmovlel %r13d, %r14d
movl %esi, 0xc(%r15,%rdx,8)
incq %rdx
incl %ebx
cmpq %r9, %rdx
jl 0x200d18
jmp 0x200d83
movl $0xc0000000, %ebp       # imm = 0xC0000000
cmpl %esi, %edx
jle 0x200e8c
movl %edx, %ebx
movslq %ebx, %rax
movl %r11d, (%r15,%rax,8)
movl $0xc0000000, 0x4(%r15,%rax,8) # imm = 0xC0000000
movl 0x68(%rsp), %r11d
cmpl 0x38(%rsp), %r11d
movq 0x80(%rsp), %r8
movq 0xc0(%rsp), %r14
jg 0x200dc6
movq 0x78(%rsp), %rax
movl (%r15,%rax,8), %edx
cmpl 0xc(%r8), %edx
jle 0x200dc6
movl %edx, 0xc(%r8)
movl %r14d, 0x10(%r8)
movq 0xb0(%rsp), %rbx
cmpq %rbx, %r14
jne 0x200ddb
movl %ebp, 0x14(%r8)
movl %ecx, 0x18(%r8)
movl (%r8), %edx
movl %edx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
subl %ebp, %esi
movq 0x88(%rsp), %r13
jge 0x200e07
andl $0x80000000, %edx       # imm = 0x80000000
orl %ebp, %edx
movl %edx, (%r8)
movl %r14d, 0x8(%r8)
movl %ecx, 0x4(%r8)
jmp 0x200e41
movslq 0x8(%r8), %rdi
cmpq %rdi, %r14
jl 0x200e41
cmpl $0x0, 0x2c(%rsp)
js 0x200e41
movl 0x4(%r8), %r9d
cmpl %ecx, %r9d
jg 0x200e41
addl %ecx, %edi
movl %r14d, %eax
subl %edi, %eax
addl %r9d, %eax
movl %eax, %edi
negl %edi
cmovsl %eax, %edi
imull %r12d, %edi
addl 0x2c(%rsp), %edi
cmpl %edi, %esi
jg 0x200f32
cmpq %rbx, %r14
setne %al
cmpl 0x38(%rsp), %r11d
setg %dl
orb %al, %dl
movl 0xc(%rsp), %ebp
jne 0x200e64
movq 0x78(%rsp), %rax
movl (%r15,%rax,8), %eax
movl %eax, 0x1c(%r8)
incq %r14
movq 0x10(%rsp), %rax
addq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq 0x98(%rsp), %r14
jne 0x200ba6
jmp 0x200f3f
movq 0x40(%rsp), %rax
leaq (%rax,%r9), %r13
movslq %edx, %rax
movslq %esi, %rdx
movq 0x10(%rsp), %r9
subq %rax, %r9
movl 0x8(%r15,%r13,8), %eax
movl 0xc(%r15,%r13,8), %esi
movl %r11d, 0x8(%r15,%r13,8)
movsbl 0x1(%rdi,%r13), %r11d
addl %eax, %r11d
xorl %eax, %eax
cmpl %esi, %r11d
setle %al
cmovlel %esi, %r11d
cmpl %r14d, %r11d
cmovlel %r14d, %r11d
movl $0x2, %r8d
cmovlel %r8d, %eax
cmpl %r11d, %ebp
cmovll %ebx, %ecx
cmovlel %r11d, %ebp
movl %r11d, %r8d
subl %r10d, %r8d
subl %r12d, %esi
cmpl %r8d, %esi
setge %r10b
cmovlel %r8d, %esi
shlb $0x3, %r10b
orb %al, %r10b
subl %r12d, %r14d
cmpl %r8d, %r14d
cmovlel %r8d, %r14d
movl %esi, 0xc(%r15,%r13,8)
setge %al
shlb $0x4, %al
orb %r10b, %al
movq 0x58(%rsp), %r10
movb %al, 0x1(%r9,%r13)
incq %r13
incl %ebx
cmpq %rdx, %r13
jl 0x200ea3
jmp 0x200d83
orl $0x80000000, %edx       # imm = 0x80000000
movl %edx, (%r8)
movl 0xc(%rsp), %ebp
movq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq 0x50(%rsp), %rsi
callq 0x1fb9e0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1fb9e0
testb $0x1, %bpl
jne 0x200fb0
movq 0x80(%rsp), %r11
leaq 0x24(%r11), %rax
movl %ebp, %esi
shrl $0x7, %esi
andl $0x1, %esi
testb $0x40, %bpl
jne 0x200fc2
cmpl $0x0, (%r11)
js 0x200fc2
movq 0x30(%rsp), %r9
decl %r9d
decl %r13d
leaq 0x20(%r11), %r10
addq $0x28, %r11
movq %rbx, %rdi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl 0x28(%rsp), %r8d
pushq %r11
pushq %rax
pushq %r10
pushq %r13
jmp 0x200ff8
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x8(%r11), %r9d
testl %r9d, %r9d
movl 0x28(%rsp), %r8d
js 0x201001
movl 0x4(%r11), %r10d
testl %r10d, %r10d
js 0x201001
movq %r11, %r14
addq $0x20, %r11
addq $0x28, %r14
movq %rbx, %rdi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
pushq %r14
pushq %rax
pushq %r11
pushq %r10
callq 0x20102c
addq $0x20, %rsp
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x1fb9e0
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1fb9e0