// File: arnoldrobbins[P]mcilroy-regex/re1.cpp
// Function: Kmp::parse(unsigned char*, Rex*, Eenv*)
int Kmp::parse(uchar *s, Rex* cont, Eenv *env)
{
	debug(KMP, "Kmp", s);
	uchar *map = env->preg->map;
	uchar *t = s;
	uchar *last = env->last;
	while(t+seg.n <= last) {
		int k = -1;
		for( ; t<last; t++) {
			while(k>=0 && seg.p[k+1] != map[*t])
				k = fail[k];
			if(seg.p[k+1] == map[*t])
				k++;
			if(k+1 == seg.n) {
				env->best[0].rm_so = ++t - s - seg.n;
				switch(follow(t, cont, env)) {
				case GOOD:
				case BEST:
					return BEST;
				case BAD:
					return BAD;
				}
				t -= seg.n - 1;
				break;
			}
		}
	}
	return NONE;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rax
movslq 0x20(%rcx), %rcx
addq %rcx, %rax
cmpq -0x40(%rbp), %rax
ja 0xa0a1
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0xa09c
jmp 0x9f54
xorl %eax, %eax
cmpl $0x0, -0x44(%rbp)
movb %al, -0x51(%rbp)
jl 0x9f8b
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %rdx
movzbl (%rdx), %edx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
setne %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0x9f94
jmp 0x9fab
movq -0x50(%rbp), %rdi
addq $0x28, %rdi
movl -0x44(%rbp), %esi
callq 0xbec0
movl (%rax), %eax
movl %eax, -0x44(%rbp)
jmp 0x9f54
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %rdx
movzbl (%rdx), %edx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x9fdc
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movq -0x50(%rbp), %rcx
movl -0x44(%rbp), %eax
addl $0x1, %eax
cmpl 0x20(%rcx), %eax
jne 0xa089
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rax
incq %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rdx
subq %rdx, %rax
movslq 0x20(%rcx), %rcx
subq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rdi
addq $0x418, %rdi            # imm = 0x418
xorl %esi, %esi
callq 0xbf80
movq -0x60(%rbp), %rcx
movq -0x50(%rbp), %rdi
movq %rcx, (%rax)
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0xbd70
movl %eax, %ecx
movl %ecx, %eax
decl %eax
movl %ecx, -0x58(%rbp)
subl $0x2, %eax
jb 0xa058
jmp 0xa04e
movl -0x58(%rbp), %eax
subl $0x3, %eax
je 0xa061
jmp 0xa06a
movl $0x2, -0x4(%rbp)
jmp 0xa0a8
movl $0x3, -0x4(%rbp)
jmp 0xa0a8
movq -0x50(%rbp), %rax
movl 0x20(%rax), %ecx
subl $0x1, %ecx
movq -0x38(%rbp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
jmp 0xa09c
jmp 0xa08b
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x9f44
jmp 0x9f24
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)