// File: /workspace/github_c_makefile_star2_O2/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
movl 0x10(%rdi), %r9d
movl 0x14(%rdi), %esi
movslq %r9d, %rax
testq %rax, %rax
movabsq $-0x1, %r8
cmovnsq %rax, %r8
movq %rdi, -0x48(%rsp)
movq (%rdi), %r10
incq %r8
leaq 0x8(%r10), %rcx
xorl %edx, %edx
cmpq %rdx, %r8
je 0x6378
andq $0x0, -0x4(%rcx)
movl %edx, -0x8(%rcx)
incq %rdx
addq $0xc, %rcx
jmp 0x6362
movq %r8, -0x40(%rsp)
imulq $0xc, %rax, %rcx
leaq (%r10,%rcx), %rdx
movq %rdx, -0x38(%rsp)
movl (%r10,%rcx), %r8d
cvtsi2sd %eax, %xmm0
mulsd 0x1b96(%rip), %xmm0     # 0x7f30
cvttsd2si %xmm0, %r15d
movl 0x8(%r10,%rcx), %eax
movl %eax, -0x64(%rsp)
leal 0x1(%r15), %ebp
xorl %ebx, %ebx
testl %esi, %esi
movl $0x0, %eax
movl %esi, -0x60(%rsp)
cmovgl %esi, %eax
incl %eax
movq %rax, -0x18(%rsp)
leaq 0x14(%r10), %rax
movq %rax, -0x30(%rsp)
movabsq $0x1, %r13
movsd 0x1b52(%rip), %xmm0     # 0x7f30
xorl %r14d, %r14d
movl %r9d, -0x5c(%rsp)
movq %r10, -0x20(%rsp)
movq %r15, -0x28(%rsp)
cmpq -0x18(%rsp), %r13
je 0x6595
movl %r14d, -0x54(%rsp)
movl %ebx, -0x50(%rsp)
movl %r8d, -0x4c(%rsp)
movl (%r10), %r12d
movl 0x4(%r10), %edx
movl 0x8(%r10), %ecx
andq $0x0, (%r10)
movl %r13d, %r11d
movl %r13d, 0x8(%r10)
xorl %r15d, %r15d
testl %ebp, %ebp
movl $0x0, %eax
cmovgl %ebp, %eax
movq %rax, -0x8(%rsp)
movq -0x30(%rsp), %rax
xorl %r8d, %r8d
movl %r13d, -0x58(%rsp)
xorl %r9d, %r9d
movq %r13, -0x10(%rsp)
cmpq %r15, -0x8(%rsp)
je 0x64f0
movl %ecx, %r10d
movq -0x48(%rsp), %rsi
movb 0x1c(%rsi,%r15), %cl
movq 0x220(%rsi), %rsi
cmpb -0x1(%rsi,%r13), %cl
sete %r14b
cmpb $0x4e, %cl
sete %sil
orb %r14b, %sil
movl %esi, %ecx
notb %cl
movzbl %cl, %r14d
andl $0x1, %r14d
movl %r12d, %ecx
addl %r14d, %ecx
movl -0x8(%rax), %r12d
leal 0x1(%r12), %r13d
leal 0x1(%r9), %r14d
cmpl %r13d, %ecx
setg %bl
cmpl %r14d, %ecx
setg %dil
orb %bl, %dil
je 0x64ba
cmpl %r12d, %r9d
jle 0x64cc
movl -0x4(%rax), %r8d
movl (%rax), %r11d
movl %r13d, %r14d
jmp 0x64cc
movzbl %sil, %esi
andl $0x1, %esi
addl %esi, %edx
movl %r10d, %r11d
movl %ecx, %r14d
movl %edx, %r8d
movl -0x4(%rax), %edx
movl (%rax), %ecx
movl %r14d, -0x8(%rax)
movl %r11d, (%rax)
movl %r8d, -0x4(%rax)
addq $0xc, %rax
incq %r15
movl %r14d, %r9d
movq -0x10(%rsp), %r13
jmp 0x6446
movslq %ebp, %rax
incl %ebp
imulq $0xc, %rax, %rax
movq -0x20(%rsp), %r10
addq %r10, %rax
movq -0x28(%rsp), %r15
decl %ebp
leaq -0xc(%rax), %rcx
cmpl %r15d, (%rax)
movq %rcx, %rax
jg 0x6506
movl -0x5c(%rsp), %r9d
cmpl %ebp, %r9d
movl -0x4c(%rsp), %edi
movl -0x50(%rsp), %r11d
movl -0x54(%rsp), %ebx
jle 0x652f
incl %ebp
jmp 0x6555
movq -0x38(%rsp), %rsi
movl 0x8(%rsi), %eax
movl %eax, %ecx
sarl $0x1f, %ecx
andl %eax, %ecx
addl %r9d, %ecx
movl (%rsi), %edx
cvtsi2sd %edx, %xmm1
cvtsi2sd %ecx, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0x6573
movl -0x64(%rsp), %eax
movl %r11d, %r8d
movl %edi, %edx
movl %ebx, %r14d
incq %r13
movl %eax, -0x64(%rsp)
movl %r8d, %ebx
movl %edx, %r8d
jmp 0x63f0
movl 0x4(%rsi), %r8d
cmpl %r11d, %r8d
jg 0x658e
sete %cl
cmpl %edi, %edx
setl %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0x6555
movl -0x58(%rsp), %r14d
jmp 0x6561
addq $0x8, %r10
xorl %r11d, %r11d
movl -0x60(%rsp), %esi
movq -0x40(%rsp), %rdi
movl -0x64(%rsp), %ebp
cmpq %r11, %rdi
je 0x65f9
movslq (%r10), %rax
movq %rax, %rcx
sarq $0x3f, %rcx
andl %eax, %ecx
leal (%r11,%rcx), %edx
movl -0x8(%r10), %ecx
cvtsi2sd %ecx, %xmm1
cvtsi2sd %edx, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jb 0x65f0
movl -0x4(%r10), %edx
cmpl %ebx, %edx
jg 0x65e3
jne 0x65f0
cmpl %r8d, %ecx
jge 0x65f0
movl %eax, %ebp
movl %edx, %ebx
movl %ecx, %r8d
movl %esi, %r14d
movl %r11d, %r9d
incq %r11
addq $0xc, %r10
jmp 0x65a9
movl %ebp, %eax
negl %eax
xorl %ecx, %ecx
testl %ebp, %ebp
cmovnsl %ecx, %eax
cmovnsl %ebp, %ecx
movq -0x48(%rsp), %rsi
movq 0x8(%rsi), %rdx
movl %eax, (%rdx)
movl %ecx, 0x8(%rdx)
movl %r9d, 0x4(%rdx)
movl %r14d, 0xc(%rdx)
movl %ebx, 0x10(%rdx)
movl %r8d, 0x14(%rdx)
subl %eax, %r9d
cmpl 0x18(%rsi), %r9d
setl %al
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq