// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/kaln.c
// Function: ka_global_score
int ka_global_score(const uint8_t *_seq1, int len1, const uint8_t *_seq2, int len2, const ka_param2_t *ap)
{
	
#define __score_aux(_p, _q0, _sc, _io, _ie, _do, _de) {					\
		int t1, t2;														\
		score_aux_t *_q;												\
		_q = _q0;														\
		_p->M = _q->M >= _q->I? _q->M : _q->I;							\
		_p->M = _p->M >= _q->D? _p->M : _q->D;							\
		_p->M += (_sc);													\
		++_q;      t1 = _q->M - _io - _ie; t2 = _q->I - _ie; _p->I = t1 >= t2? t1 : t2; \
		_q = _p-1; t1 = _q->M - _do - _de; t2 = _q->D - _de; _p->D = t1 >= t2? t1 : t2; \
	}

	int i, j, bw, scmat_size = ap->row, *scmat = ap->matrix, ret;
	const uint8_t *seq1, *seq2;
	score_aux_t *curr, *last, *swap;
	bw = abs(len1 - len2) + ap->band_width;
	i = len1 > len2? len1 : len2;
	if (bw > i + 1) bw = i + 1;
	seq1 = _seq1 - 1; seq2 = _seq2 - 1;
	curr = calloc(len1 + 2, sizeof(score_aux_t));
	last = calloc(len1 + 2, sizeof(score_aux_t));
	{ // the zero-th row
		int x, end = len1;
		score_aux_t *p;
		j = 0;
		x = j + bw; end = len1 < x? len1 : x; // band end
		p = curr;
		p->M = 0; p->I = p->D = MINUS_INF;
		for (i = 1, p = &curr[1]; i <= end; ++i, ++p)
			p->M = p->I = MINUS_INF, p->D = -(ap->edo + ap->ede * i);
		p->M = p->I = p->D = MINUS_INF;
		swap = curr; curr = last; last = swap;
	}
	for (j = 1; j < len2; ++j) {
		int x, beg = 0, end = len1, *scrow, col_end;
		score_aux_t *p;
		x = j - bw; beg =    0 > x?    0 : x; // band start
		x = j + bw; end = len1 < x? len1 : x; // band end
		if (beg == 0) { // from zero-th column
			p = curr;
			p->M = p->D = MINUS_INF; p->I = -(ap->eio + ap->eie * j);
			++beg; // then beg = 1
		}
		scrow = scmat + seq2[j] * scmat_size;
		if (end == len1) col_end = 1, --end;
		else col_end = 0;
		for (i = beg, p = &curr[beg]; i <= end; ++i, ++p)
			__score_aux(p, &last[i-1], scrow[(int)seq1[i]], ap->iio, ap->iie, ap->ido, ap->ide);
		if (col_end) {
			__score_aux(p, &last[i-1], scrow[(int)seq1[i]], ap->eio, ap->eie, ap->ido, ap->ide);
			++p;
		}
		p->M = p->I = p->D = MINUS_INF;
//		for (i = 0; i <= len1; ++i) printf("(%d,%d,%d) ", curr[i].M, curr[i].I, curr[i].D); putchar('\n');
		swap = curr; curr = last; last = swap;
	}
	{ // the last row
		int x, beg = 0, *scrow;
		score_aux_t *p;
		j = len2;
		x = j - bw; beg = 0 > x?    0 : x; // band start
		if (beg == 0) { // from zero-th column
			p = curr;
			p->M = p->D = MINUS_INF; p->I = -(ap->eio + ap->eie * j);
			++beg; // then beg = 1
		}
		scrow = scmat + seq2[j] * scmat_size;
		for (i = beg, p = &curr[beg]; i < len1; ++i, ++p)
			__score_aux(p, &last[i-1], scrow[(int)seq1[i]], ap->iio, ap->iie, ap->edo, ap->ede);
		__score_aux(p, &last[i-1], scrow[(int)seq1[i]], ap->eio, ap->eie, ap->edo, ap->ede);
//		for (i = 0; i <= len1; ++i) printf("(%d,%d,%d) ", curr[i].M, curr[i].I, curr[i].D); putchar('\n');
	}
	ret = curr[len1].M >= curr[len1].I? curr[len1].M : curr[len1].I;
	ret = ret >= curr[len1].D? ret : curr[len1].D;
	free(curr); free(last);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %rbx
movq %rdx, 0x18(%rsp)
movl %esi, %r12d
movq %rdi, %r15
movslq 0x28(%r8), %rax
movq %rax, 0x28(%rsp)
movq 0x20(%r8), %rax
movq %rax, 0x20(%rsp)
movl %esi, %eax
subl %ecx, %eax
movl %ecx, (%rsp)
movl %ecx, %r13d
cmovgl %esi, %r13d
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
addl 0x2c(%r8), %ecx
incl %r13d
cmpl %r13d, %ecx
cmovll %ecx, %r13d
leal 0x2(%r12), %eax
movslq %eax, %rbp
movl $0xc, %esi
movq %rbp, %rdi
callq 0x3610
movq %rax, %r14
movl $0xc, %esi
movq %rbp, %rdi
callq 0x3610
cmpl %r12d, %r13d
movl %r12d, %edx
movl %r13d, 0x4(%rsp)
cmovll %r13d, %edx
movabsq $-0x3fffffff40000000, %r8 # imm = 0xC0000000C0000000
movq %r8, 0x4(%r14)
movq %r14, %rcx
addq $0xc, %rcx
testl %edx, %edx
jle 0x10954
movl 0x1c(%rbx), %esi
movl 0x18(%rbx), %edi
addl %esi, %edi
negl %edi
movq %r8, (%rcx)
movl %edi, 0x8(%rcx)
addq $0xc, %rcx
subl %esi, %edi
decl %edx
jne 0x1093e
leaq -0xc(%rcx), %rdx
jmp 0x10957
movq %r14, %rdx
movl (%rsp), %esi
movq %r8, 0x10(%rdx)
movl $0xc0000000, (%rcx)     # imm = 0xC0000000
movslq %r12d, %r9
cmpl $0x2, %esi
movq %rbx, 0x8(%rsp)
movq %r9, 0x10(%rsp)
jl 0x10b79
movl $0x1, %edi
movl 0x4(%rsp), %ecx
subl %ecx, %edi
movslq %ecx, %rcx
movq %rcx, 0x50(%rsp)
movl %esi, %ecx
movq %rcx, 0x48(%rsp)
movl $0x1, %r10d
movq %r12, 0x30(%rsp)
movq %r14, %rsi
movq %rax, %r14
cmpl $0x2, %edi
movl $0x1, %ecx
cmovgel %edi, %ecx
movq %r10, %rbp
movq 0x50(%rsp), %rax
subq %rax, %rbp
leaq (%r10,%rax), %r11
cmpl %r12d, %r11d
movl %r12d, %r8d
cmovll %r11d, %r8d
testq %rbp, %rbp
jg 0x109ef
movl $0xc0000000, %eax       # imm = 0xC0000000
movl %eax, 0x8(%r14)
movl %eax, (%r14)
movl 0x14(%rbx), %eax
imull %r10d, %eax
addl 0x10(%rbx), %eax
negl %eax
movl %eax, 0x4(%r14)
movl $0x1, %ebp
movq 0x18(%rsp), %rax
movzbl -0x1(%rax,%r10), %eax
imulq 0x28(%rsp), %rax
movq 0x20(%rsp), %rdx
leaq (%rdx,%rax,4), %r13
xorl %eax, %eax
cmpq %r9, %r11
setge %al
subl %eax, %r8d
movl %ebp, %eax
leaq (%rax,%rax,2), %rax
leaq (%r14,%rax,4), %rax
cmpl %r8d, %ebp
jg 0x10ad5
movq %r14, 0x40(%rsp)
leaq -0x1(%rcx), %rbp
shlq $0x2, %rcx
leaq (%rcx,%rcx,2), %rdx
movl 0x4(%rbx), %r9d
movl 0xc(%rbx), %ecx
movslq %r8d, %r12
movl (%rbx), %r8d
addl %r9d, %r8d
movl 0x8(%rbx), %ebx
addl %ecx, %ebx
movq %rsi, 0x38(%rsp)
addq %rsi, %rdx
movl -0xc(%rdx), %r14d
movl -0x8(%rdx), %esi
cmpl %esi, %r14d
cmovgl %r14d, %esi
movl -0x4(%rdx), %r14d
cmpl %r14d, %esi
cmovlel %r14d, %esi
movl %esi, (%rax)
movzbl (%r15,%rbp), %r14d
addl (%r13,%r14,4), %esi
movl %esi, (%rax)
movl (%rdx), %esi
subl %r8d, %esi
movl 0x4(%rdx), %r14d
subl %r9d, %r14d
cmpl %r14d, %esi
cmovgl %esi, %r14d
movl %r14d, 0x4(%rax)
movl -0xc(%rax), %esi
subl %ebx, %esi
movl -0x4(%rax), %r14d
subl %ecx, %r14d
cmpl %r14d, %esi
cmovgl %esi, %r14d
movl %r14d, 0x8(%rax)
addq $0xc, %rax
incq %rbp
addq $0xc, %rdx
cmpq %r12, %rbp
jl 0x10a55
incl %ebp
movq 0x40(%rsp), %r14
movq 0x8(%rsp), %rbx
movq 0x30(%rsp), %r12
movq 0x10(%rsp), %r9
movq 0x38(%rsp), %rsi
cmpq %r9, %r11
jl 0x10b50
leal -0x1(%rbp), %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
movl (%rsi,%rcx,4), %edx
movq %rsi, %r11
movl 0x4(%rsi,%rcx,4), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movl 0x8(%r11,%rcx,4), %edx
cmpl %edx, %esi
cmovlel %edx, %esi
movl %esi, (%rax)
movl %ebp, %edx
movzbl -0x1(%r15,%rdx), %edx
addl (%r13,%rdx,4), %esi
movl %esi, (%rax)
movl 0x14(%rbx), %edx
movl 0x10(%rbx), %esi
addl %edx, %esi
movl 0xc(%r11,%rcx,4), %r8d
subl %esi, %r8d
movl 0x10(%r11,%rcx,4), %ecx
subl %edx, %ecx
cmpl %ecx, %r8d
cmovgl %r8d, %ecx
movl %ecx, 0x4(%rax)
movl 0xc(%rbx), %ecx
movl 0x8(%rbx), %edx
addl %ecx, %edx
movl -0xc(%rax), %esi
subl %edx, %esi
movl -0x4(%rax), %edx
subl %ecx, %edx
cmpl %edx, %esi
cmovgl %esi, %edx
movq %r11, %rsi
movl %edx, 0x8(%rax)
addq $0xc, %rax
movabsq $-0x3fffffff40000000, %rcx # imm = 0xC0000000C0000000
movl $0xc0000000, 0x8(%rax)  # imm = 0xC0000000
movq %rcx, (%rax)
incq %r10
incl %edi
movq %rsi, %rax
cmpq 0x48(%rsp), %r10
jne 0x1099f
jmp 0x10b7c
movq %rax, %rsi
movl (%rsp), %edx
movl %edx, %ecx
subl 0x4(%rsp), %ecx
jg 0x10ba4
movl $0xc0000000, %eax       # imm = 0xC0000000
movl %eax, 0x8(%rsi)
movl %eax, (%rsi)
movl 0x14(%rbx), %eax
imull %edx, %eax
addl 0x10(%rbx), %eax
negl %eax
movl %eax, 0x4(%rsi)
movl $0x1, %ecx
movslq %edx, %rax
movq 0x18(%rsp), %rdx
movzbl -0x1(%rdx,%rax), %eax
movq 0x28(%rsp), %rdx
imulq %rax, %rdx
movq %rsi, %rax
movq 0x20(%rsp), %rsi
leaq (%rsi,%rdx,4), %rsi
movl %ecx, %r9d
leaq (%r9,%r9,2), %rbx
movq %rax, %r13
leaq (%rax,%rbx,4), %rax
cmpl %r12d, %ecx
jge 0x10c5e
movq 0x8(%rsp), %rdi
movl 0x4(%rdi), %ecx
movl 0x1c(%rdi), %edx
movl %r12d, %r10d
movl (%rdi), %r11d
addl %ecx, %r11d
movl 0x18(%rdi), %r8d
addl %edx, %r8d
leaq (%r14,%rbx,4), %rbx
movl -0xc(%rbx), %edi
movl -0x8(%rbx), %ebp
cmpl %ebp, %edi
cmovgl %edi, %ebp
movl -0x4(%rbx), %edi
cmpl %edi, %ebp
cmovlel %edi, %ebp
movl %ebp, (%rax)
movzbl -0x1(%r15,%r9), %edi
addl (%rsi,%rdi,4), %ebp
movl %ebp, (%rax)
movl (%rbx), %edi
subl %r11d, %edi
movl 0x4(%rbx), %ebp
subl %ecx, %ebp
cmpl %ebp, %edi
cmovgl %edi, %ebp
movl %ebp, 0x4(%rax)
movl -0xc(%rax), %edi
movl -0x4(%rax), %ebp
subl %r8d, %edi
subl %edx, %ebp
cmpl %ebp, %edi
cmovgl %edi, %ebp
movl %ebp, 0x8(%rax)
incq %r9
addq $0xc, %rax
addq $0xc, %rbx
cmpq %r9, %r10
jne 0x10bfc
movq %r10, %r9
movl %r12d, %ecx
movq 0x8(%rsp), %r11
jmp 0x10c6e
movq 0x8(%rsp), %r11
movl 0x1c(%r11), %edx
movl 0x18(%r11), %r8d
addl %edx, %r8d
decl %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
movl (%r14,%rcx,4), %edi
movl 0x4(%r14,%rcx,4), %r10d
cmpl %r10d, %edi
cmovgl %edi, %r10d
movl 0x8(%r14,%rcx,4), %edi
cmpl %edi, %r10d
cmovlel %edi, %r10d
movl %r10d, (%rax)
movzbl -0x1(%r15,%r9), %edi
addl (%rsi,%rdi,4), %r10d
movl %r10d, (%rax)
movl 0x14(%r11), %esi
movl 0x10(%r11), %edi
addl %esi, %edi
movl 0xc(%r14,%rcx,4), %r9d
subl %edi, %r9d
movl 0x10(%r14,%rcx,4), %ecx
subl %esi, %ecx
cmpl %ecx, %r9d
cmovgl %r9d, %ecx
movl %ecx, 0x4(%rax)
movl -0xc(%rax), %ecx
subl %r8d, %ecx
movl -0x4(%rax), %esi
subl %edx, %esi
cmpl %esi, %ecx
cmovgl %ecx, %esi
movl %esi, 0x8(%rax)
movq 0x10(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq %r13, %rdi
movl (%r13,%rax,4), %ecx
movl 0x4(%r13,%rax,4), %ebp
cmpl %ebp, %ecx
cmovgl %ecx, %ebp
movl 0x8(%r13,%rax,4), %eax
cmpl %eax, %ebp
cmovlel %eax, %ebp
callq 0x3350
movq %r14, %rdi
callq 0x3350
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq