// File: readmapping[P]alfalfa/src/sparseSA.cpp
// Function: sparseSA::findSMAM(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::findSMAM(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 xmi(0,N/K-1,0); // max match interval

  // Right-most match used to terminate search.
  int min_lenK = min_len - (sparseQ*K-1);
  int minMin_len = sparseQ > 1 ? min_len-K : min_len;
  while( prefix <= (int)P.length() - (min_lenK)) {//bugfix (was (K-k) before)
    if(hasChild) 
        traverse_faster(P, prefix, xmi, P.length(), -1);
    else
        traverse(P, prefix, xmi, P.length(), -1);// Traverse until mismatch.
    if(xmi.depth <= 1) { xmi.reset(N/K-1); prefix += sparseQ*K; continue; }
    if(xmi.depth >= min_lenK) {//less restriction seems to give best results
        collectAllSMAMs(P, prefix, xmi, matches, minMin_len, sparseQ*maxCount, sparseQ*K); // Using LCP info to find MEM length.
        // When using ISA/LCP trick, depth = depth - K. prefix += K.
        prefix += sparseQ*K;
        if (!hasSufLink) {
            xmi.reset(N / K - 1);
            continue;
        } else {
            int i = 0;
            bool succes = true;
            while (i < sparseQ && (succes = suffixlink(xmi)))
                i++;
            if (!succes) {
                xmi.reset(N / K - 1);
                continue;
            }
        }
    }
    else {
      // When using ISA/LCP trick, depth = depth - K. prefix += K.
      prefix += sparseQ*K;
      if (!hasSufLink) {
            xmi.reset(N / K - 1);
            continue;
      } else {
        int i = 0;
        bool succes = true;
        while (i < sparseQ && (succes = suffixlink(xmi)))
            i++;
        if (!succes) {
            xmi.reset(N / K - 1);
            continue;
        }
      }
    }
  }
}

// Assembly:
subq $0xd8, %rsp
movl 0xe0(%rsp), %eax
movq %rdi, 0xd0(%rsp)
movl %esi, 0xcc(%rsp)
movq %rdx, 0xc0(%rsp)
movq %rcx, 0xb8(%rsp)
movl %r8d, 0xb4(%rsp)
movl %r9d, 0xb0(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpl $0x0, 0xcc(%rsp)
jl 0x1c87a
movq 0x58(%rsp), %rcx
movslq 0xcc(%rsp), %rax
cmpq 0xe8(%rcx), %rax
jl 0x1c8a1
movq 0x7a76f(%rip), %rdi     # 0x96ff0
leaq 0x60b14(%rip), %rsi     # 0x7d39c
callq 0x8360
movq %rax, %rdi
movq 0x7a719(%rip), %rsi     # 0x96fb0
callq 0x83b0
jmp 0x1ccd5
movq 0x58(%rsp), %rcx
movl 0xcc(%rsp), %eax
movl %eax, 0xac(%rsp)
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rdx
subq $0x1, %rdx
leaq 0x90(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %ecx, %ecx
callq 0x1eea0
movq 0x58(%rsp), %rdx
movslq 0xb4(%rsp), %rax
movslq 0xe0(%rsp), %rcx
imulq 0xe8(%rdx), %rcx
subq $0x1, %rcx
subq %rcx, %rax
movl %eax, 0x8c(%rsp)
cmpl $0x1, 0xe0(%rsp)
jle 0x1c92b
movq 0x58(%rsp), %rcx
movslq 0xb4(%rsp), %rax
subq 0xe8(%rcx), %rax
movq %rax, 0x50(%rsp)
jmp 0x1c938
movslq 0xb4(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movl %eax, 0x88(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0xc0(%rsp), %rdi
callq 0x8630
movq %rax, %rcx
movl 0x4c(%rsp), %eax
subl 0x8c(%rsp), %ecx
cmpl %ecx, %eax
jg 0x1ccd5
movq 0x58(%rsp), %rax
testb $0x1, 0xf0(%rax)
je 0x1c9cb
movq 0xc0(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0xc0(%rsp), %rdi
callq 0x8630
movq 0x58(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl 0x48(%rsp), %edx
movl %eax, %r8d
leaq 0x90(%rsp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x1fc90
jmp 0x1ca14
movq 0xc0(%rsp), %rax
movq %rax, 0x30(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0xc0(%rsp), %rdi
callq 0x8630
movq 0x58(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x3c(%rsp), %edx
movl %eax, %r8d
leaq 0x90(%rsp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x205e0
cmpl $0x1, 0xa0(%rsp)
jg 0x1ca70
movq 0x58(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x90(%rsp), %rdi
callq 0x20830
movq 0x58(%rsp), %rax
movslq 0xe0(%rsp), %rcx
imulq 0xe8(%rax), %rcx
movslq 0xac(%rsp), %rax
addq %rcx, %rax
movl %eax, 0xac(%rsp)
jmp 0x1c944
movl 0xa0(%rsp), %eax
cmpl 0x8c(%rsp), %eax
jl 0x1cbe9
movq 0xc0(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x2c(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x1fbd0
movq 0x20(%rsp), %rsi
movl 0x2c(%rsp), %edx
movq 0x58(%rsp), %rdi
movq 0xb8(%rsp), %r8
movl 0x88(%rsp), %r9d
movl 0xe0(%rsp), %r10d
imull 0xb0(%rsp), %r10d
movslq 0xe0(%rsp), %rax
imulq 0xe8(%rdi), %rax
leaq 0x70(%rsp), %rcx
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x1b130
movq 0x58(%rsp), %rax
movslq 0xe0(%rsp), %rdx
imulq 0xe8(%rax), %rdx
movslq 0xac(%rsp), %rcx
addq %rdx, %rcx
movl %ecx, 0xac(%rsp)
testb $0x1, 0xf1(%rax)
jne 0x1cb5a
movq 0x58(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x90(%rsp), %rdi
callq 0x20830
jmp 0x1c944
movl $0x0, 0x6c(%rsp)
movb $0x1, 0x6b(%rsp)
movl 0x6c(%rsp), %ecx
xorl %eax, %eax
cmpl 0xe0(%rsp), %ecx
movb %al, 0x1f(%rsp)
jge 0x1cb99
movq 0x58(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x20860
movb %al, %cl
andb $0x1, %cl
movb %cl, 0x6b(%rsp)
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x1cba3
jmp 0x1cbb0
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x1cb67
testb $0x1, 0x6b(%rsp)
jne 0x1cbe2
movq 0x58(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x90(%rsp), %rdi
callq 0x20830
jmp 0x1c944
jmp 0x1cbe4
jmp 0x1ccd0
movq 0x58(%rsp), %rax
movslq 0xe0(%rsp), %rdx
imulq 0xe8(%rax), %rdx
movslq 0xac(%rsp), %rcx
addq %rdx, %rcx
movl %ecx, 0xac(%rsp)
testb $0x1, 0xf1(%rax)
jne 0x1cc44
movq 0x58(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x90(%rsp), %rdi
callq 0x20830
jmp 0x1c944
movl $0x0, 0x64(%rsp)
movb $0x1, 0x63(%rsp)
movl 0x64(%rsp), %ecx
xorl %eax, %eax
cmpl 0xe0(%rsp), %ecx
movb %al, 0x1e(%rsp)
jge 0x1cc83
movq 0x58(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x20860
movb %al, %cl
andb $0x1, %cl
movb %cl, 0x63(%rsp)
movb %al, 0x1e(%rsp)
movb 0x1e(%rsp), %al
testb $0x1, %al
jne 0x1cc8d
jmp 0x1cc9a
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x1cc51
testb $0x1, 0x63(%rsp)
jne 0x1cccc
movq 0x58(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x90(%rsp), %rdi
callq 0x20830
jmp 0x1c944
jmp 0x1ccce
jmp 0x1ccd0
jmp 0x1c944
addq $0xd8, %rsp
retq
nopl (%rax)