// File: /workspace/github_c_makefile_star2_O3/ToniWestbrook[P]paladin/ksw.c
// Function: ksw_extend2
int ksw_extend2(int qlen, const uint8_t *query, int tlen, const uint8_t *target, int m, const int8_t *mat, int o_del, int e_del, int o_ins, int e_ins, int w, int end_bonus, int zdrop, int h0, int *_qle, int *_tle, int *_gtle, int *_gscore, int *_max_off)
{
	eh_t *eh; // score array
	int8_t *qp; // query profile
	int i, j, k, oe_del = o_del + e_del, oe_ins = o_ins + e_ins, beg, end, max, max_i, max_j, max_ins, max_del, max_ie, gscore, max_off;
	assert(h0 > 0);

	// allocate memory
	qp = malloc(qlen * m);
	eh = calloc(qlen + 1, 8);

	// 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 = h0; eh[1].h = h0 > oe_ins? h0 - oe_ins : 0;
	for (j = 2; j <= qlen && eh[j-1].h > e_ins; ++j)
		eh[j].h = eh[j-1].h - e_ins;

	// adjust $w if it is too large
	k = m * m;
	for (i = 0, max = 0; i < k; ++i) // get the max score
		max = max > mat[i]? max : mat[i];
	max_ins = (int)((double)(qlen * max + end_bonus - o_ins) / e_ins + 1.);
	max_ins = max_ins > 1? max_ins : 1;
	w = w < max_ins? w : max_ins;
	max_del = (int)((double)(qlen * max + end_bonus - o_del) / e_del + 1.);
	max_del = max_del > 1? max_del : 1;
	w = w < max_del? w : max_del; // TODO: is this necessary?

	// DP loop
	max = h0, max_i = max_j = -1; max_ie = -1, gscore = -1;
	max_off = 0;
	beg = 0, end = qlen;
	for (i = 0; LIKELY(i < tlen); ++i) {
		int t, f = 0, h1, m = 0, mj = -1;
		int8_t *q = &qp[target[i] * qlen];

		// apply the band and the constraint (if provided)
		if (beg < i - w) beg = i - w;
		if (end > i + w + 1) end = i + w + 1;
		if (end > qlen) end = qlen;

		// compute the first column
		if (beg == 0) {
			h1 = h0 - (o_del + e_del * (i + 1));
			if (h1 < 0) h1 = 0;
		} else h1 = 0;
		for (j = beg; LIKELY(j < end); ++j) {
			// At the beginning of the loop: eh[j] = { H(i-1,j-1), E(i,j) }, f = F(i,j) and h1 = H(i,j-1)
			// Similar to SSE2-SW, cells are computed in the following order:
			//   H(i,j)   = max{H(i-1,j-1)+S(i,j), E(i,j), F(i,j)}
			//   E(i+1,j) = max{H(i,j)-gapo, E(i,j)} - gape
			//   F(i,j+1) = max{H(i,j)-gapo, F(i,j)} - gape
			eh_t *p = &eh[j];
			int h, M = p->h, e = p->e; // get H(i-1,j-1) and E(i-1,j)
			p->h = h1;          // set H(i,j-1) for the next row
			M = M? M + q[j] : 0;// separating H and M to disallow a cigar like "100M3I3D20M"
			h = M > e? M : e;   // e and f are guaranteed to be non-negative, so h>=0 even if M<0
			h = h > f? h : f;
			h1 = h;             // save H(i,j) to h1 for the next column
			mj = m > h? mj : j; // record the position where max score is achieved
			m = m > h? m : h;   // m is stored at eh[mj+1]
			t = M - oe_del;
			t = t > 0? t : 0;
			e -= e_del;
			e = e > t? e : t;   // computed E(i+1,j)
			p->e = e;           // save E(i+1,j) for the next row
			t = M - oe_ins;
			t = t > 0? t : 0;
			f -= e_ins;
			f = f > t? f : t;   // computed F(i,j+1)
		}
		eh[end].h = h1; eh[end].e = 0;
		if (j == qlen) {
			max_ie = gscore > h1? max_ie : i;
			gscore = gscore > h1? gscore : h1;
		}
		if (m == 0) break;
		if (m > max) {
			max = m, max_i = i, max_j = mj;
			max_off = max_off > abs(mj - i)? max_off : abs(mj - i);
		} else if (zdrop > 0) {
			if (i - max_i > mj - max_j) {
				if (max - m - ((i - max_i) - (mj - max_j)) * e_del > zdrop) break;
			} else {
				if (max - m - ((mj - max_j) - (i - max_i)) * e_ins > zdrop) break;
			}
		}
		// update beg and end for the next round
		for (j = beg; LIKELY(j < end) && eh[j].h == 0 && eh[j].e == 0; ++j);
		beg = j;
		for (j = end; LIKELY(j >= beg) && eh[j].h == 0 && eh[j].e == 0; --j);
		end = j + 2 < qlen? j + 2 : qlen;
		//beg = 0; end = qlen; // uncomment this line for debugging
	}
	free(eh); free(qp);
	if (_qle) *_qle = max_j + 1;
	if (_tle) *_tle = max_i + 1;
	if (_gtle) *_gtle = max_ie + 1;
	if (_gscore) *_gscore = gscore;
	if (_max_off) *_max_off = max_off;
	return max;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rcx, 0x60(%rsp)
movl %edx, 0x28(%rsp)
movl 0x108(%rsp), %eax
movl %eax, 0x20(%rsp)
testl %eax, %eax
jle 0x247dc
movq %r9, %r13
movl %r8d, %ebp
movq %rsi, %r15
movl %edi, %r8d
movl 0xe8(%rsp), %eax
movl 0xe0(%rsp), %ecx
leal (%rax,%rcx), %r14d
movl %ebp, %eax
imull %edi, %eax
movslq %eax, %rdi
leaq 0x54f3(%rip), %r12      # 0x29748
leaq 0x55ae(%rip), %rbx      # 0x2980a
movq %r12, %rsi
movl $0x185, %edx            # imm = 0x185
movq %rbx, %rcx
movq %r8, 0x38(%rsp)
callq 0x23213
movq %rax, 0x40(%rsp)
movq 0x38(%rsp), %rax
incl %eax
movl %eax, 0x18(%rsp)
movslq %eax, %rdi
movl $0x8, %esi
movq %r12, %rdx
movl $0x186, %ecx            # imm = 0x186
movq %rbx, %r8
callq 0x2319c
movq 0x38(%rsp), %r11
movq %rax, %rbx
testl %ebp, %ebp
jle 0x242e8
movl %ebp, %eax
movl %r11d, %ecx
xorl %edx, %edx
xorl %esi, %esi
testl %r11d, %r11d
jle 0x242e0
movq %rdx, %rdi
imulq %rax, %rdi
addq %r13, %rdi
movslq %esi, %r8
addq 0x40(%rsp), %r8
xorl %r9d, %r9d
movzbl (%r15,%r9), %r10d
movb (%rdi,%r10), %r10b
movb %r10b, (%r8,%r9)
incq %r9
cmpq %r9, %rcx
jne 0x242c8
addl %r9d, %esi
incq %rdx
cmpq %rax, %rdx
jne 0x242ae
movl 0x108(%rsp), %eax
movl %eax, (%rbx)
xorl %esi, %esi
subl %r14d, %eax
cmovlel %esi, %eax
movl %eax, 0x8(%rbx)
cmpl $0x2, %r11d
jl 0x24325
movl 0x18(%rsp), %eax
movl $0x2, %ecx
movl -0x8(%rbx,%rcx,8), %edx
movl 0xe8(%rsp), %edi
subl %edi, %edx
jle 0x24325
movl %edx, (%rbx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x2430b
testl %ebp, %ebp
movl 0x28(%rsp), %r9d
je 0x2434e
imull %ebp, %ebp
cmpl $0x1, %ebp
adcl $0x0, %ebp
xorl %eax, %eax
xorl %esi, %esi
movsbl (%r13,%rax), %ecx
cmpl %ecx, %esi
cmovlel %ecx, %esi
incq %rax
cmpq %rax, %rbp
jne 0x2433b
testl %r9d, %r9d
movq %rbx, 0x50(%rsp)
jle 0x247b5
movl 0xf0(%rsp), %eax
movl 0xd8(%rsp), %edi
imull %r11d, %esi
addl 0xf8(%rsp), %esi
movl %esi, %ecx
movl 0xe0(%rsp), %edx
subl %edx, %ecx
cvtsi2sd %ecx, %xmm0
movl 0xd0(%rsp), %edx
movl 0xe8(%rsp), %ecx
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
movsd 0x418e(%rip), %xmm1     # 0x28530
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
cmpl $0x2, %ecx
movl $0x1, %r8d
cmovll %r8d, %ecx
cmpl %eax, %ecx
cmovgel %eax, %ecx
subl %edx, %esi
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
cvtsi2sd %edi, %xmm2
divsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
cmpl $0x2, %eax
cmovgel %eax, %r8d
cmpl %r8d, %ecx
cmovll %ecx, %r8d
leal 0x1(%r8), %ecx
movl 0x108(%rsp), %eax
subl %edx, %eax
movl %eax, 0x4c(%rsp)
addl %edi, %edx
movq %r8, 0x70(%rsp)
movl %r8d, %edi
negl %edi
movl %r9d, %eax
movq %rax, 0x58(%rsp)
movq %rbx, %r10
addq $0x4, %r10
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
xorl %r9d, %r9d
xorl %r12d, %r12d
movq %rcx, 0x68(%rsp)
movl %ecx, %r15d
xorl %r13d, %r13d
movl $0x0, 0xc(%rsp)
movl $0xffffffff, 0x1c(%rsp) # imm = 0xFFFFFFFF
movl %r11d, %ecx
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movq %r10, 0x30(%rsp)
movl %r13d, %eax
movl %r12d, %r13d
subl 0x70(%rsp), %r13d
cmpl %r13d, %eax
movq %rax, 0x28(%rsp)
cmovgl %eax, %r13d
movq 0x68(%rsp), %rax
leal (%rax,%r12), %esi
cmpl %esi, %ecx
movl %ecx, 0x18(%rsp)
cmovll %ecx, %esi
cmpl %r11d, %esi
cmovgel %r11d, %esi
movl $0x0, %ebp
testl %r13d, %r13d
jne 0x244a1
movl %r12d, %ebp
notl %ebp
imull 0xd8(%rsp), %ebp
addl 0x4c(%rsp), %ebp
testl %ebp, %ebp
cmovlel %r9d, %ebp
movl %r15d, 0x24(%rsp)
cmpl %esi, %r13d
movl %edi, 0x14(%rsp)
jge 0x24712
movq %r13, 0x88(%rsp)
movslq %edi, %r13
movq 0x60(%rsp), %rax
movq %r12, 0x90(%rsp)
movzbl (%rax,%r12), %eax
imull %r11d, %eax
movslq %eax, %r8
movq 0x28(%rsp), %r11
cmpl %edi, %r11d
movl %edi, %eax
cmovgl %r11d, %eax
movq 0x40(%rsp), %rcx
addq %rax, %rcx
addq %rcx, %r8
leaq (%r10,%rax,8), %r10
movslq %r11d, %rax
cmpq %r13, %rax
movq %r13, 0x80(%rsp)
movq %rax, 0x78(%rsp)
cmovgq %rax, %r13
xorl %r11d, %r11d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %r15d, %r15d
xorl %eax, %eax
movl -0x4(%r10,%r11,8), %ecx
movl (%r10,%r11,8), %r12d
movl %ebp, -0x4(%r10,%r11,8)
movl $0x0, %ebx
testl %ecx, %ecx
je 0x24538
movsbl (%r8,%r11), %ebx
addl %ebx, %ecx
movl %ecx, %ebx
cmpl %r12d, %ebx
movl %r12d, %ebp
cmovgl %ebx, %ebp
cmpl %eax, %ebp
cmovlel %eax, %ebp
leal (%r11,%r13), %ecx
cmpl %ebp, %r15d
cmovbel %ecx, %edi
cmovlel %ebp, %r15d
movl %ebx, %ecx
subl %edx, %ecx
subl 0xd8(%rsp), %r12d
cmpl %r12d, %ecx
cmovgl %ecx, %r12d
testl %r12d, %r12d
cmovlel %r9d, %r12d
subl %r14d, %ebx
movl 0xe8(%rsp), %ecx
subl %ecx, %eax
cmpl %eax, %ebx
cmovgl %ebx, %eax
testl %eax, %eax
cmovlel %r9d, %eax
movl %r12d, (%r10,%r11,8)
incq %r11
leal (%r11,%r13), %ecx
cmpl %ecx, %esi
jg 0x24518
movq 0x80(%rsp), %rax
movq 0x78(%rsp), %rcx
cmpq %rax, %rcx
cmovgq %rcx, %rax
addl %eax, %r11d
movq 0x50(%rsp), %rbx
movq 0x90(%rsp), %r12
movq 0x88(%rsp), %r13
movslq %esi, %rax
movl %ebp, (%rbx,%rax,8)
movl $0x0, 0x4(%rbx,%rax,8)
movq 0x38(%rsp), %rax
cmpl %eax, %r11d
movq %rax, %r11
jne 0x245f4
movl 0x10(%rsp), %eax
cmpl %ebp, %eax
movl 0x1c(%rsp), %ecx
cmovlel %r12d, %ecx
movl %ecx, 0x1c(%rsp)
cmovlel %ebp, %eax
movl %eax, 0x10(%rsp)
movl 0x18(%rsp), %ebp
testl %r15d, %r15d
je 0x24722
movl 0x20(%rsp), %eax
subl %r15d, %eax
jge 0x24633
movl %edi, %eax
subl %r12d, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
cmovlel %ecx, %eax
movl %eax, 0xc(%rsp)
movl %r12d, 0x8(%rsp)
movl %edi, 0x4(%rsp)
movl %r15d, 0x20(%rsp)
jmp 0x24668
cmpl $0x0, 0x100(%rsp)
jle 0x24668
movl %r12d, %ecx
subl 0x8(%rsp), %ecx
subl 0x4(%rsp), %edi
movl %ecx, %r8d
subl %edi, %r8d
movq 0x30(%rsp), %r10
movl 0x24(%rsp), %r15d
jle 0x24678
subl %ecx, %edi
imull 0xd8(%rsp), %edi
addl %edi, %eax
jmp 0x24686
movl 0x14(%rsp), %edi
movq 0x30(%rsp), %r10
movl 0x24(%rsp), %r15d
jmp 0x24697
movl 0xe8(%rsp), %ecx
imull %ecx, %r8d
addl %r8d, %eax
cmpl 0x100(%rsp), %eax
movl 0x14(%rsp), %edi
jg 0x24722
cmpl %esi, %r13d
jge 0x246cd
cmpl %r15d, %ebp
cmovgel %r15d, %ebp
movq 0x28(%rsp), %r13
cmpl %edi, %r13d
cmovlel %edi, %r13d
leaq (%r10,%r13,8), %rax
cmpl $0x0, -0x4(%rax)
jne 0x246cd
cmpl $0x0, (%rax)
jne 0x246cd
incl %r13d
addq $0x8, %rax
cmpl %r13d, %esi
jg 0x246b3
movl %ebp, %r13d
cmpl %r13d, %esi
jl 0x246f1
leal -0x1(%r13), %eax
movl %esi, %ecx
cmpl $0x0, (%rbx,%rcx,8)
jne 0x246f1
cmpl $0x0, 0x4(%rbx,%rcx,8)
jne 0x246f1
leal -0x1(%rsi), %ecx
cmpl %r13d, %esi
movl %ecx, %esi
jg 0x246d6
movl %eax, %esi
addl $0x2, %esi
cmpl %r11d, %esi
cmovgel %r11d, %esi
incq %r12
incl %edi
incl %r15d
movl %esi, %ecx
cmpq 0x58(%rsp), %r12
jne 0x24450
jmp 0x24722
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %r15d, %r15d
movl %r13d, %r11d
jmp 0x245c0
incl 0x4(%rsp)
incl 0x8(%rsp)
movl 0x1c(%rsp), %ebp
incl %ebp
movq 0x118(%rsp), %rbx
movq 0x50(%rsp), %rdi
callq 0x3390
movq 0x40(%rsp), %rdi
callq 0x3390
movq 0x110(%rsp), %rax
testq %rax, %rax
je 0x2475f
movl 0x4(%rsp), %ecx
movl %ecx, (%rax)
movq 0x120(%rsp), %rax
testq %rbx, %rbx
je 0x24772
movl 0x8(%rsp), %ecx
movl %ecx, (%rbx)
movq 0x128(%rsp), %rcx
testq %rax, %rax
je 0x24781
movl %ebp, (%rax)
movq 0x130(%rsp), %rax
testq %rcx, %rcx
je 0x24794
movl 0x10(%rsp), %edx
movl %edx, (%rcx)
testq %rax, %rax
je 0x2479f
movl 0xc(%rsp), %ecx
movl %ecx, (%rax)
movl 0x20(%rsp), %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
jmp 0x24730
leaq 0x4f84(%rip), %rdi      # 0x29767
leaq 0x4f5e(%rip), %rsi      # 0x29748
leaq 0x4f7d(%rip), %rcx      # 0x2976e
movl $0x182, %edx            # imm = 0x182
callq 0x31a0