// File: /workspace/github_cpp_makefile_star2_O2/readmapping[P]alfalfa/src/ksw.c
// Function: ksw_extend
int ksw_extend(int qlen, const uint8_t *query, int tlen, const uint8_t *target, int m, const int8_t *mat, int gapo, int gape, int w, 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, gapoe = gapo + gape, beg, end, max, max_i, max_j, max_gap, max_ie, gscore, max_off;
	if (h0 < 0) 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 > gapoe? h0 - gapoe : 0;
	for (j = 2; j <= qlen && eh[j-1].h > gape; ++j)
		eh[j].h = eh[j-1].h - gape;
	// 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_gap = (int)((double)(qlen * max - gapo) / gape + 1.);
	max_gap = max_gap > 1? max_gap : 1;
	w = w < max_gap? w : max_gap;
	// 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 f = 0, h1, m = 0, mj = -1;
		int8_t *q = &qp[target[i] * qlen];
		// compute the first column
		h1 = h0 - (gapo + gape * (i + 1));
		if (h1 < 0) h1 = 0;
		// 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;
		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 = 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
			h += q[j];
			h = h > e? h : e;
			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]
			h -= gapoe;
			h = h > 0? h : 0;
			e -= gape;
			e = e > h? e : h;   // computed E(i+1,j)
			p->e = e;           // save E(i+1,j) for the next row
			f -= gape;
			f = f > h? f : h;   // 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 || (zdrop > 0 && max - m - abs((i - max_i) - (j - max_j)) * gape > zdrop)) break; // drop to zero, or below Z-dropoff
		if (m > max) {
			max = m, max_i = i, max_j = mj;
			max_off = max_off > abs(mj - i)? max_off : abs(mj - i);
		}
		// update beg and end for the next round
		for (j = mj; j >= beg && eh[j].h; --j);
		beg = j + 1;
		for (j = mj + 2; j <= end && eh[j].h; ++j);
		end = j;
		//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 $0x68, %rsp
movq %r9, %rbp
movl %r8d, %r14d
movq %rcx, 0x60(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %r15
movq %rdi, 0x18(%rsp)
movl %r8d, %eax
imull %edi, %eax
movslq %eax, %rdi
movl 0xa8(%rsp), %ebx
movl 0xa0(%rsp), %eax
leal (%rbx,%rax), %r12d
callq 0x7430
movq %rax, %r13
movq 0x18(%rsp), %rax
incl %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x71b0
xorl %ecx, %ecx
movq 0x18(%rsp), %rsi
testl %esi, %esi
movl $0x0, %edx
cmovgl %esi, %edx
movslq %r14d, %rsi
testl %esi, %esi
movl $0x0, %edi
cmovgl %r14d, %edi
xorl %r8d, %r8d
cmpq %rdi, %rcx
je 0x30e7b
movq %rcx, %r9
imulq %rsi, %r9
addq %rbp, %r9
movslq %r8d, %r8
xorl %r10d, %r10d
cmpq %r10, %rdx
je 0x30e76
movzbl (%r15,%r10), %r11d
movb (%r9,%r11), %r11b
movb %r11b, (%r13,%r8)
incq %r8
incq %r10
jmp 0x30e5b
incq %rcx
jmp 0x30e46
xorl %ecx, %ecx
movl 0xc0(%rsp), %r8d
testl %r8d, %r8d
cmovlel %ecx, %r8d
movl %r8d, %edx
subl %r12d, %edx
cmovlel %ecx, %edx
movl %r8d, (%rax)
movl %edx, 0x8(%rax)
movq 0x18(%rsp), %r15
movslq %r15d, %rcx
pushq $0x2
popq %rdx
cmpq %rcx, %rdx
jg 0x30ebb
movl -0x8(%rax,%rdx,8), %esi
subl %ebx, %esi
jle 0x30ebb
movl %esi, (%rax,%rdx,8)
incq %rdx
jmp 0x30ea6
imull %r14d, %r14d
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rdx, %r14
je 0x30ed7
movsbl (%rbp,%rdx), %esi
cmpl %esi, %ecx
cmovlel %esi, %ecx
incq %rdx
jmp 0x30ec3
imull %r15d, %ecx
movl 0xa0(%rsp), %edx
movq %rdx, %rsi
subl %esi, %ecx
cvtsi2sd %ecx, %xmm0
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm0
addsd 0x1a25(%rip), %xmm0     # 0x32920
cvttsd2si %xmm0, %ecx
cmpl $0x2, %ecx
pushq $0x1
popq %rdx
cmovgel %ecx, %edx
movl 0xb0(%rsp), %ecx
cmpl %ecx, %edx
cmovgel %ecx, %edx
movl %r8d, %ecx
subl %esi, %ecx
movl %ecx, 0x34(%rsp)
leal 0x1(%rdx), %esi
movq %rdx, 0x58(%rsp)
movl %edx, %r9d
negl %r9d
xorl %r14d, %r14d
movq 0x20(%rsp), %rcx
testl %ecx, %ecx
cmovlel %r14d, %ecx
movq %rcx, 0x20(%rsp)
movl %r15d, %ecx
movq %rcx, 0x48(%rsp)
pushq $-0x1
popq %rdi
movq %rsi, 0x50(%rsp)
movl %r15d, %r10d
movl %edi, 0x10(%rsp)
movl %edi, %ecx
movq %rcx, 0x28(%rsp)
xorl %edx, %edx
movl %edi, 0x8(%rsp)
movl %edi, 0xc(%rsp)
movl $0x0, 0x4(%rsp)
movl %r8d, 0x14(%rsp)
cmpq 0x20(%rsp), %r14
je 0x3116c
movl %esi, 0x38(%rsp)
movq 0x60(%rsp), %rcx
movzbl (%rcx,%r14), %ecx
imull %r15d, %ecx
movq %r14, %rdi
movslq %ecx, %r14
addq %r13, %r14
movl %edi, %r8d
notl %r8d
imull %ebx, %r8d
addl 0x34(%rsp), %r8d
xorl %r11d, %r11d
testl %r8d, %r8d
cmovlel %r11d, %r8d
movl %edi, %esi
subl 0x58(%rsp), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movq 0x50(%rsp), %rcx
movq %rdi, 0x40(%rsp)
addl %ecx, %edi
cmpl %edi, %r10d
cmovll %r10d, %edi
cmpl %r15d, %edi
cmovgel %r15d, %edi
cmpl %r9d, %edx
movl %r9d, 0x3c(%rsp)
cmovlel %r9d, %edx
movslq %edx, %rcx
movslq %edi, %r9
xorl %edx, %edx
pushq $-0x1
popq %rdi
movl %edi, %r15d
cmpq %r9, %rcx
jge 0x31049
movl (%rax,%rcx,8), %ebp
movl 0x4(%rax,%rcx,8), %edi
movl %r8d, (%rax,%rcx,8)
movsbl (%r14,%rcx), %r8d
addl %ebp, %r8d
cmpl %edi, %r8d
cmovlel %edi, %r8d
cmpl %r11d, %r8d
cmovlel %r11d, %r8d
cmpl %r8d, %edx
cmovlel %ecx, %r15d
cmovlel %r8d, %edx
movl %r8d, %ebp
subl %r12d, %ebp
testl %ebp, %ebp
jg 0x3102e
xorl %ebp, %ebp
subl %ebx, %edi
cmpl %ebp, %edi
jg 0x31036
movl %ebp, %edi
movl %edi, 0x4(%rax,%rcx,8)
subl %ebx, %r11d
cmpl %ebp, %r11d
cmovlel %ebp, %r11d
incq %rcx
jmp 0x30ff1
movl %r8d, (%rax,%r9,8)
andl $0x0, 0x4(%rax,%r9,8)
cmpl %ecx, 0x48(%rsp)
jne 0x3108a
movl 0xc(%rsp), %edi
cmpl %r8d, %edi
movl 0x8(%rsp), %r11d
movq 0x40(%rsp), %r14
cmovlel %r14d, %r11d
movl %r11d, 0x8(%rsp)
cmovlel %r8d, %edi
movl %edi, 0xc(%rsp)
movl 0x14(%rsp), %r8d
movl 0xb8(%rsp), %r11d
jmp 0x3109c
movl 0x14(%rsp), %r8d
movl 0xb8(%rsp), %r11d
movq 0x40(%rsp), %r14
testl %edx, %edx
je 0x3116c
testl %r11d, %r11d
jle 0x310d1
movq 0x28(%rsp), %rdi
addl %r14d, %edi
subl 0x10(%rsp), %edi
subl %ecx, %edi
movl %edi, %ecx
negl %ecx
cmovsl %edi, %ecx
imull %ebx, %ecx
addl %edx, %ecx
movl %r8d, %edi
subl %ecx, %edi
cmpl %r11d, %edi
jg 0x3116c
cmpl %r8d, %edx
jbe 0x31100
movl %r15d, %ecx
subl %r14d, %ecx
movl %ecx, %edi
negl %edi
cmovsl %ecx, %edi
movl 0x4(%rsp), %ecx
cmpl %edi, %ecx
cmovlel %edi, %ecx
movl %ecx, 0x4(%rsp)
movl %edx, %r8d
movl %r14d, 0x10(%rsp)
movl %r15d, %ecx
movq %rcx, 0x28(%rsp)
incq %r14
movslq %r15d, %rcx
movslq %esi, %rsi
leal 0x1(%r15), %edx
cmpq %rsi, %rcx
jl 0x3111f
cmpl $0x0, (%rax,%rcx,8)
je 0x3111f
decq %rcx
decl %edx
jmp 0x3110d
addl $0x2, %r15d
movslq %r15d, %rcx
movq 0x18(%rsp), %r15
cmpl %r15d, %r10d
cmovgel %r15d, %r10d
movl 0x38(%rsp), %esi
cmpl %esi, %r10d
cmovgel %esi, %r10d
movslq %r10d, %r10
incq %r10
cmpq %rcx, %r10
cmovleq %rcx, %r10
cmpq %r9, %rcx
jg 0x3115d
cmpl $0x0, (%rax,%rcx,8)
je 0x3115a
incq %rcx
jmp 0x3114a
movq %rcx, %r10
movl 0x3c(%rsp), %r9d
incl %r9d
incl %esi
jmp 0x30f6e
movq %rax, %rdi
callq 0x7360
movq %r13, %rdi
callq 0x7360
movq 0xc8(%rsp), %rax
testq %rax, %rax
je 0x31192
movq 0x28(%rsp), %rcx
incl %ecx
movl %ecx, (%rax)
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
movq 0xe8(%rsp), %rdx
movq 0xe0(%rsp), %rcx
movq 0xd8(%rsp), %rsi
je 0x311bf
movl 0x10(%rsp), %eax
incl %eax
movl %eax, (%rdi)
testq %rsi, %rsi
movl 0x14(%rsp), %eax
je 0x311d0
movl 0x8(%rsp), %edi
incl %edi
movl %edi, (%rsi)
testq %rcx, %rcx
je 0x311db
movl 0xc(%rsp), %esi
movl %esi, (%rcx)
testq %rdx, %rdx
je 0x311e6
movl 0x4(%rsp), %ecx
movl %ecx, (%rdx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq