// File: /workspace/github_c_makefile_star2_O3/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), %r10
movl %r10d, %esi
movl 0x14(%rdi), %r9d
movq %rdi, -0x58(%rsp)
movq (%rdi), %r12
testq %r10, %r10
js 0x68e9
leaq 0x1(%rsi), %rax
leaq 0x8(%r12), %rcx
xorl %edx, %edx
movq $0x0, -0x4(%rcx)
movl %edx, -0x8(%rcx)
incq %rdx
addq $0xc, %rcx
cmpq %rdx, %rax
jne 0x68d2
movq %rsi, -0x40(%rsp)
leaq (%r10,%r10,2), %rax
movl (%r12,%rax,4), %r8d
movl 0x8(%r12,%rax,4), %ecx
testl %r9d, %r9d
movq %r9, -0x48(%rsp)
jle 0x6b09
leaq (%r12,%rax,4), %rax
movq %rax, -0x38(%rsp)
cvtsi2sd %r10d, %xmm0
mulsd 0x1669(%rip), %xmm0     # 0x7f88
cvttsd2si %xmm0, %esi
leal 0x1(%rsi), %ebx
leaq 0x1(%r9), %rax
movq %rax, -0x28(%rsp)
movl (%r12), %r14d
movl 0x4(%r12), %r9d
movl 0x8(%r12), %eax
movl %eax, -0x60(%rsp)
leaq 0x14(%r12), %rax
movq %rax, -0x30(%rsp)
movl $0x0, -0x64(%rsp)
movl $0x1, %edi
movsd 0x1628(%rip), %xmm0     # 0x7f88
movl %r8d, -0x68(%rsp)
xorl %eax, %eax
movl %ecx, %edx
movq %r10, -0x10(%rsp)
movq %r12, -0x18(%rsp)
movq %rsi, -0x20(%rsp)
movl %edx, -0x5c(%rsp)
movl %eax, -0x4c(%rsp)
movl -0x60(%rsp), %ebp
movq $0x0, (%r12)
movl %edi, -0x60(%rsp)
movl %edi, 0x8(%r12)
testl %ebx, %ebx
jle 0x6a50
movq -0x58(%rsp), %rax
movq 0x220(%rax), %rax
movq %rax, -0x8(%rsp)
movl %ebx, %r8d
xorl %r15d, %r15d
movq -0x30(%rsp), %rax
xorl %r11d, %r11d
movl -0x60(%rsp), %r13d
xorl %edx, %edx
movq -0x58(%rsp), %rsi
movb 0x1c(%rsi,%r15), %sil
movq %rdi, %rcx
movq -0x8(%rsp), %r10
cmpb -0x1(%r10,%rdi), %sil
sete %dil
cmpb $0x4e, %sil
sete %r12b
orb %dil, %r12b
movl %r12d, %esi
xorb $0x1, %sil
movzbl %sil, %esi
addl %r14d, %esi
movl -0x8(%rax), %r14d
leal 0x1(%r14), %r10d
leal 0x1(%rdx), %edi
cmpl %r10d, %esi
jg 0x6a1c
cmpl %edi, %esi
jg 0x6a1c
movzbl %r12b, %edx
addl %edx, %r9d
movl %ebp, %r13d
movl %esi, %edi
movl %r9d, %r11d
jmp 0x6a2b
cmpl %r14d, %edx
jle 0x6a2b
movl -0x4(%rax), %r11d
movl (%rax), %r13d
movl %r10d, %edi
movl -0x4(%rax), %r9d
movl (%rax), %ebp
movl %edi, -0x8(%rax)
movl %r13d, (%rax)
movl %r11d, -0x4(%rax)
incq %r15
addq $0xc, %rax
movl %edi, %edx
cmpq %r15, %r8
movq %rcx, %rdi
jne 0x69c3
movslq %ebx, %rax
incl %ebx
leaq (%rax,%rax,2), %rax
movq -0x18(%rsp), %r12
leaq (%r12,%rax,4), %rax
movq -0x20(%rsp), %rsi
decl %ebx
leaq -0xc(%rax), %rcx
cmpl %esi, (%rax)
movq %rcx, %rax
jg 0x6a67
movq -0x10(%rsp), %r10
cmpl %ebx, %r10d
movl -0x4c(%rsp), %r9d
jle 0x6a87
incl %ebx
jmp 0x6ab5
movq -0x38(%rsp), %rdx
movl 0x8(%rdx), %ecx
movl %ecx, %eax
sarl $0x1f, %eax
andl %ecx, %eax
addl %r10d, %eax
movl (%rdx), %r8d
xorps %xmm1, %xmm1
cvtsi2sd %r8d, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0x6ae4
movl -0x5c(%rsp), %ecx
movl %r9d, %r15d
movl -0x68(%rsp), %r8d
incq %rdi
xorl %r9d, %r9d
movl $0x0, %r14d
movl %r8d, -0x68(%rsp)
movl %r15d, %eax
movl %ecx, %edx
cmpq -0x28(%rsp), %rdi
jne 0x6978
jmp 0x6b14
movl 0x4(%rdx), %r15d
cmpl %r9d, %r15d
jg 0x6aff
sete %al
cmpl -0x68(%rsp), %r8d
setl %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x6ab5
movl -0x60(%rsp), %eax
movl %eax, -0x64(%rsp)
jmp 0x6ac1
xorl %r15d, %r15d
movl $0x0, -0x64(%rsp)
testl %r10d, %r10d
js 0x6b92
movq -0x40(%rsp), %r9
incq %r9
addq $0x8, %r12
xorl %edx, %edx
movsd 0x1459(%rip), %xmm0     # 0x7f88
movq -0x48(%rsp), %r11
movl -0x64(%rsp), %ebx
movslq (%r12), %rax
movq %rax, %rsi
sarq $0x3f, %rsi
andl %eax, %esi
leal (%rdx,%rsi), %edi
movl -0x8(%r12), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %edi, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jb 0x6b84
movl -0x4(%r12), %edi
cmpl %r15d, %edi
jg 0x6b76
jne 0x6b84
cmpl %r8d, %esi
jge 0x6b84
movl %eax, %ecx
movl %edi, %r15d
movl %esi, %r8d
movl %r11d, %ebx
movl %edx, %r10d
incq %rdx
addq $0xc, %r12
cmpq %rdx, %r9
jne 0x6b38
jmp 0x6b96
movl -0x64(%rsp), %ebx
movl %ecx, %eax
negl %eax
xorl %edx, %edx
testl %ecx, %ecx
cmovnsl %edx, %eax
cmovnsl %ecx, %edx
movq -0x58(%rsp), %rsi
movq 0x8(%rsi), %rcx
movl %eax, (%rcx)
movl %edx, 0x8(%rcx)
movl %r10d, 0x4(%rcx)
movl %ebx, 0xc(%rcx)
movl %r15d, 0x10(%rcx)
movl %r8d, 0x14(%rcx)
subl %eax, %r10d
cmpl 0x18(%rsi), %r10d
setl %al
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq