// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movq %r9, (%rsp)
movq %rcx, 0x70(%rsp)
movq %rdx, 0x40(%rsp)
movl 0xe8(%rsp), %eax
movl %eax, 0x1c(%rsp)
testl %eax, %eax
jle 0x1f55a
movl %r8d, %r12d
movq %rsi, %r15
movl %edi, %r8d
movl 0xc8(%rsp), %eax
movl 0xc0(%rsp), %ecx
leal (%rax,%rcx), %ebp
movl %r12d, %eax
imull %edi, %eax
movslq %eax, %rdi
leaq 0x5667(%rip), %r14      # 0x246c8
leaq 0x5722(%rip), %rbx      # 0x2478a
movq %r14, %rsi
movl $0x185, %edx            # imm = 0x185
movq %rbx, %rcx
movq %r8, 0x38(%rsp)
callq 0x1e045
movq %rax, %r13
movq 0x38(%rsp), %rax
incl %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
movq %r14, %rdx
movl $0x186, %ecx            # imm = 0x186
movq %rbx, %r8
callq 0x1dfd0
movq 0x38(%rsp), %r11
movq %rax, 0x20(%rsp)
xorl %eax, %eax
testl %r11d, %r11d
movl $0x0, %ecx
cmovgl %r11d, %ecx
movslq %r12d, %rdx
testl %edx, %edx
movl $0x0, %esi
movq %r12, %rbx
cmovgl %r12d, %esi
xorl %edi, %edi
cmpq %rsi, %rax
je 0x1f0fe
movq %rax, %r8
imulq %rdx, %r8
addq (%rsp), %r8
movslq %edi, %rdi
xorl %r9d, %r9d
cmpq %r9, %rcx
je 0x1f0f9
movzbl (%r15,%r9), %r10d
movb (%r8,%r10), %r10b
movb %r10b, (%r13,%rdi)
incq %rdi
incq %r9
jmp 0x1f0de
incq %rax
jmp 0x1f0c8
xorl %eax, %eax
movl 0xe8(%rsp), %edx
movl %edx, %ecx
subl %ebp, %ecx
cmovlel %eax, %ecx
movq 0x20(%rsp), %rdi
movl %edx, (%rdi)
movl %ecx, 0x8(%rdi)
movslq %r11d, %rax
pushq $0x2
popq %rcx
cmpq %rax, %rcx
jg 0x1f13f
movl -0x8(%rdi,%rcx,8), %edx
movl 0xc8(%rsp), %esi
subl %esi, %edx
movq 0x20(%rsp), %rdi
jle 0x1f13f
movl %edx, (%rdi,%rcx,8)
incq %rcx
jmp 0x1f11e
imull %ebx, %ebx
xorl %ecx, %ecx
xorl %eax, %eax
cmpq %rcx, %rbx
je 0x1f15d
movq (%rsp), %rdx
movsbl (%rdx,%rcx), %edx
cmpl %edx, %eax
cmovlel %edx, %eax
incq %rcx
jmp 0x1f146
imull %r11d, %eax
addl 0xd8(%rsp), %eax
movl %eax, %ecx
movl 0xc0(%rsp), %edx
subl %edx, %ecx
cvtsi2sd %ecx, %xmm0
movl 0xc8(%rsp), %ecx
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
movsd 0x433a(%rip), %xmm1     # 0x234c8
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
cmpl $0x2, %ecx
pushq $0x1
popq %rsi
cmovll %esi, %ecx
movl 0xd0(%rsp), %edx
cmpl %edx, %ecx
cmovgel %edx, %ecx
movl 0xb0(%rsp), %r14d
subl %r14d, %eax
cvtsi2sd %eax, %xmm0
movl 0xb8(%rsp), %edx
cvtsi2sd %edx, %xmm2
divsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
cmpl $0x2, %eax
cmovgel %eax, %esi
cmpl %esi, %ecx
cmovll %ecx, %esi
movl 0xe8(%rsp), %eax
subl %r14d, %eax
movl %eax, 0x4c(%rsp)
addl %edx, %r14d
movl %r14d, 0x50(%rsp)
movl %esi, %r10d
negl %r10d
xorl %r12d, %r12d
movq 0x40(%rsp), %rbx
testl %ebx, %ebx
cmovlel %r12d, %ebx
movq %rbx, 0x40(%rsp)
movq %rsi, 0x68(%rsp)
leal 0x1(%rsi), %eax
movq %rax, 0x60(%rsp)
movq %rdi, %r9
addq $0x4, %r9
pushq $-0x1
popq %rax
xorl %r8d, %r8d
movl %eax, 0xc(%rsp)
movl %eax, 0x8(%rsp)
movl %r11d, %edx
movl %eax, 0x14(%rsp)
movl %eax, 0x18(%rsp)
movl $0x0, 0x10(%rsp)
xorl %esi, %esi
movq %r13, 0x58(%rsp)
movq %r9, 0x30(%rsp)
cmpq 0x40(%rsp), %r8
je 0x1f4d5
movl %r8d, %eax
subl 0x68(%rsp), %eax
cmpl %eax, %esi
cmovgl %esi, %eax
movq 0x60(%rsp), %rcx
addl %r8d, %ecx
cmpl %ecx, %edx
cmovgel %ecx, %edx
cmpl %r11d, %edx
cmovgel %r11d, %edx
xorl %ecx, %ecx
movl $0x0, %r14d
testl %eax, %eax
movq %rsi, %rdi
jne 0x1f2a1
movl %r8d, %r14d
notl %r14d
imull 0xb8(%rsp), %r14d
addl 0x4c(%rsp), %r14d
testl %r14d, %r14d
cmovlel %r12d, %r14d
movl %r10d, 0x54(%rsp)
movslq %r10d, %rsi
movq 0x70(%rsp), %rax
movq %r8, (%rsp)
movzbl (%rax,%r8), %eax
imull %r11d, %eax
movslq %eax, %r11
cmpl %esi, %edi
movl %esi, %eax
cmovgl %edi, %eax
addq %r13, %r11
addq %rax, %r11
movq %rdi, 0x28(%rsp)
movslq %edi, %r8
cmpq %rsi, %r8
cmovgq %r8, %rsi
leaq (%r9,%rax,8), %r10
xorl %r13d, %r13d
xorl %r8d, %r8d
pushq $-0x1
popq %r9
movl 0x50(%rsp), %edi
leaq (%rsi,%r13), %rax
cmpl %eax, %edx
jle 0x1f371
movl -0x4(%r10,%r13,8), %ebx
movl (%r10,%r13,8), %r15d
movl %r14d, -0x4(%r10,%r13,8)
testl %ebx, %ebx
je 0x1f310
movsbl (%r11,%r13), %r14d
addl %r14d, %ebx
jmp 0x1f312
xorl %ebx, %ebx
cmpl %r15d, %ebx
movl %r15d, %r14d
cmovgl %ebx, %r14d
cmpl %ecx, %r14d
cmovlel %ecx, %r14d
cmpl %r14d, %r8d
cmoval %r9d, %eax
cmovlel %r14d, %r8d
movl %ebx, %r9d
subl %edi, %r9d
subl 0xb8(%rsp), %r15d
cmpl %r15d, %r9d
cmovgl %r9d, %r15d
testl %r15d, %r15d
cmovlel %r12d, %r15d
movl %r15d, (%r10,%r13,8)
subl %ebp, %ebx
movl 0xc8(%rsp), %r9d
subl %r9d, %ecx
cmpl %ecx, %ebx
cmovgl %ebx, %ecx
testl %ecx, %ecx
cmovlel %r12d, %ecx
incq %r13
movl %eax, %r9d
jmp 0x1f2ec
movslq %edx, %rax
movq 0x20(%rsp), %rdi
movl %r14d, (%rdi,%rax,8)
andl $0x0, 0x4(%rdi,%rax,8)
movl 0x54(%rsp), %r10d
movq 0x28(%rsp), %rcx
cmpl %r10d, %ecx
movl %r10d, %eax
cmovgl %ecx, %eax
movq 0x38(%rsp), %r11
movl %r11d, %ecx
subl %eax, %ecx
cmpl %r13d, %ecx
jne 0x1f3bf
movl 0x18(%rsp), %eax
cmpl %r14d, %eax
movl 0x14(%rsp), %ecx
cmovlel (%rsp), %ecx
movl %ecx, 0x14(%rsp)
cmovlel %r14d, %eax
movl %eax, 0x18(%rsp)
movq 0x58(%rsp), %r13
testl %r8d, %r8d
je 0x1f4d5
movl 0x1c(%rsp), %eax
subl %r8d, %eax
jge 0x1f403
movl %r9d, %eax
movq (%rsp), %rsi
subl %esi, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movl 0x10(%rsp), %eax
cmpl %ecx, %eax
cmovlel %ecx, %eax
movl %eax, 0x10(%rsp)
movl %esi, 0xc(%rsp)
movl %r9d, 0x8(%rsp)
movl %r8d, 0x1c(%rsp)
jmp 0x1f452
cmpl $0x0, 0xe0(%rsp)
jle 0x1f452
movq (%rsp), %rcx
subl 0xc(%rsp), %ecx
subl 0x8(%rsp), %r9d
movl %ecx, %esi
subl %r9d, %esi
jle 0x1f4af
subl %ecx, %r9d
imull 0xb8(%rsp), %r9d
addl %r9d, %eax
cmpl 0xe0(%rsp), %eax
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rcx
jg 0x1f4d5
movq 0x30(%rsp), %r9
jmp 0x1f45c
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %rcx
cmpl %r10d, %ecx
cmovlel %r10d, %ecx
leaq (%r9,%rcx,8), %rax
movl %ecx, %esi
cmpl %ecx, %edx
jle 0x1f47f
cmpl $0x0, -0x4(%rax)
jne 0x1f47f
cmpl $0x0, (%rax)
leaq 0x8(%rax), %rax
leal 0x1(%rsi), %ecx
je 0x1f467
movq (%rsp), %r8
cmpl %esi, %edx
jl 0x1f49a
movl %edx, %eax
cmpl $0x0, (%rdi,%rax,8)
jne 0x1f49a
cmpl $0x0, 0x4(%rdi,%rax,8)
jne 0x1f49a
decl %edx
jmp 0x1f483
addl $0x2, %edx
cmpl %r11d, %edx
cmovgel %r11d, %edx
incq %r8
incl %r10d
jmp 0x1f24c
movl 0xc8(%rsp), %ecx
imull %ecx, %esi
addl %esi, %eax
cmpl 0xe0(%rsp), %eax
movq 0x30(%rsp), %r9
jg 0x1f4d0
movq 0x20(%rsp), %rdi
jmp 0x1f457
movq 0x20(%rsp), %rdi
callq 0x3390
movq %r13, %rdi
callq 0x3390
movq 0xf0(%rsp), %rax
testq %rax, %rax
je 0x1f4f7
movl 0x8(%rsp), %ecx
incl %ecx
movl %ecx, (%rax)
movq 0xf8(%rsp), %rsi
testq %rsi, %rsi
movq 0x110(%rsp), %rax
movq 0x108(%rsp), %rcx
movq 0x100(%rsp), %rdx
je 0x1f524
movl 0xc(%rsp), %edi
incl %edi
movl %edi, (%rsi)
testq %rdx, %rdx
je 0x1f531
movl 0x14(%rsp), %esi
incl %esi
movl %esi, (%rdx)
testq %rcx, %rcx
je 0x1f53c
movl 0x18(%rsp), %edx
movl %edx, (%rcx)
testq %rax, %rax
je 0x1f547
movl 0x10(%rsp), %ecx
movl %ecx, (%rax)
movl 0x1c(%rsp), %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5186(%rip), %rdi      # 0x246e7
leaq 0x5160(%rip), %rsi      # 0x246c8
leaq 0x517f(%rip), %rcx      # 0x246ee
movl $0x182, %edx            # imm = 0x182
callq 0x31a0