// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %r9, %rbp
movl %r8d, %r14d
movq %rcx, 0x40(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %r15
movl %edi, %r13d
movl 0x98(%rsp), %ebx
movl 0x90(%rsp), %ecx
movl 0xb0(%rsp), %eax
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
movl %eax, (%rsp)
leal (%rbx,%rcx), %r12d
movl %r8d, %eax
imull %edi, %eax
movslq %eax, %rdi
callq 0x7430
movq %rax, 0x30(%rsp)
movq %r13, 0x18(%rsp)
incl %r13d
movslq %r13d, %rdi
pushq $0x8
popq %rsi
callq 0x71a0
testl %r14d, %r14d
jle 0x7c78
movl %r14d, %ecx
movl 0x18(%rsp), %edx
xorl %esi, %esi
xorl %edi, %edi
cmpl $0x0, 0x18(%rsp)
jle 0x7c70
movq %rsi, %r8
imulq %rcx, %r8
addq %rbp, %r8
movslq %edi, %r9
addq 0x30(%rsp), %r9
xorl %r10d, %r10d
movzbl (%r15,%r10), %r11d
movb (%r8,%r11), %r11b
movb %r11b, (%r9,%r10)
incq %r10
cmpq %r10, %rdx
jne 0x7c58
addl %r10d, %edi
incq %rsi
cmpq %rcx, %rsi
jne 0x7c3c
movl (%rsp), %ecx
movl %ecx, (%rax)
subl %r12d, %ecx
movl $0x0, %edx
cmovgl %ecx, %edx
movl %edx, 0x8(%rax)
cmpl $0x2, 0x18(%rsp)
jl 0x7ca8
pushq $0x2
popq %rcx
movl -0x8(%rax,%rcx,8), %edx
subl %ebx, %edx
jle 0x7ca8
movl %edx, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r13
jne 0x7c95
movl 0xa0(%rsp), %ecx
xorl %edi, %edi
movl $0x0, %edx
testl %r14d, %r14d
je 0x7ce0
imull %r14d, %r14d
cmpl $0x1, %r14d
adcl $0x0, %r14d
xorl %esi, %esi
xorl %edx, %edx
movsbl (%rbp,%rsi), %r8d
cmpl %r8d, %edx
cmovlel %r8d, %edx
incq %rsi
cmpq %rsi, %r14
jne 0x7ccb
movq 0x18(%rsp), %r14
imull %r14d, %edx
movl 0x90(%rsp), %esi
movq %rsi, %r8
subl %r8d, %edx
cvtsi2sd %edx, %xmm0
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm0
addsd 0x38b9e(%rip), %xmm0    # 0x408a8
cvttsd2si %xmm0, %edx
cmpl $0x2, %edx
pushq $0x1
popq %rsi
cmovgel %edx, %esi
cmpl %ecx, %esi
cmovgel %ecx, %esi
movl (%rsp), %ecx
subl %r8d, %ecx
movl %ecx, 0x3c(%rsp)
leal 0x1(%rsi), %ecx
movq %rcx, 0x48(%rsp)
movq %rsi, 0x50(%rsp)
movl %esi, %ebp
negl %ebp
movq 0x28(%rsp), %r13
testl %r13d, %r13d
cmovlel %edi, %r13d
movq %r13, 0x28(%rsp)
pushq $-0x1
popq %rdx
xorl %r10d, %r10d
movl %r14d, %r11d
movl %edx, 0xc(%rsp)
movl %edx, %ecx
movq %rcx, 0x20(%rsp)
movl %edx, 0x10(%rsp)
movl %edx, 0x14(%rsp)
movl $0x0, 0x8(%rsp)
cmpq 0x28(%rsp), %r10
je 0x7f55
movl %r10d, %r8d
notl %r8d
imull %ebx, %r8d
addl 0x3c(%rsp), %r8d
xorl %r15d, %r15d
testl %r8d, %r8d
cmovlel %r15d, %r8d
movq 0x48(%rsp), %rcx
addl %r10d, %ecx
movl %r10d, %edx
subl 0x50(%rsp), %edx
cmpl %edx, %edi
cmovgl %edi, %edx
cmpl %ecx, %r11d
cmovgel %ecx, %r11d
cmpl %r14d, %r11d
cmovgel %r14d, %r11d
movslq %r11d, %r9
cmpl %r11d, %edx
movl %ebp, 0x4(%rsp)
jge 0x7f4b
movq 0x40(%rsp), %rcx
movzbl (%rcx,%r10), %ecx
imull %r14d, %ecx
movslq %ecx, %rcx
addq 0x30(%rsp), %rcx
cmpl %ebp, %edi
cmovlel %ebp, %edi
movslq %edi, %rdi
xorl %r15d, %r15d
pushq $-0x1
popq %rbp
xorl %r14d, %r14d
movl %ebp, %esi
movl (%rax,%rdi,8), %ebp
movl 0x4(%rax,%rdi,8), %r13d
movl %r8d, (%rax,%rdi,8)
movsbl (%rcx,%rdi), %r8d
addl %ebp, %r8d
cmpl %r13d, %r8d
cmovlel %r13d, %r8d
cmpl %r14d, %r8d
cmovlel %r14d, %r8d
cmpl %r8d, %r15d
movl %edi, %ebp
cmovgl %esi, %ebp
cmovlel %r8d, %r15d
movl %r8d, %esi
subl %r12d, %esi
testl %esi, %esi
jg 0x7e25
xorl %esi, %esi
subl %ebx, %r13d
cmpl %esi, %r13d
jg 0x7e30
movl %esi, %r13d
movl %r13d, 0x4(%rax,%rdi,8)
subl %ebx, %r14d
cmpl %esi, %r14d
cmovlel %esi, %r14d
incq %rdi
cmpq %r9, %rdi
jl 0x7de9
movq 0x18(%rsp), %r14
movl %r8d, (%rax,%r9,8)
andl $0x0, 0x4(%rax,%r9,8)
cmpl %r14d, %edi
jne 0x7e76
movl 0x14(%rsp), %ecx
cmpl %r8d, %ecx
movl 0x10(%rsp), %esi
cmovlel %r10d, %esi
movl %esi, 0x10(%rsp)
cmovlel %r8d, %ecx
movl %ecx, 0x14(%rsp)
movl 0xa8(%rsp), %r8d
testl %r15d, %r15d
je 0x7ead
testl %r8d, %r8d
jle 0x7eb1
movq 0x20(%rsp), %rcx
addl %r10d, %ecx
addl 0xc(%rsp), %edi
subl %edi, %ecx
movl %ecx, %esi
negl %esi
cmovsl %ecx, %esi
imull %ebx, %esi
addl %r15d, %esi
movl (%rsp), %ecx
subl %esi, %ecx
cmpl %r8d, %ecx
jle 0x7eb1
xorl %esi, %esi
jmp 0x7f15
cmpl (%rsp), %r15d
jle 0x7ee0
movl %ebp, %ecx
subl %r10d, %ecx
movl %ecx, %esi
negl %esi
cmovsl %ecx, %esi
movl 0x8(%rsp), %ecx
cmpl %esi, %ecx
cmovlel %esi, %ecx
movl %ecx, 0x8(%rsp)
movl %r15d, (%rsp)
movl %r10d, 0xc(%rsp)
movl %ebp, %ecx
movq %rcx, 0x20(%rsp)
movl %ebp, %ecx
cmpl %edx, %ebp
jl 0x7f03
movslq %ebp, %rsi
movslq %edx, %rdx
cmpl $0x0, (%rax,%rsi,8)
je 0x7f00
leaq -0x1(%rsi), %rcx
cmpq %rdx, %rsi
movq %rcx, %rsi
jg 0x7eec
jmp 0x7f03
movq %rsi, %rcx
movl %ecx, %edx
incl %edx
addl $0x2, %ebp
movb $0x1, %sil
cmpl %r11d, %ebp
jle 0x7f2b
movl %ebp, %r11d
movl 0x4(%rsp), %ebp
incq %r10
incl %ebp
movl %edx, %edi
testb %sil, %sil
jne 0x7d6c
jmp 0x7f55
movslq %ebp, %rcx
movl 0x4(%rsp), %ebp
cmpl $0x0, (%rax,%rcx,8)
je 0x7f46
leaq 0x1(%rcx), %r11
cmpq %r9, %rcx
movq %r11, %rcx
jl 0x7f32
jmp 0x7f19
movq %rcx, %r11
jmp 0x7f19
pushq $-0x1
popq %rbp
movl %edx, %edi
jmp 0x7e4c
movq %rax, %rdi
callq 0x7380
movq 0x30(%rsp), %rdi
callq 0x7380
movq 0xb8(%rsp), %rax
testq %rax, %rax
je 0x7f7d
movq 0x20(%rsp), %rcx
incl %ecx
movl %ecx, (%rax)
movq 0xc0(%rsp), %rsi
testq %rsi, %rsi
movq 0xd8(%rsp), %rax
movq 0xd0(%rsp), %rcx
movq 0xc8(%rsp), %rdx
je 0x7faa
movl 0xc(%rsp), %edi
incl %edi
movl %edi, (%rsi)
testq %rdx, %rdx
je 0x7fb7
movl 0x10(%rsp), %esi
incl %esi
movl %esi, (%rdx)
testq %rcx, %rcx
je 0x7fc2
movl 0x14(%rsp), %edx
movl %edx, (%rcx)
testq %rax, %rax
je 0x7fcd
movl 0x8(%rsp), %ecx
movl %ecx, (%rax)
movl (%rsp), %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq