// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movl %r8d, %ebx
movq %rcx, 0x8(%rsp)
movl %edx, %ebp
movq %rsi, %r13
movl %edi, %r14d
movl 0x60(%rsp), %r15d
movl %r8d, %eax
imull %edi, %eax
movslq %eax, %rdi
callq 0x7430
movq %rax, (%rsp)
leal 0x1(%r14), %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x71a0
testl %ebx, %ebx
jle 0x86d7
movl %ebx, %ecx
movl %r14d, %edx
xorl %esi, %esi
xorl %edi, %edi
testl %r14d, %r14d
jle 0x86cf
movq %rsi, %r8
imulq %rcx, %r8
addq %r12, %r8
movslq %edi, %r9
addq (%rsp), %r9
xorl %r10d, %r10d
movzbl (%r13,%r10), %r11d
movb (%r8,%r11), %r11b
movb %r11b, (%r9,%r10)
incq %r10
cmpq %r10, %rdx
jne 0x86b6
addl %r10d, %edi
incq %rsi
cmpq %rcx, %rsi
jne 0x869d
movl 0x58(%rsp), %ecx
movl 0x50(%rsp), %ebx
movabsq $-0x4000000000000000, %rdx # imm = 0xC000000000000000
movq %rdx, (%rax)
cmpl %r15d, %r14d
movl %r15d, %esi
cmovll %r14d, %esi
testl %esi, %esi
jle 0x871b
incl %esi
leal (%rcx,%rbx), %edi
negl %edi
pushq $0x1
popq %rdx
movl %edi, (%rax,%rdx,8)
movl $0xc0000000, 0x4(%rax,%rdx,8) # imm = 0xC0000000
incq %rdx
subl %ecx, %edi
cmpq %rdx, %rsi
jne 0x8704
jmp 0x871e
pushq $0x1
popq %rdx
cmpl %r14d, %edx
jg 0x8748
movl %edx, %esi
decl %edx
leaq (%rax,%rsi,8), %rsi
addq $0x4, %rsi
movabsq $-0x3fffffff40000000, %rdi # imm = 0xC0000000C0000000
movq %rdi, -0x4(%rsi)
incl %edx
addq $0x8, %rsi
cmpl %r14d, %edx
jl 0x8739
testl %ebp, %ebp
jle 0x883c
leal 0x1(%r15), %edx
movq %rdx, 0x10(%rsp)
leal (%rcx,%rbx), %edi
movslq %r15d, %r8
movl %ebp, %r9d
xorl %r12d, %r12d
movl %r12d, %r10d
subl %r15d, %r10d
cmpq %r8, %r12
movl $0x0, %edx
cmovlel %edx, %r10d
movq 0x10(%rsp), %rdx
leal (%rdx,%r12), %r11d
cmpl %r14d, %r11d
cmovgel %r14d, %r11d
movl %r12d, %ebp
notl %ebp
imull %ecx, %ebp
movq %rbx, %rsi
subl %ebx, %ebp
testl %r10d, %r10d
movl $0xc0000000, %edx       # imm = 0xC0000000
cmovnel %edx, %ebp
movslq %r11d, %rdx
cmpl %r11d, %r10d
jge 0x881d
movq 0x8(%rsp), %r10
movzbl (%r10,%r12), %r10d
imull %r14d, %r10d
movslq %r10d, %rbx
addq (%rsp), %rbx
cmpl %r12d, %r15d
movl %r12d, %r10d
cmovgl %r15d, %r10d
subl %r15d, %r10d
movslq %r10d, %r15
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 0x8806
movl %r13d, %r11d
movl %r11d, 0x4(%rax,%r15,8)
subl %ecx, %r10d
cmpl %r13d, %r10d
cmovlel %r13d, %r10d
incq %r15
cmpq %rdx, %r15
jl 0x87d2
movl %ebp, (%rax,%rdx,8)
movl $0xc0000000, 0x4(%rax,%rdx,8) # imm = 0xC0000000
incq %r12
cmpq %r9, %r12
movl 0x60(%rsp), %r15d
movq %rsi, %rbx
jne 0x8765
movslq %r14d, %rcx
movl (%rax,%rcx,8), %ebx
movq %rax, %rdi
callq 0x7380
movq (%rsp), %rdi
callq 0x7380
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq