// File: /workspace/github_c_makefile_star2_O1/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, 0x20(%rsp)
movl %esi, %r14d
movq %rdi, %r15
movslq 0x28(%r8), %rax
movq %rax, 0x30(%rsp)
movq 0x20(%r8), %rax
movq %rax, 0x28(%rsp)
movl %esi, %eax
subl %ecx, %eax
movl %ecx, 0x8(%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(%r14), %eax
movslq %eax, %rbp
movl $0xc, %esi
movq %rbp, %rdi
callq 0x3610
movq %rax, %r12
movl $0xc, %esi
movq %rbp, %rdi
callq 0x3610
movq %r14, %r10
cmpl %r10d, %r13d
movl %r10d, %edx
movl %r13d, 0xc(%rsp)
cmovll %r13d, %edx
movabsq $-0x4000000000000000, %rcx # imm = 0xC000000000000000
movq %rcx, (%r12)
movl $0xc0000000, 0x8(%r12)  # imm = 0xC0000000
movq %r12, %rcx
addq $0xc, %rcx
movabsq $-0x3fffffff40000000, %r8 # imm = 0xC0000000C0000000
testl %edx, %edx
jle 0x105ad
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 0x10597
leaq -0xc(%rcx), %rdx
jmp 0x105b0
movq %r12, %rdx
movl 0x8(%rsp), %esi
movq %r8, 0x10(%rdx)
movl $0xc0000000, (%rcx)     # imm = 0xC0000000
movslq %r10d, %r13
cmpl $0x2, %esi
movq %rbx, 0x18(%rsp)
movq %r13, 0x10(%rsp)
jl 0x107ce
movslq 0xc(%rsp), %rcx
movq %rcx, 0x48(%rsp)
movl %esi, %ecx
movq %rcx, 0x40(%rsp)
movl $0x1, %r9d
movq %r10, 0x50(%rsp)
movq %r12, %rdi
movq %rax, %r12
movq %r9, %rax
movq 0x48(%rsp), %rdx
subq %rdx, %rax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
leaq (%r9,%rdx), %r11
cmpl %r10d, %r11d
movl %r10d, %esi
cmovll %r11d, %esi
testq %rax, %rax
jg 0x10642
movl $0xc0000000, %eax       # imm = 0xC0000000
movl %eax, 0x8(%r12)
movl %eax, (%r12)
movl 0x14(%rbx), %eax
imull %r9d, %eax
addl 0x10(%rbx), %eax
negl %eax
movl %eax, 0x4(%r12)
movl $0x1, %ecx
movq 0x20(%rsp), %rax
movzbl -0x1(%rax,%r9), %eax
imulq 0x30(%rsp), %rax
movq 0x28(%rsp), %rdx
leaq (%rdx,%rax,4), %rbp
xorl %eax, %eax
cmpq %r13, %r11
setge %al
subl %eax, %esi
movl %ecx, %r10d
leaq (%r10,%r10,2), %r14
leaq (%r12,%r14,4), %rax
cmpl %esi, %ecx
jg 0x10721
movq %r12, 0x38(%rsp)
movl 0x4(%rbx), %r13d
movl 0xc(%rbx), %edx
movslq %esi, %r8
movl (%rbx), %ecx
addl %r13d, %ecx
movl 0x8(%rbx), %r12d
addl %edx, %r12d
decq %r10
leaq (%rdi,%r14,4), %rsi
movl -0xc(%rsi), %r14d
movl -0x8(%rsi), %ebx
cmpl %ebx, %r14d
cmovgl %r14d, %ebx
movl %ebx, (%rax)
movl -0x4(%rsi), %r14d
cmpl %r14d, %ebx
cmovlel %r14d, %ebx
movl %ebx, (%rax)
movzbl (%r15,%r10), %r14d
addl (%rbp,%r14,4), %ebx
movl %ebx, (%rax)
movl (%rsi), %ebx
subl %ecx, %ebx
movl 0x4(%rsi), %r14d
subl %r13d, %r14d
cmpl %r14d, %ebx
cmovgl %ebx, %r14d
movl %r14d, 0x4(%rax)
movl -0xc(%rax), %ebx
subl %r12d, %ebx
movl -0x4(%rax), %r14d
subl %edx, %r14d
cmpl %r14d, %ebx
cmovgl %ebx, %r14d
movl %r14d, 0x8(%rax)
addq $0xc, %rax
incq %r10
addq $0xc, %rsi
cmpq %r8, %r10
jl 0x1069b
incl %r10d
movl %r10d, %ecx
movq 0x18(%rsp), %rbx
movq 0x38(%rsp), %r12
movq 0x10(%rsp), %r13
movabsq $-0x3fffffff40000000, %r8 # imm = 0xC0000000C0000000
cmpq %r13, %r11
movq 0x50(%rsp), %r10
jl 0x107b1
leal -0x1(%rcx), %edx
movslq %edx, %rdx
leaq (%rdx,%rdx,2), %rdx
movl (%rdi,%rdx,4), %esi
movl 0x4(%rdi,%rdx,4), %r8d
cmpl %r8d, %esi
cmovgl %esi, %r8d
movl %r8d, (%rax)
movl 0x8(%rdi,%rdx,4), %esi
cmpl %esi, %r8d
cmovlel %esi, %r8d
movl %r8d, (%rax)
movl %ecx, %ecx
movzbl -0x1(%r15,%rcx), %ecx
addl (%rbp,%rcx,4), %r8d
movl %r8d, (%rax)
movl 0x14(%rbx), %ecx
movl 0x10(%rbx), %esi
addl %ecx, %esi
movl 0xc(%rdi,%rdx,4), %r8d
subl %esi, %r8d
movl 0x10(%rdi,%rdx,4), %edx
subl %ecx, %edx
cmpl %edx, %r8d
cmovgl %r8d, %edx
movabsq $-0x3fffffff40000000, %r8 # imm = 0xC0000000C0000000
movl %edx, 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
movl %edx, 0x8(%rax)
addq $0xc, %rax
movl $0xc0000000, 0x8(%rax)  # imm = 0xC0000000
movq %r8, (%rax)
incq %r9
movq %rdi, %rax
cmpq 0x40(%rsp), %r9
jne 0x105f0
jmp 0x107d1
movq %rax, %rdi
movl 0x8(%rsp), %edx
movl %edx, %eax
subl 0xc(%rsp), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovgl %eax, %ecx
jg 0x10801
movl $0xc0000000, %eax       # imm = 0xC0000000
movl %eax, 0x8(%rdi)
movl %eax, (%rdi)
movl 0x14(%rbx), %eax
imull %edx, %eax
addl 0x10(%rbx), %eax
negl %eax
movl %eax, 0x4(%rdi)
movl $0x1, %ecx
movslq %edx, %rax
movq 0x20(%rsp), %rdx
movzbl -0x1(%rdx,%rax), %eax
movq 0x30(%rsp), %rdx
imulq %rax, %rdx
movq 0x28(%rsp), %rax
leaq (%rax,%rdx,4), %rdx
movl %ecx, %esi
leaq (%rsi,%rsi,2), %r14
leaq (%rdi,%r14,4), %rax
cmpl %r10d, %ecx
jge 0x108c4
movl 0x4(%rbx), %r8d
movl 0x1c(%rbx), %r9d
movl %r10d, %ecx
movl (%rbx), %r10d
addl %r8d, %r10d
movl 0x18(%rbx), %r11d
addl %r9d, %r11d
movq %r12, %r13
leaq (%r12,%r14,4), %r12
movl -0xc(%r12), %ebx
movl -0x8(%r12), %ebp
cmpl %ebp, %ebx
cmovgl %ebx, %ebp
movl %ebp, (%rax)
movl -0x4(%r12), %ebx
cmpl %ebx, %ebp
cmovlel %ebx, %ebp
movl %ebp, (%rax)
movzbl -0x1(%r15,%rsi), %ebx
addl (%rdx,%rbx,4), %ebp
movl %ebp, (%rax)
movl (%r12), %ebx
subl %r10d, %ebx
movl 0x4(%r12), %ebp
subl %r8d, %ebp
cmpl %ebp, %ebx
cmovgl %ebx, %ebp
movl %ebp, 0x4(%rax)
movl -0xc(%rax), %ebx
subl %r11d, %ebx
movl -0x4(%rax), %ebp
subl %r9d, %ebp
cmpl %ebp, %ebx
cmovgl %ebx, %ebp
movl %ebp, 0x8(%rax)
incq %rsi
addq $0xc, %rax
addq $0xc, %r12
cmpq %rsi, %rcx
jne 0x10852
movl %esi, %ecx
movq 0x18(%rsp), %rbx
movq %r13, %r12
movq 0x10(%rsp), %r13
leal -0x1(%rcx), %esi
movslq %esi, %rsi
leaq (%rsi,%rsi,2), %rsi
movl (%r12,%rsi,4), %r8d
movl 0x4(%r12,%rsi,4), %r9d
cmpl %r9d, %r8d
cmovgl %r8d, %r9d
movl %r9d, (%rax)
movl 0x8(%r12,%rsi,4), %r8d
cmpl %r8d, %r9d
cmovlel %r8d, %r9d
movl %r9d, (%rax)
movl %ecx, %ecx
movzbl -0x1(%r15,%rcx), %ecx
addl (%rdx,%rcx,4), %r9d
movl %r9d, (%rax)
movl 0x14(%rbx), %ecx
movl 0x10(%rbx), %edx
addl %ecx, %edx
movl 0xc(%r12,%rsi,4), %r8d
subl %edx, %r8d
movl 0x10(%r12,%rsi,4), %edx
subl %ecx, %edx
cmpl %edx, %r8d
cmovgl %r8d, %edx
movl %edx, 0x4(%rax)
movl 0x1c(%rbx), %ecx
movl 0x18(%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
movl %edx, 0x8(%rax)
leaq (,%r13,2), %rax
addq %r13, %rax
movl (%rdi,%rax,4), %ecx
movl 0x4(%rdi,%rax,4), %ebx
cmpl %ebx, %ecx
cmovgl %ecx, %ebx
movl 0x8(%rdi,%rax,4), %eax
cmpl %eax, %ebx
cmovlel %eax, %ebx
callq 0x3350
movq %r12, %rdi
callq 0x3350
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq