// File: /workspace/github_cpp_makefile_star2_O2/readmapping[P]alfalfa/src/sparseSA.cpp
// Function: sparseSA::findMEM(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<match_t, std::allocator<match_t>>&, int, int, int) const
void sparseSA::findMEM(int k, const string &P, vector<match_t> &matches, int min_len, int maxCount, int sparseQ) const {
  if(k < 0 || k >= K) { cerr << "Invalid k." << endl; return; }
  // Offset all intervals at different start points.
  int prefix = k;
  interval_t mli(0,N/K-1,0); // min length interval
  interval_t xmi(0,N/K-1,0); // max match interval

  // Right-most match used to terminate search.
  int min_lenK = min_len - (sparseQ * K - 1);

  while( prefix <= (int)P.length() - (min_lenK)) {//bugfix (was (K-k) before)
    if (hasChild)
        traverse_faster(P, prefix, mli, min_lenK, -1); // Traverse until minimum length matched.
    else
        traverse(P, prefix, mli, min_lenK, -1);    // Traverse until minimum length matched.
    if(mli.depth > xmi.depth) xmi = mli;
    if(mli.depth <= 1) { mli.reset(N/K-1); xmi.reset(N/K-1); prefix += sparseQ*K; continue; }

    if(mli.depth >= min_lenK) {
      if (hasChild)
          traverse_faster(P, prefix, xmi, P.length(), -1); // Traverse until mismatch.
      else
          traverse(P, prefix, xmi, P.length(), -1); // Traverse until mismatch.
      collectAllMEMs(P, prefix, mli, xmi, matches, min_len, sparseQ*K); // Using LCP info to find MEM length.
      // When using ISA/LCP trick, depth = depth - K. prefix += K.
      prefix += sparseQ*K;
      if (!hasSufLink) {
        mli.reset(N / K - 1);
        xmi.reset(N / K - 1);
        continue;
      }
      else{
        int i = 0;
        bool succes = true;
        while (i < sparseQ && (succes = suffixlink(mli))) {
            suffixlink(xmi);
            i++;
        }
        if (!succes) {
            mli.reset(N / K - 1);
            xmi.reset(N / K - 1);
            continue;
        }
      }
    }
    else {
        // When using ISA/LCP trick, depth = depth - K. prefix += K.
        prefix += sparseQ*K;
        if (!hasSufLink) {
            mli.reset(N / K - 1);
            xmi.reset(N / K - 1);
            continue;
        } else {
            int i = 0;
            bool succes = true;
            while (i < sparseQ && (succes = suffixlink(mli))) {
                i++;
            }
            if (!succes) {
                mli.reset(N / K - 1);
                xmi.reset(N / K - 1);
                continue;
            }
            xmi = mli;
        }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r8, 0x58(%rsp)
movq %rcx, 0x68(%rsp)
testl %esi, %esi
js 0xff20
movl %esi, %ebp
movq %rdi, %r12
movl %esi, %eax
movq 0xe8(%rdi), %rcx
cmpq %rax, %rcx
jle 0xff20
movq %rdx, %r15
movl 0xe0(%rsp), %r13d
movq 0x38(%r12), %rax
cqto
idivq %rcx
decq %rax
leaq 0x10(%rsp), %rdx
andq $0x0, (%rdx)
movq %rax, 0x8(%rdx)
andl $0x0, 0x10(%rdx)
leaq 0x30(%rsp), %rdx
andq $0x0, (%rdx)
movq %rax, 0x8(%rdx)
andl $0x0, 0x10(%rdx)
imull %r13d, %ecx
movq 0x58(%rsp), %rax
movl %eax, %ebx
subl %ecx, %ebx
incl %ebx
xorl %eax, %eax
testl %r13d, %r13d
cmovgl %r13d, %eax
movq %rax, 0x60(%rsp)
incl %eax
movl %eax, 0x54(%rsp)
movl %r13d, 0xc(%rsp)
movl 0x8(%r15), %eax
subl %ebx, %eax
cmpl %eax, %ebp
jg 0xff4c
cmpb $0x1, 0xf0(%r12)
jne 0xfd28
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
leaq 0x10(%rsp), %rcx
movl %ebx, %r8d
pushq $-0x1
popq %r9
callq 0x112ce
jmp 0xfd41
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
leaq 0x10(%rsp), %rcx
movl %ebx, %r8d
pushq $-0x1
popq %r9
callq 0x11590
movl 0x20(%rsp), %eax
cmpl 0x40(%rsp), %eax
jle 0xfd5d
movl 0x20(%rsp), %ecx
movl %ecx, 0x40(%rsp)
movaps 0x10(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
cmpl $0x1, %eax
jg 0xfd9c
movq 0x38(%r12), %rax
movq 0xe8(%r12), %r14
cqto
idivq %r14
decq %rax
andq $0x0, 0x10(%rsp)
movq %rax, 0x18(%rsp)
andl $0x0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq %rax, 0x38(%rsp)
andl $0x0, 0x40(%rsp)
jmp 0xfefd
cmpl %ebx, %eax
jge 0xfdea
movq 0xe8(%r12), %r14
cmpb $0x1, 0xf1(%r12)
movl 0x54(%rsp), %r13d
jne 0xfecb
decl %r13d
je 0xff0c
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x116b4
testb %al, %al
jne 0xfdbc
movq 0x38(%r12), %rax
cqto
idivq 0xe8(%r12)
jmp 0xfed5
movl 0x8(%r15), %r8d
cmpb $0x1, 0xf0(%r12)
jne 0xfe11
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
leaq 0x30(%rsp), %rcx
pushq $-0x1
popq %r9
callq 0x112ce
jmp 0xfe27
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
leaq 0x30(%rsp), %rcx
pushq $-0x1
popq %r9
callq 0x11590
movaps 0x10(%rsp), %xmm0
movaps %xmm0, 0x90(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0xa0(%rsp)
movaps 0x30(%rsp), %xmm0
movaps %xmm0, 0x70(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x80(%rsp)
movl 0xe8(%r12), %eax
imull %r13d, %eax
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
leaq 0x90(%rsp), %rcx
leaq 0x70(%rsp), %r8
movq 0x68(%rsp), %r9
pushq %rax
pushq 0x60(%rsp)
callq 0xf42a
popq %rax
popq %rcx
movq 0xe8(%r12), %r14
cmpb $0x1, 0xf1(%r12)
movq 0x60(%rsp), %rax
movl %eax, %r13d
jne 0xfecb
subl $0x1, %r13d
jb 0xfef8
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x116b4
testb %al, %al
je 0xfdd6
movq %r12, %rdi
leaq 0x30(%rsp), %rsi
callq 0x116b4
jmp 0xfea1
movq 0x38(%r12), %rax
cqto
idivq %r14
decq %rax
andq $0x0, 0x10(%rsp)
movq %rax, 0x18(%rsp)
andl $0x0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq %rax, 0x38(%rsp)
andl $0x0, 0x40(%rsp)
movl 0xc(%rsp), %r13d
imull %r13d, %r14d
addl %ebp, %r14d
movl %r14d, %ebp
jmp 0xfcf4
movl 0x20(%rsp), %eax
movl %eax, 0x40(%rsp)
movaps 0x10(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
jmp 0xfef8
movq 0x3a0c1(%rip), %rdi     # 0x49fe8
leaq 0x264ae(%rip), %rsi     # 0x363dc
callq 0x72b0
movq %rax, %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7160
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq