// File: /workspace/github_c_makefile_star2_O3/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 $0x88, %rsp
movq %r8, 0x80(%rsp)
movl %ecx, %r15d
movq %rdx, %r12
movl %esi, %ebx
movq %rdi, %r14
movl 0xd8(%rsp), %eax
cmpl %esi, %ecx
movl %esi, %ecx
cmovgl %r15d, %ecx
movsbl 0xc8(%rsp), %edx
movq %rdx, 0x48(%rsp)
testl %eax, %eax
cmovnsl %eax, %ecx
movq %rcx, 0x50(%rsp)
leal 0x1(,%rcx,2), %r13d
cmpl %esi, %r13d
cmovgel %esi, %r13d
movsbl 0xd0(%rsp), %ebp
movl %r9d, 0x24(%rsp)
movl %r9d, %eax
imull %esi, %eax
movslq %eax, %rsi
callq 0x1fbb34
movq %rax, 0x18(%rsp)
movq %rbx, 0x38(%rsp)
leal 0x1(%rbx), %eax
movslq %eax, %rsi
movl $0x8, %edx
movq %r14, %rdi
callq 0x1fbc89
movq 0xe8(%rsp), %rdx
movq %rax, %rbx
cmpq $0x0, 0xe0(%rsp)
setne %al
testq %rdx, %rdx
setne %cl
andb %al, %cl
cmpq $0x0, 0xf0(%rsp)
setne %al
andb %cl, %al
movb %al, 0xb(%rsp)
cmpb $0x1, %al
movq %r14, 0x10(%rsp)
movl %r13d, 0xc(%rsp)
movq %r15, 0x40(%rsp)
jne 0x202b59
movl $0x0, (%rdx)
movslq %r13d, %rsi
movslq %r15d, %r15
imulq %r15, %rsi
movq %r14, %rdi
callq 0x1fbb34
movq %rax, 0x28(%rsp)
movl $0x4, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1fbc89
movq %rax, 0x30(%rsp)
jmp 0x202b6b
movq $0x0, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x48(%rsp), %r15
leal (%r15,%rbp), %eax
movl 0x24(%rsp), %edx
testb %dl, %dl
movq 0x38(%rsp), %r10
jle 0x202bd2
movq 0xc0(%rsp), %rcx
movl %edx, %edx
movl %r10d, %esi
xorl %edi, %edi
xorl %r8d, %r8d
testl %r10d, %r10d
jle 0x202bca
movq %rdi, %r9
imulq %rdx, %r9
addq %rcx, %r9
movslq %r8d, %r10
addq 0x18(%rsp), %r10
xorl %r11d, %r11d
movzbl (%r12,%r11), %r14d
movb (%r9,%r14), %r14b
movb %r14b, (%r10,%r11)
incq %r11
cmpq %r11, %rsi
jne 0x202bad
addl %r11d, %r8d
movq 0x38(%rsp), %r10
incq %rdi
cmpq %rdx, %rdi
jne 0x202b93
movl $0x0, (%rbx)
leal (%rax,%rax), %ecx
negl %ecx
movl %ecx, 0x24(%rsp)
movl %ecx, 0x4(%rbx)
movq 0x50(%rsp), %rcx
cmpl %ecx, %r10d
cmovll %r10d, %ecx
testl %ecx, %ecx
jle 0x202c25
leal 0x1(%rcx), %edx
movl %ecx, %esi
leal (%rbp,%rbp,2), %edi
leal (%rdi,%r15,2), %edi
negl %edi
movl %eax, %r8d
negl %r8d
xorl %r9d, %r9d
movl %r8d, 0x8(%rbx,%r9,8)
movl %edi, 0xc(%rbx,%r9,8)
subl %ebp, %edi
subl %ebp, %r8d
incq %r9
cmpq %r9, %rsi
jne 0x202c0c
jmp 0x202c2a
movl $0x1, %edx
cmpl %r10d, %edx
jg 0x202c57
xorl %edx, %edx
testl %ecx, %ecx
cmovgl %ecx, %edx
leaq (%rbx,%rdx,8), %rcx
addq $0xc, %rcx
movabsq $-0x3fffffff40000000, %rsi # imm = 0xC0000000C0000000
movq %rsi, -0x4(%rcx)
incl %edx
addq $0x8, %rcx
cmpl %r10d, %edx
jl 0x202c48
movslq %r10d, %rcx
movq %rcx, 0x58(%rsp)
cmpl $0x0, 0x40(%rsp)
jle 0x202e33
movq 0x50(%rsp), %rdx
leal (%rdx,%rdx), %ecx
leal 0x1(%rdx), %esi
movq %rsi, 0x48(%rsp)
movl %edx, %edx
movq %rdx, 0x78(%rsp)
movl %ebp, %edx
movq %rdx, 0x70(%rsp)
movl 0x40(%rsp), %edx
movq %rdx, 0x68(%rsp)
movslq %ecx, %rdx
incq %rdx
movq 0x58(%rsp), %rcx
cmpq %rcx, %rdx
cmovgeq %rcx, %rdx
movq %rdx, 0x60(%rsp)
movq 0x28(%rsp), %r12
xorl %ecx, %ecx
movq 0x50(%rsp), %rdx
movl %edx, %r11d
subl %ecx, %r11d
movl %ecx, %esi
cmovgl %edx, %esi
negl %r11d
subl %edx, %esi
movslq %esi, %r14
movq 0x80(%rsp), %rdx
movzbl (%rdx,%rcx), %esi
movq 0x38(%rsp), %rdx
imull %edx, %esi
movslq %esi, %rsi
addq 0x18(%rsp), %rsi
cmpq 0x78(%rsp), %rcx
movl $0x0, %edi
cmovbel %edi, %r11d
movq 0x48(%rsp), %rdi
leal (%rdi,%rcx), %r10d
cmpl %edx, %r10d
cmovgel %edx, %r10d
movl %ecx, %r8d
imull 0x70(%rsp), %r8d
leal (%rax,%r8), %edi
negl %edi
movl 0x24(%rsp), %r9d
subl %r8d, %r9d
testl %r11d, %r11d
movl $0xc0000000, %edx       # imm = 0xC0000000
cmovgl %edx, %edi
cmovgl %edx, %r9d
movslq %r10d, %r8
cmpb $0x0, 0xb(%rsp)
je 0x202dc4
movq 0x30(%rsp), %rdx
movl %r11d, (%rdx,%rcx,4)
cmpl %r10d, %r11d
jge 0x202e13
movslq %r11d, %r10
movq %r12, %r11
subq %r10, %r11
movl (%rbx,%r14,8), %r10d
movl 0x4(%rbx,%r14,8), %r15d
movl %edi, (%rbx,%r14,8)
movsbl (%rsi,%r14), %edi
addl %r10d, %edi
xorl %r10d, %r10d
cmpl %r15d, %edi
setl %r10b
cmovlel %r15d, %edi
cmpl %r9d, %edi
cmovlel %r9d, %edi
movl $0x2, %edx
cmovll %edx, %r10d
movl %edi, %r13d
subl %eax, %r13d
subl %ebp, %r15d
cmpl %r13d, %r15d
setg %dl
cmovlel %r13d, %r15d
shlb $0x3, %dl
orb %r10b, %dl
movl %r15d, 0x4(%rbx,%r14,8)
subl %ebp, %r9d
cmpl %r13d, %r9d
setg %r10b
cmovlel %r13d, %r9d
shlb $0x4, %r10b
orb %dl, %r10b
movb %r10b, (%r11,%r14)
incq %r14
cmpq %r8, %r14
jl 0x202d50
jmp 0x202e13
cmpl %r10d, %r11d
jge 0x202e13
movl (%rbx,%r14,8), %edx
movl 0x4(%rbx,%r14,8), %r10d
movl %edi, (%rbx,%r14,8)
movsbl (%rsi,%r14), %edi
addl %edx, %edi
cmpl %r10d, %edi
cmovlel %r10d, %edi
cmpl %r9d, %edi
cmovlel %r9d, %edi
movl %edi, %r11d
subl %eax, %r11d
subl %ebp, %r10d
cmpl %r11d, %r10d
jg 0x202dfc
movl %r11d, %r10d
movl %r10d, 0x4(%rbx,%r14,8)
subl %ebp, %r9d
cmpl %r11d, %r9d
cmovlel %r11d, %r9d
incq %r14
cmpq %r8, %r14
jl 0x202dc9
movl %edi, (%rbx,%r8,8)
movl $0xc0000000, 0x4(%rbx,%r8,8) # imm = 0xC0000000
incq %rcx
addq 0x60(%rsp), %r12
cmpq 0x68(%rsp), %rcx
jne 0x202caf
movq 0x58(%rsp), %rax
movl (%rbx,%rax,8), %r13d
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x1fb9e0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1fb9e0
cmpb $0x0, 0xb(%rsp)
je 0x2031fc
movl %r13d, 0x18(%rsp)
movq 0x40(%rsp), %rsi
leal -0x1(%rsi), %ebp
movq 0x38(%rsp), %rdx
leal -0x1(%rdx), %r12d
movq 0xe0(%rsp), %rax
movl (%rax), %r14d
movq 0xf0(%rsp), %rax
movq (%rax), %rax
movl %ebp, %ecx
orl %r12d, %ecx
js 0x2030a6
xorl %ebx, %ebx
movl $0x2, %r9d
xorl %r15d, %r15d
movl 0xc(%rsp), %r8d
movl %ebp, %ecx
movq 0x30(%rsp), %rdx
movl (%rdx,%rcx,4), %edx
movl $0x0, %esi
cmpl %edx, %r12d
jl 0x202ed6
movl %ebp, %ecx
imull %r8d, %ecx
addl %r12d, %ecx
subl %edx, %ecx
movslq %ecx, %rcx
movq 0x28(%rsp), %rsi
movzbl (%rsi,%rcx), %esi
testl %ebx, %ebx
je 0x202ef1
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 0x202ef6
andl $0x7, %esi
movl %esi, %ebx
testl %esi, %esi
cmovnel %esi, %ebx
cmpl %edx, %r12d
cmovll %r9d, %ebx
cmpl $0x3, %ebx
je 0x202f72
cmpl $0x1, %ebx
je 0x202f72
testl %ebx, %ebx
jne 0x202fd9
testl %r15d, %r15d
je 0x202f29
movslq %r15d, %rcx
movl -0x4(%rax,%rcx,4), %edx
testb $0xf, %dl
je 0x20303e
cmpl %r14d, %r15d
jne 0x202f60
addl %r14d, %r14d
testl %r15d, %r15d
movl $0x4, %ecx
cmovel %ecx, %r14d
leal (,%r14,4), %ecx
movslq %ecx, %rdx
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x1fbaa1
movl $0x2, %r9d
movl 0xc(%rsp), %r8d
movslq %r15d, %rcx
incl %r15d
movl $0x10, (%rax,%rcx,4)
jmp 0x203045
testl %r15d, %r15d
je 0x202f91
movslq %r15d, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x2, %esi
jne 0x202f91
addl $0x10, %edx
movl %edx, -0x4(%rax,%rcx,4)
jmp 0x202fd5
cmpl %r14d, %r15d
jne 0x202fc8
addl %r14d, %r14d
testl %r15d, %r15d
movl $0x4, %ecx
cmovel %ecx, %r14d
leal (,%r14,4), %ecx
movslq %ecx, %rdx
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x1fbaa1
movl $0x2, %r9d
movl 0xc(%rsp), %r8d
movslq %r15d, %rcx
incl %r15d
movl $0x12, (%rax,%rcx,4)
decl %ebp
jmp 0x20304a
movslq %r15d, %r13
testl %r15d, %r15d
je 0x202ffa
movl -0x4(%rax,%r13,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x1, %edx
jne 0x202ffa
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%r13,4)
jmp 0x203047
cmpl %r14d, %r15d
jne 0x203031
addl %r14d, %r14d
testl %r15d, %r15d
movl $0x4, %ecx
cmovel %ecx, %r14d
leal (,%r14,4), %ecx
movslq %ecx, %rdx
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x1fbaa1
movl $0x2, %r9d
movl 0xc(%rsp), %r8d
incl %r15d
movl $0x11, (%rax,%r13,4)
jmp 0x203047
addl $0x10, %edx
movl %edx, -0x4(%rax,%rcx,4)
decl %ebp
decl %r12d
testl %r12d, %r12d
setns %r13b
testl %ebp, %ebp
js 0x20305e
testl %r12d, %r12d
jns 0x202eab
testl %ebp, %ebp
js 0x2030b5
incl %ebp
testl %r15d, %r15d
je 0x2030c7
movslq %r15d, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x2, %esi
jne 0x2030ce
shll $0x4, %ebp
addl %ebp, %edx
movl %edx, -0x4(%rax,%rcx,4)
testl %r12d, %r12d
js 0x203211
incl %r12d
movq 0x10(%rsp), %rbx
movq 0xe8(%rsp), %rbp
movl 0x18(%rsp), %r13d
jmp 0x203140
testl %edx, %edx
setg %r13b
xorl %r15d, %r15d
movb $0x1, %cl
testl %esi, %esi
jg 0x2030d2
testb %r13b, %r13b
movq 0x10(%rsp), %rbx
movq 0xe8(%rsp), %rbp
jmp 0x20312a
xorl %r15d, %r15d
movb $0x1, %cl
jmp 0x2030d0
xorl %ecx, %ecx
movl %ebp, %esi
cmpl %r14d, %r15d
movq 0x10(%rsp), %rbx
movq 0xe8(%rsp), %rbp
jne 0x203118
leal (%r15,%r15), %edx
testb %cl, %cl
movl $0x4, %r14d
cmovel %edx, %r14d
leal (,%r14,4), %ecx
movslq %ecx, %rdx
movq %rbx, %rdi
movq %rsi, %rbp
movq %rax, %rsi
callq 0x1fbaa1
movq %rbp, %rsi
movq 0xe8(%rsp), %rbp
shll $0x4, %esi
orl $0x2, %esi
movslq %r15d, %rcx
incl %r15d
movl %esi, (%rax,%rcx,4)
testb %r13b, %r13b
movl 0x18(%rsp), %r13d
je 0x20319c
incl %r12d
testl %r15d, %r15d
je 0x20315b
movslq %r15d, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x1, %esi
jne 0x203157
shll $0x4, %r12d
addl %r12d, %edx
movl %edx, -0x4(%rax,%rcx,4)
jmp 0x20319c
xorl %ecx, %ecx
jmp 0x203160
xorl %r15d, %r15d
movb $0x1, %cl
cmpl %r14d, %r15d
jne 0x20318b
leal (%r15,%r15), %edx
testb %cl, %cl
movl $0x4, %r14d
cmovel %edx, %r14d
leal (,%r14,4), %ecx
movslq %ecx, %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1fbaa1
shll $0x4, %r12d
incl %r12d
movslq %r15d, %rcx
incl %r15d
movl %r12d, (%rax,%rcx,4)
movl %r15d, %ecx
sarl %ecx
testl %ecx, %ecx
jle 0x2031c8
movl %ecx, %ecx
leal -0x1(%r15), %edx
xorl %esi, %esi
movl (%rax,%rsi,4), %edi
movslq %edx, %rdx
movl (%rax,%rdx,4), %r8d
movl %r8d, (%rax,%rsi,4)
movl %edi, (%rax,%rdx,4)
incq %rsi
decl %edx
cmpq %rsi, %rcx
jne 0x2031ad
movq 0xe0(%rsp), %rcx
movl %r14d, (%rcx)
movl %r15d, (%rbp)
movq 0xf0(%rsp), %rcx
movq %rax, (%rcx)
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
callq 0x1fb9e0
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
callq 0x1fb9e0
movl %r13d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rbx
movq 0xe8(%rsp), %rbp
movl 0x18(%rsp), %r13d
jmp 0x20319c