// File: /workspace/github_c_makefile_star2_O2/nh13[P]ksw/src/ksw2/ksw2_gg.c
// Function: ksw_gg
int ksw_gg(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, int8_t gapo, int8_t gape, int w, int *m_cigar_, int *n_cigar_, uint32_t **cigar_)
{
	eh_t *eh;
	int8_t *qp; // query profile
	int32_t i, j, k, gapoe = gapo + gape, score, n_col, *off = 0;
	uint8_t *z = 0; // backtrack matrix; in each cell: f<<4|e<<2|h; in principle, we can halve the memory, but backtrack will be more complex

	// allocate memory
	if (w < 0) w = tlen > qlen? tlen : qlen;
	n_col = qlen < 2*w+1? qlen : 2*w+1; // maximum #columns of the backtrack matrix
	qp = (int8_t*)kmalloc(km, qlen * m);
	eh = (eh_t*)kcalloc(km, qlen + 1, 8);
	if (m_cigar_ && n_cigar_ && cigar_) {
		*n_cigar_ = 0;
		z = (uint8_t*)kmalloc(km, (size_t)n_col * tlen);
		off = (int32_t*)kcalloc(km, tlen, 4);
	}

	// 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 = -gapoe - gapoe;
	for (j = 1; j <= qlen && j <= w; ++j)
		eh[j].h = -(gapoe + gape * (j - 1)), eh[j].e = -(gapoe + gapoe + gape * j);
	for (; j <= qlen; ++j) eh[j].h = eh[j].e = KSW_NEG_INF; // everything is -inf outside the band

	// DP loop
	for (i = 0; i < tlen; ++i) { // target sequence is in the outer loop
		int32_t f = KSW_NEG_INF, h1, st, en;
		int8_t *q = &qp[target[i] * qlen];
		st = i > w? i - w : 0;
		en = i + w + 1 < qlen? i + w + 1 : qlen;
		h1 = st > 0? KSW_NEG_INF : -(gapoe + gape * i);
		f  = st > 0? KSW_NEG_INF : -(gapoe + gapoe + gape * i);
		if (m_cigar_ && n_cigar_ && cigar_) {
			uint8_t *zi = &z[(long)i * n_col];
			off[i] = st;
			for (j = st; j < en; ++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)
				// 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];
				int32_t h = p->h, e = p->e;
				uint8_t d; // direction
				p->h = h1;
				h += q[j];
				d = h >= e? 0 : 1;
				h = h >= e? h : e;
				d = h >= f? d : 2;
				h = h >= f? h : f;
				h1 = h;
				h -= gapoe;
				e -= gape;
				d |= e > h? 0x08 : 0;
				e  = e > h? e    : h;
				p->e = e;
				f -= gape;
				d |= f > h? 0x10 : 0; // if we want to halve the memory, use one bit only, instead of two
				f  = f > h? f    : h;
				zi[j - st] = d; // z[i,j] keeps h for the current cell and e/f for the next cell
			}
		} else {
			for (j = st; j < en; ++j) {
				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[en].h = h1, eh[en].e = KSW_NEG_INF;
	}

	// backtrack
	score = eh[qlen].h;
	kfree(km, qp); kfree(km, eh);
	if (m_cigar_ && n_cigar_ && cigar_) {
		ksw_backtrack(km, 0, 0, 0, z, off, 0, n_col, tlen-1, qlen-1, m_cigar_, n_cigar_, cigar_);
		kfree(km, z);
		kfree(km, off);
	}
	return score;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, 0x90(%rsp)
movq %rdx, %r13
movl %esi, %r15d
movq %rdi, %r12
movq 0xf8(%rsp), %rbx
movl 0xe8(%rsp), %eax
movl %ecx, %edx
movl %ecx, 0x2c(%rsp)
cmpl %esi, %ecx
movl %esi, %ecx
cmovgl %edx, %ecx
movsbl 0xd8(%rsp), %edx
movq %rdx, 0x18(%rsp)
testl %eax, %eax
cmovnsl %eax, %ecx
movq %rcx, 0x30(%rsp)
leal 0x1(,%rcx,2), %eax
cmpl %esi, %eax
cmovgel %esi, %eax
movl %eax, 0x20(%rsp)
movsbl 0xe0(%rsp), %ebp
movq %r9, 0x48(%rsp)
movl %r9d, %eax
imull %esi, %eax
movslq %eax, %rsi
callq 0x19e512
movq %rax, %r14
movq %r15, 0x50(%rsp)
leal 0x1(%r15), %eax
movslq %eax, %rsi
pushq $0x8
popq %rdx
movq %r12, %rdi
callq 0x19e66e
movq %rbx, %rdx
movq %rax, %rbx
cmpq $0x0, 0xf0(%rsp)
setne %al
testq %rdx, %rdx
setne %cl
andb %al, %cl
cmpq $0x0, 0x100(%rsp)
setne %al
andb %cl, %al
movl 0x2c(%rsp), %ecx
xorl %r15d, %r15d
movb %al, 0x14(%rsp)
cmpb $0x1, %al
movq %r12, 0x60(%rsp)
jne 0x1a4aba
andl $0x0, (%rdx)
movslq 0x20(%rsp), %rsi
movslq %ecx, %rax
movq %rax, 0x20(%rsp)
imulq %rax, %rsi
movq %r12, %rdi
callq 0x19e512
movq %rax, 0x40(%rsp)
pushq $0x4
popq %rdx
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x19e66e
movq %rax, 0x20(%rsp)
jmp 0x1a4acc
movq $0x0, 0x40(%rsp)
movq $0x0, 0x20(%rsp)
movq 0xd0(%rsp), %rcx
movq 0x18(%rsp), %rax
addl %ebp, %eax
movq 0x30(%rsp), %rdx
addl %edx, %edx
movl %edx, 0x3c(%rsp)
movq 0x50(%rsp), %r11
testl %r11d, %r11d
movl $0x0, %edx
cmovgl %r11d, %edx
movq 0x48(%rsp), %r12
movsbq %r12b, %rsi
testl %r12d, %r12d
cmovlel %r15d, %r12d
xorl %edi, %edi
cmpq %r12, %r15
je 0x1a4b3e
movq %r15, %r8
imulq %rsi, %r8
addq %rcx, %r8
movslq %edi, %rdi
xorl %r9d, %r9d
cmpq %r9, %rdx
je 0x1a4b39
movzbl (%r13,%r9), %r10d
movb (%r8,%r10), %r10b
movb %r10b, (%r14,%rdi)
incq %rdi
incq %r9
jmp 0x1a4b1e
incq %r15
jmp 0x1a4b09
andl $0x0, (%rbx)
leal (%rax,%rax), %r12d
negl %r12d
movq 0x30(%rsp), %r10
cmpl %r10d, %r11d
movl %r10d, %edx
cmovll %r11d, %edx
movl %r12d, 0x4(%rbx)
movl %ebp, %ecx
movq %rcx, 0x88(%rsp)
xorl %esi, %esi
testl %edx, %edx
cmovlel %esi, %edx
leal 0x1(%rdx), %ecx
leal (%rbp,%rbp,2), %edi
movq 0x18(%rsp), %r8
leal (%rdi,%r8,2), %edi
negl %edi
movl %eax, %r8d
negl %r8d
movq 0x60(%rsp), %r15
cmpq %rsi, %rdx
je 0x1a4ba1
movl %r8d, 0x8(%rbx,%rsi,8)
movl %edi, 0xc(%rbx,%rsi,8)
subl %ebp, %edi
subl %ebp, %r8d
incq %rsi
jmp 0x1a4b89
movslq %r11d, %rsi
movabsq $-0x3fffffff40000000, %rdx # imm = 0xC0000000C0000000
cmpq %rsi, %rcx
jg 0x1a4bbc
movq %rdx, (%rbx,%rcx,8)
incq %rcx
jmp 0x1a4bae
leal 0x1(%r10), %ecx
movq %rcx, 0x80(%rsp)
movslq %r10d, %rcx
movq %rcx, 0x78(%rsp)
movl 0x2c(%rsp), %ecx
testl %ecx, %ecx
movl $0x0, %edx
cmovgl %ecx, %edx
movq %rdx, 0x48(%rsp)
movslq 0x3c(%rsp), %rdx
incq %rdx
cmpq %rsi, %rdx
movq %rsi, 0x68(%rsp)
cmovgeq %rsi, %rdx
movq %rdx, 0x70(%rsp)
movq 0x40(%rsp), %rcx
movq %rcx, 0x18(%rsp)
xorl %edx, %edx
movl %r12d, 0x5c(%rsp)
cmpl %edx, %r10d
movl %edx, %esi
cmovgl %r10d, %esi
cmpq 0x48(%rsp), %rdx
je 0x1a4dae
subl %r10d, %esi
movq 0x90(%rsp), %rcx
movzbl (%rcx,%rdx), %ecx
imull %r11d, %ecx
movslq %ecx, %rcx
addq %r14, %rcx
movl %edx, %r9d
subl %r10d, %r9d
cmpq 0x78(%rsp), %rdx
movl $0x0, %edi
cmovlel %edi, %r9d
movq %r11, %rdi
movq 0x80(%rsp), %r8
leal (%r8,%rdx), %r11d
cmpl %edi, %r11d
cmovgel %edi, %r11d
movl %edx, %edi
imull 0x88(%rsp), %edi
movl %r12d, %r8d
subl %edi, %r8d
leal (%rax,%rdi), %r10d
negl %r10d
testl %r9d, %r9d
movl $0xc0000000, %edi       # imm = 0xC0000000
cmovgl %edi, %r10d
movslq %esi, %r13
cmovgl %edi, %r8d
cmpb $0x0, 0x14(%rsp)
je 0x1a4d27
movq 0x20(%rsp), %rsi
movl %r9d, (%rsi,%rdx,4)
movslq %r11d, %rsi
movslq %r9d, %r11
movq 0x18(%rsp), %r9
subq %r11, %r9
cmpq %rsi, %r13
jge 0x1a4d77
movl (%rbx,%r13,8), %r11d
movl 0x4(%rbx,%r13,8), %r12d
movl %r10d, (%rbx,%r13,8)
movsbl (%rcx,%r13), %r10d
addl %r11d, %r10d
xorl %r11d, %r11d
cmpl %r12d, %r10d
setl %r11b
cmovlel %r12d, %r10d
cmpl %r8d, %r10d
cmovlel %r8d, %r10d
pushq $0x2
popq %rdi
cmovll %edi, %r11d
movl %r10d, %edi
subl %eax, %edi
subl %ebp, %r12d
cmpl %edi, %r12d
setg %r15b
cmovlel %edi, %r12d
shlb $0x3, %r15b
orb %r11b, %r15b
movl %r12d, 0x4(%rbx,%r13,8)
subl %ebp, %r8d
cmpl %edi, %r8d
setg %r11b
cmovlel %edi, %r8d
shlb $0x4, %r11b
orb %r15b, %r11b
movb %r11b, (%r9,%r13)
incq %r13
jmp 0x1a4cb0
movslq %r11d, %rsi
cmpq %rsi, %r13
jge 0x1a4d81
movl (%rbx,%r13,8), %edi
movl 0x4(%rbx,%r13,8), %r9d
movl %r10d, (%rbx,%r13,8)
movsbl (%rcx,%r13), %r10d
addl %edi, %r10d
cmpl %r9d, %r10d
cmovlel %r9d, %r10d
cmpl %r8d, %r10d
cmovlel %r8d, %r10d
movl %r10d, %r11d
subl %eax, %r11d
subl %ebp, %r9d
cmpl %r11d, %r9d
jg 0x1a4d63
movl %r11d, %r9d
movl %r9d, 0x4(%rbx,%r13,8)
subl %ebp, %r8d
cmpl %r11d, %r8d
cmovlel %r11d, %r8d
incq %r13
jmp 0x1a4d2a
movq 0x60(%rsp), %r15
movl 0x5c(%rsp), %r12d
movl %r10d, (%rbx,%rsi,8)
movl $0xc0000000, 0x4(%rbx,%rsi,8) # imm = 0xC0000000
incq %rdx
movq 0x18(%rsp), %rcx
addq 0x70(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movq 0x50(%rsp), %r11
movq 0x30(%rsp), %r10
jmp 0x1a4c0d
movq 0x68(%rsp), %rax
movl (%rbx,%rax,8), %ebp
movq %r15, %rdi
movq %r14, %rsi
callq 0x19e38c
movq %r15, %rdi
movq %rbx, %rsi
callq 0x19e38c
cmpb $0x0, 0x14(%rsp)
je 0x1a4fe0
movl %ebp, 0x14(%rsp)
leaq 0xc(%rsp), %rax
andl $0x0, (%rax)
movl 0x2c(%rsp), %r9d
decl %r9d
movq 0xf0(%rsp), %rax
movl (%rax), %eax
leaq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movq 0x100(%rsp), %rax
movq (%rax), %rax
movq 0x50(%rsp), %r10
leal -0x1(%r10), %ecx
movl 0x3c(%rsp), %r13d
incl %r13d
cmpl %r13d, %r10d
cmovll %r10d, %r13d
xorl %ebx, %ebx
leal -0x1(%r10), %edx
movl %edx, 0x30(%rsp)
movl %r13d, %r12d
imull %r9d, %r12d
movl %ecx, 0x18(%rsp)
addl %ecx, %r12d
testl %r9d, %r9d
js 0x1a4f21
testl %r10d, %r10d
jle 0x1a4f21
movl %r9d, %ebp
movl %r9d, %ecx
movq 0x20(%rsp), %rdx
movl (%rdx,%rcx,4), %edx
xorl %esi, %esi
cmpl %edx, %r10d
jle 0x1a4e6f
movl %r12d, %ecx
subl %edx, %ecx
movslq %ecx, %rcx
movq 0x40(%rsp), %rsi
movzbl (%rsi,%rcx), %esi
testl %ebx, %ebx
je 0x1a4e8a
movl %esi, %edi
movl %ebx, %ecx
shrl %cl, %edi
shll $0x1d, %edi
sarl $0x1f, %edi
andl %ebx, %edi
andl $0x7, %esi
movl %esi, %ebx
movl %edi, %esi
jmp 0x1a4e8f
andl $0x7, %esi
movl %esi, %ebx
testl %esi, %esi
cmovnel %esi, %ebx
cmpl %edx, %r10d
pushq $0x2
popq %rcx
cmovlel %ecx, %ebx
cmpl $0x1, %ebx
je 0x1a4ea7
cmpl $0x3, %ebx
jne 0x1a4ed8
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rax, %rcx
pushq $0x2
popq %r8
pushq $0x1
popq %r9
movq %r10, %r14
callq 0x1a4ff4
movq %r14, %r10
movl %ebp, %r9d
decl %r9d
subl %r13d, %r12d
jmp 0x1a4e37
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rax, %rcx
testl %ebx, %ebx
jne 0x1a4f00
xorl %r8d, %r8d
pushq $0x1
popq %r9
callq 0x1a4ff4
movl %ebp, %r9d
decl %r9d
jmp 0x1a4f0f
pushq $0x1
popq %r8
movl %r8d, %r9d
callq 0x1a4ff4
movl %ebp, %r9d
movl 0x18(%rsp), %ecx
decl %ecx
movl 0x30(%rsp), %edx
movl %edx, %r10d
jmp 0x1a4e21
testl %r9d, %r9d
js 0x1a4f48
incl %r9d
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
pushq $0x2
popq %r8
movq %r15, %rdi
movq %rax, %rcx
movq %r10, %rbx
callq 0x1a4ff4
movq %rbx, %r10
movl 0x14(%rsp), %ebp
testl %r10d, %r10d
jle 0x1a4f6d
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
pushq $0x1
popq %r8
movq %r15, %rdi
movq %rax, %rcx
movl %r10d, %r9d
callq 0x1a4ff4
pushq $-0x1
popq %rcx
xorl %edx, %edx
movl 0xc(%rsp), %esi
movl %esi, %edi
sarl %edi
movslq %edi, %rdi
cmpq %rdi, %rdx
jge 0x1a4fa3
movl (%rax,%rdx,4), %edi
addl %ecx, %esi
movslq %esi, %rsi
movl (%rax,%rsi,4), %esi
movl %esi, (%rax,%rdx,4)
movl 0xc(%rsp), %esi
addl %ecx, %esi
movslq %esi, %rsi
movl %edi, (%rax,%rsi,4)
incq %rdx
decl %ecx
jmp 0x1a4f72
movl 0x10(%rsp), %ecx
movq 0xf0(%rsp), %rdx
movl %ecx, (%rdx)
movq 0xf8(%rsp), %rcx
movl %esi, (%rcx)
movq 0x100(%rsp), %rcx
movq %rax, (%rcx)
movq %r15, %rdi
movq 0x40(%rsp), %rsi
callq 0x19e38c
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0x19e38c
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq