// File: /workspace/github_cpp_makefile_star2_O1/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, %rbp
movl %r8d, %r15d
movq %rcx, 0x30(%rsp)
movl %edx, %r14d
movq %rsi, %r13
movl %edi, %ecx
movq 0xa8(%rsp), %rax
movl 0xa0(%rsp), %r12d
testq %rax, %rax
je 0x8018
andl $0x0, (%rax)
leal 0x1(,%r12,2), %eax
cmpl %ecx, %eax
cmovgel %ecx, %eax
movl %eax, 0x4(%rsp)
imull %r14d, %eax
movslq %eax, %rdi
movq %rcx, 0x18(%rsp)
callq 0x7430
movq %rax, %rbx
movl %r15d, %eax
imull 0x18(%rsp), %eax
movslq %eax, %rdi
callq 0x7430
movq %rax, 0x28(%rsp)
movq 0x18(%rsp), %rax
incl %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x71a0
movq 0x18(%rsp), %r11
movl %r15d, %ecx
movq %rax, %r15
testl %ecx, %ecx
jle 0x80b7
movl %ecx, %eax
movl %r11d, %ecx
xorl %edx, %edx
xorl %esi, %esi
testl %r11d, %r11d
jle 0x80af
movq %rdx, %rdi
imulq %rax, %rdi
addq %rbp, %rdi
movslq %esi, %r8
addq 0x28(%rsp), %r8
xorl %r9d, %r9d
movzbl (%r13,%r9), %r10d
movb (%rdi,%r10), %r10b
movb %r10b, (%r8,%r9)
incq %r9
cmpq %r9, %rcx
jne 0x8096
addl %r9d, %esi
incq %rdx
cmpq %rax, %rdx
jne 0x807c
movl 0x98(%rsp), %eax
movl 0x90(%rsp), %edi
movabsq $-0x4000000000000000, %rcx # imm = 0xC000000000000000
movq %rcx, (%r15)
cmpl %r12d, %r11d
movl %r12d, %edx
cmovll %r11d, %edx
testl %edx, %edx
jle 0x8103
incl %edx
leal (%rax,%rdi), %esi
negl %esi
pushq $0x1
popq %rcx
movl %esi, (%r15,%rcx,8)
movl $0xc0000000, 0x4(%r15,%rcx,8) # imm = 0xC0000000
incq %rcx
subl %eax, %esi
cmpq %rcx, %rdx
jne 0x80ea
jmp 0x8106
pushq $0x1
popq %rcx
movq %rbx, 0x10(%rsp)
cmpl %r11d, %ecx
jg 0x8135
movl %ecx, %edx
decl %ecx
leaq (%r15,%rdx,8), %rdx
addq $0x4, %rdx
movabsq $-0x3fffffff40000000, %rsi # imm = 0xC0000000C0000000
movq %rsi, -0x4(%rdx)
incl %ecx
addq $0x8, %rdx
cmpl %r11d, %ecx
jl 0x8126
movq %r14, 0x8(%rsp)
testl %r14d, %r14d
jle 0x8292
leal 0x1(%r12), %ecx
movq %rcx, 0x50(%rsp)
leal (%rax,%rdi), %esi
movslq 0x4(%rsp), %rcx
movq %rcx, 0x48(%rsp)
movslq %r12d, %rcx
movq %rcx, 0x40(%rsp)
movl 0x8(%rsp), %ecx
movq %rcx, 0x38(%rsp)
pushq $0x2
popq %r13
movq 0x10(%rsp), %r14
xorl %ecx, %ecx
movl %ecx, %edx
subl %r12d, %edx
cmpq 0x40(%rsp), %rcx
movl $0x0, %r8d
cmovlel %r8d, %edx
movq 0x50(%rsp), %r8
leal (%r8,%rcx), %r10d
cmpl %r11d, %r10d
cmovgel %r11d, %r10d
movl %ecx, %ebp
notl %ebp
imull %eax, %ebp
subl %edi, %ebp
testl %edx, %edx
movl $0xc0000000, %edi       # imm = 0xC0000000
cmovnel %edi, %ebp
movslq %r10d, %rbx
cmpl %r10d, %edx
movq %r14, 0x20(%rsp)
jge 0x8259
movq 0x30(%rsp), %rdi
movzbl (%rdi,%rcx), %r10d
imull %r11d, %r10d
movslq %r10d, %r10
addq 0x28(%rsp), %r10
cmpl %ecx, %r12d
movl %ecx, %r11d
cmovgl %r12d, %r11d
subl %r12d, %r11d
movslq %r11d, %r11
movslq %edx, %rdx
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 %r13d, %edi
movl %ebp, %r9d
subl %esi, %r9d
subl %eax, %r8d
cmpl %r9d, %r8d
setg %r12b
cmovlel %r9d, %r8d
shlb $0x2, %r12b
orb %dil, %r12b
movl %r8d, 0x4(%r15,%r11,8)
subl %eax, %edx
cmpl %r9d, %edx
setg %dil
cmovlel %r9d, %edx
shlb $0x5, %dil
orb %r12b, %dil
movb %dil, (%r14,%r11)
incq %r11
cmpq %rbx, %r11
jl 0x81ef
movl %ebp, (%r15,%rbx,8)
movl $0xc0000000, 0x4(%r15,%rbx,8) # imm = 0xC0000000
incq %rcx
movq 0x20(%rsp), %r14
addq 0x48(%rsp), %r14
cmpq 0x38(%rsp), %rcx
movq 0x18(%rsp), %r11
movl 0xa0(%rsp), %r12d
movl 0x90(%rsp), %edi
jne 0x8176
movq 0xa8(%rsp), %r9
testq %r9, %r9
sete %al
movslq %r11d, %rcx
movl (%r15,%rcx,8), %ebp
cmpq $0x0, 0xb0(%rsp)
sete %cl
orb %al, %cl
movq 0x10(%rsp), %r14
jne 0x861e
movq 0x8(%rsp), %r8
movl 0xa0(%rsp), %edi
leal (%rdi,%r8), %eax
cmpl %r11d, %eax
cmovll %eax, %r11d
testl %r8d, %r8d
setg %cl
testl %r11d, %r11d
setg %r13b
movl %ecx, %eax
andb %r13b, %al
cmpb $0x1, %al
jne 0x84e2
movl %ebp, 0x20(%rsp)
decl %r11d
decl %r8d
xorl %ebx, %ebx
xorl %eax, %eax
xorl %ebp, %ebp
xorl %edx, %edx
xorl %r10d, %r10d
movl 0x4(%rsp), %r9d
movl %r8d, %ecx
imull %r9d, %ecx
movl %edi, %esi
subl %r8d, %esi
cmovgel %ebx, %esi
addl %r11d, %ecx
addl %esi, %ecx
movslq %ecx, %rcx
movzbl (%r14,%rcx), %esi
addl %ebp, %ebp
movl %ebp, %ecx
shrl %cl, %esi
movl %esi, %ebp
andl $0x3, %ebp
movslq %r10d, %r13
cmpl $0x1, %ebp
je 0x83b2
testl %ebp, %ebp
jne 0x83d0
testl %r10d, %r10d
je 0x8355
movl -0x4(%rax,%r13,4), %ecx
testb $0xf, %cl
je 0x84a8
cmpl %edx, %r10d
jne 0x83a2
leal (%rdx,%rdx), %ebx
testl %edx, %edx
pushq $0x4
popq %rcx
cmovel %ecx, %ebx
leal (,%rbx,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
movq %r8, 0x8(%rsp)
movq %r11, %r12
movq %r10, %r14
callq 0x7590
movq %r14, %r10
movl 0x4(%rsp), %r9d
movq %r12, %r11
movl 0xa0(%rsp), %edi
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r14
movl %ebx, %edx
xorl %ebx, %ebx
incl %r10d
movl $0x10, (%rax,%r13,4)
jmp 0x84b0
testl %r10d, %r10d
je 0x83f1
movl -0x4(%rax,%r13,4), %ecx
movl %ecx, %esi
andl $0xf, %esi
cmpl $0x2, %esi
jne 0x83f1
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%r13,4)
jmp 0x8449
testl %r10d, %r10d
je 0x844e
movl -0x4(%rax,%r13,4), %ecx
movl %ecx, %esi
andl $0xf, %esi
cmpl $0x1, %esi
jne 0x844e
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%r13,4)
jmp 0x84b3
cmpl %edx, %r10d
jne 0x843e
leal (%rdx,%rdx), %ebx
testl %edx, %edx
pushq $0x4
popq %rcx
cmovel %ecx, %ebx
leal (,%rbx,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
movq %r8, 0x8(%rsp)
movq %r11, %r12
movq %r10, %r14
callq 0x7590
movq %r14, %r10
movl 0x4(%rsp), %r9d
movq %r12, %r11
movl 0xa0(%rsp), %edi
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r14
movl %ebx, %edx
xorl %ebx, %ebx
incl %r10d
movl $0x12, (%rax,%r13,4)
decl %r8d
jmp 0x84b6
cmpl %edx, %r10d
jne 0x849b
leal (%rdx,%rdx), %ebx
testl %edx, %edx
pushq $0x4
popq %rcx
cmovel %ecx, %ebx
leal (,%rbx,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
movq %r8, 0x8(%rsp)
movq %r11, %r12
movq %r10, %r14
callq 0x7590
movq %r14, %r10
movl 0x4(%rsp), %r9d
movq %r12, %r11
movl 0xa0(%rsp), %edi
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r14
movl %ebx, %edx
xorl %ebx, %ebx
incl %r10d
movl $0x11, (%rax,%r13,4)
jmp 0x84b3
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%r13,4)
decl %r8d
decl %r11d
testl %r11d, %r11d
setns %r13b
testl %r8d, %r8d
setns %cl
js 0x84ce
testl %r11d, %r11d
jns 0x830b
incl %r8d
incl %r11d
movl 0x20(%rsp), %ebp
movq 0xa8(%rsp), %r9
jmp 0x84e9
xorl %r10d, %r10d
xorl %edx, %edx
xorl %eax, %eax
testb %cl, %cl
je 0x8578
movslq %r10d, %rbx
testl %r10d, %r10d
je 0x8514
movl -0x4(%rax,%rbx,4), %ecx
movl %ecx, %esi
andl $0xf, %esi
cmpl $0x2, %esi
jne 0x8514
shll $0x4, %r8d
addl %r8d, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
jmp 0x8578
cmpl %edx, %r10d
jne 0x8569
leal (%rdx,%rdx), %r14d
testl %edx, %edx
pushq $0x4
popq %rcx
cmovel %ecx, %r14d
leal (,%r14,4), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
movl %ebp, 0x20(%rsp)
movq %r8, 0x8(%rsp)
movq %r11, %r12
movq %r9, %rbp
movq %r10, 0x18(%rsp)
callq 0x7590
movq 0x18(%rsp), %r10
movq %rbp, %r9
movq %r12, %r11
movq 0x8(%rsp), %r8
movl 0x20(%rsp), %ebp
movl %r14d, %edx
movq 0x10(%rsp), %r14
shll $0x4, %r8d
orl $0x2, %r8d
incl %r10d
movl %r8d, (%rax,%rbx,4)
testb %r13b, %r13b
je 0x85e4
movslq %r10d, %rbx
testl %r10d, %r10d
je 0x85a0
movl -0x4(%rax,%rbx,4), %ecx
movl %ecx, %esi
andl $0xf, %esi
cmpl $0x1, %esi
jne 0x85a0
shll $0x4, %r11d
addl %r11d, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
jmp 0x85e4
cmpl %edx, %r10d
jne 0x85d6
leal (,%rdx,8), %ecx
testl %edx, %edx
pushq $0x10
popq %rdx
cmovnel %ecx, %edx
movslq %edx, %rsi
movq %rax, %rdi
movq %r11, %r12
movq %r9, %r14
movq %r10, %r13
callq 0x7590
movq %r13, %r10
movq %r14, %r9
movq 0x10(%rsp), %r14
movq %r12, %r11
shll $0x4, %r11d
incl %r11d
incl %r10d
movl %r11d, (%rax,%rbx,4)
movl %r10d, %ecx
sarl %ecx
testl %ecx, %ecx
jle 0x8610
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 0x85f5
movl %r10d, (%r9)
movq 0xb0(%rsp), %rcx
movq %rax, (%rcx)
movq %r15, %rdi
callq 0x7380
movq 0x28(%rsp), %rdi
callq 0x7380
movq %r14, %rdi
callq 0x7380
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq