// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %r9, %r15
movl %r8d, %r14d
movq %rcx, 0x70(%rsp)
movq %rsi, %rbp
movq 0xd8(%rsp), %rax
testq %rax, %rax
je 0x4514
andl $0x0, (%rax)
testq %rax, %rax
setne %al
movl 0xd0(%rsp), %ecx
leal 0x1(,%rcx,2), %ecx
cmpl %edi, %ecx
cmovgel %edi, %ecx
cmpq $0x0, 0xe0(%rsp)
setne %sil
andb %al, %sil
movslq %ecx, %rax
movb %sil, 0xf(%rsp)
cmpb $0x1, %sil
movq %rdi, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
jne 0x4585
movslq %edx, %rdi
imulq %rax, %rdi
leaq 0x250b0(%rip), %rsi     # 0x29618
leaq 0x25177(%rip), %rcx     # 0x296e6
movl $0x209, %edx            # imm = 0x209
callq 0x22d2b
movq 0x30(%rsp), %rdi
movq %rax, 0x18(%rsp)
jmp 0x458e
movq $0x0, 0x18(%rsp)
movl 0xc8(%rsp), %r13d
movl %r14d, %eax
imull %edi, %eax
movslq %eax, %rdi
leaq 0x25072(%rip), %r12     # 0x29618
leaq 0x25139(%rip), %rbx     # 0x296e6
movq %r12, %rsi
movl $0x20a, %edx            # imm = 0x20A
movq %rbx, %rcx
callq 0x22d2b
movq %rax, 0x38(%rsp)
movq 0x30(%rsp), %rax
incl %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
movq %r12, %rdx
movl $0x20b, %ecx            # imm = 0x20B
movq %rbx, %r8
callq 0x22cb4
movq 0x30(%rsp), %r11
movq %rax, %r12
testl %r14d, %r14d
jle 0x4631
movl %r14d, %eax
movl %r11d, %ecx
xorl %edx, %edx
xorl %esi, %esi
testl %r11d, %r11d
jle 0x4629
movq %rdx, %rdi
imulq %rax, %rdi
addq %r15, %rdi
movslq %esi, %r8
addq 0x38(%rsp), %r8
xorl %r9d, %r9d
movzbl (%rbp,%r9), %r10d
movb (%rdi,%r10), %r10b
movb %r10b, (%r8,%r9)
incq %r9
cmpq %r9, %rcx
jne 0x4610
addl %r9d, %esi
incq %rdx
cmpq %rax, %rdx
jne 0x45f6
movl 0xc0(%rsp), %eax
addl %r13d, %eax
movabsq $-0x4000000000000000, %rcx # imm = 0xC000000000000000
movq %rcx, (%r12)
movl 0xd0(%rsp), %edx
cmpl %edx, %r11d
cmovll %r11d, %edx
testl %edx, %edx
jle 0x4683
incl %edx
movl %eax, %esi
negl %esi
pushq $0x1
popq %rcx
movq 0x20(%rsp), %rdi
movl %esi, (%r12,%rcx,8)
movl $0xc0000000, 0x4(%r12,%rcx,8) # imm = 0xC0000000
incq %rcx
subl %r13d, %esi
cmpq %rcx, %rdx
jne 0x4669
jmp 0x468b
pushq $0x1
popq %rcx
movq 0x20(%rsp), %rdi
cmpl %r11d, %ecx
jg 0x46b5
movl %ecx, %edx
decl %ecx
leaq (%r12,%rdx,8), %rdx
addq $0x4, %rdx
movabsq $-0x3fffffff40000000, %rsi # imm = 0xC0000000C0000000
movq %rsi, -0x4(%rdx)
incl %ecx
addq $0x8, %rdx
cmpl %r11d, %ecx
jl 0x46a6
movslq %r11d, %rcx
movq %rcx, 0x48(%rsp)
testl %edi, %edi
jle 0x48b2
movl 0xb8(%rsp), %edx
movl 0xb0(%rsp), %ecx
leal (%rdx,%rcx), %edi
movl 0xd0(%rsp), %ecx
movq %rcx, %rsi
addl %ecx, %ecx
leal 0x1(%rsi), %r8d
movq %r8, 0x68(%rsp)
movslq %esi, %rsi
movq %rsi, 0x60(%rsp)
movl 0x20(%rsp), %esi
movq %rsi, 0x58(%rsp)
movslq %ecx, %rsi
incq %rsi
movq 0x48(%rsp), %rcx
cmpq %rcx, %rsi
cmovgeq %rcx, %rsi
movq %rsi, 0x50(%rsp)
movq 0x18(%rsp), %rbp
xorl %esi, %esi
movl 0xd0(%rsp), %ecx
movl %ecx, %r10d
subl %esi, %r10d
movl %esi, %r8d
cmovgl %ecx, %r8d
negl %r10d
subl %ecx, %r8d
movslq %r8d, %rbx
movq 0x70(%rsp), %rcx
movzbl (%rcx,%rsi), %r8d
imull %r11d, %r8d
movslq %r8d, %r15
addq 0x38(%rsp), %r15
cmpq 0x60(%rsp), %rsi
movl $0x0, %ecx
cmovlel %ecx, %r10d
movq 0x68(%rsp), %rcx
leal (%rcx,%rsi), %r9d
cmpl %r11d, %r9d
cmovgel %r11d, %r9d
movl %esi, %r8d
notl %r8d
imull %edx, %r8d
movl 0xb0(%rsp), %ecx
subl %ecx, %r8d
testl %r10d, %r10d
movl $0xc0000000, %ecx       # imm = 0xC0000000
cmovnel %ecx, %r8d
movslq %r9d, %r14
cmpb $0x0, 0xf(%rsp)
movq %rbp, 0x10(%rsp)
movq %rsi, 0x40(%rsp)
je 0x482c
cmpl %r9d, %r10d
jge 0x4883
movslq %r10d, %r9
subq %r9, %rbp
movl $0xc0000000, %r10d      # imm = 0xC0000000
movl (%r12,%rbx,8), %r11d
movl 0x4(%r12,%rbx,8), %ecx
movl %r8d, (%r12,%rbx,8)
movsbl (%r15,%rbx), %esi
addl %r11d, %esi
xorl %r11d, %r11d
cmpl %ecx, %esi
setl %r11b
movl %ecx, %r8d
cmovgl %esi, %r8d
movl %esi, %r9d
subl %edi, %r9d
subl %edx, %ecx
cmpl %r9d, %ecx
cmovlel %r9d, %ecx
movl %ecx, 0x4(%r12,%rbx,8)
setg %cl
shlb $0x2, %cl
cmpl %r10d, %r8d
cmovlel %r10d, %r8d
pushq $0x2
popq %r9
cmovll %r9d, %r11d
orb %cl, %r11b
subl %eax, %esi
subl %r13d, %r10d
cmpl %esi, %r10d
cmovlel %esi, %r10d
setg %cl
shlb $0x5, %cl
orb %r11b, %cl
movb %cl, (%rbp,%rbx)
incq %rbx
cmpq %r14, %rbx
jl 0x47b8
jmp 0x4883
cmpl %r9d, %r10d
jge 0x4883
movl $0xc0000000, %r10d      # imm = 0xC0000000
movl (%r12,%rbx,8), %ecx
movl 0x4(%r12,%rbx,8), %esi
movl %r8d, (%r12,%rbx,8)
movsbl (%r15,%rbx), %r9d
addl %ecx, %r9d
cmpl %esi, %r9d
movl %esi, %r8d
cmovgl %r9d, %r8d
cmpl %r10d, %r8d
cmovlel %r10d, %r8d
movl %r9d, %ecx
subl %edi, %ecx
subl %edx, %esi
cmpl %ecx, %esi
cmovlel %ecx, %esi
movl %esi, 0x4(%r12,%rbx,8)
subl %eax, %r9d
subl %r13d, %r10d
cmpl %r9d, %r10d
cmovlel %r9d, %r10d
incq %rbx
cmpq %r14, %rbx
jl 0x4837
movl %r8d, (%r12,%r14,8)
movl $0xc0000000, 0x4(%r12,%r14,8) # imm = 0xC0000000
movq 0x40(%rsp), %rsi
incq %rsi
movq 0x10(%rsp), %rbp
addq 0x50(%rsp), %rbp
cmpq 0x58(%rsp), %rsi
movq 0x30(%rsp), %r11
jne 0x471a
movq 0x48(%rsp), %rax
movl (%r12,%rax,8), %ebp
cmpb $0x0, 0xf(%rsp)
movq 0x18(%rsp), %r14
je 0x4c70
movq 0x20(%rsp), %r9
movl 0xd0(%rsp), %eax
addl %r9d, %eax
cmpl %r11d, %eax
cmovll %eax, %r11d
testl %r9d, %r9d
setg %cl
testl %r11d, %r11d
setg %bl
movl %ecx, %eax
andb %bl, %al
cmpb $0x1, %al
jne 0x4b1e
movl %ebp, 0x40(%rsp)
decl %r11d
decl %r9d
xorl %r8d, %r8d
xorl %eax, %eax
xorl %ebp, %ebp
xorl %edx, %edx
xorl %r13d, %r13d
movq 0x28(%rsp), %rdi
movl %r9d, %ecx
imulq %rdi, %rcx
movl 0xd0(%rsp), %esi
subl %r9d, %esi
cmovgel %r8d, %esi
addl %r11d, %esi
movslq %esi, %rsi
addq %rcx, %rsi
movzbl (%r14,%rsi), %esi
addl %ebp, %ebp
movl %ebp, %ecx
shrl %cl, %esi
movl %esi, %ebp
andl $0x3, %ebp
movslq %r13d, %rbx
cmpl $0x1, %ebp
je 0x49d5
testl %ebp, %ebp
jne 0x49f4
testl %r13d, %r13d
je 0x4968
movl -0x4(%rax,%rbx,4), %ecx
testb $0xf, %cl
je 0x4aee
cmpl %edx, %r13d
jne 0x49c6
leal (%rdx,%rdx), %r14d
testl %edx, %edx
pushq $0x4
popq %rcx
cmovel %ecx, %r14d
leal (,%r14,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
leaq 0x24c89(%rip), %rdx     # 0x29618
movl $0x1f9, %ecx            # imm = 0x1F9
leaq 0x24d57(%rip), %r8      # 0x296f2
movq %r13, 0x10(%rsp)
movq %r9, %r13
movq %r11, %r15
callq 0x22d64
xorl %r8d, %r8d
movq 0x28(%rsp), %rdi
movq %r15, %r11
movq %r13, %r9
movq 0x10(%rsp), %r13
movl %r14d, %edx
movq 0x18(%rsp), %r14
incl %r13d
movl $0x10, (%rax,%rbx,4)
jmp 0x4af5
testl %r13d, %r13d
je 0x4a17
movl -0x4(%rax,%rbx,4), %ecx
movl %ecx, %esi
andl $0xf, %esi
cmpl $0x2, %esi
jne 0x4a17
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
jmp 0x4a7f
testl %r13d, %r13d
je 0x4a84
movl -0x4(%rax,%rbx,4), %ecx
movl %ecx, %esi
andl $0xf, %esi
cmpl $0x1, %esi
jne 0x4a84
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
jmp 0x4af8
cmpl %edx, %r13d
jne 0x4a75
leal (%rdx,%rdx), %r14d
testl %edx, %edx
pushq $0x4
popq %rcx
cmovel %ecx, %r14d
leal (,%r14,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
leaq 0x24bda(%rip), %rdx     # 0x29618
movl $0x1f9, %ecx            # imm = 0x1F9
leaq 0x24ca8(%rip), %r8      # 0x296f2
movq %r13, 0x10(%rsp)
movq %r9, %r13
movq %r11, %r15
callq 0x22d64
xorl %r8d, %r8d
movq 0x28(%rsp), %rdi
movq %r15, %r11
movq %r13, %r9
movq 0x10(%rsp), %r13
movl %r14d, %edx
movq 0x18(%rsp), %r14
incl %r13d
movl $0x12, (%rax,%rbx,4)
decl %r9d
jmp 0x4afb
cmpl %edx, %r13d
jne 0x4ae2
leal (%rdx,%rdx), %r14d
testl %edx, %edx
pushq $0x4
popq %rcx
cmovel %ecx, %r14d
leal (,%r14,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
leaq 0x24b6d(%rip), %rdx     # 0x29618
movl $0x1f9, %ecx            # imm = 0x1F9
leaq 0x24c3b(%rip), %r8      # 0x296f2
movq %r13, 0x10(%rsp)
movq %r9, %r13
movq %r11, %r15
callq 0x22d64
xorl %r8d, %r8d
movq 0x28(%rsp), %rdi
movq %r15, %r11
movq %r13, %r9
movq 0x10(%rsp), %r13
movl %r14d, %edx
movq 0x18(%rsp), %r14
incl %r13d
movl $0x11, (%rax,%rbx,4)
jmp 0x4af8
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
decl %r9d
decl %r11d
testl %r11d, %r11d
setns %bl
testl %r9d, %r9d
setns %cl
js 0x4b12
testl %r11d, %r11d
jns 0x4914
incl %r9d
incl %r11d
movl 0x40(%rsp), %ebp
jmp 0x4b25
xorl %r13d, %r13d
xorl %edx, %edx
xorl %eax, %eax
testb %cl, %cl
je 0x4bc1
movslq %r13d, %r14
testl %r13d, %r13d
je 0x4b52
movl -0x4(%rax,%r14,4), %ecx
movl %ecx, %esi
andl $0xf, %esi
cmpl $0x2, %esi
jne 0x4b52
shll $0x4, %r9d
addl %r9d, %ecx
movl %ecx, -0x4(%rax,%r14,4)
jmp 0x4bbc
cmpl %edx, %r13d
jne 0x4bad
movq %r9, 0x20(%rsp)
leal (%rdx,%rdx), %r15d
testl %edx, %edx
pushq $0x4
popq %rcx
cmovel %ecx, %r15d
leal (,%r15,4), %ecx
movslq %ecx, %rsi
leaq 0x24a9d(%rip), %rdx     # 0x29618
leaq 0x24b70(%rip), %r8      # 0x296f2
movq %rax, %rdi
movl $0x1f9, %ecx            # imm = 0x1F9
movq %r13, 0x10(%rsp)
movl %ebp, %r13d
movq %r11, %rbp
callq 0x22d64
movq %rbp, %r11
movl %r13d, %ebp
movq 0x10(%rsp), %r13
movl %r15d, %edx
movq 0x20(%rsp), %r9
shll $0x4, %r9d
orl $0x2, %r9d
incl %r13d
movl %r9d, (%rax,%r14,4)
movq 0x18(%rsp), %r14
testb %bl, %bl
je 0x4c2e
movslq %r13d, %rbx
testl %r13d, %r13d
je 0x4be8
movl -0x4(%rax,%rbx,4), %ecx
movl %ecx, %esi
andl $0xf, %esi
cmpl $0x1, %esi
jne 0x4be8
shll $0x4, %r11d
addl %r11d, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
jmp 0x4c2e
cmpl %edx, %r13d
jne 0x4c20
leal (,%rdx,8), %ecx
testl %edx, %edx
pushq $0x10
popq %rdx
cmovnel %ecx, %edx
movslq %edx, %rsi
leaq 0x24a12(%rip), %rdx     # 0x29618
leaq 0x24ae5(%rip), %r8      # 0x296f2
movq %rax, %rdi
movl $0x1f9, %ecx            # imm = 0x1F9
movq %r11, %r15
callq 0x22d64
movq %r15, %r11
shll $0x4, %r11d
incl %r11d
incl %r13d
movl %r11d, (%rax,%rbx,4)
movl %r13d, %ecx
sarl %ecx
testl %ecx, %ecx
jle 0x4c5a
movl %ecx, %ecx
leal -0x1(%r13), %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 0x4c3f
movq 0xd8(%rsp), %rcx
movl %r13d, (%rcx)
movq 0xe0(%rsp), %rcx
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x3390
movq 0x38(%rsp), %rdi
callq 0x3390
movq %r14, %rdi
callq 0x3390
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq