// File: /workspace/github_c_makefile_star2_O1/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 0x242a2
movq %r9, %r13
movl %r8d, %ebp
movq %rsi, %r15
movl %edi, %ebx
movl 0xe8(%rsp), %eax
movl 0xe0(%rsp), %ecx
leal (%rax,%rcx), %r14d
movl %r8d, %eax
imull %edi, %eax
movslq %eax, %rdi
leaq 0x5910(%rip), %rsi      # 0x29618
leaq 0x59cb(%rip), %rcx      # 0x296da
movl $0x185, %edx            # imm = 0x185
callq 0x22d2b
movq %rax, 0x30(%rsp)
leal 0x1(%rbx), %r12d
movslq %r12d, %rdi
movl $0x8, %esi
leaq 0x58e7(%rip), %rdx      # 0x29618
movl $0x186, %ecx            # imm = 0x186
leaq 0x599d(%rip), %r8       # 0x296da
callq 0x22cb4
movq %rax, %r11
testl %ebp, %ebp
jle 0x23d8a
movl %ebp, %eax
movl %ebx, %ecx
xorl %edx, %edx
xorl %esi, %esi
testl %ebx, %ebx
jle 0x23d82
movq %rdx, %rdi
imulq %rax, %rdi
addq %r13, %rdi
movslq %esi, %r8
addq 0x30(%rsp), %r8
xorl %r9d, %r9d
movzbl (%r15,%r9), %r10d
movb (%rdi,%r10), %r10b
movb %r10b, (%r8,%r9)
incq %r9
cmpq %r9, %rcx
jne 0x23d6a
addl %r9d, %esi
incq %rdx
cmpq %rax, %rdx
jne 0x23d51
movl 0x108(%rsp), %ecx
movl %ecx, (%r11)
xorl %eax, %eax
subl %r14d, %ecx
cmovlel %eax, %ecx
movl %ecx, 0x8(%r11)
cmpl $0x2, %ebx
jl 0x23dc6
movl $0x2, %ecx
movl -0x8(%r11,%rcx,8), %edx
movl 0xe8(%rsp), %esi
subl %esi, %edx
jle 0x23dc6
movl %edx, (%r11,%rcx,8)
incq %rcx
cmpq %rcx, %r12
jne 0x23daa
testl %ebp, %ebp
movl 0x28(%rsp), %r9d
je 0x23def
imull %ebp, %ebp
cmpl $0x1, %ebp
adcl $0x0, %ebp
xorl %ecx, %ecx
xorl %eax, %eax
movsbl (%r13,%rcx), %edx
cmpl %edx, %eax
cmovlel %edx, %eax
incq %rcx
cmpq %rcx, %rbp
jne 0x23ddc
testl %r9d, %r9d
jle 0x2427b
movl 0xf0(%rsp), %ecx
movl 0xd8(%rsp), %edi
imull %ebx, %eax
addl 0xf8(%rsp), %eax
movl %eax, %edx
movl 0xe0(%rsp), %esi
subl %esi, %edx
cvtsi2sd %edx, %xmm0
movl 0xd0(%rsp), %edx
movl 0xe8(%rsp), %esi
cvtsi2sd %esi, %xmm1
divsd %xmm1, %xmm0
movsd 0x465b(%rip), %xmm1     # 0x28498
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %esi
cmpl $0x2, %esi
movl $0x1, %r8d
cmovll %r8d, %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
subl %edx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
cvtsi2sd %edi, %xmm2
divsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
cmpl $0x2, %eax
cmovgel %eax, %r8d
cmpl %r8d, %esi
cmovll %esi, %r8d
leal 0x1(%r8), %ecx
movl 0x108(%rsp), %eax
subl %edx, %eax
movl %eax, 0x38(%rsp)
addl %edi, %edx
movq %r8, 0x70(%rsp)
movl %r8d, %r10d
negl %r10d
movl %r9d, %eax
movq %rax, 0x48(%rsp)
movq %r11, %rax
addq $0x4, %rax
movq %rax, 0x40(%rsp)
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
xorl %r9d, %r9d
xorl %r15d, %r15d
movq %rcx, 0x68(%rsp)
movl %ecx, %edi
xorl %r13d, %r13d
movl $0x0, 0x14(%rsp)
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
movl %ebx, %ecx
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movq %rbx, 0x58(%rsp)
movq %r11, 0x50(%rsp)
movq 0x68(%rsp), %rax
leal (%rax,%r15), %esi
movl %r15d, %eax
subl 0x70(%rsp), %eax
cmpl %eax, %r13d
cmovgl %r13d, %eax
cmpl %esi, %ecx
cmovll %ecx, %esi
cmpl %ebx, %esi
cmovgel %ebx, %esi
movl $0x0, %ebp
testl %eax, %eax
jne 0x23f35
movl %r15d, %ebp
notl %ebp
imull 0xd8(%rsp), %ebp
addl 0x38(%rsp), %ebp
testl %ebp, %ebp
cmovlel %r9d, %ebp
movl %ecx, 0x1c(%rsp)
movl %edi, 0x8(%rsp)
cmpl %esi, %eax
movq %rax, 0x28(%rsp)
movq %r13, 0x78(%rsp)
jge 0x241da
movslq %r10d, %r11
movq 0x60(%rsp), %rax
movq %r15, 0x90(%rsp)
movzbl (%rax,%r15), %eax
imull %ebx, %eax
movslq %eax, %r15
cmpl %r10d, %r13d
movl %r10d, 0x3c(%rsp)
movl %r10d, %eax
cmovgl %r13d, %eax
movq 0x30(%rsp), %rcx
addq %rax, %rcx
addq %rcx, %r15
movq 0x40(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movslq %r13d, %rcx
cmpq %r11, %rcx
movq %r11, 0x88(%rsp)
movq %rcx, 0x80(%rsp)
cmovgq %rcx, %r11
xorl %r13d, %r13d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %r12d, %r12d
xorl %r10d, %r10d
movl -0x4(%rax,%r13,8), %ecx
movl (%rax,%r13,8), %r8d
movl %ebp, -0x4(%rax,%r13,8)
movl $0x0, %ebx
testl %ecx, %ecx
je 0x23fd5
movsbl (%r15,%r13), %ebx
addl %ebx, %ecx
movl %ecx, %ebx
cmpl %r8d, %ebx
movl %r8d, %ebp
cmovgl %ebx, %ebp
cmpl %r10d, %ebp
cmovlel %r10d, %ebp
leal (%r11,%r13), %ecx
cmpl %ebp, %r12d
cmovbel %ecx, %edi
cmovlel %ebp, %r12d
movl %ebx, %ecx
subl %edx, %ecx
subl 0xd8(%rsp), %r8d
cmpl %r8d, %ecx
cmovgl %ecx, %r8d
testl %r8d, %r8d
cmovlel %r9d, %r8d
subl %r14d, %ebx
movl 0xe8(%rsp), %ecx
subl %ecx, %r10d
cmpl %r10d, %ebx
cmovgl %ebx, %r10d
testl %r10d, %r10d
cmovlel %r9d, %r10d
movl %r8d, (%rax,%r13,8)
incq %r13
leal (%r11,%r13), %ecx
cmpl %ecx, %esi
jg 0x23fb5
movq 0x88(%rsp), %rax
movq 0x80(%rsp), %rcx
cmpq %rax, %rcx
cmovgq %rcx, %rax
addl %eax, %r13d
movq 0x58(%rsp), %rbx
movq 0x50(%rsp), %r11
movl 0x3c(%rsp), %r10d
movq 0x90(%rsp), %r15
movslq %esi, %rax
movl %ebp, (%r11,%rax,8)
movl $0x0, 0x4(%r11,%rax,8)
cmpl %ebx, %r13d
jne 0x2409a
movl 0x18(%rsp), %eax
cmpl %ebp, %eax
movl 0x24(%rsp), %ecx
cmovlel %r15d, %ecx
movl %ecx, 0x24(%rsp)
cmovlel %ebp, %eax
movl %eax, 0x18(%rsp)
movq 0x28(%rsp), %r13
testl %r12d, %r12d
je 0x24114
movl 0x20(%rsp), %eax
subl %r12d, %eax
jge 0x240d6
movl %edi, %eax
subl %r15d, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
cmovlel %ecx, %eax
movl %eax, 0x14(%rsp)
movl %r15d, 0x10(%rsp)
movl %edi, 0xc(%rsp)
movl %r12d, 0x20(%rsp)
jmp 0x2411e
cmpl $0x0, 0x100(%rsp)
jle 0x2411e
movl %r15d, %ecx
subl 0x10(%rsp), %ecx
subl 0xc(%rsp), %edi
movl %ecx, %r8d
subl %edi, %r8d
jle 0x241aa
subl %ecx, %edi
imull 0xd8(%rsp), %edi
addl %edi, %eax
cmpl 0x100(%rsp), %eax
movl 0x1c(%rsp), %ecx
jle 0x241d1
xorl %eax, %eax
movl %esi, %ecx
movl 0x8(%rsp), %edi
jmp 0x24191
movl 0x8(%rsp), %edi
movl 0x1c(%rsp), %ecx
cmpl %esi, %r13d
jge 0x2415f
cmpl %edi, %ecx
cmovgel %edi, %ecx
movq 0x78(%rsp), %r13
cmpl %r10d, %r13d
cmovlel %r10d, %r13d
movq 0x40(%rsp), %rax
leaq (%rax,%r13,8), %rax
cmpl $0x0, -0x4(%rax)
jne 0x2415f
cmpl $0x0, (%rax)
jne 0x2415f
incl %r13d
addq $0x8, %rax
cmpl %r13d, %esi
jg 0x24145
movl %ecx, %r13d
cmpl %r13d, %esi
jl 0x24185
leal -0x1(%r13), %eax
movl %esi, %ecx
cmpl $0x0, (%r11,%rcx,8)
jne 0x24185
cmpl $0x0, 0x4(%r11,%rcx,8)
jne 0x24185
leal -0x1(%rsi), %ecx
cmpl %r13d, %esi
movl %ecx, %esi
jg 0x24168
movl %eax, %esi
addl $0x2, %esi
cmpl %ebx, %esi
cmovgel %ebx, %esi
movb $0x1, %al
movl %esi, %ecx
testb %al, %al
je 0x241ea
incq %r15
incl %r10d
incl %edi
cmpq 0x48(%rsp), %r15
jne 0x23ef4
jmp 0x241ea
movl 0xe8(%rsp), %ecx
imull %ecx, %r8d
addl %r8d, %eax
cmpl 0x100(%rsp), %eax
movl 0x8(%rsp), %edi
movl 0x1c(%rsp), %ecx
jle 0x24126
xorl %eax, %eax
jmp 0x2418f
movl 0x8(%rsp), %edi
jmp 0x24126
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %r12d, %r12d
movq %rax, %r13
jmp 0x2406c
incl 0xc(%rsp)
incl 0x10(%rsp)
movl 0x24(%rsp), %ebp
incl %ebp
movq 0x118(%rsp), %rbx
movq %r11, %rdi
callq 0x3390
movq 0x30(%rsp), %rdi
callq 0x3390
movq 0x110(%rsp), %rax
testq %rax, %rax
je 0x24225
movl 0xc(%rsp), %ecx
movl %ecx, (%rax)
movq 0x120(%rsp), %rax
testq %rbx, %rbx
je 0x24238
movl 0x10(%rsp), %ecx
movl %ecx, (%rbx)
movq 0x128(%rsp), %rcx
testq %rax, %rax
je 0x24247
movl %ebp, (%rax)
movq 0x130(%rsp), %rax
testq %rcx, %rcx
je 0x2425a
movl 0x18(%rsp), %edx
movl %edx, (%rcx)
testq %rax, %rax
je 0x24265
movl 0x14(%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, 0x18(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
movl $0x0, 0x14(%rsp)
jmp 0x241f8
leaq 0x538e(%rip), %rdi      # 0x29637
leaq 0x5368(%rip), %rsi      # 0x29618
leaq 0x5387(%rip), %rcx      # 0x2963e
movl $0x182, %edx            # imm = 0x182
callq 0x31a0