// File: /workspace/github_cpp_makefile_star2_O2/readmapping[P]alfalfa/src/sparseSA.cpp
// Function: sparseSA::SMEM(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<match_t, std::allocator<match_t>>&, int, int, int, int) const
void sparseSA::SMEM(const string &P, vector<match_t> &matches, int min_len, int maxCount, int sparseQ, int max_smem_cont) const {
  if(min_len < K) return;
  // Offset all intervals at different start points.
  int min_lenK = min_len - (sparseQ*K-1);
  interval_t mli(0,N/K-1,0); // min length interval
  interval_t xmi(0,N/K-1,0); // max match interval
  vector<intervTuple_t> intervals;
  intervals.reserve(P.length() - (min_lenK));
  for(int prefix = 0; prefix <= (int)P.length() - (min_lenK); prefix++) {
      // Right-most match used to terminate search.
      traverse_faster(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); continue; }
      if(mli.depth >= min_lenK) {
          traverse_faster(P, prefix, xmi, P.length(), -1); // Traverse until mismatch.
          //check if we want to add this prefix to priority queue
          if(xmi.depth + prefix >= min_len){
              intervals.push_back(intervTuple_t(mli, xmi, prefix));
          }
      }
      mli.reset(N / K - 1);
      xmi.reset(N / K - 1);
      if(prefix % K == K-1) prefix += (sparseQ-1)*K;
  }
  sort(intervals.begin(),intervals.end(),compIntervTupleRev);
  vector<int> beginLong;
  vector<int> smemCount;
  beginLong.assign(P.length()+1,-1);
  smemCount.assign(P.length()+1,-1);
  //fill tables: for every RSMEM ending: make note if this position has a high number of RSMEMs.
  size_t i = 0;
  while(i < intervals.size()){
      long curSize = 0L;
      long curEnd = intervals[i].xmiEnd();
      bool foundMaxSmemCont = false;
      while(i < intervals.size() && intervals[i].xmiEnd() == curEnd){
          if(!foundMaxSmemCont || (intervals[i].prefix <= beginLong[curEnd] + K))
                curSize += intervals[i].xmi.size();
          if(!foundMaxSmemCont && curSize >= max_smem_cont){
              beginLong[intervals[i].xmiEnd()] = intervals[i].prefix;
              foundMaxSmemCont = true;
          }
          i++;
      }
      smemCount[intervals[i-1].xmiEnd()] = curSize;
  }
  i = 0;
  int curMinPos = P.length();
  int minMin_len = sparseQ > 1 ? min_len-K : min_len;
  while(i < intervals.size()){
      int curEnd = intervals[i].xmiEnd();
      //check if we need to skip all rsmems for this end position
      //do this only if there is a frequent rsmem with higher ending position and lower starting position
      bool skipEnd = smemCount[curEnd] >= maxCount || intervals[i].prefix > curMinPos+K;
      if(!skipEnd && smemCount[curEnd] >= max_smem_cont)
          curMinPos = min(curMinPos, beginLong[curEnd]);
      //iterate over this end pos
      while(i < intervals.size() && intervals[i].xmiEnd() == curEnd){
          //check if we need to collect SMEMs
          if(!skipEnd && intervals[i].prefix <= curMinPos+K && 
                  (smemCount[curEnd] < max_smem_cont || intervals[i].prefix <= beginLong[curEnd] + K )){
              //collect all smems in xmi
              //calculate stop criterium for mli
              //if xmi occurs frequent: do not calc mli
              if(smemCount[curEnd] >= max_smem_cont){// && (curPrefix + K <= beginLong[curEnd] || intervals[i].prefix > curPrefix + K)){
                  collectXmiAndMli(P, intervals[i], matches, false, 0L, 0L, minMin_len, maxCount, K*sparseQ);
              }
              else{
                  //if xmi does not occur frequent: calc mli until mem length reaches frequent smem
                  int nextEndStop = curEnd-1;
                  int nextEndPrefix = curEnd;
                  while(nextEndStop>=0 && smemCount[nextEndStop] < max_smem_cont)
                      nextEndStop--;
                  if(nextEndStop>=0)
                      nextEndPrefix = beginLong[nextEndStop];
                  collectXmiAndMli(P, intervals[i], matches, true, nextEndStop, nextEndPrefix, minMin_len, maxCount, K*sparseQ);
              }
          }
          i++;
      }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %r8, 0x78(%rsp)
movq %rdx, 0x88(%rsp)
movslq %ecx, %rax
movq 0xe8(%rdi), %r8
cmpq %rax, %r8
jg 0x1081a
movl %r8d, %eax
movq %r9, 0x58(%rsp)
imull %r9d, %eax
movq %rcx, 0x18(%rsp)
movl %ecx, %r12d
subl %eax, %r12d
incl %r12d
movq %rdi, 0x8(%rsp)
movq 0x38(%rdi), %rax
cqto
idivq %r8
decq %rax
andq $0x0, 0x40(%rsp)
movq %rax, 0x48(%rsp)
andl $0x0, 0x50(%rsp)
andq $0x0, 0x20(%rsp)
movq %rax, 0x28(%rsp)
andl $0x0, 0x30(%rsp)
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rdi
andq $0x0, 0x10(%rdi)
movaps %xmm0, (%rdi)
movq %rsi, (%rsp)
movq 0x8(%rsi), %rsi
movslq %r12d, %rax
subq %rax, %rsi
callq 0x11704
movl 0x1a0(%rsp), %r15d
movq 0x58(%rsp), %rax
leal -0x1(%rax), %ebp
xorl %ebx, %ebx
leaq 0x40(%rsp), %r14
pushq $-0x1
popq %r13
movq (%rsp), %rsi
movq 0x8(%rsp), %rdi
movl 0x8(%rsi), %eax
subl %r12d, %eax
cmpl %eax, %ebx
jg 0x1045a
movl %ebx, %edx
movq %r14, %rcx
movl %r12d, %r8d
movl %r13d, %r9d
callq 0x112ce
movl 0x50(%rsp), %eax
cmpl 0x30(%rsp), %eax
jle 0x1034d
movl 0x50(%rsp), %ecx
movl %ecx, 0x30(%rsp)
movaps 0x40(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
cmpl $0x1, %eax
movq 0x8(%rsp), %rdi
jg 0x1038c
movq 0x38(%rdi), %rax
cqto
idivq 0xe8(%rdi)
decq %rax
andq $0x0, 0x40(%rsp)
movq %rax, 0x48(%rsp)
andl $0x0, 0x50(%rsp)
andq $0x0, 0x20(%rsp)
movq %rax, 0x28(%rsp)
andl $0x0, 0x30(%rsp)
jmp 0x1044f
cmpl %r12d, %eax
jl 0x10404
movq (%rsp), %rsi
movl 0x8(%rsi), %r8d
movl %ebx, %edx
leaq 0x20(%rsp), %rcx
movl %r13d, %r9d
callq 0x112ce
movl 0x30(%rsp), %eax
leal (%rax,%rbx), %ecx
cmpl 0x18(%rsp), %ecx
movq 0x8(%rsp), %rdi
jl 0x10404
movaps 0x40(%rsp), %xmm0
movaps %xmm0, 0xb0(%rsp)
movl 0x50(%rsp), %ecx
movl %ecx, 0xc0(%rsp)
movaps 0x20(%rsp), %xmm0
movups %xmm0, 0xc8(%rsp)
movl %eax, 0xd8(%rsp)
movl %ebx, 0xe0(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x13fe2
movq 0x8(%rsp), %rdi
movq 0x38(%rdi), %rax
movq 0xe8(%rdi), %rcx
cqto
idivq %rcx
decq %rax
andq $0x0, 0x40(%rsp)
movq %rax, 0x48(%rsp)
andl $0x0, 0x50(%rsp)
andq $0x0, 0x20(%rsp)
movq %rax, 0x28(%rsp)
andl $0x0, 0x30(%rsp)
movslq %ebx, %rax
cqto
idivq %rcx
leaq -0x1(%rcx), %rax
cmpq %rax, %rdx
jne 0x1044f
imull %ebp, %ecx
addl %ebx, %ecx
movl %ecx, %ebx
movq (%rsp), %rsi
incl %ebx
jmp 0x10313
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
leaq -0x3414(%rip), %rdx     # 0xd057
callq 0x1415f
leaq 0xb0(%rsp), %rdi
andq $0x0, 0x10(%rdi)
xorps %xmm0, %xmm0
andq $0x0, 0xa0(%rsp)
movaps %xmm0, (%rdi)
movaps %xmm0, 0x90(%rsp)
movq (%rsp), %rbx
movq 0x8(%rbx), %rsi
incq %rsi
leaq 0x84(%rsp), %rdx
orl $-0x1, (%rdx)
callq 0x1496c
movq 0x8(%rbx), %rsi
incq %rsi
leaq 0x84(%rsp), %rdx
orl $-0x1, (%rdx)
leaq 0x90(%rsp), %rdi
callq 0x1496c
movq 0x60(%rsp), %rcx
movq 0x68(%rsp), %rax
movq %rcx, 0x10(%rsp)
subq %rcx, %rax
pushq $0x38
popq %r10
cqto
idivq %r10
movslq %r15d, %rdx
movq 0x90(%rsp), %rsi
xorl %edi, %edi
cmpq %rax, %rdi
jae 0x105a8
imulq $0x38, %rdi, %r8
movq 0x10(%rsp), %rcx
addq %rcx, %r8
addq $0x30, %r8
movslq (%r8), %r9
movslq -0x8(%r8), %r10
addq %r9, %r10
movq 0xb0(%rsp), %r11
movq 0x8(%rsp), %rcx
movq 0xe8(%rcx), %rbx
xorl %r9d, %r9d
xorl %r14d, %r14d
cmpq %rax, %rdi
jae 0x10590
movl (%r8), %ebp
movl -0x8(%r8), %r12d
addl %ebp, %r12d
cmpl %r10d, %r12d
jne 0x10590
testb $0x1, %r14b
je 0x1055f
movslq %ebp, %r13
movslq (%r11,%r10,4), %rcx
addq %rbx, %rcx
movb $0x1, %r12b
cmpq %r13, %rcx
jl 0x10584
addq -0x10(%r8), %r9
subq -0x18(%r8), %r9
incq %r9
cmpq %rdx, %r9
setl %cl
orb %r14b, %cl
testb $0x1, %cl
je 0x1057d
movl %r14d, %r12d
jmp 0x10584
movl %ebp, (%r11,%r10,4)
movb $0x1, %r12b
incq %rdi
addq $0x38, %r8
movl %r12d, %r14d
jmp 0x10533
movslq -0x38(%r8), %rcx
movslq -0x40(%r8), %r8
addq %rcx, %r8
movl %r9d, (%rsi,%r8,4)
pushq $0x38
popq %r10
jmp 0x104f6
xorl %r12d, %r12d
movl $0x0, %eax
cmpl $0x2, 0x58(%rsp)
movq 0x8(%rsp), %rdi
jl 0x105c2
movl 0xe8(%rdi), %eax
movq (%rsp), %rcx
movl 0x8(%rcx), %ecx
movl %ecx, 0x10(%rsp)
movq 0x18(%rsp), %rcx
subl %eax, %ecx
movq %rcx, 0x18(%rsp)
movq 0x60(%rsp), %rcx
movq 0x68(%rsp), %rsi
movq %rsi, %rax
subq %rcx, %rax
cqto
idivq %r10
cmpq %rax, %r12
jae 0x107f6
imulq $0x38, %r12, %rdx
movslq 0x30(%rcx,%rdx), %rax
movslq 0x28(%rcx,%rdx), %r14
addq %rax, %r14
movq 0x90(%rsp), %rdx
movl (%rdx,%r14,4), %edx
movb $0x1, %r13b
cmpl 0x78(%rsp), %edx
jge 0x1064f
movslq 0x10(%rsp), %r8
addq 0xe8(%rdi), %r8
cmpq %rax, %r8
jl 0x1064f
cmpl %r15d, %edx
jl 0x1064c
movq 0xb0(%rsp), %rax
movl (%rax,%r14,4), %eax
movl 0x10(%rsp), %edx
cmpl %edx, %eax
cmovll %eax, %edx
movl %edx, 0x10(%rsp)
xorl %r13d, %r13d
movslq 0x10(%rsp), %rbx
movl %r14d, %ebp
movq %rsi, %rax
subq %rcx, %rax
cqto
idivq %r10
cmpq %rax, %r12
jae 0x105e3
imulq $0x38, %r12, %rax
leaq (%rcx,%rax), %r8
movl 0x28(%rcx,%rax), %r11d
movl 0x30(%rcx,%rax), %edx
movslq %edx, %r9
leal (%r11,%rdx), %eax
cmpl %r14d, %eax
jne 0x105e3
testb %r13b, %r13b
jne 0x107e4
movq 0xe8(%rdi), %rax
leaq (%rax,%rbx), %rcx
cmpq %r9, %rcx
jl 0x107e4
movq 0x90(%rsp), %rcx
movq %rbp, %rsi
cmpl %r15d, (%rcx,%r14,4)
jge 0x106e2
movq %rsi, %r9
testl %r9d, %r9d
jle 0x1076e
leaq -0x1(%r9), %rsi
cmpl %r15d, -0x4(%rcx,%r9,4)
jl 0x106ba
movq 0xb0(%rsp), %rcx
movl (%rcx,%rsi,4), %r10d
jmp 0x10771
movq 0xb0(%rsp), %rcx
movslq (%rcx,%r14,4), %rcx
addq %rax, %rcx
cmpq %r9, %rcx
jl 0x107e4
movups (%r8), %xmm0
movaps %xmm0, 0x130(%rsp)
movl 0x10(%r8), %ecx
movl %ecx, 0x140(%rsp)
movups 0x18(%r8), %xmm0
movups %xmm0, 0x148(%rsp)
movl %r11d, 0x158(%rsp)
movl %edx, 0x160(%rsp)
imull 0x58(%rsp), %eax
movq (%rsp), %rsi
leaq 0x130(%rsp), %rdx
movq 0x88(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq 0x80(%rsp)
pushq 0x28(%rsp)
pushq $0x0
callq 0xf648
pushq $0x38
popq %r10
movq 0x28(%rsp), %rdi
addq $0x20, %rsp
jmp 0x107e4
movl %r14d, %r10d
decl %r9d
movups (%r8), %xmm0
movaps %xmm0, 0xf0(%rsp)
movl 0x10(%r8), %ecx
movl %ecx, 0x100(%rsp)
movups 0x18(%r8), %xmm0
movups %xmm0, 0x108(%rsp)
movl %r11d, 0x118(%rsp)
movl %edx, 0x120(%rsp)
imull 0x58(%rsp), %eax
movq (%rsp), %rsi
leaq 0xf0(%rsp), %rdx
movq 0x88(%rsp), %rcx
pushq $0x1
popq %r8
pushq %rax
pushq 0x80(%rsp)
pushq 0x28(%rsp)
pushq %r10
callq 0xf648
movq 0x28(%rsp), %rdi
addq $0x20, %rsp
pushq $0x38
popq %r10
incq %r12
movq 0x60(%rsp), %rcx
movq 0x68(%rsp), %rsi
jmp 0x10657
leaq 0x90(%rsp), %rdi
callq 0xa268
leaq 0xb0(%rsp), %rdi
callq 0xa268
leaq 0x60(%rsp), %rdi
callq 0x13e9c
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1083b
jmp 0x1083b
jmp 0x10836
jmp 0x10836
jmp 0x1083b
movq %rax, %rbx
jmp 0x10858
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0xa268
leaq 0xb0(%rsp), %rdi
callq 0xa268
leaq 0x60(%rsp), %rdi
callq 0x13e9c
movq %rbx, %rdi
callq 0x75e0