// File: /workspace/github_cpp_makefile_star2_O3/readmapping[P]alfalfa/src/ksw.c
// Function: ksw_nw
int ksw_nw(int qlen, const uint8_t *query, int tlen, const uint8_t *target, int m, const int8_t *mat, int gapo, int gape, int w)
{
	eh_t *eh;
	int8_t *qp; // query profile
	int i, j, k, gapoe = gapo + gape, score;
	// 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 = 0; eh[0].e = MINUS_INF;
	for (j = 1; j <= qlen && j <= w; ++j)
		eh[j].h = -(gapo + gape * j), eh[j].e = MINUS_INF;
	for (; j <= qlen; ++j) eh[j].h = eh[j].e = MINUS_INF; // everything is -inf outside the band
	// DP loop
	for (i = 0; LIKELY(i < tlen); ++i) { // target sequence is in the outer loop
		int32_t f = MINUS_INF, h1, beg, end;
		int8_t *q = &qp[target[i] * qlen];
		beg = i > w? i - w : 0;
		end = i + w + 1 < qlen? i + w + 1 : qlen; // only loop through [beg,end) of the query sequence
		h1 = beg == 0? -(gapo + gape * (i + 1)) : MINUS_INF;
		for (j = beg; LIKELY(j < end); ++j) {
			// This loop is organized in a similar way to ksw_extend() and ksw_sse2(), except:
			// 1) not checking h>0; 2) recording direction for backtracking
			eh_t *p = &eh[j];
			int32_t h = p->h, e = p->e;
			p->h = h1;
			h += q[j];
			h = h >= e? h : e;
			h = h >= f? h : f;
			h1 = h;
			h -= gapoe;
			e -= gape;
			e = e > h? e : h;
			p->e = e;
			f -= gape;
			f = f > h? f : h;
		}
		eh[end].h = h1; eh[end].e = MINUS_INF;
	}
	score = eh[qlen].h;
	free(eh); free(qp);
	return score;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r12
movl %r8d, %ebp
movq %rcx, 0x10(%rsp)
movl %edx, %ebx
movq %rsi, %r13
movl %edi, %r14d
movl 0x70(%rsp), %r15d
movl %r8d, %eax
imull %edi, %eax
movslq %eax, %rdi
callq 0x7430
movq %rax, 0x8(%rsp)
leal 0x1(%r14), %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x71a0
testl %ebp, %ebp
jle 0x86e1
movl %ebp, %ecx
movl %r14d, %edx
xorl %esi, %esi
xorl %edi, %edi
testl %r14d, %r14d
jle 0x86d9
movq %rsi, %r8
imulq %rcx, %r8
addq %r12, %r8
movslq %edi, %r9
addq 0x8(%rsp), %r9
xorl %r10d, %r10d
movzbl (%r13,%r10), %r11d
movb (%r8,%r11), %r11b
movb %r11b, (%r9,%r10)
incq %r10
cmpq %r10, %rdx
jne 0x86c0
addl %r10d, %edi
incq %rsi
cmpq %rcx, %rsi
jne 0x86a6
movl 0x68(%rsp), %ecx
movl 0x60(%rsp), %r11d
movl $0xc0000000, 0x4(%rax)  # imm = 0xC0000000
cmpl %r15d, %r14d
movl %r15d, %edx
cmovll %r14d, %edx
testl %edx, %edx
jle 0x8729
leal 0x1(%rdx), %esi
movl %edx, %edi
leal (%rcx,%r11), %r8d
negl %r8d
xorl %r9d, %r9d
movl %r8d, 0x8(%rax,%r9,8)
movl $0xc0000000, 0xc(%rax,%r9,8) # imm = 0xC0000000
subl %ecx, %r8d
incq %r9
cmpq %r9, %rdi
jne 0x870e
jmp 0x872c
pushq $0x1
popq %rsi
cmpl %r14d, %esi
jg 0x8759
xorl %esi, %esi
testl %edx, %edx
cmovgl %edx, %esi
leaq (%rax,%rsi,8), %rdx
addq $0xc, %rdx
movabsq $-0x3fffffff40000000, %rdi # imm = 0xC0000000C0000000
movq %rdi, -0x4(%rdx)
incl %esi
addq $0x8, %rdx
cmpl %r14d, %esi
jl 0x874a
testl %ebx, %ebx
jle 0x8862
leal 0x1(%r15), %edx
movq %rdx, 0x20(%rsp)
leal (%rcx,%r11), %edi
movslq %r15d, %rdx
movq %rdx, 0x18(%rsp)
movl %ebx, %r9d
xorl %r12d, %r12d
movl %r12d, %r10d
subl %r15d, %r10d
cmpq 0x18(%rsp), %r12
movl $0x0, %edx
cmovlel %edx, %r10d
movq 0x20(%rsp), %rdx
addl %r12d, %edx
cmpl %r14d, %edx
cmovgel %r14d, %edx
movl %r12d, %ebp
notl %ebp
imull %ecx, %ebp
subl %r11d, %ebp
testl %r10d, %r10d
movl $0xc0000000, %esi       # imm = 0xC0000000
cmovnel %esi, %ebp
cmpl %edx, %r10d
jge 0x885d
movq %r11, %r8
movq 0x10(%rsp), %r10
movzbl (%r10,%r12), %r10d
imull %r14d, %r10d
movslq %r10d, %rbx
addq 0x8(%rsp), %rbx
cmpl %r12d, %r15d
movl %r12d, %r10d
cmovgl %r15d, %r10d
movq %r15, %rsi
subl %r15d, %r10d
movslq %r10d, %r15
movslq %edx, %rdx
movl $0xc0000000, %r10d      # imm = 0xC0000000
movl (%rax,%r15,8), %r13d
movl 0x4(%rax,%r15,8), %r11d
movl %ebp, (%rax,%r15,8)
movsbl (%rbx,%r15), %ebp
addl %r13d, %ebp
cmpl %r11d, %ebp
cmovlel %r11d, %ebp
cmpl %r10d, %ebp
cmovlel %r10d, %ebp
movl %ebp, %r13d
subl %edi, %r13d
subl %ecx, %r11d
cmpl %r13d, %r11d
jg 0x8827
movl %r13d, %r11d
movl %r11d, 0x4(%rax,%r15,8)
subl %ecx, %r10d
cmpl %r13d, %r10d
cmovlel %r13d, %r10d
incq %r15
cmpq %rdx, %r15
jl 0x87f3
movq %rsi, %r15
movq %r8, %r11
movl %ebp, (%rax,%rdx,8)
movl $0xc0000000, 0x4(%rax,%rdx,8) # imm = 0xC0000000
incq %r12
cmpq %r9, %r12
jne 0x877c
jmp 0x8862
movslq %edx, %rdx
jmp 0x8844
movslq %r14d, %rcx
movl (%rax,%rcx,8), %ebx
movq %rax, %rdi
callq 0x7380
movq 0x8(%rsp), %rdi
callq 0x7380
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq