// File: 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:
subq $0xf8, %rsp
movl 0x100(%rsp), %eax
movq %rdi, 0xf0(%rsp)
movl %esi, 0xec(%rsp)
movq %rdx, 0xe0(%rsp)
movq %rcx, 0xd8(%rsp)
movl %r8d, 0xd4(%rsp)
movl %r9d, 0xd0(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpl $0x0, 0xec(%rsp)
jl 0x1c20a
movq 0x48(%rsp), %rcx
movslq 0xec(%rsp), %rax
cmpq 0xe8(%rcx), %rax
jl 0x1c231
movq 0x7addf(%rip), %rdi     # 0x96ff0
leaq 0x61184(%rip), %rsi     # 0x7d39c
callq 0x8360
movq %rax, %rdi
movq 0x7ad89(%rip), %rsi     # 0x96fb0
callq 0x83b0
jmp 0x1c801
movq 0x48(%rsp), %rcx
movl 0xec(%rsp), %eax
movl %eax, 0xcc(%rsp)
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rdx
subq $0x1, %rdx
leaq 0xb0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %ecx, %ecx
callq 0x1eea0
movq 0x48(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rdx
subq $0x1, %rdx
leaq 0x98(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %ecx, %ecx
callq 0x1eea0
movq 0x48(%rsp), %rdx
movslq 0xd4(%rsp), %rax
movslq 0x100(%rsp), %rcx
imulq 0xe8(%rdx), %rcx
subq $0x1, %rcx
subq %rcx, %rax
movl %eax, 0x94(%rsp)
movl 0xcc(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x8630
movq %rax, %rcx
movl 0x44(%rsp), %eax
subl 0x94(%rsp), %ecx
cmpl %ecx, %eax
jg 0x1c801
movq 0x48(%rsp), %rax
testb $0x1, 0xf0(%rax)
je 0x1c32f
movq 0x48(%rsp), %rdi
movq 0xe0(%rsp), %rsi
movl 0xcc(%rsp), %edx
movl 0x94(%rsp), %r8d
leaq 0xb0(%rsp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x1fc90
jmp 0x1c35e
movq 0x48(%rsp), %rdi
movq 0xe0(%rsp), %rsi
movl 0xcc(%rsp), %edx
movl 0x94(%rsp), %r8d
leaq 0xb0(%rsp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x205e0
movl 0xc0(%rsp), %eax
cmpl 0xa8(%rsp), %eax
jle 0x1c39c
movq 0xb0(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0xa0(%rsp)
movl 0xc0(%rsp), %eax
movl %eax, 0xa8(%rsp)
cmpl $0x1, 0xc0(%rsp)
jg 0x1c41e
movq 0x48(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0xb0(%rsp), %rdi
callq 0x20830
movq 0x48(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x98(%rsp), %rdi
callq 0x20830
movq 0x48(%rsp), %rax
movslq 0x100(%rsp), %rcx
imulq 0xe8(%rax), %rcx
movslq 0xcc(%rsp), %rax
addq %rcx, %rax
movl %eax, 0xcc(%rsp)
jmp 0x1c2c2
movl 0xc0(%rsp), %eax
cmpl 0x94(%rsp), %eax
jl 0x1c69d
movq 0x48(%rsp), %rax
testb $0x1, 0xf0(%rax)
je 0x1c48b
movq 0xe0(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0xcc(%rsp), %eax
movl %eax, 0x40(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x8630
movq 0x48(%rsp), %rdi
movq 0x38(%rsp), %rsi
movl 0x40(%rsp), %edx
movl %eax, %r8d
leaq 0x98(%rsp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x1fc90
jmp 0x1c4d4
movq 0xe0(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0xcc(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x8630
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl 0x34(%rsp), %edx
movl %eax, %r8d
leaq 0x98(%rsp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x205e0
movq 0xe0(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0xcc(%rsp), %eax
movl %eax, 0x24(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x1fbd0
leaq 0x60(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x1fbd0
movq 0x18(%rsp), %rsi
movl 0x24(%rsp), %edx
movq 0x48(%rsp), %rdi
movq 0xd8(%rsp), %r9
movl 0xd4(%rsp), %r10d
movslq 0x100(%rsp), %rax
imulq 0xe8(%rdi), %rax
leaq 0x78(%rsp), %rcx
leaq 0x60(%rsp), %r8
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x1ada0
movq 0x48(%rsp), %rax
movslq 0x100(%rsp), %rdx
imulq 0xe8(%rax), %rdx
movslq 0xcc(%rsp), %rcx
addq %rdx, %rcx
movl %ecx, 0xcc(%rsp)
testb $0x1, 0xf1(%rax)
jne 0x1c5d6
movq 0x48(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0xb0(%rsp), %rdi
callq 0x20830
movq 0x48(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x98(%rsp), %rdi
callq 0x20830
jmp 0x1c2c2
movl $0x0, 0x5c(%rsp)
movb $0x1, 0x5b(%rsp)
movl 0x5c(%rsp), %ecx
xorl %eax, %eax
cmpl 0x100(%rsp), %ecx
movb %al, 0x17(%rsp)
jge 0x1c615
movq 0x48(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x20860
movb %al, %cl
andb $0x1, %cl
movb %cl, 0x5b(%rsp)
movb %al, 0x17(%rsp)
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0x1c61f
jmp 0x1c63e
movq 0x48(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x20860
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x1c5e3
testb $0x1, 0x5b(%rsp)
jne 0x1c696
movq 0x48(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0xb0(%rsp), %rdi
callq 0x20830
movq 0x48(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x98(%rsp), %rdi
callq 0x20830
jmp 0x1c2c2
jmp 0x1c698
jmp 0x1c7fc
movq 0x48(%rsp), %rax
movslq 0x100(%rsp), %rdx
imulq 0xe8(%rax), %rdx
movslq 0xcc(%rsp), %rcx
addq %rdx, %rcx
movl %ecx, 0xcc(%rsp)
testb $0x1, 0xf1(%rax)
jne 0x1c71e
movq 0x48(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0xb0(%rsp), %rdi
callq 0x20830
movq 0x48(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x98(%rsp), %rdi
callq 0x20830
jmp 0x1c2c2
movl $0x0, 0x54(%rsp)
movb $0x1, 0x53(%rsp)
movl 0x54(%rsp), %ecx
xorl %eax, %eax
cmpl 0x100(%rsp), %ecx
movb %al, 0x16(%rsp)
jge 0x1c75d
movq 0x48(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x20860
movb %al, %cl
andb $0x1, %cl
movb %cl, 0x53(%rsp)
movb %al, 0x16(%rsp)
movb 0x16(%rsp), %al
testb $0x1, %al
jne 0x1c767
jmp 0x1c774
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x1c72b
testb $0x1, 0x53(%rsp)
jne 0x1c7cc
movq 0x48(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0xb0(%rsp), %rdi
callq 0x20830
movq 0x48(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x98(%rsp), %rdi
callq 0x20830
jmp 0x1c2c2
movq 0xb0(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0xa0(%rsp)
movl 0xc0(%rsp), %eax
movl %eax, 0xa8(%rsp)
jmp 0x1c7fc
jmp 0x1c2c2
addq $0xf8, %rsp
retq
nopl (%rax)