// File: /workspace/github_c_makefile_star2_O1/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, %r14d
movq %rdx, %r13
movl %esi, %r12d
movq %rdi, %rbx
movl 0xd8(%rsp), %eax
cmpl %esi, %ecx
movl %esi, %ecx
cmovgl %r14d, %ecx
movsbl 0xc8(%rsp), %edx
movq %rdx, 0x48(%rsp)
testl %eax, %eax
cmovnsl %eax, %ecx
movq %rcx, 0x50(%rsp)
leal 0x1(,%rcx,2), %r15d
cmpl %esi, %r15d
cmovgel %esi, %r15d
movsbl 0xd0(%rsp), %ebp
movl %r9d, 0x14(%rsp)
movl %r9d, %eax
imull %esi, %eax
movslq %eax, %rsi
callq 0x1fbb36
movq %rax, 0x30(%rsp)
movq %r12, 0x18(%rsp)
leal 0x1(%r12), %eax
movq %rbx, %r12
movslq %eax, %rsi
movl $0x8, %edx
movq %rbx, %rdi
callq 0x1fbc8d
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, 0xf(%rsp)
cmpb $0x1, %al
movq %r12, 0x28(%rsp)
movl %r15d, 0x10(%rsp)
movq %r14, 0x20(%rsp)
jne 0x202a1b
movl $0x0, (%rdx)
movslq %r15d, %rsi
movslq %r14d, %r14
imulq %r14, %rsi
movq %r12, %rdi
callq 0x1fbb36
movq %rax, 0x38(%rsp)
movl $0x4, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x1fbc8d
movq %rax, 0x40(%rsp)
jmp 0x202a2d
movq $0x0, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x48(%rsp), %r15
leal (%r15,%rbp), %eax
movl 0x14(%rsp), %edx
testb %dl, %dl
movq 0x18(%rsp), %r9
jle 0x202a95
movq 0xc0(%rsp), %rcx
movl %edx, %edx
movl %r9d, %esi
xorl %edi, %edi
xorl %r8d, %r8d
testl %r9d, %r9d
jle 0x202a8d
movq %rdi, %r9
imulq %rdx, %r9
addq %rcx, %r9
movslq %r8d, %r10
addq 0x30(%rsp), %r10
xorl %r11d, %r11d
movzbl (%r13,%r11), %r14d
movb (%r9,%r14), %r14b
movb %r14b, (%r10,%r11)
incq %r11
cmpq %r11, %rsi
jne 0x202a6f
addl %r11d, %r8d
movq 0x18(%rsp), %r9
incq %rdi
cmpq %rdx, %rdi
jne 0x202a55
movl $0x0, (%rbx)
leal (%rax,%rax), %ecx
negl %ecx
movl %ecx, 0x14(%rsp)
movl %ecx, 0x4(%rbx)
movq 0x50(%rsp), %rdx
cmpl %edx, %r9d
cmovll %r9d, %edx
testl %edx, %edx
jle 0x202ae1
incl %edx
leal (%rbp,%rbp,2), %ecx
leal (%rcx,%r15,2), %esi
negl %esi
movl %eax, %edi
negl %edi
movl $0x1, %ecx
movl %edi, (%rbx,%rcx,8)
movl %esi, 0x4(%rbx,%rcx,8)
incq %rcx
subl %ebp, %esi
subl %ebp, %edi
cmpq %rcx, %rdx
jne 0x202acc
jmp 0x202ae6
movl $0x1, %ecx
cmpl %r9d, %ecx
jg 0x202b10
movl %ecx, %edx
decl %ecx
leaq (%rbx,%rdx,8), %rdx
addq $0x4, %rdx
movabsq $-0x3fffffff40000000, %rsi # imm = 0xC0000000C0000000
movq %rsi, -0x4(%rdx)
incl %ecx
addq $0x8, %rdx
cmpl %r9d, %ecx
jl 0x202b01
movslq %r9d, %rcx
movq %rcx, 0x58(%rsp)
cmpl $0x0, 0x20(%rsp)
jle 0x202cec
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 0x20(%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 0x38(%rsp), %r13
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 0x18(%rsp), %rdx
imull %edx, %esi
movslq %esi, %rsi
addq 0x30(%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 0x14(%rsp), %r9d
subl %r8d, %r9d
testl %r11d, %r11d
movl $0xc0000000, %edx       # imm = 0xC0000000
cmovgl %edx, %edi
cmovgl %edx, %r9d
movslq %r10d, %r8
cmpb $0x0, 0xf(%rsp)
je 0x202c7d
movq 0x40(%rsp), %rdx
movl %r11d, (%rdx,%rcx,4)
cmpl %r10d, %r11d
jge 0x202ccc
movslq %r11d, %r10
movq %r13, %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, %r12d
subl %eax, %r12d
subl %ebp, %r15d
cmpl %r12d, %r15d
setg %dl
cmovlel %r12d, %r15d
shlb $0x3, %dl
orb %r10b, %dl
movl %r15d, 0x4(%rbx,%r14,8)
subl %ebp, %r9d
cmpl %r12d, %r9d
setg %r10b
cmovlel %r12d, %r9d
shlb $0x4, %r10b
orb %dl, %r10b
movb %r10b, (%r11,%r14)
incq %r14
cmpq %r8, %r14
jl 0x202c09
jmp 0x202ccc
cmpl %r10d, %r11d
jge 0x202ccc
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 0x202cb5
movl %r11d, %r10d
movl %r10d, 0x4(%rbx,%r14,8)
subl %ebp, %r9d
cmpl %r11d, %r9d
cmovlel %r11d, %r9d
incq %r14
cmpq %r8, %r14
jl 0x202c82
movl %edi, (%rbx,%r8,8)
movl $0xc0000000, 0x4(%rbx,%r8,8) # imm = 0xC0000000
incq %rcx
addq 0x60(%rsp), %r13
cmpq 0x68(%rsp), %rcx
jne 0x202b68
movq 0x58(%rsp), %rax
movl (%rbx,%rax,8), %ebp
movq 0x28(%rsp), %r14
movq %r14, %rdi
movq 0x30(%rsp), %rsi
callq 0x1fb9e5
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1fb9e5
cmpb $0x0, 0xf(%rsp)
je 0x20307f
movl %ebp, 0x30(%rsp)
movq 0x20(%rsp), %rsi
leal -0x1(%rsi), %r13d
movq 0x18(%rsp), %r8
leal -0x1(%r8), %ebp
movq 0xe0(%rsp), %rax
movl (%rax), %r9d
movq 0xf0(%rsp), %rax
movq (%rax), %rax
testl %esi, %esi
setg %cl
testl %r8d, %r8d
setg %r15b
movl %r13d, %edx
orl %ebp, %edx
js 0x202f2d
xorl %ebx, %ebx
movl $0x2, %r12d
xorl %r14d, %r14d
movl 0x10(%rsp), %r8d
movl %r13d, %ecx
movq 0x40(%rsp), %rdx
movl (%rdx,%rcx,4), %edx
movl $0x0, %esi
cmpl %edx, %ebp
jl 0x202d9a
movl %r13d, %ecx
imull %r8d, %ecx
addl %ebp, %ecx
subl %edx, %ecx
movslq %ecx, %rcx
movq 0x38(%rsp), %rsi
movzbl (%rsi,%rcx), %esi
movl %esi, %edi
testl %ebx, %ebx
je 0x202dae
movl %ebx, %ecx
shrl %cl, %edi
shll $0x1d, %edi
sarl $0x1f, %edi
andl %ebx, %edi
jmp 0x202db1
andl $0x7, %edi
andl $0x7, %esi
testl %edi, %edi
cmovnel %edi, %esi
cmpl %edx, %ebp
movl %esi, %ebx
cmovll %r12d, %ebx
cmpl $0x3, %ebx
je 0x202e2f
cmpl $0x1, %ebx
je 0x202e2f
testl %ebx, %ebx
jne 0x202e95
testl %r14d, %r14d
je 0x202de8
movslq %r14d, %rcx
movl -0x4(%rax,%rcx,4), %edx
testb $0xf, %dl
je 0x202efe
cmpl %r9d, %r14d
jne 0x202e1d
leal (%r9,%r9), %r15d
testl %r9d, %r9d
movl $0x4, %ecx
cmovel %ecx, %r15d
leal (,%r15,4), %ecx
movslq %ecx, %rdx
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x1fbaa3
movl 0x10(%rsp), %r8d
movl %r15d, %r9d
movslq %r14d, %rcx
incl %r14d
movl $0x10, (%rax,%rcx,4)
jmp 0x202f05
testl %r14d, %r14d
je 0x202e4e
movslq %r14d, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x2, %esi
jne 0x202e4e
addl $0x10, %edx
movl %edx, -0x4(%rax,%rcx,4)
jmp 0x202e90
cmpl %r9d, %r14d
jne 0x202e83
leal (%r9,%r9), %r15d
testl %r9d, %r9d
movl $0x4, %ecx
cmovel %ecx, %r15d
leal (,%r15,4), %ecx
movslq %ecx, %rdx
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x1fbaa3
movl 0x10(%rsp), %r8d
movl %r15d, %r9d
movslq %r14d, %rcx
incl %r14d
movl $0x12, (%rax,%rcx,4)
decl %r13d
jmp 0x202f0a
movslq %r14d, %r15
testl %r14d, %r14d
je 0x202eb6
movl -0x4(%rax,%r15,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x1, %edx
jne 0x202eb6
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%r15,4)
jmp 0x202f08
cmpl %r9d, %r14d
jne 0x202ef1
leal (%r9,%r9), %r12d
testl %r9d, %r9d
movl $0x4, %ecx
cmovel %ecx, %r12d
leal (,%r12,4), %ecx
movslq %ecx, %rdx
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x1fbaa3
movl 0x10(%rsp), %r8d
movl %r12d, %r9d
movl $0x2, %r12d
incl %r14d
movl $0x11, (%rax,%r15,4)
jmp 0x202f08
addl $0x10, %edx
movl %edx, -0x4(%rax,%rcx,4)
decl %r13d
decl %ebp
testl %ebp, %ebp
setns %r15b
testl %r13d, %r13d
setns %cl
js 0x202f20
testl %ebp, %ebp
jns 0x202d6f
incl %r13d
incl %ebp
movl %ebp, %r8d
movl %r13d, %esi
jmp 0x202f30
xorl %r14d, %r14d
testb %cl, %cl
movq 0x28(%rsp), %r12
movl 0x30(%rsp), %ebp
je 0x202fad
movslq %r14d, %rbx
testl %r14d, %r14d
je 0x202f5e
movl -0x4(%rax,%rbx,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x2, %edx
jne 0x202f5e
shll $0x4, %esi
addl %esi, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
jmp 0x202fad
cmpl %r9d, %r14d
jne 0x202fa1
leal (%r9,%r9), %ecx
testl %r9d, %r9d
movl $0x4, %r13d
cmovnel %ecx, %r13d
leal (,%r13,4), %ecx
movslq %ecx, %rdx
movq %r12, %rdi
movq %rsi, 0x20(%rsp)
movq %rax, %rsi
movq %r8, 0x18(%rsp)
callq 0x1fbaa3
movq %r13, %r9
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %r8
shll $0x4, %esi
orl $0x2, %esi
incl %r14d
movl %esi, (%rax,%rbx,4)
testb %r15b, %r15b
je 0x203018
movslq %r14d, %rbx
testl %r14d, %r14d
je 0x202fd5
movl -0x4(%rax,%rbx,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x1, %edx
jne 0x202fd5
shll $0x4, %r8d
addl %r8d, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
jmp 0x203018
cmpl %r9d, %r14d
jne 0x20300a
leal (%r9,%r9), %ecx
testl %r9d, %r9d
movl $0x4, %r13d
cmovnel %ecx, %r13d
leal (,%r13,4), %ecx
movslq %ecx, %rdx
movq %r12, %rdi
movq %rax, %rsi
movq %r8, %r15
callq 0x1fbaa3
movq %r13, %r9
movq %r15, %r8
shll $0x4, %r8d
incl %r8d
incl %r14d
movl %r8d, (%rax,%rbx,4)
movl %r14d, %ecx
sarl %ecx
testl %ecx, %ecx
jle 0x203044
movl %ecx, %ecx
leal -0x1(%r14), %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 0x203029
movq 0xe0(%rsp), %rcx
movl %r9d, (%rcx)
movq 0xe8(%rsp), %rcx
movl %r14d, (%rcx)
movq 0xf0(%rsp), %rcx
movq %rax, (%rcx)
movq %r12, %rdi
movq 0x38(%rsp), %rsi
callq 0x1fb9e5
movq %r12, %rdi
movq 0x40(%rsp), %rsi
callq 0x1fb9e5
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop