// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %r9, %rbx
movl %r8d, %ebp
movq %rcx, 0x38(%rsp)
movq %rsi, %r12
movl %edi, %r14d
movq 0xa8(%rsp), %rax
movl 0xa0(%rsp), %ecx
testq %rax, %rax
je 0x8004
andl $0x0, (%rax)
leal 0x1(,%rcx,2), %eax
cmpl %r14d, %eax
cmovgel %r14d, %eax
movl %eax, 0x24(%rsp)
movq %rdx, 0x8(%rsp)
imull %edx, %eax
movslq %eax, %rdi
callq 0x7430
movq %rax, 0x10(%rsp)
movl %ebp, %eax
imull %r14d, %eax
movslq %eax, %rdi
callq 0x7430
movq %rax, 0x28(%rsp)
leal 0x1(%r14), %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x71a0
movq %rax, %r15
testl %ebp, %ebp
jle 0x8097
movl %ebp, %eax
movl %r14d, %ecx
xorl %edx, %edx
xorl %esi, %esi
testl %r14d, %r14d
jle 0x808f
movq %rdx, %rdi
imulq %rax, %rdi
addq %rbx, %rdi
movslq %esi, %r8
addq 0x28(%rsp), %r8
xorl %r9d, %r9d
movzbl (%r12,%r9), %r10d
movb (%rdi,%r10), %r10b
movb %r10b, (%r8,%r9)
incq %r9
cmpq %r9, %rcx
jne 0x8077
addl %r9d, %esi
incq %rdx
cmpq %rax, %rdx
jne 0x805d
movl 0x98(%rsp), %eax
movl 0x90(%rsp), %r10d
movl $0xc0000000, 0x4(%r15)  # imm = 0xC0000000
movl 0xa0(%rsp), %r9d
cmpl %r9d, %r14d
movl %r9d, %ecx
cmovll %r14d, %ecx
testl %ecx, %ecx
jle 0x80ec
leal 0x1(%rcx), %edx
movl %ecx, %esi
leal (%rax,%r10), %edi
negl %edi
xorl %r8d, %r8d
movl %edi, 0x8(%r15,%r8,8)
movl $0xc0000000, 0xc(%r15,%r8,8) # imm = 0xC0000000
subl %eax, %edi
incq %r8
cmpq %r8, %rsi
jne 0x80d2
jmp 0x80ef
pushq $0x1
popq %rdx
cmpl %r14d, %edx
jg 0x811c
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 %r14d, %edx
jl 0x810d
cmpl $0x0, 0x8(%rsp)
jle 0x8281
leal 0x1(%r9), %ecx
movq %rcx, 0x18(%rsp)
leal (%rax,%r10), %esi
movslq 0x24(%rsp), %rcx
movq %rcx, 0x50(%rsp)
movslq %r9d, %rcx
movq %rcx, 0x48(%rsp)
movl 0x8(%rsp), %ecx
movq %rcx, 0x40(%rsp)
pushq $0x2
popq %r12
movq 0x10(%rsp), %r8
xorl %ecx, %ecx
movq %r14, 0x30(%rsp)
movl %ecx, %edx
subl %r9d, %edx
cmpq 0x48(%rsp), %rcx
movl $0x0, %edi
cmovlel %edi, %edx
movq 0x18(%rsp), %rdi
leal (%rdi,%rcx), %ebx
cmpl %r14d, %ebx
cmovgel %r14d, %ebx
movl %ecx, %ebp
notl %ebp
imull %eax, %ebp
subl %r10d, %ebp
testl %edx, %edx
movl $0xc0000000, %edi       # imm = 0xC0000000
cmovnel %edi, %ebp
cmpl %ebx, %edx
jge 0x827c
movq 0x38(%rsp), %rdi
movzbl (%rdi,%rcx), %r10d
imull %r14d, %r10d
movslq %r10d, %r10
addq 0x28(%rsp), %r10
cmpl %ecx, %r9d
movl %ecx, %r11d
cmovgl %r9d, %r11d
subl %r9d, %r11d
movslq %r11d, %r11
movslq %edx, %rdx
movslq %ebx, %rbx
movq %r8, (%rsp)
movq %r8, %r14
subq %rdx, %r14
movl $0xc0000000, %edx       # imm = 0xC0000000
movl (%r15,%r11,8), %edi
movl 0x4(%r15,%r11,8), %r8d
movl %ebp, (%r15,%r11,8)
movsbl (%r10,%r11), %ebp
addl %edi, %ebp
xorl %edi, %edi
cmpl %r8d, %ebp
setl %dil
cmovlel %r8d, %ebp
cmpl %edx, %ebp
cmovlel %edx, %ebp
cmovll %r12d, %edi
movl %ebp, %r9d
subl %esi, %r9d
subl %eax, %r8d
cmpl %r9d, %r8d
setg %r13b
cmovlel %r9d, %r8d
shlb $0x2, %r13b
orb %dil, %r13b
movl %r8d, 0x4(%r15,%r11,8)
subl %eax, %edx
cmpl %r9d, %edx
setg %dil
cmovlel %r9d, %edx
shlb $0x5, %dil
orb %r13b, %dil
movb %dil, (%r14,%r11)
incq %r11
cmpq %rbx, %r11
jl 0x81d7
movl 0xa0(%rsp), %r9d
movq 0x30(%rsp), %r14
movl 0x90(%rsp), %r10d
movq (%rsp), %r8
movl %ebp, (%r15,%rbx,8)
movl $0xc0000000, 0x4(%r15,%rbx,8) # imm = 0xC0000000
incq %rcx
addq 0x50(%rsp), %r8
cmpq 0x40(%rsp), %rcx
jne 0x815f
jmp 0x8281
movslq %ebx, %rbx
jmp 0x825a
movq 0xa8(%rsp), %rbx
testq %rbx, %rbx
sete %al
movslq %r14d, %rcx
movl (%r15,%rcx,8), %ebp
movq 0xb0(%rsp), %r13
testq %r13, %r13
sete %cl
orb %al, %cl
jne 0x8624
movq %r14, %rsi
movq 0x8(%rsp), %r14
testl %r14d, %r14d
setle %al
leal (%r9,%r14), %ecx
cmpl %esi, %ecx
cmovll %ecx, %esi
testl %esi, %esi
setg %r12b
setle %cl
orb %al, %cl
jne 0x84f6
movl %ebp, 0x18(%rsp)
decl %esi
decl %r14d
xorl %ebx, %ebx
xorl %eax, %eax
xorl %ebp, %ebp
movl %esi, %r11d
xorl %r8d, %r8d
xorl %r10d, %r10d
movq 0x10(%rsp), %rsi
movq %r14, %rdi
movl %edi, %ecx
imull 0x24(%rsp), %ecx
movl %r9d, %edx
subl %edi, %edx
cmovgel %ebx, %edx
addl %r11d, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movzbl (%rsi,%rcx), %edx
addl %ebp, %ebp
movl %ebp, %ecx
shrl %cl, %edx
movl %edx, %ebp
andl $0x3, %ebp
movslq %r10d, %r13
cmpl $0x1, %ebp
je 0x8395
testl %ebp, %ebp
jne 0x83b3
testl %r10d, %r10d
je 0x833d
movl -0x4(%rax,%r13,4), %ecx
testb $0xf, %cl
je 0x8481
cmpl %r8d, %r10d
jne 0x8385
addl %r8d, %r8d
testl %r10d, %r10d
pushq $0x4
popq %rcx
cmovel %ecx, %r8d
movq %r8, (%rsp)
leal (,%r8,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
movq %r10, %r12
movq %r11, %rbx
callq 0x7590
movq %rbx, %r11
xorl %ebx, %ebx
movq (%rsp), %r8
movq %r12, %r10
movl 0xa0(%rsp), %r9d
movq 0x10(%rsp), %rsi
incl %r10d
movl $0x10, (%rax,%r13,4)
jmp 0x8489
testl %r10d, %r10d
je 0x83d4
movl -0x4(%rax,%r13,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x2, %edx
jne 0x83d4
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%r13,4)
jmp 0x8427
testl %r10d, %r10d
je 0x842c
movl -0x4(%rax,%r13,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x1, %edx
jne 0x842c
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%r13,4)
jmp 0x848c
cmpl %r8d, %r10d
jne 0x841c
addl %r8d, %r8d
testl %r10d, %r10d
pushq $0x4
popq %rcx
cmovel %ecx, %r8d
movq %r8, (%rsp)
leal (,%r8,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
movq %r10, %r12
movq %r11, %rbx
callq 0x7590
movq %rbx, %r11
xorl %ebx, %ebx
movq (%rsp), %r8
movq %r12, %r10
movl 0xa0(%rsp), %r9d
movq 0x10(%rsp), %rsi
incl %r10d
movl $0x12, (%rax,%r13,4)
decl %r14d
jmp 0x848f
cmpl %r8d, %r10d
jne 0x8474
addl %r8d, %r8d
testl %r10d, %r10d
pushq $0x4
popq %rcx
cmovel %ecx, %r8d
movq %r8, (%rsp)
leal (,%r8,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
movq %r10, %r12
movq %r11, %rbx
callq 0x7590
movq %rbx, %r11
xorl %ebx, %ebx
movq (%rsp), %r8
movq %r12, %r10
movl 0xa0(%rsp), %r9d
movq 0x10(%rsp), %rsi
incl %r10d
movl $0x11, (%rax,%r13,4)
jmp 0x848c
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%r13,4)
decl %r14d
decl %r11d
testl %r11d, %r11d
setns %r12b
testl %r14d, %r14d
js 0x84a4
testl %r11d, %r11d
jns 0x82f1
leal 0x1(%r11), %edi
testl %r14d, %r14d
js 0x8515
incl %r14d
movq %r14, 0x8(%rsp)
testl %r10d, %r10d
movq 0xb0(%rsp), %r13
movl 0x18(%rsp), %ebp
je 0x852c
movslq %r10d, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x2, %esi
movq %rdi, %r14
jne 0x8536
movq 0x8(%rsp), %rsi
shll $0x4, %esi
addl %esi, %edx
movl %edx, -0x4(%rax,%rcx,4)
testl %r11d, %r11d
jns 0x858a
jmp 0x85e9
xorl %r10d, %r10d
movb $0x1, %cl
movl $0x0, %r8d
movl $0x0, %eax
movl $0x0, %edx
testl %r14d, %r14d
movq %rsi, %r14
jg 0x8538
jmp 0x8527
movl %r8d, %edx
movq 0xb0(%rsp), %r13
movq %rdi, %r14
movl 0x18(%rsp), %ebp
movl %edx, %r8d
jmp 0x8580
xorl %r10d, %r10d
movb $0x1, %cl
movq %rdi, %r14
jmp 0x8538
xorl %ecx, %ecx
cmpl %r8d, %r10d
jne 0x856c
movl %ebp, 0x18(%rsp)
leal (%r10,%r10), %ebp
testb %cl, %cl
pushq $0x4
popq %rcx
cmovnel %ecx, %ebp
leal (,%rbp,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
movq %r10, %rbx
callq 0x7590
movq %rbp, %r8
movl 0x18(%rsp), %ebp
movq %rbx, %r10
movq 0x8(%rsp), %rdx
shll $0x4, %edx
orl $0x2, %edx
movslq %r10d, %rcx
incl %r10d
movl %edx, (%rax,%rcx,4)
testb %r12b, %r12b
je 0x85e9
testl %r10d, %r10d
je 0x85ac
movslq %r10d, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x1, %esi
jne 0x85a8
shll $0x4, %r14d
addl %r14d, %edx
movl %edx, -0x4(%rax,%rcx,4)
jmp 0x85e9
xorl %ecx, %ecx
jmp 0x85b1
xorl %r10d, %r10d
movb $0x1, %cl
cmpl %r8d, %r10d
jne 0x85d8
leal (,%r10,8), %edx
movslq %edx, %rdx
testb %cl, %cl
pushq $0x10
popq %rsi
cmoveq %rdx, %rsi
movq %rax, %rdi
movq %r10, %rbx
callq 0x7590
movq %rbx, %r10
shll $0x4, %r14d
incl %r14d
movslq %r10d, %rcx
incl %r10d
movl %r14d, (%rax,%rcx,4)
movl %r10d, %ecx
sarl %ecx
testl %ecx, %ecx
jle 0x8615
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 0x85fa
movq 0xa8(%rsp), %rcx
movl %r10d, (%rcx)
movq %rax, (%r13)
movq %r15, %rdi
callq 0x7380
movq 0x28(%rsp), %rdi
callq 0x7380
movq 0x10(%rsp), %rdi
callq 0x7380
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq