// File: /workspace/github_cpp_makefile_star2_O3/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 $0xb8, %rsp
testl %esi, %esi
js 0x1405a
movq %rcx, %rbx
movl %esi, %ebp
movq %rdi, %r12
movl %esi, %eax
movq 0xe8(%rdi), %rcx
cmpq %rax, %rcx
jle 0x1405a
movl %r8d, %r13d
movq %rdx, %r14
movl 0xf0(%rsp), %r15d
movq 0x38(%r12), %rax
cqto
idivq %rcx
decq %rax
xorl %edx, %edx
movq %rdx, (%rsp)
movq %rax, 0x8(%rsp)
movl %edx, 0x10(%rsp)
movq %rdx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movl %edx, 0x30(%rsp)
imull %r15d, %ecx
movl %r8d, %edx
subl %ecx, %edx
incl %edx
movl 0x8(%r14), %eax
movl %edx, 0x58(%rsp)
subl %edx, %eax
cmpl %ebp, %eax
jge 0x140ad
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3ff87(%rip), %rbx     # 0x53fe8
leaq 0x30324(%rip), %rsi     # 0x4438c
movl $0xa, %edx
movq %rbx, %rdi
callq 0x7340
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x7040
movq %rax, %rdi
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x71d0
leaq 0xa0(%r12), %rax
movq %rax, 0x48(%rsp)
movq %rbx, 0x70(%rsp)
movq %r14, 0x68(%rsp)
movl %r15d, 0x54(%rsp)
movq %r13, 0x78(%rsp)
cmpb $0x1, 0xf0(%r12)
jne 0x140f6
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
movq %rsp, %rcx
movl 0x58(%rsp), %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x15b88
jmp 0x14111
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
movq %rsp, %rcx
movl 0x58(%rsp), %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x1605e
movl 0x10(%rsp), %ecx
cmpl 0x30(%rsp), %ecx
jle 0x1412c
movl 0x10(%rsp), %eax
movl %eax, 0x30(%rsp)
movaps (%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
cmpl $0x1, %ecx
jg 0x14143
movq 0x38(%r12), %rax
movq 0xe8(%r12), %rsi
jmp 0x1457f
cmpl 0x58(%rsp), %ecx
jge 0x142b8
movq 0xe8(%r12), %rsi
cmpb $0x1, 0xf1(%r12)
jne 0x1457a
movq %rsi, 0x60(%rsp)
movl %ebp, 0x44(%rsp)
testl %r15d, %r15d
jle 0x1428a
xorl %eax, %eax
movl %eax, 0x5c(%rsp)
movq 0xe8(%r12), %rsi
subl %esi, %ecx
movl %ecx, 0x10(%rsp)
testl %ecx, %ecx
jle 0x1460f
movq 0x70(%r12), %rdi
movq 0x88(%r12), %r8
movq (%rsp), %rax
movq 0x8(%rsp), %r9
movl (%rdi,%rax,4), %eax
xorl %edx, %edx
idivq %rsi
movslq 0x4(%r8,%rax,4), %rbx
movq %rbx, (%rsp)
movl (%rdi,%r9,4), %eax
xorl %edx, %edx
idivq %rsi
movslq 0x4(%r8,%rax,4), %rbp
movq %rbp, 0x8(%rsp)
addl %ecx, %ecx
movslq %ecx, %r14
imulq 0x40(%r12), %r14
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x15a0e
movl 0x10(%rsp), %ecx
cmpl %ecx, %eax
jge 0x141f4
movq %r14, %r13
xorl %r14d, %r14d
jmp 0x1422f
cmpq $0x2, %r14
movl $0x1, %r15d
movq %r14, %r13
cmovgeq %r14, %r15
decq %r15
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x1460f
incq %r14
decq %rbx
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x15a0e
movl 0x10(%rsp), %ecx
cmpl %ecx, %eax
jge 0x1420b
cmpq %rbp, 0x48(%r12)
jle 0x1426a
incq %r14
leaq 0x1(%rbp), %r15
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x15a0e
movl 0x10(%rsp), %ecx
cmpl %ecx, %eax
jl 0x1426a
cmpq %r13, %r14
jge 0x1460f
incq %r14
movq %r15, %rbp
cmpq 0x48(%r12), %r15
jl 0x14239
jmp 0x1426d
movq %rbp, %r15
movq %rbx, (%rsp)
movq %r15, 0x8(%rsp)
movl 0x5c(%rsp), %eax
incl %eax
movl 0x54(%rsp), %r15d
cmpl %r15d, %eax
jne 0x14178
movl 0x10(%rsp), %eax
movl %eax, 0x30(%rsp)
movq (%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x70(%rsp), %rbx
movq 0x68(%rsp), %r14
movl 0x44(%rsp), %ebp
jmp 0x14651
movl 0x8(%r14), %r8d
cmpb $0x1, 0xf0(%r12)
jne 0x142e1
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x15b88
jmp 0x142f9
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x1605e
movaps (%rsp), %xmm0
movaps %xmm0, 0xa0(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0xb0(%rsp)
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0x80(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0x90(%rsp)
movl 0xe8(%r12), %eax
imull %r15d, %eax
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
leaq 0xa0(%rsp), %rcx
leaq 0x80(%rsp), %r8
movq %rbx, %r9
pushq %rax
pushq %r13
callq 0x13064
addq $0x10, %rsp
movq 0xe8(%r12), %rsi
cmpb $0x1, 0xf1(%r12)
jne 0x1457a
testl %r15d, %r15d
jle 0x145a4
movq %rsi, 0x60(%rsp)
movl %ebp, 0x44(%rsp)
xorl %eax, %eax
movl %eax, 0x5c(%rsp)
movq 0xe8(%r12), %rsi
movl 0x10(%rsp), %ecx
subl %esi, %ecx
movl %ecx, 0x10(%rsp)
testl %ecx, %ecx
jle 0x145c1
movq 0x70(%r12), %rdi
movq 0x88(%r12), %r8
movq (%rsp), %rax
movq 0x8(%rsp), %r9
movl (%rdi,%rax,4), %eax
xorl %edx, %edx
idivq %rsi
movslq 0x4(%r8,%rax,4), %rbx
movq %rbx, (%rsp)
movl (%rdi,%r9,4), %eax
xorl %edx, %edx
idivq %rsi
movslq 0x4(%r8,%rax,4), %r14
movq %r14, 0x8(%rsp)
addl %ecx, %ecx
movslq %ecx, %rbp
imulq 0x40(%r12), %rbp
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x15a0e
cmpl 0x10(%rsp), %eax
jge 0x14401
xorl %r13d, %r13d
jmp 0x14437
cmpq $0x2, %rbp
movl $0x1, %r15d
cmovgeq %rbp, %r15
decq %r15
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x145c1
incq %r13
decq %rbx
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x15a0e
cmpl 0x10(%rsp), %eax
jge 0x14415
cmpq 0x48(%r12), %r14
jge 0x14465
leaq 0x1(%r14), %r15
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x15a0e
cmpl 0x10(%rsp), %eax
jl 0x14465
incq %r13
movq %r15, %r14
cmpq %rbp, %r13
jl 0x14437
jmp 0x145c1
movq %rbx, (%rsp)
movq %r14, 0x8(%rsp)
movq 0xe8(%r12), %rsi
movl 0x30(%rsp), %ecx
subl %esi, %ecx
movl %ecx, 0x30(%rsp)
testl %ecx, %ecx
jle 0x1454e
movq 0x70(%r12), %rdi
movq 0x88(%r12), %r8
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %r9
movl (%rdi,%rax,4), %eax
xorl %edx, %edx
idivq %rsi
movslq 0x4(%r8,%rax,4), %rbx
movq %rbx, 0x20(%rsp)
movl (%rdi,%r9,4), %eax
xorl %edx, %edx
idivq %rsi
movslq 0x4(%r8,%rax,4), %r14
movq %r14, 0x28(%rsp)
addl %ecx, %ecx
movslq %ecx, %rbp
imulq 0x40(%r12), %rbp
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x15a0e
cmpl 0x30(%rsp), %eax
jge 0x144e7
xorl %r13d, %r13d
jmp 0x14519
cmpq $0x2, %rbp
movl $0x1, %r15d
cmovgeq %rbp, %r15
decq %r15
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x1454e
incq %r13
decq %rbx
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x15a0e
cmpl 0x30(%rsp), %eax
jge 0x144fb
cmpq 0x48(%r12), %r14
jge 0x14544
leaq 0x1(%r14), %r15
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x15a0e
cmpl 0x30(%rsp), %eax
jl 0x14544
incq %r13
movq %r15, %r14
cmpq %rbp, %r13
jl 0x14519
jmp 0x1454e
movq %rbx, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movl 0x5c(%rsp), %eax
incl %eax
movl 0x54(%rsp), %r15d
cmpl %r15d, %eax
jne 0x14386
movq 0x78(%rsp), %r13
movq 0x70(%rsp), %rbx
movq 0x68(%rsp), %r14
movl 0x44(%rsp), %ebp
jmp 0x14608
movq 0x38(%r12), %rax
cqto
idivq %rsi
decq %rax
xorl %ecx, %ecx
movq %rcx, (%rsp)
movq %rax, 0x8(%rsp)
movl %ecx, 0x10(%rsp)
movq %rcx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movl %ecx, 0x30(%rsp)
imull %r15d, %esi
addl %ebp, %esi
movl 0x8(%r14), %eax
subl 0x58(%rsp), %eax
movl %esi, %ebp
cmpl %eax, %esi
jle 0x140ce
jmp 0x14048
movq 0x38(%r12), %rax
cqto
idivq 0xe8(%r12)
decq %rax
xorl %ecx, %ecx
movq %rcx, (%rsp)
movq %rax, 0x8(%rsp)
movl %ecx, 0x10(%rsp)
movq %rcx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movl %ecx, 0x30(%rsp)
movq 0x78(%rsp), %r13
movq 0x70(%rsp), %rbx
movq 0x68(%rsp), %r14
movl 0x44(%rsp), %ebp
movl 0x54(%rsp), %r15d
movq 0x60(%rsp), %rsi
jmp 0x145a4
movq 0x38(%r12), %rax
cqto
idivq 0xe8(%r12)
decq %rax
xorl %ecx, %ecx
movq %rcx, (%rsp)
movq %rax, 0x8(%rsp)
movl %ecx, 0x10(%rsp)
movq %rcx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movl %ecx, 0x30(%rsp)
movq 0x70(%rsp), %rbx
movq 0x68(%rsp), %r14
movl 0x44(%rsp), %ebp
movl 0x54(%rsp), %r15d
movq 0x60(%rsp), %rsi
movq 0x78(%rsp), %r13
jmp 0x145a4