// File: auton1[P]motiflocation/motif.cpp
// Function: motif::IsMotifInSequence(char const*, char const*, int)
bool motif::IsMotifInSequence(const char* inputseq, const char* wild, int allowed_mutations)
{
	const char *cp = inputseq;
	const char *mp = wild;
	bool match;
	int max_gap_size;
	int max_gap_size_plus_one;
	char *buffer;
	size_t sizeof_char = sizeof( char ); 
	char null_char = (char)NULL;
	int mutations = 0;

	while ((*inputseq) && (*wild))
	{
		if (basematch(*inputseq, *wild))	// Compare degenerate bases
		{
			wild++;		// Move to next character
			inputseq++;
		} 
		else if ((*wild > '0') && (*wild <= '9'))	// This bit allows for gaps in the motif.
		{
			max_gap_size = (*wild - '0') + max_length;
			max_gap_size_plus_one = max_gap_size+1;
			while ((*wild) && (*wild > '0') && (*wild <= '9'))		// Remove all prefixed numbers
				wild++;
			buffer = new char[max_gap_size_plus_one];
			strncpy(buffer, inputseq, sizeof_char * max_gap_size_plus_one);
			buffer[max_gap_size] = null_char;
			match = IsMotifInSequence(buffer, wild, allowed_mutations - mutations);
			delete [] buffer;
			if (match)
				return true;	// The call to the recursion returned true, so we should return true.
			else
			{
				wild = mp;	// We do not have a match,
				cp++;		// so reset the wild string, and goto the next char on the inputsequence.
				inputseq = cp;
				mutations = 0;
			}
		}
		else if (mutations < allowed_mutations)
		{
			wild++;		// Move to next character
			inputseq++;
			mutations++;
		}
		else 
		{
			wild = mp;	// We do not have a match,
			cp++;		// so reset the wild string, and goto the next char on the inputsequence.
			inputseq = cp;
			mutations = 0;
		}
	}

	return (bool)!*wild;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movl %ecx, 0x54(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x40(%rsp)
movq $0x1, 0x20(%rsp)
movb $0x0, 0x1f(%rsp)
movl $0x0, 0x18(%rsp)
movq 0x60(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, 0xf(%rsp)
je 0xb05a
movq 0x58(%rsp), %rax
cmpb $0x0, (%rax)
setne %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0xb067
jmp 0xb257
movq 0x10(%rsp), %rdi
movq 0x60(%rsp), %rax
movb (%rax), %cl
movq 0x58(%rsp), %rax
movsbl %cl, %esi
movsbl (%rax), %edx
callq 0xb680
testb $0x1, %al
jne 0xb089
jmp 0xb0aa
movq 0x58(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x58(%rsp)
movq 0x60(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x60(%rsp)
jmp 0xb252
movq 0x58(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x30, %eax
jle 0xb1f1
movq 0x58(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x39, %eax
jg 0xb1f1
movq 0x10(%rsp), %rcx
movq 0x58(%rsp), %rax
movsbl (%rax), %eax
subl $0x30, %eax
addl 0x70(%rcx), %eax
movl %eax, 0x38(%rsp)
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
movq 0x58(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, 0xe(%rsp)
je 0xb123
movq 0x58(%rsp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x30, %ecx
movb %al, 0xe(%rsp)
jle 0xb123
movq 0x58(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x39, %eax
setle %al
movb %al, 0xe(%rsp)
movb 0xe(%rsp), %al
testb $0x1, %al
jne 0xb12d
jmp 0xb13d
movq 0x58(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x58(%rsp)
jmp 0xb0ee
movslq 0x34(%rsp), %rdi
callq 0x4030
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x20(%rsp), %rdx
movslq 0x34(%rsp), %rax
imulq %rax, %rdx
callq 0x41a0
movq 0x10(%rsp), %rdi
movb 0x1f(%rsp), %dl
movq 0x28(%rsp), %rax
movslq 0x38(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x28(%rsp), %rsi
movq 0x58(%rsp), %rdx
movl 0x54(%rsp), %ecx
subl 0x18(%rsp), %ecx
callq 0xaff0
andb $0x1, %al
movb %al, 0x3f(%rsp)
movq 0x28(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xb1b4
movq (%rsp), %rdi
callq 0x43f0
testb $0x1, 0x3f(%rsp)
je 0xb1c5
movb $0x1, 0x77(%rsp)
jmp 0xb26a
movq 0x40(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x60(%rsp)
movl $0x0, 0x18(%rsp)
jmp 0xb250
movl 0x18(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0xb224
movq 0x58(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x58(%rsp)
movq 0x60(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x60(%rsp)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0xb24e
movq 0x40(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x60(%rsp)
movl $0x0, 0x18(%rsp)
jmp 0xb250
jmp 0xb252
jmp 0xb03b
movq 0x58(%rsp), %rax
cmpb $0x0, (%rax)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, 0x77(%rsp)
movb 0x77(%rsp), %al
andb $0x1, %al
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)