// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %rbx
movb (%rsi), %al
testb %al, %al
je 0x5aff
movl %ecx, %ebp
movq %rsi, %r12
movq %rdi, %r14
movl $0x0, (%rsp)
movq %rbx, %r15
movq %rsi, %r13
movq %rbx, 0x8(%rsp)
movl %ecx, 0x4(%rsp)
movb (%r15), %cl
testb %cl, %cl
je 0x5b02
movsbl %al, %esi
movsbl %cl, %edx
movq %r14, %rdi
callq 0x5cf2
testb %al, %al
je 0x5a3d
incq %r15
incq %r12
jmp 0x5af1
movb (%r15), %al
leal -0x31(%rax), %ecx
cmpb $0x8, %cl
ja 0x5acd
movq %r13, 0x10(%rsp)
addb $-0x30, %al
movzbl %al, %ebp
addl 0x70(%r14), %ebp
decq %r15
movb 0x1(%r15), %al
incq %r15
addb $-0x31, %al
cmpb $0x9, %al
jb 0x5a5d
leal 0x1(%rbp), %eax
movslq %eax, %r13
movq %r13, %rdi
callq 0x3030
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3120
movslq %ebp, %rax
movb $0x0, (%rbx,%rax)
movl 0x4(%rsp), %ebp
movl %ebp, %ecx
subl (%rsp), %ecx
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x59dc
movl %eax, %r15d
movq %rbx, %rdi
callq 0x32f0
testb %r15b, %r15b
jne 0x5b18
movq 0x10(%rsp), %r13
incq %r13
movl $0x0, (%rsp)
movq 0x8(%rsp), %rbx
jmp 0x5aeb
movl (%rsp), %eax
cmpl %eax, %ebp
jle 0x5ae1
incq %r15
incq %r12
incl %eax
movl %eax, (%rsp)
jmp 0x5af1
incq %r13
movl $0x0, (%rsp)
movq %rbx, %r15
movq %r13, %r12
movb (%r12), %al
testb %al, %al
jne 0x5a15
jmp 0x5b02
movq %rbx, %r15
cmpb $0x0, (%r15)
sete %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %al
jmp 0x5b09