// File: 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:
subq $0xa8, %rsp
movl 0xc0(%rsp), %eax
movl 0xb8(%rsp), %eax
movl 0xb0(%rsp), %eax
movl %edi, 0xa4(%rsp)
movq %rsi, 0x98(%rsp)
movl %edx, 0x94(%rsp)
movq %rcx, 0x88(%rsp)
movl %r8d, 0x84(%rsp)
movq %r9, 0x78(%rsp)
movl 0xb0(%rsp), %eax
addl 0xb8(%rsp), %eax
movl %eax, 0x58(%rsp)
movl 0xa4(%rsp), %eax
imull 0x84(%rsp), %eax
movslq %eax, %rdi
callq 0x8560
movq %rax, 0x68(%rsp)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdi
movl $0x8, %esi
callq 0x8220
movq %rax, 0x70(%rsp)
movl $0x0, 0x64(%rsp)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x84(%rsp), %eax
jge 0x77d03
movq 0x78(%rsp), %rax
movl 0x5c(%rsp), %ecx
imull 0x84(%rsp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movl $0x0, 0x60(%rsp)
movl 0x60(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jge 0x77cf1
movq 0x48(%rsp), %rax
movq 0x98(%rsp), %rcx
movslq 0x60(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
movb (%rax,%rcx), %dl
movq 0x68(%rsp), %rax
movl 0x64(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x64(%rsp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x77ca6
jmp 0x77cf3
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x77c71
movq 0x70(%rsp), %rax
movl $0x0, (%rax)
movq 0x70(%rsp), %rax
movl $0xc0000000, 0x4(%rax)  # imm = 0xC0000000
movl $0x1, 0x60(%rsp)
movl 0x60(%rsp), %ecx
xorl %eax, %eax
cmpl 0xa4(%rsp), %ecx
movb %al, 0x1f(%rsp)
jg 0x77d47
movl 0x60(%rsp), %eax
cmpl 0xc0(%rsp), %eax
setle %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x77d51
jmp 0x77d96
movl 0xb0(%rsp), %eax
movl 0xb8(%rsp), %ecx
imull 0x60(%rsp), %ecx
addl %ecx, %eax
xorl %edx, %edx
subl %eax, %edx
movq 0x70(%rsp), %rax
movslq 0x60(%rsp), %rcx
movl %edx, (%rax,%rcx,8)
movq 0x70(%rsp), %rax
movslq 0x60(%rsp), %rcx
movl $0xc0000000, 0x4(%rax,%rcx,8) # imm = 0xC0000000
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x77d22
jmp 0x77d98
movl 0x60(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jg 0x77dd5
movq 0x70(%rsp), %rax
movslq 0x60(%rsp), %rcx
movl $0xc0000000, 0x4(%rax,%rcx,8) # imm = 0xC0000000
movq 0x70(%rsp), %rax
movslq 0x60(%rsp), %rcx
movl $0xc0000000, (%rax,%rcx,8) # imm = 0xC0000000
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x77d98
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x94(%rsp), %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x7805d
movl $0xc0000000, 0x44(%rsp) # imm = 0xC0000000
movq 0x68(%rsp), %rax
movq 0x88(%rsp), %rcx
movslq 0x64(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
imull 0xa4(%rsp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movl 0x64(%rsp), %eax
cmpl 0xc0(%rsp), %eax
jle 0x77e4b
movl 0x64(%rsp), %eax
subl 0xc0(%rsp), %eax
movl %eax, 0x18(%rsp)
jmp 0x77e53
xorl %eax, %eax
movl %eax, 0x18(%rsp)
jmp 0x77e53
movl 0x18(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x64(%rsp), %eax
addl 0xc0(%rsp), %eax
addl $0x1, %eax
cmpl 0xa4(%rsp), %eax
jge 0x77e86
movl 0x64(%rsp), %eax
addl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x77e91
movl 0xa4(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x38(%rsp)
cmpl $0x0, 0x3c(%rsp)
jne 0x77ec4
movl 0xb0(%rsp), %ecx
movl 0xb8(%rsp), %eax
movl 0x64(%rsp), %edx
addl $0x1, %edx
imull %edx, %eax
addl %eax, %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, 0x10(%rsp)
jmp 0x77ecf
movl $0xc0000000, %eax       # imm = 0xC0000000
movl %eax, 0x10(%rsp)
jmp 0x77ecf
movl 0x10(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0x60(%rsp)
movl 0x60(%rsp), %eax
cmpl 0x38(%rsp), %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x7802a
movq 0x70(%rsp), %rax
movslq 0x60(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x24(%rsp)
movq 0x28(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x20(%rsp)
movl 0x40(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, (%rax)
movq 0x30(%rsp), %rax
movslq 0x60(%rsp), %rcx
movsbl (%rax,%rcx), %eax
addl 0x24(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x20(%rsp), %eax
jl 0x77f5d
movl 0x24(%rsp), %eax
movl %eax, 0xc(%rsp)
jmp 0x77f65
movl 0x20(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x44(%rsp), %eax
jl 0x77f81
movl 0x24(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0x77f89
movl 0x44(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x58(%rsp), %ecx
movl 0x24(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x24(%rsp)
movl 0xb8(%rsp), %ecx
movl 0x20(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x24(%rsp), %eax
jle 0x77fcc
movl 0x20(%rsp), %eax
movl %eax, 0x4(%rsp)
jmp 0x77fd4
movl 0x24(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x4(%rax)
movl 0xb8(%rsp), %ecx
movl 0x44(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x24(%rsp), %eax
jle 0x7800c
movl 0x44(%rsp), %eax
movl %eax, (%rsp)
jmp 0x78013
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
movl (%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x77edf
movl 0x40(%rsp), %edx
movq 0x70(%rsp), %rax
movslq 0x38(%rsp), %rcx
movl %edx, (%rax,%rcx,8)
movq 0x70(%rsp), %rax
movslq 0x38(%rsp), %rcx
movl $0xc0000000, 0x4(%rax,%rcx,8) # imm = 0xC0000000
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x77ddd
movq 0x70(%rsp), %rax
movslq 0xa4(%rsp), %rcx
movl (%rax,%rcx,8), %eax
movl %eax, 0x54(%rsp)
movq 0x70(%rsp), %rdi
callq 0x8470
movq 0x68(%rsp), %rdi
callq 0x8470
movl 0x54(%rsp), %eax
addq $0xa8, %rsp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)