// File: /workspace/github_c_makefile_star2_O2/nh13[P]ksw/src/ksw2/ksw2_gg2.c
// Function: ksw_gg2
int ksw_gg2(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, int8_t q, int8_t e, int w, int *m_cigar_, int *n_cigar_, uint32_t **cigar_)
{
	int qe = q + e, qe2 = qe + qe, r, t, n_col, *off = 0, H0 = 0, last_H0_t = 0;
	int8_t *u, *v, *x, *y, *s;
	uint8_t *p = 0, *qr;

	u = (int8_t*)kcalloc(km, tlen + 1, 1);
	v = (int8_t*)kcalloc(km, tlen + 1, 1);
	x = (int8_t*)kcalloc(km, tlen + 1, 1);
	y = (int8_t*)kcalloc(km, tlen + 1, 1);
	s = (int8_t*)kmalloc(km, tlen);
	qr = (uint8_t*)kmalloc(km, qlen);
	if (w < 0) w = tlen > qlen? tlen : qlen;
	n_col = w + 1 < tlen? w + 1 : tlen;
	if (m_cigar_ && n_cigar_ && cigar_) {
		p = (uint8_t*)kcalloc(km, (qlen + tlen) * n_col, 1);
		off = (int*)kmalloc(km, (qlen + tlen) * sizeof(int));
	}

	for (t = 0; t < qlen; ++t)
		qr[t] = query[qlen - 1 - t];

	for (r = 0; r < qlen + tlen - 1; ++r) {
		int st = 0, en = tlen - 1;
		int8_t x1, v1;
		// find the boundaries
		if (st < r - qlen + 1) st = r - qlen + 1;
		if (en > r) en = r;
		if (st < (r-w+1)>>1) st = (r-w+1)>>1; // take the ceil
		if (en > (r+w)>>1) en = (r+w)>>1; // take the floor
		// set boundary conditions
		if (st != 0) {
			if (r > st + st + w - 1) x1 = v1 = 0;
			else x1 = x[st-1], v1 = v[st-1]; // (r-1, st-1) in the band
		} else x1 = 0, v1 = r? q : 0;
		if (en != r) {
			if (r < en + en - w - 1) y[en] = u[en] = 0; // (r-1,en) out of the band; TODO: is this line necessary?
		} else y[r] = 0, u[r] = r? q : 0;
		// loop fission: set scores first
		for (t = st; t <= en; ++t)
			s[t] = mat[target[t] * m + qr[t + qlen - 1 - r]];
		// core loop
		if (m_cigar_ && n_cigar_ && cigar_) {
			uint8_t *pr = p + r * n_col;
			off[r] = st;
			for (t = st; t <= en; ++t) {
				/* At the beginning of the loop, v1=v(r-1,t-1), x1=x(r-1,t-1), u[t]=u(r-1,t), v[t]=v(r-1,t), x[t]=x(r-1,t), y[t]=y(r-1,t)
				   a      = x(r-1,t-1) + v(r-1,t-1)
				   b      = y(r-1,t)   + u(r-1,t)
				   z      = max{ S(t,r-t) + 2q + 2r, a, b }
				   u(r,t) = z - v(r-1,t-1)
				   v(r,t) = z - u(r-1,t)
				   x(r,t) = max{ 0, a - z + q }
				   y(r,t) = max{ 0, b - z + q }
				 */
				uint8_t d;
				int8_t u1;
				int8_t z = s[t] + qe2;
				int8_t a = x1   + v1;
				int8_t b = y[t] + u[t];
				d = a > z? 1 : 0; // d = z >= a? 0 : 1
				z = a > z? a : z;
				d = b > z? 2 : d;
				z = b > z? b : z;
				u1 = u[t];              // u1   = u(r-1,t) (temporary variable)
				u[t] = z - v1;          // u[t] = u(r,t)
				v1 = v[t];              // v1   = v(r-1,t) (set for the next iteration)
				v[t] = z - u1;          // v[t] = v(r,t)
				z -= q;
				a -= z;
				b -= z;
				x1 = x[t];              // x1   = x(r-1,t) (set for the next iteration)
				d   |= a > 0? 0x08 : 0;
				x[t] = a > 0? a    : 0; // x[t] = x(r,t)
				d   |= b > 0? 0x10 : 0;
				y[t] = b > 0? b    : 0; // y[t] = y(r,t)
				pr[t - st] = d;
			}
		} else {
			for (t = st; t <= en; ++t) {
				int8_t u1;
				int8_t z = s[t] + qe2;
				int8_t a = x1   + v1;
				int8_t b = y[t] + u[t];
				z = a > z? a : z;
				z = b > z? b : z;
				u1 = u[t];
				u[t] = z - v1;
				v1 = v[t];
				v[t] = z - u1;
				z -= q;
				a -= z;
				b -= z;
				x1 = x[t];
				x[t] = a > 0? a : 0;
				y[t] = b > 0? b : 0;
			}
		}
		if (r > 0) {
			if (last_H0_t >= st && last_H0_t <= en)
				H0 += v[last_H0_t] - qe;
			else ++last_H0_t, H0 += u[last_H0_t] - qe;
		} else H0 = v[0] - qe - qe, last_H0_t = 0;
	}
	kfree(km, u); kfree(km, v); kfree(km, x); kfree(km, y); kfree(km, s); kfree(km, qr);
	if (m_cigar_ && n_cigar_ && cigar_) {
		ksw_backtrack(km, 1, 0, 0, p, off, 0, n_col, tlen-1, qlen-1, m_cigar_, n_cigar_, cigar_);
		kfree(km, p); kfree(km, off);
	}
	return H0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl %r9d, 0x70(%rsp)
movq %r8, 0x90(%rsp)
movl %ecx, %r13d
movq %rdx, 0xa8(%rsp)
movl %esi, %r12d
movq %rdi, %r14
movsbl 0x138(%rsp), %eax
movl 0x148(%rsp), %ebp
movsbl 0x140(%rsp), %ecx
movl %eax, 0x4c(%rsp)
addl %eax, %ecx
movq %rcx, 0x78(%rsp)
leal 0x1(%r13), %eax
movslq %eax, %rbx
pushq $0x1
popq %r15
movq %rbx, %rsi
movq %r15, %rdx
callq 0x19e66e
movq %rax, 0x30(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x19e66e
movq %rax, 0x28(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x19e66e
movq %rax, 0x68(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x19e66e
movq %rax, 0x40(%rsp)
movslq %r13d, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x19e512
movq %rax, 0x38(%rsp)
movslq %r12d, %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x19e512
movq %rax, %r8
movq %rbx, 0xe0(%rsp)
cmpl %ebx, %r15d
movl %r12d, %r9d
cmovgl %r13d, %r9d
testl %ebp, %ebp
cmovnsl %ebp, %r9d
leal 0x1(%r9), %edx
cmpl %r15d, %edx
cmovgel %r13d, %edx
cmpq $0x0, 0x150(%rsp)
setne %al
cmpq $0x0, 0x158(%rsp)
setne %cl
andb %al, %cl
cmpq $0x0, 0x160(%rsp)
setne %al
andb %cl, %al
xorl %ebx, %ebx
movb %al, 0xf(%rsp)
cmpb $0x1, %al
movq %r14, 0x20(%rsp)
movq %r12, 0xb0(%rsp)
movl %edx, 0x1c(%rsp)
movq %r8, 0x88(%rsp)
movq %r9, 0x80(%rsp)
movq %r13, 0x60(%rsp)
jne 0x1a51ee
leal (%r12,%r13), %ebp
movl %edx, %eax
imull %ebp, %eax
movslq %eax, %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0x19e66e
movq %rax, 0x50(%rsp)
movslq %ebp, %rsi
shlq $0x2, %rsi
movq %r14, %rdi
callq 0x19e512
movq 0x80(%rsp), %r9
movq 0x88(%rsp), %r8
movq 0xb0(%rsp), %r12
movq %rax, 0x58(%rsp)
jmp 0x1a5200
movq $0x0, 0x50(%rsp)
movq $0x0, 0x58(%rsp)
movq 0x78(%rsp), %rax
leal (%rax,%rax), %r10d
testl %r12d, %r12d
movl $0x0, %eax
cmovgl %r12d, %eax
leal -0x1(%r12), %edx
movq 0x60(%rsp), %r14
movq 0x90(%rsp), %rcx
movl 0x4c(%rsp), %edi
movq 0xa8(%rsp), %r11
cmpq %rbx, %rax
je 0x1a524a
movslq %edx, %rdx
movb (%r11,%rdx), %sil
movb %sil, (%r8,%rbx)
incq %rbx
decl %edx
jmp 0x1a5233
movl %r14d, %eax
decl %r14d
addl %r12d, %eax
decl %eax
movl %r9d, %edx
notl %edx
movq %rdx, 0xd0(%rsp)
movsbq 0x70(%rsp), %rdx
movq %rdx, 0xa8(%rsp)
xorl %edx, %edx
testl %eax, %eax
movl $0x0, %r15d
cmovlel %edx, %eax
movq %rax, 0xe8(%rsp)
movslq 0x1c(%rsp), %rax
movq %rax, 0xd8(%rsp)
movl %r10d, 0x48(%rsp)
leal -0x1(%r12), %r11d
movq 0x50(%rsp), %rax
movq %rax, 0x98(%rsp)
movq %r11, 0xc8(%rsp)
movq %r11, 0x70(%rsp)
movl 0x48(%rsp), %r11d
xorl %r10d, %r10d
xorl %esi, %esi
xorl %ebp, %ebp
movq %r14, 0x60(%rsp)
cmpq 0xe8(%rsp), %r10
movl %ebp, 0x18(%rsp)
je 0x1a569b
movq %rsi, 0xf0(%rsp)
movq %r10, %rax
subq 0xe0(%rsp), %rax
leal 0x1(%rax), %r13d
testq %rax, %rax
cmovsl %r15d, %r13d
cmpl %r10d, %r14d
movl %r10d, %eax
cmovll %r14d, %eax
leal (%r9,%r10), %edx
movl %r10d, %esi
subl %r9d, %esi
incl %esi
sarl %esi
cmpl %esi, %r13d
movl %esi, %r14d
cmovgl %r13d, %r14d
sarl %edx
cmpl %edx, %eax
cmovll %eax, %edx
movzbl %dil, %ebx
testl %r14d, %r14d
movq %r14, 0xa0(%rsp)
je 0x1a535a
leal (%r9,%r14,2), %eax
cltq
cmpq %rax, %r10
jge 0x1a5368
leal -0x1(%r14), %r9d
movq 0x68(%rsp), %rax
movb (%rax,%r9), %al
movq %rdx, %r14
movq 0x28(%rsp), %rdx
movb (%rdx,%r9), %r9b
movq %r14, %rdx
jmp 0x1a536d
xorl %eax, %eax
testq %r10, %r10
movl %ebx, %r9d
cmovel %eax, %r9d
jmp 0x1a536d
xorl %r9d, %r9d
xorl %eax, %eax
movq %r10, %r12
movl %edx, %r14d
movslq %edx, %r10
cmpq %r14, %r12
movq %r12, 0xb8(%rsp)
movq %rdx, 0xc0(%rsp)
jne 0x1a53a7
movq 0x40(%rsp), %rdx
movb $0x0, (%rdx,%r12)
testq %r12, %r12
cmovel %r15d, %ebx
movq 0x30(%rsp), %rdx
movb %bl, (%rdx,%r12)
jmp 0x1a53d5
movq %rdx, %r14
movq 0xd0(%rsp), %rdx
leal (%rdx,%r14,2), %ebx
movslq %ebx, %rbx
cmpq %rbx, %r12
jge 0x1a53d5
movq 0x30(%rsp), %rdx
xorl %ebx, %ebx
movb %bl, (%rdx,%r10)
movq 0x40(%rsp), %rdx
xorl %r15d, %r15d
movb %bl, (%rdx,%r10)
cmpl %esi, %r13d
cmovgl %r13d, %esi
movslq %esi, %r13
movq %r13, %rbx
movq 0x130(%rsp), %rdx
cmpq %r10, %rbx
jg 0x1a5423
movzbl (%rcx,%rbx), %r14d
imulq 0xa8(%rsp), %r14
movq 0x70(%rsp), %r12
leal (%r12,%rbx), %ebp
movslq %ebp, %r12
movzbl (%r8,%r12), %r12d
addq %rdx, %r14
movb (%r12,%r14), %bpl
movq 0x38(%rsp), %r14
movb %bpl, (%r14,%rbx)
incq %rbx
jmp 0x1a53ea
cmpb $0x0, 0xf(%rsp)
je 0x1a5519
movq 0x58(%rsp), %rcx
movq 0xb8(%rsp), %rdx
movq 0xa0(%rsp), %rsi
movl %esi, (%rcx,%rdx,4)
movq 0x98(%rsp), %rsi
cmpq %r10, %r13
jg 0x1a55df
movq 0x38(%rsp), %rcx
movb (%rcx,%r13), %bpl
addb %r11b, %bpl
addb %r9b, %al
movq 0x30(%rsp), %r8
movb (%r8,%r13), %r14b
movq 0x40(%rsp), %r11
movb (%r11,%r13), %r12b
addb %r14b, %r12b
xorl %ebx, %ebx
cmpb %bpl, %al
setg %bl
movzbl %bpl, %ecx
movzbl %al, %eax
cmovgl %eax, %ecx
cmpb %cl, %r12b
pushq $0x2
popq %rdx
cmovgl %edx, %ebx
movzbl %r12b, %ebp
cmovgl %ebp, %ecx
movl %ecx, %r12d
subb %r9b, %r12b
movb %r12b, (%r8,%r13)
movl %ecx, %r12d
subb %r14b, %r12b
subb %dil, %cl
subb %cl, %al
subb %cl, %bpl
movq 0x28(%rsp), %rcx
movb (%rcx,%r13), %r9b
movb %r12b, (%rcx,%r13)
testb %al, %al
movzbl %al, %ecx
movl $0x0, %r8d
cmovlel %r8d, %ecx
setg %r14b
shlb $0x3, %r14b
orb %bl, %r14b
movq 0x68(%rsp), %rdx
movb (%rdx,%r13), %al
testb %bpl, %bpl
movzbl %bpl, %ebx
movl $0x0, %r15d
cmovlel %r8d, %ebx
movb %cl, (%rdx,%r13)
movb %bl, (%r11,%r13)
movl 0x48(%rsp), %r11d
setg %cl
shlb $0x4, %cl
orb %r14b, %cl
movb %cl, (%rsi)
incq %r13
incq %rsi
jmp 0x1a544e
movl 0xa0(%rsp), %r10d
movq 0x68(%rsp), %rcx
leaq (%rcx,%r10), %r13
movq 0x28(%rsp), %rcx
leaq (%rcx,%r10), %r12
movq 0x30(%rsp), %rcx
leaq (%rcx,%r10), %rbx
movq 0x40(%rsp), %rcx
leaq (%rcx,%r10), %rbp
addq 0x38(%rsp), %r10
xorl %r14d, %r14d
leal (%rsi,%r14), %ecx
cmpl %ecx, 0xc0(%rsp)
jl 0x1a55df
movb (%r10,%r14), %cl
addb %r11b, %cl
addb %r9b, %al
movb (%rbx,%r14), %dil
movb (%rbp,%r14), %dl
addb %dil, %dl
movzbl %cl, %ecx
movzbl %al, %eax
cmpb %cl, %al
cmovgl %eax, %ecx
movzbl %dl, %edx
cmpb %cl, %dl
cmovgl %edx, %ecx
movl %ecx, %r8d
subb %r9b, %r8b
movb %r8b, (%rbx,%r14)
movb (%r12,%r14), %r9b
movl %ecx, %r8d
subb %dil, %r8b
movl 0x4c(%rsp), %edi
movb %r8b, (%r12,%r14)
subb %dil, %cl
subb %cl, %al
subb %cl, %dl
movzbl %al, %ecx
testb %cl, %cl
movl $0x0, %r8d
cmovlel %r8d, %ecx
movb (%r13,%r14), %al
movb %cl, (%r13,%r14)
movzbl %dl, %ecx
testb %cl, %cl
movl $0x0, %r15d
cmovlel %r8d, %ecx
movb %cl, (%rbp,%r14)
incq %r14
jmp 0x1a554d
movq 0xb8(%rsp), %r10
testq %r10, %r10
je 0x1a5630
movq 0xf0(%rsp), %rsi
cmpl 0xa0(%rsp), %esi
movq 0x60(%rsp), %r14
movq 0x90(%rsp), %rcx
movq 0x88(%rsp), %r8
movq 0x80(%rsp), %r9
jl 0x1a565c
cmpl 0xc0(%rsp), %esi
jg 0x1a565c
movl %esi, %eax
movq 0x28(%rsp), %rdx
movsbl (%rdx,%rax), %eax
jmp 0x1a566c
movq 0x28(%rsp), %rax
movsbl (%rax), %ebp
subl %r11d, %ebp
xorl %esi, %esi
movq 0x60(%rsp), %r14
movq 0x90(%rsp), %rcx
movq 0x88(%rsp), %r8
movq 0x80(%rsp), %r9
jmp 0x1a5676
movslq %esi, %rsi
movq 0x30(%rsp), %rax
movsbl 0x1(%rax,%rsi), %eax
incq %rsi
movl 0x18(%rsp), %ebp
subl 0x78(%rsp), %ebp
addl %eax, %ebp
incq %r10
decq 0x70(%rsp)
movq 0x98(%rsp), %rax
addq 0xd8(%rsp), %rax
movq %rax, 0x98(%rsp)
jmp 0x1a52c7
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
movq %r8, %r12
callq 0x19e38c
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
callq 0x19e38c
movq %rbx, %rdi
movq 0x68(%rsp), %rsi
callq 0x19e38c
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
callq 0x19e38c
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
callq 0x19e38c
movq %rbx, %rdi
movq %r12, %rsi
callq 0x19e38c
cmpb $0x0, 0xf(%rsp)
movl 0x1c(%rsp), %ecx
je 0x1a5924
leaq 0x10(%rsp), %rax
andl $0x0, (%rax)
movq 0x150(%rsp), %rax
movl (%rax), %eax
leaq 0x14(%rsp), %rdx
movl %eax, (%rdx)
movq 0x160(%rsp), %rax
movq (%rax), %rax
movl %ecx, %r13d
notl %r13d
xorl %r15d, %r15d
movq 0xb0(%rsp), %r10
movq 0xc8(%rsp), %rbp
leal -0x1(%r10), %edx
movl %edx, 0x38(%rsp)
leal -0x1(%r14), %r12d
leal (%r14,%rbp), %ebx
imull %ecx, %ebx
addl %r14d, %ebx
movl %r12d, %r14d
incl %r14d
js 0x1a5855
testl %r10d, %r10d
jle 0x1a5855
leal (%r10,%r12), %ecx
movq 0x58(%rsp), %rdx
movl (%rdx,%rcx,4), %edx
xorl %esi, %esi
cmpl %edx, %r14d
jl 0x1a5789
movl %ebx, %ecx
subl %edx, %ecx
movslq %ecx, %rcx
movq 0x50(%rsp), %rsi
movzbl (%rsi,%rcx), %esi
testl %r15d, %r15d
je 0x1a57a8
movl %esi, %edi
movl %r15d, %ecx
shrl %cl, %edi
shll $0x1d, %edi
sarl $0x1f, %edi
andl %r15d, %edi
andl $0x7, %esi
movl %esi, %r15d
movl %edi, %esi
jmp 0x1a57ae
andl $0x7, %esi
movl %esi, %r15d
testl %esi, %esi
cmovnel %esi, %r15d
cmpl %edx, %r14d
pushq $0x2
popq %rcx
cmovll %ecx, %r15d
cmpl $0x1, %r15d
je 0x1a57ca
cmpl $0x3, %r15d
jne 0x1a57fa
movq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x14(%rsp), %rdx
movq %rax, %rcx
pushq $0x2
popq %r8
pushq $0x1
popq %r9
movq %r10, %r14
callq 0x1a5938
movq %r14, %r10
decl %r12d
addl %r13d, %ebx
jmp 0x1a5751
testl %r15d, %r15d
jne 0x1a581f
movq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x14(%rsp), %rdx
movq %rax, %rcx
xorl %r8d, %r8d
pushq $0x1
popq %r9
callq 0x1a5938
jmp 0x1a5840
movq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x14(%rsp), %rdx
movq %rax, %rcx
pushq $0x1
popq %r8
movl %r8d, %r9d
callq 0x1a5938
movl %r14d, %r12d
decl %ebp
movl 0x38(%rsp), %ecx
movl %ecx, %r10d
movl %r12d, %r14d
movl 0x1c(%rsp), %ecx
jmp 0x1a573b
testl %r14d, %r14d
js 0x1a5887
addl $0x2, %r12d
leaq 0x10(%rsp), %rsi
leaq 0x14(%rsp), %rdx
pushq $0x2
popq %r8
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rcx
movl %r12d, %r9d
movq %r10, %r14
callq 0x1a5938
movq %r14, %r10
jmp 0x1a588c
movq 0x20(%rsp), %rbx
movl 0x18(%rsp), %ebp
testl %r10d, %r10d
jle 0x1a58b1
leaq 0x10(%rsp), %rsi
leaq 0x14(%rsp), %rdx
pushq $0x1
popq %r8
movq %rbx, %rdi
movq %rax, %rcx
movl %r10d, %r9d
callq 0x1a5938
pushq $-0x1
popq %rcx
xorl %edx, %edx
movl 0x10(%rsp), %esi
movl %esi, %edi
sarl %edi
movslq %edi, %rdi
cmpq %rdi, %rdx
jge 0x1a58e7
movl (%rax,%rdx,4), %edi
addl %ecx, %esi
movslq %esi, %rsi
movl (%rax,%rsi,4), %esi
movl %esi, (%rax,%rdx,4)
movl 0x10(%rsp), %esi
addl %ecx, %esi
movslq %esi, %rsi
movl %edi, (%rax,%rsi,4)
incq %rdx
decl %ecx
jmp 0x1a58b6
movl 0x14(%rsp), %ecx
movq 0x150(%rsp), %rdx
movl %ecx, (%rdx)
movq 0x158(%rsp), %rcx
movl %esi, (%rcx)
movq 0x160(%rsp), %rcx
movq %rax, (%rcx)
movq %rbx, %rdi
movq 0x50(%rsp), %rsi
callq 0x19e38c
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
callq 0x19e38c
movl %ebp, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq