// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movl %r8d, %r12d
movq %rcx, 0x50(%rsp)
movl %edx, 0x1c(%rsp)
movq %rsi, %r15
movq %rdi, 0x28(%rsp)
movl 0xa8(%rsp), %r14d
movl 0xa0(%rsp), %eax
movl 0xc0(%rsp), %ebp
xorl %ecx, %ecx
testl %ebp, %ebp
cmovlel %ecx, %ebp
leal (%r14,%rax), %ebx
movl %r8d, %eax
imull %edi, %eax
movslq %eax, %rdi
callq 0x7430
movq %rax, 0x38(%rsp)
movq 0x28(%rsp), %rax
incl %eax
movl %eax, 0x30(%rsp)
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x71a0
testl %r12d, %r12d
jle 0x7c14
movl %r12d, %ecx
movl 0x28(%rsp), %edx
xorl %esi, %esi
xorl %edi, %edi
cmpl $0x0, 0x28(%rsp)
jle 0x7c0c
movq %rsi, %r8
imulq %rcx, %r8
addq %r13, %r8
movslq %edi, %r9
addq 0x38(%rsp), %r9
xorl %r10d, %r10d
movzbl (%r15,%r10), %r11d
movb (%r8,%r11), %r11b
movb %r11b, (%r9,%r10)
incq %r10
cmpq %r10, %rdx
jne 0x7bf4
addl %r10d, %edi
incq %rsi
cmpq %rcx, %rsi
jne 0x7bd8
movl %ebp, (%rax)
movl %ebp, %ecx
subl %ebx, %ecx
movl $0x0, %edx
cmovgl %ecx, %edx
movl %edx, 0x8(%rax)
cmpl $0x2, 0x28(%rsp)
jl 0x7c47
movl 0x30(%rsp), %ecx
pushq $0x2
popq %rdx
movl -0x8(%rax,%rdx,8), %esi
subl %r14d, %esi
jle 0x7c47
movl %esi, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, %rcx
jne 0x7c33
movl $0x0, 0xc(%rsp)
movl $0x0, %ecx
testl %r12d, %r12d
movl 0x1c(%rsp), %edi
je 0x7c80
imull %r12d, %r12d
cmpl $0x1, %r12d
adcl $0x0, %r12d
xorl %edx, %edx
xorl %ecx, %ecx
movsbl (%r13,%rdx), %esi
cmpl %esi, %ecx
cmovlel %esi, %ecx
incq %rdx
cmpq %rdx, %r12
jne 0x7c6d
movq 0xc8(%rsp), %r15
testl %edi, %edi
jle 0x7fae
movq 0x28(%rsp), %r13
imull %r13d, %ecx
movl 0xa0(%rsp), %edx
movq %rdx, %r8
subl %r8d, %ecx
cvtsi2sd %ecx, %xmm0
movl 0xb0(%rsp), %ecx
cvtsi2sd %r14d, %xmm1
divsd %xmm1, %xmm0
addsd 0x38be6(%rip), %xmm0    # 0x408a8
cvttsd2si %xmm0, %edx
cmpl $0x2, %edx
pushq $0x1
popq %rsi
cmovgel %edx, %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
movl %ebp, %ecx
subl %r8d, %ecx
movl %ecx, 0x44(%rsp)
leal 0x1(%rsi), %ecx
movq %rcx, 0x58(%rsp)
movq %rsi, 0x60(%rsp)
movl %esi, %ecx
negl %ecx
movslq %ecx, %rsi
movl %edi, %ecx
movq %rcx, 0x48(%rsp)
pushq $-0x1
popq %rdx
xorl %r11d, %r11d
movl $0x0, 0x10(%rsp)
movl %edx, 0x14(%rsp)
movl %edx, 0x18(%rsp)
xorl %edi, %edi
movl %edx, %ecx
movq %rcx, 0x20(%rsp)
movl %edx, 0xc(%rsp)
movl %r13d, %r12d
movl %r11d, %r8d
notl %r8d
imull %r14d, %r8d
addl 0x44(%rsp), %r8d
testl %r8d, %r8d
movl $0x0, %ecx
cmovlel %ecx, %r8d
movl %r11d, %r9d
subl 0x60(%rsp), %r9d
cmpl %r9d, %edi
cmovgl %edi, %r9d
movq 0x58(%rsp), %rcx
leal (%rcx,%r11), %r10d
cmpl %r10d, %r12d
cmovll %r12d, %r10d
cmpl %r13d, %r10d
cmovgel %r13d, %r10d
cmpl %r10d, %r9d
movq %rsi, 0x30(%rsp)
jge 0x7f02
movl %ebp, 0x1c(%rsp)
movq 0x50(%rsp), %rcx
movzbl (%rcx,%r11), %ecx
imull %r13d, %ecx
movslq %ecx, %r13
addq 0x38(%rsp), %r13
movslq %edi, %r15
cmpq %r15, %rsi
cmovgq %rsi, %r15
movslq %r10d, %rcx
xorl %edi, %edi
pushq $-0x1
popq %rdx
movl %edx, %r12d
xorl %edx, %edx
movl %r12d, %esi
movl (%rax,%r15,8), %r12d
movl 0x4(%rax,%r15,8), %ebp
movl %r8d, (%rax,%r15,8)
movsbl (%r13,%r15), %r8d
addl %r12d, %r8d
cmpl %ebp, %r8d
cmovlel %ebp, %r8d
cmpl %edx, %r8d
cmovlel %edx, %r8d
cmpl %r8d, %edi
movl %r15d, %r12d
cmovgl %esi, %r12d
cmovlel %r8d, %edi
movl %r8d, %esi
subl %ebx, %esi
testl %esi, %esi
jg 0x7dde
xorl %esi, %esi
subl %r14d, %ebp
cmpl %esi, %ebp
jg 0x7de7
movl %esi, %ebp
movl %ebp, 0x4(%rax,%r15,8)
subl %r14d, %edx
cmpl %esi, %edx
cmovlel %esi, %edx
incq %r15
cmpq %rcx, %r15
jl 0x7d9e
movl 0x1c(%rsp), %ebp
movq 0x28(%rsp), %r13
movl %r8d, (%rax,%rcx,8)
andl $0x0, 0x4(%rax,%rcx,8)
cmpl %r13d, %r15d
jne 0x7e2e
movl 0x14(%rsp), %edx
cmpl %r8d, %edx
movl 0x18(%rsp), %esi
cmovlel %r11d, %esi
movl %esi, 0x18(%rsp)
cmovlel %r8d, %edx
movl %edx, 0x14(%rsp)
movl 0xb8(%rsp), %r8d
testl %edi, %edi
je 0x7f15
testl %r8d, %r8d
jle 0x7e6d
movq 0x20(%rsp), %rdx
addl %r11d, %edx
addl 0xc(%rsp), %r15d
subl %r15d, %edx
movl %edx, %esi
negl %esi
cmovsl %edx, %esi
imull %r14d, %esi
addl %edi, %esi
movl %ebp, %edx
subl %esi, %edx
cmpl %r8d, %edx
jg 0x7f15
cmpl %ebp, %edi
jbe 0x7e9a
movl %r12d, %edx
subl %r11d, %edx
movl %edx, %esi
negl %esi
cmovsl %edx, %esi
movl 0x10(%rsp), %edx
cmpl %esi, %edx
cmovlel %esi, %edx
movl %edx, 0x10(%rsp)
movl %edi, %ebp
movl %r11d, 0xc(%rsp)
movl %r12d, %edx
movq %rdx, 0x20(%rsp)
movq 0x30(%rsp), %rsi
movl %r12d, %edi
cmpl %r9d, %r12d
jl 0x7eca
movq %rsi, %r8
movslq %r12d, %rdx
movslq %r9d, %rsi
cmpl $0x0, (%rax,%rdx,8)
je 0x7ec4
leaq -0x1(%rdx), %rdi
cmpq %rsi, %rdx
movq %rdi, %rdx
jg 0x7eb0
jmp 0x7ec7
movq %rdx, %rdi
movq %r8, %rsi
addl $0x2, %r12d
cmpl %r10d, %r12d
jg 0x7eed
movslq %r12d, %rdx
cmpl $0x0, (%rax,%rdx,8)
je 0x7eea
leaq 0x1(%rdx), %r12
cmpq %rcx, %rdx
movq %r12, %rdx
jl 0x7ed6
jmp 0x7eed
movq %rdx, %r12
incq %r11
incl %edi
incq %rsi
cmpq 0x48(%rsp), %r11
jne 0x7d1e
jmp 0x7f15
movslq %r10d, %rcx
xorl %edi, %edi
pushq $-0x1
popq %rdx
movl %edx, %r12d
movl %r9d, %r15d
jmp 0x7e05
movq 0x20(%rsp), %rcx
incl %ecx
movq %rcx, 0x20(%rsp)
incl 0xc(%rsp)
movl 0x18(%rsp), %r14d
incl %r14d
movq 0xc8(%rsp), %r15
movq 0xd0(%rsp), %rbx
movq %rax, %rdi
callq 0x7380
movq 0x38(%rsp), %rdi
callq 0x7380
testq %r15, %r15
je 0x7f5c
movq 0x20(%rsp), %rax
movl %eax, (%r15)
movq 0xd8(%rsp), %rax
testq %rbx, %rbx
je 0x7f6f
movl 0xc(%rsp), %ecx
movl %ecx, (%rbx)
movq 0xe0(%rsp), %rcx
testq %rax, %rax
je 0x7f7f
movl %r14d, (%rax)
movq 0xe8(%rsp), %rax
testq %rcx, %rcx
je 0x7f92
movl 0x14(%rsp), %edx
movl %edx, (%rcx)
testq %rax, %rax
je 0x7f9d
movl 0x10(%rsp), %ecx
movl %ecx, (%rax)
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rcx
movl %ecx, 0x14(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x10(%rsp)
xorl %r14d, %r14d
jmp 0x7f35