// File: readmapping[P]alfalfa/src/ksw.c
// Function: ksw_global
int ksw_global(int qlen, const uint8_t *query, int tlen, const uint8_t *target, int m, const int8_t *mat, int gapo, int gape, int w, int *n_cigar_, uint32_t **cigar_)
{
	eh_t *eh;
	int8_t *qp; // query profile
	int i, j, k, gapoe = gapo + gape, 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 = malloc(n_col * tlen);
	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 = -(gapo + gape * 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;
		int8_t *q = &qp[target[i] * qlen];
		uint8_t *zi = &z[i * n_col];
		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? -(gapo + gape * (i + 1)) : MINUS_INF;
		for (j = beg; LIKELY(j < end); ++j) {
			// This loop is organized in a similar way to ksw_extend() and ksw_sse2(), except:
			// 1) not checking h>0; 2) recording direction for backtracking
			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? 1<<2 : 0;
			e = e > h? e : h;
			p->e = e;
			f -= gape;
			d |= f > h? 2<<4 : 0; // if we want to halve the memory, use one bit only, instead of two
			f = f > h? f : h;
			zi[j - beg] = d; // z[i,j] keeps h for the current cell and e/f for the next cell
		}
		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[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:
subq $0x108, %rsp            # imm = 0x108
movq 0x130(%rsp), %rax
movq 0x128(%rsp), %rax
movl 0x120(%rsp), %eax
movl 0x118(%rsp), %eax
movl 0x110(%rsp), %eax
movl %edi, 0x104(%rsp)
movq %rsi, 0xf8(%rsp)
movl %edx, 0xf4(%rsp)
movq %rcx, 0xe8(%rsp)
movl %r8d, 0xe4(%rsp)
movq %r9, 0xd8(%rsp)
movl 0x110(%rsp), %eax
addl 0x118(%rsp), %eax
movl %eax, 0xb8(%rsp)
cmpq $0x0, 0x128(%rsp)
je 0x77108
movq 0x128(%rsp), %rax
movl $0x0, (%rax)
movl 0x104(%rsp), %eax
movl 0x120(%rsp), %ecx
shll %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x7712c
movl 0x104(%rsp), %eax
movl %eax, 0x50(%rsp)
jmp 0x7713c
movl 0x120(%rsp), %eax
shll %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
movl 0x50(%rsp), %eax
movl %eax, 0xb0(%rsp)
movl 0xb0(%rsp), %eax
imull 0xf4(%rsp), %eax
movslq %eax, %rdi
callq 0x8560
movq %rax, 0xa8(%rsp)
movl 0x104(%rsp), %eax
imull 0xe4(%rsp), %eax
movslq %eax, %rdi
callq 0x8560
movq %rax, 0xc8(%rsp)
movl 0x104(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdi
movl $0x8, %esi
callq 0x8220
movq %rax, 0xd0(%rsp)
movl $0x0, 0xc4(%rsp)
movl $0x0, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
cmpl 0xe4(%rsp), %eax
jge 0x77279
movq 0xd8(%rsp), %rax
movl 0xbc(%rsp), %ecx
imull 0xe4(%rsp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0xa0(%rsp)
movl $0x0, 0xc0(%rsp)
movl 0xc0(%rsp), %eax
cmpl 0x104(%rsp), %eax
jge 0x77261
movq 0xa0(%rsp), %rax
movq 0xf8(%rsp), %rcx
movslq 0xc0(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
movb (%rax,%rcx), %dl
movq 0xc8(%rsp), %rax
movl 0xc4(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0xc4(%rsp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x771fe
jmp 0x77263
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x771ba
movq 0xd0(%rsp), %rax
movl $0x0, (%rax)
movq 0xd0(%rsp), %rax
movl $0xc0000000, 0x4(%rax)  # imm = 0xC0000000
movl $0x1, 0xc0(%rsp)
movl 0xc0(%rsp), %ecx
xorl %eax, %eax
cmpl 0x104(%rsp), %ecx
movb %al, 0x4f(%rsp)
jg 0x772cc
movl 0xc0(%rsp), %eax
cmpl 0x120(%rsp), %eax
setle %al
movb %al, 0x4f(%rsp)
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0x772d6
jmp 0x77333
movl 0x110(%rsp), %eax
movl 0x118(%rsp), %ecx
imull 0xc0(%rsp), %ecx
addl %ecx, %eax
xorl %edx, %edx
subl %eax, %edx
movq 0xd0(%rsp), %rax
movslq 0xc0(%rsp), %rcx
movl %edx, (%rax,%rcx,8)
movq 0xd0(%rsp), %rax
movslq 0xc0(%rsp), %rcx
movl $0xc0000000, 0x4(%rax,%rcx,8) # imm = 0xC0000000
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x772a1
jmp 0x77335
movl 0xc0(%rsp), %eax
cmpl 0x104(%rsp), %eax
jg 0x77387
movq 0xd0(%rsp), %rax
movslq 0xc0(%rsp), %rcx
movl $0xc0000000, 0x4(%rax,%rcx,8) # imm = 0xC0000000
movq 0xd0(%rsp), %rax
movslq 0xc0(%rsp), %rcx
movl $0xc0000000, (%rax,%rcx,8) # imm = 0xC0000000
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x77335
movl $0x0, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
cmpl 0xf4(%rsp), %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x77759
movl $0xc0000000, 0x9c(%rsp) # imm = 0xC0000000
movq 0xc8(%rsp), %rax
movq 0xe8(%rsp), %rcx
movslq 0xc4(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
imull 0x104(%rsp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x88(%rsp)
movq 0xa8(%rsp), %rax
movl 0xc4(%rsp), %ecx
imull 0xb0(%rsp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x80(%rsp)
movl 0xc4(%rsp), %eax
cmpl 0x120(%rsp), %eax
jle 0x7743a
movl 0xc4(%rsp), %eax
subl 0x120(%rsp), %eax
movl %eax, 0x48(%rsp)
jmp 0x77442
xorl %eax, %eax
movl %eax, 0x48(%rsp)
jmp 0x77442
movl 0x48(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0xc4(%rsp), %eax
addl 0x120(%rsp), %eax
addl $0x1, %eax
cmpl 0x104(%rsp), %eax
jge 0x7747e
movl 0xc4(%rsp), %eax
addl 0x120(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x77489
movl 0x104(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x90(%rsp)
cmpl $0x0, 0x94(%rsp)
jne 0x774c5
movl 0x110(%rsp), %ecx
movl 0x118(%rsp), %eax
movl 0xc4(%rsp), %edx
addl $0x1, %edx
imull %edx, %eax
addl %eax, %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, 0x40(%rsp)
jmp 0x774d0
movl $0xc0000000, %eax       # imm = 0xC0000000
movl %eax, 0x40(%rsp)
jmp 0x774d0
movl 0x40(%rsp), %eax
movl %eax, 0x98(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0xc0(%rsp)
movl 0xc0(%rsp), %eax
cmpl 0x90(%rsp), %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x77711
movq 0xd0(%rsp), %rax
movslq 0xc0(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x74(%rsp)
movq 0x78(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x70(%rsp)
movl 0x98(%rsp), %ecx
movq 0x78(%rsp), %rax
movl %ecx, (%rax)
movq 0x88(%rsp), %rax
movslq 0xc0(%rsp), %rcx
movsbl (%rax,%rcx), %eax
addl 0x74(%rsp), %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %edx
movl 0x70(%rsp), %esi
movl $0x1, %eax
xorl %ecx, %ecx
cmpl %esi, %edx
cmovgel %ecx, %eax
movb %al, 0x6f(%rsp)
movl 0x74(%rsp), %eax
cmpl 0x70(%rsp), %eax
jl 0x77594
movl 0x74(%rsp), %eax
movl %eax, 0x3c(%rsp)
jmp 0x7759c
movl 0x70(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jl 0x775bc
movzbl 0x6f(%rsp), %eax
movl %eax, 0x38(%rsp)
jmp 0x775c7
movl $0x2, %eax
movl %eax, 0x38(%rsp)
jmp 0x775c7
movl 0x38(%rsp), %eax
movb %al, 0x6f(%rsp)
movl 0x74(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jl 0x775e6
movl 0x74(%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x775f1
movl 0x9c(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x98(%rsp)
movl 0xb8(%rsp), %ecx
movl 0x74(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x74(%rsp)
movl 0x118(%rsp), %ecx
movl 0x70(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x70(%rsp)
movl 0x70(%rsp), %edx
movl 0x74(%rsp), %esi
xorl %ecx, %ecx
movl $0x4, %eax
cmpl %esi, %edx
cmovgl %eax, %ecx
movzbl 0x6f(%rsp), %eax
orl %ecx, %eax
movb %al, 0x6f(%rsp)
movl 0x70(%rsp), %eax
cmpl 0x74(%rsp), %eax
jle 0x77659
movl 0x70(%rsp), %eax
movl %eax, 0x30(%rsp)
jmp 0x77661
movl 0x74(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0x70(%rsp)
movl 0x70(%rsp), %ecx
movq 0x78(%rsp), %rax
movl %ecx, 0x4(%rax)
movl 0x118(%rsp), %ecx
movl 0x9c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %edx
movl 0x74(%rsp), %esi
xorl %ecx, %ecx
movl $0x20, %eax
cmpl %esi, %edx
cmovgl %eax, %ecx
movzbl 0x6f(%rsp), %eax
orl %ecx, %eax
movb %al, 0x6f(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0x74(%rsp), %eax
jle 0x776c8
movl 0x9c(%rsp), %eax
movl %eax, 0x2c(%rsp)
jmp 0x776d0
movl 0x74(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x9c(%rsp)
movb 0x6f(%rsp), %dl
movq 0x80(%rsp), %rax
movl 0xc0(%rsp), %ecx
subl 0x94(%rsp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x774e9
movl 0x98(%rsp), %edx
movq 0xd0(%rsp), %rax
movslq 0x90(%rsp), %rcx
movl %edx, (%rax,%rcx,8)
movq 0xd0(%rsp), %rax
movslq 0x90(%rsp), %rcx
movl $0xc0000000, 0x4(%rax,%rcx,8) # imm = 0xC0000000
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x77392
movq 0xd0(%rsp), %rax
movslq 0x104(%rsp), %rcx
movl (%rax,%rcx,8), %eax
movl %eax, 0xb4(%rsp)
cmpq $0x0, 0x128(%rsp)
je 0x77a8e
cmpq $0x0, 0x130(%rsp)
je 0x77a8e
movl $0x0, 0x68(%rsp)
movl $0x0, 0x64(%rsp)
movl $0x0, 0x60(%rsp)
movq $0x0, 0x58(%rsp)
movl 0xf4(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
addl 0x120(%rsp), %eax
addl $0x1, %eax
cmpl 0x104(%rsp), %eax
jge 0x777f4
movl 0xc4(%rsp), %eax
addl 0x120(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x777ff
movl 0x104(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xbc(%rsp)
xorl %eax, %eax
cmpl $0x0, 0xc4(%rsp)
movb %al, 0x27(%rsp)
jl 0x7782c
cmpl $0x0, 0xbc(%rsp)
setge %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x77839
jmp 0x7797f
movq 0xa8(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0xc4(%rsp), %eax
imull 0xb0(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0xc4(%rsp), %eax
cmpl 0x120(%rsp), %eax
jle 0x77888
movl 0xc4(%rsp), %eax
subl 0x120(%rsp), %eax
movl %eax, 0xc(%rsp)
jmp 0x77890
xorl %eax, %eax
movl %eax, 0xc(%rsp)
jmp 0x77890
movq 0x10(%rsp), %rax
movl 0x1c(%rsp), %ecx
movl 0x20(%rsp), %edx
movl 0xc(%rsp), %esi
subl %esi, %edx
addl %edx, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl 0x60(%rsp), %ecx
shll %ecx
sarl %cl, %eax
andl $0x3, %eax
movl %eax, 0x60(%rsp)
cmpl $0x0, 0x60(%rsp)
jne 0x77907
movq 0x58(%rsp), %rdx
leaq 0x68(%rsp), %rdi
leaq 0x64(%rsp), %rsi
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x77ad0
movq %rax, 0x58(%rsp)
movl 0xc4(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xc4(%rsp)
movl 0xbc(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x7797a
cmpl $0x1, 0x60(%rsp)
jne 0x77945
movq 0x58(%rsp), %rdx
leaq 0x68(%rsp), %rdi
leaq 0x64(%rsp), %rsi
movl $0x2, %ecx
movl $0x1, %r8d
callq 0x77ad0
movq %rax, 0x58(%rsp)
movl 0xc4(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x77978
movq 0x58(%rsp), %rdx
leaq 0x68(%rsp), %rdi
leaq 0x64(%rsp), %rsi
movl $0x1, %r8d
movl %r8d, %ecx
callq 0x77ad0
movq %rax, 0x58(%rsp)
movl 0xbc(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x7797a
jmp 0x7780d
cmpl $0x0, 0xc4(%rsp)
jl 0x779b3
movq 0x58(%rsp), %rdx
movl 0xc4(%rsp), %r8d
addl $0x1, %r8d
leaq 0x68(%rsp), %rdi
leaq 0x64(%rsp), %rsi
movl $0x2, %ecx
callq 0x77ad0
movq %rax, 0x58(%rsp)
cmpl $0x0, 0xbc(%rsp)
jl 0x779e7
movq 0x58(%rsp), %rdx
movl 0xbc(%rsp), %r8d
addl $0x1, %r8d
leaq 0x68(%rsp), %rdi
leaq 0x64(%rsp), %rsi
movl $0x1, %ecx
callq 0x77ad0
movq %rax, 0x58(%rsp)
movl $0x0, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
movl 0x68(%rsp), %ecx
sarl %ecx
cmpl %ecx, %eax
jge 0x77a70
movq 0x58(%rsp), %rax
movslq 0xc4(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x54(%rsp)
movq 0x58(%rsp), %rax
movl 0x68(%rsp), %ecx
subl $0x1, %ecx
subl 0xc4(%rsp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq 0x58(%rsp), %rax
movslq 0xc4(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x54(%rsp), %edx
movq 0x58(%rsp), %rax
movl 0x68(%rsp), %ecx
subl $0x1, %ecx
subl 0xc4(%rsp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x779f2
movl 0x68(%rsp), %ecx
movq 0x128(%rsp), %rax
movl %ecx, (%rax)
movq 0x58(%rsp), %rcx
movq 0x130(%rsp), %rax
movq %rcx, (%rax)
movq 0xd0(%rsp), %rdi
callq 0x8470
movq 0xc8(%rsp), %rdi
callq 0x8470
movq 0xa8(%rsp), %rdi
callq 0x8470
movl 0xb4(%rsp), %eax
addq $0x108, %rsp            # imm = 0x108
retq
nopw %cs:(%rax,%rax)