// File: /workspace/github_cpp_makefile_star2_O1/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
movq %r8, 0x70(%rsp)
movq %rcx, 0x78(%rsp)
testl %esi, %esi
js 0x142b0
movl %esi, %ebp
movq %rdi, %r14
movl %esi, %eax
movq 0xe8(%rdi), %rcx
cmpq %rax, %rcx
jle 0x142b0
movq %rdx, %rbx
movl 0xf0(%rsp), %r15d
movq 0x38(%r14), %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
movq 0x70(%rsp), %rax
movl %eax, %edx
subl %ecx, %edx
incl %edx
movl 0x8(%rbx), %eax
movl %edx, 0x54(%rsp)
subl %edx, %eax
cmpl %ebp, %eax
jge 0x14303
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3fd31(%rip), %rbx     # 0x53fe8
leaq 0x300ce(%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(%r14), %rax
movq %rax, 0x48(%rsp)
movq %rbx, 0x60(%rsp)
movq %r14, 0x40(%rsp)
movl %r15d, 0x1c(%rsp)
cmpb $0x1, 0xf0(%r14)
jne 0x14345
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movq %rsp, %rcx
movl 0x54(%rsp), %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x15cf4
jmp 0x14360
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movq %rsp, %rcx
movl 0x54(%rsp), %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x16208
movl 0x10(%rsp), %eax
cmpl 0x30(%rsp), %eax
jle 0x1437b
movl 0x10(%rsp), %ecx
movl %ecx, 0x30(%rsp)
movaps (%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
cmpl $0x1, %eax
jg 0x14390
movq 0x38(%r14), %rax
movq 0xe8(%r14), %rdi
jmp 0x1480a
cmpl 0x54(%rsp), %eax
movl %ebp, 0x3c(%rsp)
jge 0x144e9
movq 0xe8(%r14), %rdi
cmpb $0x1, 0xf1(%r14)
jne 0x14806
testl %r15d, %r15d
jle 0x144d3
xorl %ebp, %ebp
movq %rdi, 0x58(%rsp)
movq 0xe8(%r14), %rsi
movl 0x10(%rsp), %ecx
subl %esi, %ecx
movl %ecx, 0x10(%rsp)
testl %ecx, %ecx
jle 0x14432
movq 0x70(%r14), %rdi
movq 0x88(%r14), %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), %r13
movq %r13, 0x8(%rsp)
addl %ecx, %ecx
movslq %ecx, %r12
imulq 0x40(%r14), %r12
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x15b7a
cmpl 0x10(%rsp), %eax
jge 0x14436
xorl %r14d, %r14d
jmp 0x14468
xorl %eax, %eax
jmp 0x144b3
cmpq $0x2, %r12
movl $0x1, %r15d
cmovgeq %r12, %r15
decq %r15
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x14495
incq %r14
decq %rbx
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x15b7a
cmpl 0x10(%rsp), %eax
jge 0x1444a
movq 0x40(%rsp), %rax
cmpq 0x48(%rax), %r13
jge 0x14499
leaq 0x1(%r13), %r15
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x15b7a
cmpl 0x10(%rsp), %eax
jl 0x14499
incq %r14
movq %r15, %r13
cmpq %r12, %r14
jl 0x14468
xorl %eax, %eax
jmp 0x144a4
movq %rbx, (%rsp)
movq %r13, 0x8(%rsp)
movb $0x1, %al
movq 0x40(%rsp), %r14
movl 0x1c(%rsp), %r15d
movq 0x58(%rsp), %rdi
testb %al, %al
je 0x144c2
incl %ebp
cmpl %r15d, %ebp
jl 0x143c3
testb %al, %al
movq 0x60(%rsp), %rbx
movl 0x3c(%rsp), %ebp
je 0x1484b
movl 0x10(%rsp), %eax
movl %eax, 0x30(%rsp)
movaps (%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
jmp 0x1482f
movl 0x8(%rbx), %r8d
cmpb $0x1, 0xf0(%r14)
jne 0x14511
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x15cf4
jmp 0x14529
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x16208
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(%r14), %eax
imull %r15d, %eax
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
leaq 0xa0(%rsp), %rcx
leaq 0x80(%rsp), %r8
movq 0x78(%rsp), %r9
pushq %rax
pushq 0x78(%rsp)
callq 0x13206
addq $0x10, %rsp
movq 0xe8(%r14), %rdi
cmpb $0x1, 0xf1(%r14)
jne 0x14806
testl %r15d, %r15d
jle 0x1482f
xorl %eax, %eax
movq %rdi, 0x58(%rsp)
movl %eax, 0x6c(%rsp)
movq 0xe8(%r14), %rsi
movl 0x10(%rsp), %ecx
subl %esi, %ecx
movl %ecx, 0x10(%rsp)
testl %ecx, %ecx
jle 0x14626
movq 0x70(%r14), %rdi
movq 0x88(%r14), %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), %r13
movq %r13, 0x8(%rsp)
addl %ecx, %ecx
movslq %ecx, %rbp
imulq 0x40(%r14), %rbp
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x15b7a
cmpl 0x10(%rsp), %eax
jge 0x1462d
xorl %r12d, %r12d
jmp 0x1465f
xorl %eax, %eax
jmp 0x146b7
cmpq $0x2, %rbp
movl $0x1, %r14d
cmovgeq %rbp, %r14
decq %r14
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x146a2
incq %r12
decq %rbx
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x15b7a
cmpl 0x10(%rsp), %eax
jge 0x14641
movq 0x40(%rsp), %r14
cmpq 0x48(%r14), %r13
jge 0x14690
leaq 0x1(%r13), %r15
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x15b7a
cmpl 0x10(%rsp), %eax
jl 0x14690
incq %r12
movq %r15, %r13
cmpq %rbp, %r12
jl 0x14664
xorl %eax, %eax
jmp 0x1469b
movq %rbx, (%rsp)
movq %r13, 0x8(%rsp)
movb $0x1, %al
movq 0x60(%rsp), %rbx
jmp 0x146ae
xorl %eax, %eax
movq 0x60(%rsp), %rbx
movq 0x40(%rsp), %r14
movl 0x3c(%rsp), %ebp
movq 0x58(%rsp), %rdi
testb %al, %al
je 0x147d2
movl %eax, 0x68(%rsp)
movq 0xe8(%r14), %rsi
movl 0x30(%rsp), %ecx
subl %esi, %ecx
movl %ecx, 0x30(%rsp)
testl %ecx, %ecx
jle 0x147a8
movq 0x40(%rsp), %r10
movq 0x70(%r10), %rdi
movq 0x88(%r10), %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), %r13
movq %r13, 0x28(%rsp)
addl %ecx, %ecx
movslq %ecx, %rbp
imulq 0x40(%r10), %rbp
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x15b7a
cmpl 0x30(%rsp), %eax
jge 0x1473d
xorl %r12d, %r12d
jmp 0x1476f
cmpq $0x2, %rbp
movl $0x1, %r14d
cmovgeq %rbp, %r14
decq %r14
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x147a8
incq %r12
decq %rbx
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x15b7a
cmpl 0x30(%rsp), %eax
jge 0x14751
movq 0x40(%rsp), %rax
cmpq 0x48(%rax), %r13
jge 0x1479e
leaq 0x1(%r13), %r15
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x15b7a
cmpl 0x30(%rsp), %eax
jl 0x1479e
incq %r12
movq %r15, %r13
cmpq %rbp, %r12
jl 0x1476f
jmp 0x147a8
movq %rbx, 0x20(%rsp)
movq %r13, 0x28(%rsp)
movl 0x6c(%rsp), %eax
incl %eax
cmpl 0x1c(%rsp), %eax
movq 0x60(%rsp), %rbx
movq 0x40(%rsp), %r14
movl 0x3c(%rsp), %ebp
movq 0x58(%rsp), %rdi
jne 0x145b3
cmpb $0x0, 0x68(%rsp)
jne 0x147ff
movq 0x38(%r14), %rax
cqto
idivq 0xe8(%r14)
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)
movl 0x1c(%rsp), %r15d
jmp 0x1482f
movq 0x38(%r14), %rax
cqto
idivq %rdi
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, %edi
addl %ebp, %edi
movl 0x8(%rbx), %eax
subl 0x54(%rsp), %eax
movl %edi, %ebp
cmpl %eax, %edi
jle 0x1431e
jmp 0x1429e
movq 0x38(%r14), %rax
cqto
idivq 0xe8(%r14)
jmp 0x1480f