// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %r13
movl %r8d, %ebp
movq %rcx, 0x60(%rsp)
movq %rsi, %r12
movq 0xb8(%rsp), %rax
testq %rax, %rax
je 0x31221
andl $0x0, (%rax)
movl 0xa8(%rsp), %r14d
movl 0xb0(%rsp), %eax
leal 0x1(,%rax,2), %eax
cmpl %edi, %eax
cmovgel %edi, %eax
movl %eax, 0xc(%rsp)
movq %rdx, 0x38(%rsp)
imull %edx, %eax
movq %rdi, %rbx
movslq %eax, %rdi
callq 0x7430
movq %rax, 0x18(%rsp)
movl %ebp, %eax
imull %ebx, %eax
movslq %eax, %rdi
callq 0x7430
movq %rax, 0x28(%rsp)
leal 0x1(%rbx), %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x71b0
movq %rax, %r15
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %ecx
movq %rbx, 0x10(%rsp)
cmovgl %ebx, %ecx
movslq %ebp, %rdx
testl %edx, %edx
movl $0x0, %esi
cmovgl %edx, %esi
xorl %edi, %edi
movq 0x28(%rsp), %r11
cmpq %rsi, %rax
je 0x312d4
movq %rax, %r8
imulq %rdx, %r8
addq %r13, %r8
movslq %edi, %rdi
xorl %r9d, %r9d
cmpq %r9, %rcx
je 0x312cf
movzbl (%r12,%r9), %r10d
movb (%r8,%r10), %r10b
movb %r10b, (%r11,%rdi)
incq %rdi
incq %r9
jmp 0x312b5
incq %rax
jmp 0x312a0
movl $0xc0000000, 0x4(%r15)  # imm = 0xC0000000
movl 0xb0(%rsp), %edx
movq 0x10(%rsp), %rax
cmpl %edx, %eax
cmovll %eax, %edx
xorl %esi, %esi
testl %edx, %edx
cmovlel %esi, %edx
leal 0x1(%rdx), %ecx
movl 0xa0(%rsp), %eax
addl %r14d, %eax
movl %eax, %edi
negl %edi
movq 0x38(%rsp), %r8
cmpq %rsi, %rdx
je 0x31325
movl %edi, 0x8(%r15,%rsi,8)
movl $0xc0000000, 0xc(%r15,%rsi,8) # imm = 0xC0000000
subl %r14d, %edi
incq %rsi
jmp 0x3130a
movq 0x10(%rsp), %rdi
movslq %edi, %rsi
movabsq $-0x3fffffff40000000, %rdx # imm = 0xC0000000C0000000
cmpq %rsi, %rcx
jg 0x31345
movq %rdx, (%r15,%rcx,8)
incq %rcx
jmp 0x31337
movq %rsi, 0x40(%rsp)
movl 0xb0(%rsp), %ecx
leal 0x1(%rcx), %edx
movq %rdx, 0x58(%rsp)
movslq %ecx, %rcx
movq %rcx, 0x50(%rsp)
testl %r8d, %r8d
movl $0x0, %ecx
cmovgl %r8d, %ecx
movq %rcx, 0x20(%rsp)
movslq 0xc(%rsp), %rcx
movq %rcx, 0x48(%rsp)
pushq $0x2
popq %r10
movq 0x18(%rsp), %r11
xorl %r12d, %r12d
cmpq 0x20(%rsp), %r12
je 0x3149c
movl 0xb0(%rsp), %ecx
movl %ecx, %esi
subl %r12d, %esi
movl %r12d, %edx
cmovgl %ecx, %edx
negl %esi
subl %ecx, %edx
movq 0x60(%rsp), %rcx
movzbl (%rcx,%r12), %ecx
imull %edi, %ecx
movslq %ecx, %rcx
addq 0x28(%rsp), %rcx
cmpq 0x50(%rsp), %r12
movl $0x0, %r8d
cmovlel %r8d, %esi
movq 0x58(%rsp), %r8
addl %r12d, %r8d
cmpl %edi, %r8d
cmovgel %edi, %r8d
movl %r12d, %r9d
notl %r9d
imull %r14d, %r9d
movl 0xa0(%rsp), %edi
subl %edi, %r9d
testl %esi, %esi
movl $0xc0000000, %edi       # imm = 0xC0000000
cmovnel %edi, %r9d
movslq %esi, %rsi
movq %r11, 0x30(%rsp)
subq %rsi, %r11
movslq %edx, %rsi
movslq %r8d, %rdx
cmpq %rdx, %rsi
jge 0x31478
movl (%r15,%rsi,8), %r8d
movl 0x4(%r15,%rsi,8), %r13d
movl %r9d, (%r15,%rsi,8)
movsbl (%rcx,%rsi), %r9d
addl %r8d, %r9d
xorl %r8d, %r8d
cmpl %r13d, %r9d
setl %r8b
cmovlel %r13d, %r9d
cmpl %edi, %r9d
cmovlel %edi, %r9d
cmovll %r10d, %r8d
movl %r9d, %ebp
subl %eax, %ebp
subl %r14d, %r13d
cmpl %ebp, %r13d
setg %bl
cmovlel %ebp, %r13d
shlb $0x2, %bl
orb %r8b, %bl
movl %r13d, 0x4(%r15,%rsi,8)
subl %r14d, %edi
cmpl %ebp, %edi
setg %r8b
cmovlel %ebp, %edi
shlb $0x5, %r8b
orb %bl, %r8b
movb %r8b, (%r11,%rsi)
incq %rsi
jmp 0x3140c
movl %r9d, (%r15,%rdx,8)
movl $0xc0000000, 0x4(%r15,%rdx,8) # imm = 0xC0000000
incq %r12
movq 0x30(%rsp), %r11
addq 0x48(%rsp), %r11
movq 0x10(%rsp), %rdi
jmp 0x31388
cmpq $0x0, 0xb8(%rsp)
sete %al
movq 0x40(%rsp), %rcx
movl (%r15,%rcx,8), %ebx
cmpq $0x0, 0xc0(%rsp)
sete %cl
orb %al, %cl
jne 0x31667
movl %ebx, 0x20(%rsp)
leaq 0x4(%rsp), %rax
andl $0x0, (%rax)
leaq 0x8(%rsp), %rax
andl $0x0, (%rax)
movq 0x38(%rsp), %r9
movl 0xb0(%rsp), %eax
addl %r9d, %eax
decl %r9d
cmpl %edi, %eax
cmovll %eax, %edi
leal -0x1(%rdi), %ecx
pushq $0x1
popq %rbp
movl 0xc(%rsp), %ebx
subl %ebx, %ebp
decl %ebx
xorl %r12d, %r12d
xorl %eax, %eax
movl %ebx, 0xc(%rsp)
leal -0x1(%rdi), %edx
movl %edx, 0x10(%rsp)
imull %r9d, %ebx
movl %ecx, 0x30(%rsp)
addl %ecx, %ebx
movl 0xb0(%rsp), %ecx
movq %rcx, %rdx
cmpl %r9d, %edx
movl %r9d, %ecx
cmovll %edx, %ecx
testl %r9d, %r9d
js 0x315d6
testl %edi, %edi
jle 0x315d6
addl %ebx, %ecx
movslq %ecx, %rcx
movq 0x18(%rsp), %rdx
movzbl (%rdx,%rcx), %edx
addl %r12d, %r12d
movl %r12d, %ecx
shrl %cl, %edx
movl %edx, %r12d
andl $0x3, %r12d
cmpl $0x1, %r12d
jne 0x3158b
movq %rdi, %r14
leaq 0x4(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %rax, %rdx
pushq $0x2
popq %rcx
pushq $0x1
popq %r8
movq %r9, %r13
callq 0x31694
movq %r14, %rdi
movq %r13, %r9
decl %r9d
addl %ebp, %ebx
jmp 0x31518
movq %r9, %rbx
leaq 0x4(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %rax, %rdx
testl %r12d, %r12d
jne 0x315b3
xorl %ecx, %ecx
pushq $0x1
popq %r8
callq 0x31694
movq %rbx, %r9
decl %r9d
jmp 0x315c1
pushq $0x1
popq %rcx
movl %ecx, %r8d
callq 0x31694
movq %rbx, %r9
movl 0x30(%rsp), %ecx
decl %ecx
movl 0x10(%rsp), %edx
movl %edx, %edi
movl 0xc(%rsp), %ebx
jmp 0x31507
testl %r9d, %r9d
js 0x315fc
incl %r9d
movq %rdi, %rbx
leaq 0x4(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x2
popq %rcx
movq %rax, %rdx
movl %r9d, %r8d
callq 0x31694
movq %rbx, %rdi
movq 0xb8(%rsp), %rbx
testl %edi, %edi
jle 0x31620
movq %rdi, %r8
leaq 0x4(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rcx
movq %rax, %rdx
callq 0x31694
pushq $-0x1
popq %rcx
xorl %edx, %edx
movl 0x4(%rsp), %esi
movl %esi, %edi
sarl %edi
movslq %edi, %rdi
cmpq %rdi, %rdx
jge 0x31656
movl (%rax,%rdx,4), %edi
addl %ecx, %esi
movslq %esi, %rsi
movl (%rax,%rsi,4), %esi
movl %esi, (%rax,%rdx,4)
movl 0x4(%rsp), %esi
addl %ecx, %esi
movslq %esi, %rsi
movl %edi, (%rax,%rsi,4)
incq %rdx
decl %ecx
jmp 0x31625
movl %esi, (%rbx)
movq 0xc0(%rsp), %rcx
movq %rax, (%rcx)
movl 0x20(%rsp), %ebx
movq %r15, %rdi
callq 0x7360
movq 0x28(%rsp), %rdi
callq 0x7360
movq 0x18(%rsp), %rdi
callq 0x7360
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq