// File: /workspace/github_c_makefile_star2_O3/ToniWestbrook[P]paladin/ksw.c
// Function: ksw_global2
int ksw_global2(int qlen, const uint8_t *query, int tlen, const uint8_t *target, int m, const int8_t *mat, int o_del, int e_del, int o_ins, int e_ins, int w, int *n_cigar_, uint32_t **cigar_)
{
	eh_t *eh;
	int8_t *qp; // query profile
	int i, j, k, oe_del = o_del + e_del, oe_ins = o_ins + e_ins, score, n_col;
	uint8_t *z; // backtrack matrix; in each cell: f<<4|e<<2|h; in principle, we can halve the memory, but backtrack will be a little more complex
	if (n_cigar_) *n_cigar_ = 0;
	// allocate memory
	n_col = qlen < 2*w+1? qlen : 2*w+1; // maximum #columns of the backtrack matrix
	z = n_cigar_ && cigar_? malloc((long)n_col * tlen) : 0;
	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 = -(o_ins + e_ins * 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, t;
		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? -(o_del + e_del * (i + 1)) : MINUS_INF;
		if (n_cigar_ && cigar_) {
			uint8_t *zi = &z[(long)i * n_col];
			for (j = beg; LIKELY(j < end); ++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:
				//   M(i,j)   = H(i-1,j-1) + S(i,j)
				//   H(i,j)   = max{M(i,j), E(i,j), F(i,j)}
				//   E(i+1,j) = max{M(i,j)-gapo, E(i,j)} - gape
				//   F(i,j+1) = max{M(i,j)-gapo, F(i,j)} - gape
				// We have to separate M(i,j); otherwise the direction may not be recorded correctly.
				// However, a CIGAR like "10M3I3D10M" allowed by local() is disallowed by global().
				// Such a CIGAR may occur, in theory, if mismatch_penalty > 2*gap_ext_penalty + 2*gap_open_penalty/k.
				// In practice, this should happen very rarely given a reasonable scoring system.
				eh_t *p = &eh[j];
				int32_t h, m = p->h, e = p->e;
				uint8_t d; // direction
				p->h = h1;
				m += q[j];
				d = m >= e? 0 : 1;
				h = m >= e? m : e;
				d = h >= f? d : 2;
				h = h >= f? h : f;
				h1 = h;
				t = m - oe_del;
				e -= e_del;
				d |= e > t? 1<<2 : 0;
				e  = e > t? e    : t;
				p->e = e;
				t = m - oe_ins;
				f -= e_ins;
				d |= f > t? 2<<4 : 0; // if we want to halve the memory, use one bit only, instead of two
				f  = f > t? f    : t;
				zi[j - beg] = d; // z[i,j] keeps h for the current cell and e/f for the next cell
			}
		} else {
			for (j = beg; LIKELY(j < end); ++j) {
				eh_t *p = &eh[j];
				int32_t h, m = p->h, e = p->e;
				p->h = h1;
				m += q[j];
				h = m >= e? m : e;
				h = h >= f? h : f;
				h1 = h;
				t = m - oe_del;
				e -= e_del;
				e  = e > t? e : t;
				p->e = e;
				t = m - oe_ins;
				f -= e_ins;
				f  = f > t? f : t;
			}
		}
		eh[end].h = h1; eh[end].e = MINUS_INF;
	}
	score = eh[qlen].h;
	if (n_cigar_ && cigar_) { // backtrack
		int n_cigar = 0, m_cigar = 0, which = 0;
		uint32_t *cigar = 0, tmp;
		i = tlen - 1; k = (i + w + 1 < qlen? i + w + 1 : qlen) - 1; // (i,k) points to the last cell
		while (i >= 0 && k >= 0) {
			which = z[(long)i * n_col + (k - (i > w? i - w : 0))] >> (which<<1) & 3;
			if (which == 0)      cigar = push_cigar(&n_cigar, &m_cigar, cigar, 0, 1), --i, --k;
			else if (which == 1) cigar = push_cigar(&n_cigar, &m_cigar, cigar, 2, 1), --i;
			else                 cigar = push_cigar(&n_cigar, &m_cigar, cigar, 1, 1), --k;
		}
		if (i >= 0) cigar = push_cigar(&n_cigar, &m_cigar, cigar, 2, i + 1);
		if (k >= 0) cigar = push_cigar(&n_cigar, &m_cigar, cigar, 1, k + 1);
		for (i = 0; i < n_cigar>>1; ++i) // reverse CIGAR
			tmp = cigar[i], cigar[i] = cigar[n_cigar-1-i], cigar[n_cigar-1-i] = tmp;
		*n_cigar_ = n_cigar, *cigar_ = cigar;
	}
	free(eh); free(qp); free(z);
	return score;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r8d, %r13d
movq %rcx, 0x60(%rsp)
movl %edi, %ebp
movq 0xc8(%rsp), %rax
movl 0xc0(%rsp), %r14d
testq %rax, %rax
movq %rdx, 0x30(%rsp)
movq %rsi, 0x18(%rsp)
movq %r9, 0x28(%rsp)
je 0x456a
andl $0x0, (%rax)
leal 0x1(,%r14,2), %eax
cmpl %ebp, %eax
cmovgel %ebp, %eax
cmpq $0x0, 0xd0(%rsp)
movl %eax, 0x10(%rsp)
je 0x457b
cltq
movslq %edx, %rdi
imulq %rax, %rdi
leaq 0x251fd(%rip), %rsi     # 0x29748
leaq 0x252c4(%rip), %rcx     # 0x29816
movl $0x209, %edx            # imm = 0x209
callq 0x23213
movq %rax, 0x8(%rsp)
movb $0x1, %al
movq %rax, 0x20(%rsp)
jmp 0x458d
leal 0x1(,%r14,2), %eax
cmpl %ebp, %eax
cmovgel %ebp, %eax
movl %eax, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
movl 0xb8(%rsp), %r12d
movl %r13d, %eax
imull %ebp, %eax
movslq %eax, %rdi
leaq 0x251a3(%rip), %r15     # 0x29748
leaq 0x2526a(%rip), %rbx     # 0x29816
movq %r15, %rsi
movl $0x20a, %edx            # imm = 0x20A
movq %rbx, %rcx
callq 0x23213
movq %rax, 0x38(%rsp)
leal 0x1(%rbp), %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
movq %r15, %rdx
movl $0x20b, %ecx            # imm = 0x20B
movq %rbx, %r8
callq 0x2319c
movq %rax, %r15
testl %r13d, %r13d
movq 0x28(%rsp), %rbx
movq 0x18(%rsp), %r11
jle 0x462e
movl %r13d, %eax
movl %ebp, %ecx
xorl %edx, %edx
xorl %esi, %esi
testl %ebp, %ebp
jle 0x4626
movq %rdx, %rdi
imulq %rax, %rdi
addq %rbx, %rdi
movslq %esi, %r8
addq 0x38(%rsp), %r8
xorl %r9d, %r9d
movzbl (%r11,%r9), %r10d
movb (%rdi,%r10), %r10b
movb %r10b, (%r8,%r9)
incq %r9
cmpq %r9, %rcx
jne 0x460e
addl %r9d, %esi
incq %rdx
cmpq %rax, %rdx
jne 0x45f5
movl 0xb0(%rsp), %eax
addl %r12d, %eax
movabsq $-0x4000000000000000, %rcx # imm = 0xC000000000000000
movq %rcx, (%r15)
cmpl %r14d, %ebp
movl %r14d, %ecx
cmovll %ebp, %ecx
testl %ecx, %ecx
movq 0x30(%rsp), %r9
jle 0x4683
leal 0x1(%rcx), %edx
movl %ecx, %esi
movl %eax, %edi
negl %edi
xorl %r8d, %r8d
movl 0x10(%rsp), %r10d
movl %edi, 0x8(%r15,%r8,8)
movl $0xc0000000, 0xc(%r15,%r8,8) # imm = 0xC0000000
subl %r12d, %edi
incq %r8
cmpq %r8, %rsi
jne 0x4668
jmp 0x468b
pushq $0x1
popq %rdx
movl 0x10(%rsp), %r10d
cmpl %ebp, %edx
jg 0x46b6
xorl %edx, %edx
testl %ecx, %ecx
cmovgl %ecx, %edx
leaq (%r15,%rdx,8), %rcx
addq $0xc, %rcx
movabsq $-0x3fffffff40000000, %rsi # imm = 0xC0000000C0000000
movq %rsi, -0x4(%rcx)
incl %edx
addq $0x8, %rcx
cmpl %ebp, %edx
jl 0x46a8
movslq %r10d, %rcx
movq %rcx, 0x28(%rsp)
testl %r9d, %r9d
jle 0x488f
movl 0xa8(%rsp), %ecx
movl 0xa0(%rsp), %edx
leal (%rcx,%rdx), %esi
leal 0x1(%r14), %edx
movq %rdx, 0x50(%rsp)
movslq %r14d, %rdx
movq %rdx, 0x48(%rsp)
movl 0x30(%rsp), %edx
movq %rdx, 0x40(%rsp)
movq 0x8(%rsp), %r9
xorl %r8d, %r8d
movq %rbp, 0x58(%rsp)
movl %r14d, %edx
subl %r8d, %edx
movl %r8d, %edi
cmovgl %r14d, %edi
negl %edx
subl %r14d, %edi
movslq %edi, %r10
movq 0x60(%rsp), %rdi
movzbl (%rdi,%r8), %edi
imull %ebp, %edi
movslq %edi, %r11
addq 0x38(%rsp), %r11
cmpq 0x48(%rsp), %r8
movl $0x0, %edi
cmovlel %edi, %edx
movq 0x50(%rsp), %rdi
addl %r8d, %edi
cmpl %ebp, %edi
cmovgel %ebp, %edi
movq %r8, 0x10(%rsp)
movl %r8d, %r13d
notl %r13d
imull %ecx, %r13d
movl 0xa0(%rsp), %r8d
subl %r8d, %r13d
testl %edx, %edx
movl $0xc0000000, %r8d       # imm = 0xC0000000
cmovnel %r8d, %r13d
movslq %edi, %r14
cmpb $0x0, 0x20(%rsp)
movq %r9, 0x18(%rsp)
je 0x4800
cmpl %edi, %edx
jge 0x4858
movslq %edx, %rdi
movq %r9, %rdx
subq %rdi, %rdx
movl (%r15,%r10,8), %r9d
movl 0x4(%r15,%r10,8), %ebp
movl %r13d, (%r15,%r10,8)
movsbl (%r11,%r10), %ebx
addl %r9d, %ebx
xorl %r9d, %r9d
cmpl %ebp, %ebx
setl %r9b
movl %ebp, %r13d
cmovgl %ebx, %r13d
movl %ebx, %edi
subl %esi, %edi
subl %ecx, %ebp
cmpl %edi, %ebp
cmovlel %edi, %ebp
movl %ebp, 0x4(%r15,%r10,8)
setg %dil
shlb $0x2, %dil
cmpl %r8d, %r13d
cmovlel %r8d, %r13d
pushq $0x2
popq %rbp
cmovll %ebp, %r9d
orb %dil, %r9b
subl %eax, %ebx
subl %r12d, %r8d
cmpl %ebx, %r8d
cmovlel %ebx, %r8d
setg %dil
shlb $0x5, %dil
orb %r9b, %dil
movb %dil, (%rdx,%r10)
incq %r10
cmpq %r14, %r10
jl 0x478d
jmp 0x4858
cmpl %edi, %edx
jge 0x4858
movl $0xc0000000, %edx       # imm = 0xC0000000
movl (%r15,%r10,8), %edi
movl 0x4(%r15,%r10,8), %r8d
movl %r13d, (%r15,%r10,8)
movsbl (%r11,%r10), %r9d
addl %edi, %r9d
cmpl %r8d, %r9d
movl %r8d, %r13d
cmovgl %r9d, %r13d
cmpl %edx, %r13d
cmovlel %edx, %r13d
movl %r9d, %edi
subl %esi, %edi
subl %ecx, %r8d
cmpl %edi, %r8d
cmovlel %edi, %r8d
movl %r8d, 0x4(%r15,%r10,8)
subl %eax, %r9d
subl %r12d, %edx
cmpl %r9d, %edx
cmovlel %r9d, %edx
incq %r10
cmpq %r14, %r10
jl 0x4809
movl %r13d, (%r15,%r14,8)
movl $0xc0000000, 0x4(%r15,%r14,8) # imm = 0xC0000000
movq 0x10(%rsp), %r8
incq %r8
movq 0x18(%rsp), %r9
addq 0x28(%rsp), %r9
cmpq 0x40(%rsp), %r8
movl 0xc0(%rsp), %r14d
movq 0x58(%rsp), %rbp
jne 0x46ff
movslq %ebp, %rax
movl (%r15,%rax,8), %ebx
cmpb $0x0, 0x20(%rsp)
movq 0x8(%rsp), %r13
je 0x4ca5
movl %ebx, 0x10(%rsp)
movq 0x30(%rsp), %r9
testl %r9d, %r9d
setle %al
leal (%r14,%r9), %ecx
cmpl %ebp, %ecx
cmovll %ecx, %ebp
testl %ebp, %ebp
setg %bl
setle %cl
orb %al, %cl
movq %r13, 0x8(%rsp)
jne 0x4b23
decl %ebp
decl %r9d
xorl %esi, %esi
xorl %eax, %eax
movq %rbp, %r12
xorl %ebp, %ebp
xorl %edi, %edi
xorl %r10d, %r10d
movl %r9d, %ecx
imulq 0x28(%rsp), %rcx
movl %r14d, %edx
subl %r9d, %edx
cmovgel %esi, %edx
addl %r12d, %edx
movslq %edx, %rdx
addq %rcx, %rdx
movzbl (%r13,%rdx), %edx
addl %ebp, %ebp
movl %ebp, %ecx
shrl %cl, %edx
movl %edx, %ebp
andl $0x3, %ebp
movslq %r10d, %rbx
cmpl $0x1, %ebp
je 0x49a1
testl %ebp, %ebp
jne 0x49c0
testl %r10d, %r10d
je 0x4938
movl -0x4(%rax,%rbx,4), %ecx
testb $0xf, %cl
je 0x4ab2
cmpl %edi, %r10d
jne 0x4992
addl %edi, %edi
testl %r10d, %r10d
pushq $0x4
popq %rcx
cmovel %ecx, %edi
movq %rdi, 0x18(%rsp)
leal (,%rdi,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
leaq 0x24de7(%rip), %rdx     # 0x29748
movl $0x1f9, %ecx            # imm = 0x1F9
leaq 0x24eb5(%rip), %r8      # 0x29822
movq %r9, %r14
movq %r10, %r13
callq 0x2324c
xorl %esi, %esi
movq 0x18(%rsp), %rdi
movq %r13, %r10
movq 0x8(%rsp), %r13
movq %r14, %r9
movl 0xc0(%rsp), %r14d
incl %r10d
movl $0x10, (%rax,%rbx,4)
jmp 0x4ab9
testl %r10d, %r10d
je 0x49e3
movl -0x4(%rax,%rbx,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x2, %edx
jne 0x49e3
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
jmp 0x4a47
testl %r10d, %r10d
je 0x4a4c
movl -0x4(%rax,%rbx,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x1, %edx
jne 0x4a4c
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
jmp 0x4abc
cmpl %edi, %r10d
jne 0x4a3d
addl %edi, %edi
testl %r10d, %r10d
pushq $0x4
popq %rcx
cmovel %ecx, %edi
movq %rdi, 0x18(%rsp)
leal (,%rdi,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
leaq 0x24d3c(%rip), %rdx     # 0x29748
movl $0x1f9, %ecx            # imm = 0x1F9
leaq 0x24e0a(%rip), %r8      # 0x29822
movq %r9, %r14
movq %r10, %r13
callq 0x2324c
xorl %esi, %esi
movq 0x18(%rsp), %rdi
movq %r13, %r10
movq 0x8(%rsp), %r13
movq %r14, %r9
movl 0xc0(%rsp), %r14d
incl %r10d
movl $0x12, (%rax,%rbx,4)
decl %r9d
jmp 0x4abf
cmpl %edi, %r10d
jne 0x4aa6
addl %edi, %edi
testl %r10d, %r10d
pushq $0x4
popq %rcx
cmovel %ecx, %edi
movq %rdi, 0x18(%rsp)
leal (,%rdi,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
leaq 0x24cd3(%rip), %rdx     # 0x29748
movl $0x1f9, %ecx            # imm = 0x1F9
leaq 0x24da1(%rip), %r8      # 0x29822
movq %r9, %r14
movq %r10, %r13
callq 0x2324c
xorl %esi, %esi
movq 0x18(%rsp), %rdi
movq %r13, %r10
movq 0x8(%rsp), %r13
movq %r14, %r9
movl 0xc0(%rsp), %r14d
incl %r10d
movl $0x11, (%rax,%rbx,4)
jmp 0x4abc
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
decl %r9d
decl %r12d
testl %r12d, %r12d
setns %bl
testl %r9d, %r9d
js 0x4ad3
testl %r12d, %r12d
jns 0x48e6
leal 0x1(%r12), %ebp
testl %r9d, %r9d
js 0x4b3e
incl %r9d
testl %r10d, %r10d
je 0x4b59
movslq %r10d, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x2, %esi
jne 0x4b60
shll $0x4, %r9d
addl %r9d, %edx
movl %edx, -0x4(%rax,%rcx,4)
testl %r12d, %r12d
movq 0xd0(%rsp), %r11
movq 0xc8(%rsp), %r12
movl 0x10(%rsp), %ebx
jns 0x4bf7
jmp 0x4c72
xorl %r10d, %r10d
movb $0x1, %cl
movl $0x0, %edi
movl $0x0, %eax
movl $0x0, %edx
testl %r9d, %r9d
jg 0x4b62
jmp 0x4b40
movl %edi, %edx
movl %edx, %edi
testb %bl, %bl
movq 0xd0(%rsp), %r11
movq 0xc8(%rsp), %r12
jmp 0x4be8
xorl %r10d, %r10d
movb $0x1, %cl
jmp 0x4b62
xorl %ecx, %ecx
cmpl %edi, %r10d
movq 0xd0(%rsp), %r11
movq 0xc8(%rsp), %r12
jne 0x4bd4
leal (%r10,%r10), %r12d
testb %cl, %cl
pushq $0x4
popq %rcx
cmovnel %ecx, %r12d
leal (,%r12,4), %ecx
movslq %ecx, %rsi
leaq 0x24bb2(%rip), %rdx     # 0x29748
leaq 0x24c85(%rip), %r8      # 0x29822
movq %rax, %rdi
movl $0x1f9, %ecx            # imm = 0x1F9
movq %rbp, %r14
movq %r9, %rbp
movq %r10, %r13
callq 0x2324c
movq 0xd0(%rsp), %r11
movq %r12, %rdi
movq %r13, %r10
movq 0x8(%rsp), %r13
movq %rbp, %r9
movq %r14, %rbp
movq 0xc8(%rsp), %r12
shll $0x4, %r9d
orl $0x2, %r9d
movslq %r10d, %rcx
incl %r10d
movl %r9d, (%rax,%rcx,4)
testb %bl, %bl
movl 0x10(%rsp), %ebx
je 0x4c72
testl %r10d, %r10d
je 0x4c17
movslq %r10d, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x1, %esi
jne 0x4c13
shll $0x4, %ebp
addl %ebp, %edx
movl %edx, -0x4(%rax,%rcx,4)
jmp 0x4c72
xorl %ecx, %ecx
jmp 0x4c1c
xorl %r10d, %r10d
movb $0x1, %cl
cmpl %edi, %r10d
jne 0x4c64
leal (,%r10,8), %edx
movslq %edx, %rdx
testb %cl, %cl
pushq $0x10
popq %rsi
cmoveq %rdx, %rsi
leaq 0x24b0c(%rip), %rdx     # 0x29748
leaq 0x24bdf(%rip), %r8      # 0x29822
movq %rax, %rdi
movl $0x1f9, %ecx            # imm = 0x1F9
movq %r10, %r14
movq %r11, %r12
callq 0x2324c
movq %r12, %r11
movq %r14, %r10
movq 0xc8(%rsp), %r12
shll $0x4, %ebp
incl %ebp
movslq %r10d, %rcx
incl %r10d
movl %ebp, (%rax,%rcx,4)
movl %r10d, %ecx
sarl %ecx
testl %ecx, %ecx
jle 0x4c9e
movl %ecx, %ecx
leal -0x1(%r10), %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 0x4c83
movl %r10d, (%r12)
movq %rax, (%r11)
movq %r15, %rdi
callq 0x3390
movq 0x38(%rsp), %rdi
callq 0x3390
movq %r13, %rdi
callq 0x3390
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq