// File: /workspace/github_c_makefile_star2_O1/annoroad[P]fqtools_plus/cutadapt.c
// Function: match
static void alignread(adapter_* adapter)
{

	int m = adapter->m;
	int n = adapter->n;
	Entry_* column = adapter->columns;

	int i, j, best_i, best_j, best_cost, best_matches, best_origin;

	for (i = 0; i <= m; i++) {
		column[i].matches = 0;
		column[i].cost = i * DELETION_COST;
		column[i].origin = 0;
	}

	best_i = m;
	best_j = 0;
	best_cost = column[m].cost;
	best_matches = 0;
	best_origin = column[m].origin;

	// maximum no. of errors
	int k = error_rate * m;
	int last = k + 1;
	int match;
	int cost_diag;
	int cost_deletion;
	int cost_insertion;
	int origin, cost, matches;
	for (j = 1; j <= n; j++) {
		Entry_ tmp_entry = column[0];
		column[0].cost = 0;
		column[0].origin = j;
		column[0].matches = 0;
		for (i = 1; i <= last; ++i) {

			match = (((adapter->s1)[i-1] == (adapter->s2)[j-1]) || ((adapter->s1)[i-1] == 'N'));
			cost_diag = tmp_entry.cost + (match?MATCH_COST : MISMATCH_COST);
			cost_deletion = column[i].cost + DELETION_COST;
			cost_insertion = column[i-1].cost + INSERTION_COST;

			if (cost_diag <= cost_deletion && cost_diag <= cost_insertion) {
				// MATCH or MISMATCH
				cost = cost_diag;
				origin = tmp_entry.origin;
				matches = tmp_entry.matches + match;
			} else if (cost_insertion <= cost_deletion) {
				// INSERTION
				cost = cost_insertion;
				origin = column[i-1].origin;
				matches = column[i-1].matches;
			} else {
				// DELETION
				cost = cost_deletion;
				origin = column[i].origin;
				matches = column[i].matches;
			}
			//memcpy(&tmp_entry,column[i],l);
			tmp_entry = column[i];
			// remember current cell for next iteration
			column[i].cost = cost;
			column[i].origin = origin;
			column[i].matches = matches;

		}

		while (column[last].cost > k) {
			last--;
		}
		if (last < m) {
			last++;
		} else {
		
			int length = m + min(column[m].origin, 0);
			cost = column[m].cost;
			matches = column[m].matches;
			if (cost <= length * error_rate && (matches > best_matches || (matches == best_matches && cost < best_cost))) {
				// update
				best_matches = matches;
				best_cost = cost;
				best_origin = column[m].origin;
				best_i = m;
				best_j = j;
			}

		}
		// column finished
	}


	for (i = 0; i <= m; ++i) {
		int length = i + min(column[i].origin, 0);
		int cost = column[i].cost;
		int matches = column[i].matches;
		if (cost <= length * error_rate && (matches > best_matches || (matches == best_matches && cost < best_cost))) {
			best_matches = matches;
			best_cost = cost;
			best_origin = column[i].origin;
			best_i = i;
			best_j = n;
		}
	}


	if (best_origin >= 0) {
		adapter->result->start1 = 0;
		adapter->result->start2 = best_origin;
	} else {
		adapter->result->start1 = -best_origin;
		adapter->result->start2 = 0;
	}
	adapter->result->stop1 = best_i;
	adapter->result->stop2 = best_j;
	adapter->result->matches = best_matches;
	adapter->result->errors = best_cost;
	// return (start1, stop1, start2, stop2, matches, errors)
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movslq 0x10(%rdi), %r11
movl %r11d, %esi
movl 0x14(%rdi), %r8d
movq %rdi, -0x58(%rsp)
movq (%rdi), %r14
testq %r11, %r11
js 0x68e0
leaq 0x1(%rsi), %rax
leaq 0x8(%r14), %rcx
xorl %edx, %edx
movq $0x0, -0x4(%rcx)
movl %edx, -0x8(%rcx)
incq %rdx
addq $0xc, %rcx
cmpq %rdx, %rax
jne 0x68c9
movq %rsi, -0x40(%rsp)
leaq (%r11,%r11,2), %rax
movl (%r14,%rax,4), %edx
movl 0x8(%r14,%rax,4), %r15d
testl %r8d, %r8d
movq %r8, -0x48(%rsp)
jle 0x6af3
cvtsi2sd %r11d, %xmm0
mulsd 0x167b(%rip), %xmm0     # 0x7f88
leaq (%r14,%rax,4), %rax
movq %rax, -0x38(%rsp)
cvttsd2si %xmm0, %esi
leal 0x1(%rsi), %ebx
leaq 0x1(%r8), %rax
movq %rax, -0x28(%rsp)
leaq 0x14(%r14), %rax
movq %rax, -0x30(%rsp)
movq $0x0, -0x60(%rsp)
movl $0x1, %edi
movsd 0x1643(%rip), %xmm0     # 0x7f88
movl %edx, -0x68(%rsp)
xorl %eax, %eax
movl %r15d, %ecx
movq %r11, -0x10(%rsp)
movq %r14, -0x18(%rsp)
movq %rsi, -0x20(%rsp)
movl %ecx, -0x64(%rsp)
movl %eax, -0x4c(%rsp)
movl (%r14), %edx
movl 0x4(%r14), %eax
movl 0x8(%r14), %r9d
movq $0x0, (%r14)
movl %edi, 0x8(%r14)
testl %ebx, %ebx
jle 0x6a44
movq -0x58(%rsp), %rcx
movq 0x220(%rcx), %rcx
movq %rcx, -0x8(%rsp)
movl %ebx, %r15d
movq -0x30(%rsp), %rcx
xorl %r14d, %r14d
movq -0x58(%rsp), %rsi
movb 0x1c(%rsi,%r14), %sil
movq %rdi, %r8
movq -0x8(%rsp), %r10
cmpb -0x1(%r10,%rdi), %sil
sete %bpl
cmpb $0x4e, %sil
sete %r11b
orb %bpl, %r11b
movl %r11d, %esi
notb %sil
movzbl %sil, %ebp
andl $0x1, %ebp
addl %edx, %ebp
movl -0x14(%rcx), %r12d
movl -0x8(%rcx), %edx
leal 0x1(%rdx), %r13d
leal 0x1(%r12), %esi
cmpl %r13d, %ebp
setg %dil
cmpl %esi, %ebp
setg %r10b
orb %dil, %r10b
je 0x6a07
cmpl %edx, %r12d
jle 0x6a15
movl -0x4(%rcx), %r11d
movl (%rcx), %r9d
movl %r13d, %ebp
jmp 0x6a1f
movzbl %r11b, %esi
andl $0x1, %esi
addl %esi, %eax
movl %eax, %r11d
jmp 0x6a1f
movl -0x10(%rcx), %r11d
movl -0xc(%rcx), %r9d
movl %esi, %ebp
movq %r8, %rdi
movl -0x4(%rcx), %eax
movl (%rcx), %esi
movl %ebp, -0x8(%rcx)
movl %r9d, (%rcx)
movl %r11d, -0x4(%rcx)
incq %r14
addq $0xc, %rcx
movl %esi, %r9d
cmpq %r14, %r15
jne 0x699f
movslq %ebx, %rax
incl %ebx
leaq (%rax,%rax,2), %rax
movq -0x18(%rsp), %r14
leaq (%r14,%rax,4), %rax
movq -0x20(%rsp), %rsi
decl %ebx
leaq -0xc(%rax), %rcx
cmpl %esi, (%rax)
movq %rcx, %rax
jg 0x6a5b
movq -0x10(%rsp), %r11
cmpl %ebx, %r11d
movl -0x4c(%rsp), %r9d
jle 0x6a7b
incl %ebx
jmp 0x6aaa
movq -0x38(%rsp), %rcx
movl 0x8(%rcx), %r15d
movl %r15d, %eax
sarl $0x1f, %eax
andl %r15d, %eax
addl %r11d, %eax
movl (%rcx), %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0x6ad0
movl -0x64(%rsp), %r15d
movl %r9d, %r8d
movl -0x68(%rsp), %edx
incq %rdi
movl %edx, -0x68(%rsp)
movl %r8d, %eax
movl %r15d, %ecx
cmpq -0x28(%rsp), %rdi
jne 0x695d
jmp 0x6aff
movl 0x4(%rcx), %r8d
cmpl %r9d, %r8d
jg 0x6aea
sete %al
cmpl -0x68(%rsp), %edx
setl %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x6aaa
movl %edi, %eax
movq %rax, -0x60(%rsp)
jmp 0x6ab6
xorl %r8d, %r8d
movq $0x0, -0x60(%rsp)
testl %r11d, %r11d
js 0x6b79
movq -0x40(%rsp), %rdi
incq %rdi
addq $0x8, %r14
xorl %r9d, %r9d
movsd 0x146d(%rip), %xmm0     # 0x7f88
movq -0x48(%rsp), %rbx
movslq (%r14), %rax
movq %rax, %rcx
sarq $0x3f, %rcx
andl %eax, %ecx
leal (%r9,%rcx), %esi
movl -0x8(%r14), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %esi, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jb 0x6b6d
movl -0x4(%r14), %r10d
cmpl %r8d, %r10d
jg 0x6b5b
jne 0x6b6d
cmpl %edx, %ecx
jge 0x6b6d
movl %eax, %r15d
movl %r10d, %r8d
movl %ecx, %edx
movl %ebx, %eax
movq %rax, -0x60(%rsp)
movl %r9d, %r11d
incq %r9
addq $0xc, %r14
cmpq %r9, %rdi
jne 0x6b20
movl %r15d, %eax
negl %eax
xorl %ecx, %ecx
testl %r15d, %r15d
cmovnsl %ecx, %eax
cmovnsl %r15d, %ecx
movq -0x58(%rsp), %rdi
movq 0x8(%rdi), %rsi
movl %eax, (%rsi)
movl %ecx, 0x8(%rsi)
movl %r11d, 0x4(%rsi)
movq -0x60(%rsp), %rcx
movl %ecx, 0xc(%rsi)
movl %r8d, 0x10(%rsi)
movl %edx, 0x14(%rsi)
subl %eax, %r11d
cmpl 0x18(%rdi), %r11d
setl %al
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq