// File: /workspace/github_cpp_makefile_star2_O2/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 $0x18, %rsp
movq %r9, %r13
movl %r8d, %r12d
movq %rcx, 0x10(%rsp)
movl %edx, %r14d
movq %rsi, %rbp
movl %edi, %r15d
movl 0x58(%rsp), %ebx
movl %r8d, %eax
imull %edi, %eax
movslq %eax, %rdi
callq 0x7430
movq %rax, (%rsp)
leal 0x1(%r15), %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x71b0
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
movslq %r12d, %rsi
testl %esi, %esi
movl $0x0, %edi
cmovgl %esi, %edi
xorl %r8d, %r8d
movq (%rsp), %r12
cmpq %rdi, %rcx
je 0x31799
movq %rcx, %r9
imulq %rsi, %r9
addq %r13, %r9
movslq %r8d, %r8
xorl %r10d, %r10d
cmpq %r10, %rdx
je 0x31794
movzbl (%rbp,%r10), %r11d
movb (%r9,%r11), %r11b
movb %r11b, (%r12,%r8)
incq %r8
incq %r10
jmp 0x31779
incq %rcx
jmp 0x31764
movl $0xc0000000, 0x4(%rax)  # imm = 0xC0000000
movl 0x60(%rsp), %esi
cmpl %esi, %r15d
cmovll %r15d, %esi
xorl %edi, %edi
testl %esi, %esi
cmovlel %edi, %esi
leal 0x1(%rsi), %edx
movl 0x50(%rsp), %ecx
addl %ebx, %ecx
movl %ecx, %r8d
negl %r8d
cmpq %rdi, %rsi
je 0x317db
movl %r8d, 0x8(%rax,%rdi,8)
movl $0xc0000000, 0xc(%rax,%rdi,8) # imm = 0xC0000000
subl %ebx, %r8d
incq %rdi
jmp 0x317c1
movslq %r15d, %rdi
movabsq $-0x3fffffff40000000, %rsi # imm = 0xC0000000C0000000
cmpq %rdi, %rdx
jg 0x317f6
movq %rsi, (%rax,%rdx,8)
incq %rdx
jmp 0x317e8
movq %rdi, 0x8(%rsp)
movl 0x60(%rsp), %edx
leal 0x1(%rdx), %esi
movslq %edx, %rdi
xorl %r8d, %r8d
movq %r14, %r13
testl %r13d, %r13d
cmovlel %r8d, %r13d
cmpq %r13, %r8
je 0x318df
movl 0x60(%rsp), %edx
movq %rdx, %r11
cmpl %r8d, %r11d
movl %r8d, %edx
cmovgl %r11d, %edx
subl %r11d, %edx
movslq %edx, %r9
movq 0x10(%rsp), %rdx
movzbl (%rdx,%r8), %edx
imull %r15d, %edx
movslq %edx, %r10
addq (%rsp), %r10
leal (%rsi,%r8), %edx
cmpl %r15d, %edx
cmovgel %r15d, %edx
movl %r8d, %ebp
notl %ebp
imull %ebx, %ebp
movl 0x50(%rsp), %r14d
subl %r14d, %ebp
cmpl %r11d, %r8d
movl $0xc0000000, %r11d      # imm = 0xC0000000
movl $0xc0000000, %r12d      # imm = 0xC0000000
cmovel %ebp, %r12d
cmpq %rdi, %r8
cmovlel %ebp, %r12d
movslq %edx, %rdx
cmpq %rdx, %r9
jge 0x318cb
movl (%rax,%r9,8), %ebp
movl 0x4(%rax,%r9,8), %r14d
movl %r12d, (%rax,%r9,8)
movsbl (%r10,%r9), %r12d
addl %ebp, %r12d
cmpl %r14d, %r12d
cmovlel %r14d, %r12d
cmpl %r11d, %r12d
cmovlel %r11d, %r12d
movl %r12d, %ebp
subl %ecx, %ebp
subl %ebx, %r14d
cmpl %ebp, %r14d
jg 0x318b7
movl %ebp, %r14d
movl %r14d, 0x4(%rax,%r9,8)
subl %ebx, %r11d
cmpl %ebp, %r11d
cmovlel %ebp, %r11d
incq %r9
jmp 0x3187f
movl %r12d, (%rax,%rdx,8)
movl $0xc0000000, 0x4(%rax,%rdx,8) # imm = 0xC0000000
incq %r8
jmp 0x31812
movq 0x8(%rsp), %rcx
movl (%rax,%rcx,8), %ebx
movq %rax, %rdi
callq 0x7360
movq (%rsp), %rdi
callq 0x7360
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)