// File: /workspace/github_cpp_makefile_star2_O3/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 $0x178, %rsp            # imm = 0x178
movq %rdx, 0xb8(%rsp)
movq %rsi, %rbp
movslq %ecx, %rax
movq 0xe8(%rdi), %rsi
cmpq %rax, %rsi
jg 0x152ee
movl %r9d, %ebx
movq %r8, 0x88(%rsp)
movl %esi, %eax
imull %r9d, %eax
movq %rcx, 0x8(%rsp)
movl %ecx, %r12d
subl %eax, %r12d
incl %r12d
movq %rdi, (%rsp)
movq 0x38(%rdi), %rax
cqto
idivq %rsi
decq %rax
xorl %ecx, %ecx
movq %rcx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movl %ecx, 0x50(%rsp)
movq %rcx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movl %ecx, 0x30(%rsp)
leaq 0x60(%rsp), %rdi
movq %rcx, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movq 0x8(%rbp), %rsi
movslq %r12d, %rax
subq %rax, %rsi
callq 0x162a2
movq %rbx, 0x10(%rsp)
movq 0x8(%rbp), %rsi
cmpl %esi, %r12d
movq (%rsp), %rdi
jg 0x14e03
movq 0x10(%rsp), %rax
leal -0x1(%rax), %r14d
xorl %r13d, %r13d
leaq 0x40(%rsp), %rbx
xorl %r15d, %r15d
movq %rbp, %rsi
movl %r15d, %edx
movq %rbx, %rcx
movl %r12d, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x15b88
movl 0x50(%rsp), %eax
cmpl 0x30(%rsp), %eax
jle 0x14ce7
movl 0x50(%rsp), %ecx
movl %ecx, 0x30(%rsp)
movaps 0x40(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
cmpl $0x1, %eax
movq (%rsp), %rdi
jg 0x14d23
movq 0x38(%rdi), %rax
cqto
idivq 0xe8(%rdi)
decq %rax
movq %r13, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movl %r13d, 0x50(%rsp)
movq %r13, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movl %r13d, 0x30(%rsp)
jmp 0x14dea
cmpl %r12d, %eax
jl 0x14d9e
movl 0x8(%rbp), %r8d
movq %rbp, %rsi
movl %r15d, %edx
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x15b88
movl 0x30(%rsp), %eax
leal (%rax,%r15), %ecx
cmpl 0x8(%rsp), %ecx
movq (%rsp), %rdi
jl 0x14d9e
movaps 0x40(%rsp), %xmm0
movaps %xmm0, 0xc0(%rsp)
movl 0x50(%rsp), %ecx
movl %ecx, 0xd0(%rsp)
movaps 0x20(%rsp), %xmm0
movups %xmm0, 0xd8(%rsp)
movl %eax, 0xe8(%rsp)
movl %r15d, 0xf0(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x18764
movq (%rsp), %rdi
movq 0x38(%rdi), %rax
movq 0xe8(%rdi), %rcx
cqto
idivq %rcx
decq %rax
movq %r13, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movl %r13d, 0x50(%rsp)
movq %r13, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movl %r13d, 0x30(%rsp)
movslq %r15d, %rax
cqto
idivq %rcx
leaq -0x1(%rcx), %rax
cmpq %rax, %rdx
jne 0x14dea
imull %r14d, %ecx
addl %r15d, %ecx
movl %ecx, %r15d
leal 0x1(%r15), %eax
movq 0x8(%rbp), %rsi
movl %esi, %ecx
subl %r12d, %ecx
cmpl %ecx, %r15d
movl %eax, %r15d
jl 0x14cb4
movq 0x60(%rsp), %rbx
movq 0x68(%rsp), %r15
movabsq $0x6db6db6db6db6db7, %rcx # imm = 0x6DB6DB6DB6DB6DB7
cmpq %r15, %rbx
movq 0x10(%rsp), %r14
je 0x14e64
movq %r15, %rax
subq %rbx, %rax
sarq $0x3, %rax
imulq %rcx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0x5370(%rip), %rcx     # 0xfad3
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1894d
leaq -0x5382(%rip), %rdx     # 0xfad3
movq %rbx, %rdi
movq %r15, %rsi
callq 0x18a24
movq 0x8(%rbp), %rsi
xorps %xmm0, %xmm0
leaq 0xc0(%rsp), %rdi
movaps %xmm0, (%rdi)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
movq %rax, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
incq %rsi
leaq 0x9c(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
callq 0x19194
movq %rbp, 0x80(%rsp)
movq 0x8(%rbp), %rsi
incq %rsi
leaq 0x9c(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0xa0(%rsp), %rdi
callq 0x19194
movq 0x8(%rsp), %rbp
movq 0x60(%rsp), %rbx
movq 0x68(%rsp), %rcx
movq %rcx, %rdx
subq %rbx, %rdx
movabsq $0x6db6db6db6db6db7, %rax # imm = 0x6DB6DB6DB6DB6DB7
je 0x14fd4
sarq $0x3, %rdx
imulq %rax, %rdx
movslq 0x1b0(%rsp), %rcx
movq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
xorl %edi, %edi
imulq $0x38, %rdi, %r8
addq %rbx, %r8
addq $0x30, %r8
movslq (%r8), %r9
movslq -0x8(%r8), %r10
addq %r9, %r10
movq 0xc0(%rsp), %r11
movq (%rsp), %rax
movq 0xe8(%rax), %r14
xorl %r15d, %r15d
xorl %r9d, %r9d
movl (%r8), %r12d
movl -0x8(%r8), %r13d
addl %r12d, %r13d
cmpl %r10d, %r13d
jne 0x14f9a
testb $0x1, %r15b
je 0x14f64
movslq %r12d, %rsi
movslq (%r11,%r10,4), %rbp
addq %r14, %rbp
movb $0x1, %r13b
cmpq %rsi, %rbp
jl 0x14f8b
addq -0x10(%r8), %r9
subq -0x18(%r8), %r9
incq %r9
cmpq %rcx, %r9
setl %sil
orb %r15b, %sil
testb $0x1, %sil
je 0x14f84
movl %r15d, %r13d
jmp 0x14f8b
movl %r12d, (%r11,%r10,4)
movb $0x1, %r13b
incq %rdi
addq $0x38, %r8
movl %r13d, %r15d
cmpq %rdx, %rdi
jb 0x14f3d
imulq $0x38, %rdi, %rsi
movslq -0x8(%rbx,%rsi), %r8
movslq -0x10(%rbx,%rsi), %rsi
addq %r8, %rsi
movq 0x90(%rsp), %rax
movl %r9d, (%rax,%rsi,4)
cmpq %rdx, %rdi
jb 0x14f0f
movq 0x60(%rsp), %rbx
movq 0x68(%rsp), %rcx
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rbp
xorl %edx, %edx
cmpl $0x2, %r14d
movq 0x88(%rsp), %r11
movq (%rsp), %rdi
jl 0x14fee
movl 0xe8(%rdi), %edx
cmpq %rbx, %rcx
movabsq $0x6db6db6db6db6db7, %r9 # imm = 0x6DB6DB6DB6DB6DB7
je 0x1529d
subl %edx, %ebp
movq 0x80(%rsp), %rdx
movl 0x8(%rdx), %r10d
xorl %r14d, %r14d
movq %rbp, 0x8(%rsp)
imulq $0x38, %r14, %rax
movslq 0x30(%rbx,%rax), %rdx
movslq 0x28(%rbx,%rax), %r15
addq %rdx, %r15
movq 0xa0(%rsp), %rsi
movl (%rsi,%r15,4), %esi
movb $0x1, %r13b
cmpl %r11d, %esi
jge 0x1506a
movslq %r10d, %r8
addq 0xe8(%rdi), %r8
cmpq %rdx, %r8
jl 0x1506a
cmpl 0x1b0(%rsp), %esi
jl 0x15067
movq 0xc0(%rsp), %rdx
movl (%rdx,%r15,4), %edx
cmpl %r10d, %edx
cmovll %edx, %r10d
xorl %r13d, %r13d
movq %rcx, %rdx
subq %rbx, %rdx
sarq $0x3, %rdx
imulq %r9, %rdx
cmpq %rdx, %r14
jae 0x15294
movl 0x28(%rbx,%rax), %esi
movl 0x30(%rbx,%rax), %edx
leal (%rsi,%rdx), %r8d
cmpl %r15d, %r8d
movl %r10d, 0x1c(%rsp)
jne 0x15281
movslq %r10d, %rbp
movl %r15d, %ecx
movq %rcx, 0x90(%rsp)
addq %rax, %rbx
movq %rbx, %rax
testb %r13b, %r13b
jne 0x15244
movslq %edx, %rcx
movq 0xe8(%rdi), %r12
leaq (%r12,%rbp), %r8
cmpq %rcx, %r8
jl 0x15244
movq 0xa0(%rsp), %r8
movq 0x90(%rsp), %r10
movl 0x1b0(%rsp), %edi
cmpl %edi, (%r8,%r15,4)
jge 0x15114
movq %r10, %r9
testl %r9d, %r9d
jle 0x151bc
leaq -0x1(%r9), %r10
cmpl %edi, -0x4(%r8,%r9,4)
jl 0x150ec
movq 0xc0(%rsp), %rcx
movl (%rcx,%r10,4), %r10d
jmp 0x151bf
movq 0xc0(%rsp), %r8
movslq (%r8,%r15,4), %r8
addq %r12, %r8
cmpq %rcx, %r8
movq (%rsp), %rdi
movabsq $0x6db6db6db6db6db7, %r9 # imm = 0x6DB6DB6DB6DB6DB7
jl 0x15244
movups (%rax), %xmm0
movaps %xmm0, 0x140(%rsp)
movl 0x10(%rax), %ecx
movl %ecx, 0x150(%rsp)
movups 0x18(%rax), %xmm0
movups %xmm0, 0x158(%rsp)
movl %esi, 0x168(%rsp)
movl %edx, 0x170(%rsp)
imull 0x10(%rsp), %r12d
movq 0x80(%rsp), %rsi
leaq 0x140(%rsp), %rdx
movq 0xb8(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r12
pushq %r11
pushq 0x18(%rsp)
pushq $0x0
callq 0x1352a
movabsq $0x6db6db6db6db6db7, %r9 # imm = 0x6DB6DB6DB6DB6DB7
movq 0x20(%rsp), %rdi
movq 0xa8(%rsp), %r11
addq $0x20, %rsp
jmp 0x15244
movl %r15d, %r10d
decl %r9d
movups (%rax), %xmm0
movaps %xmm0, 0x100(%rsp)
movl 0x10(%rax), %ecx
movl %ecx, 0x110(%rsp)
movups 0x18(%rax), %xmm0
movups %xmm0, 0x118(%rsp)
movl %esi, 0x128(%rsp)
movl %edx, 0x130(%rsp)
imull 0x10(%rsp), %r12d
movq (%rsp), %rbx
movq %rbx, %rdi
movq 0x80(%rsp), %rsi
leaq 0x100(%rsp), %rdx
movq 0xb8(%rsp), %rcx
movl $0x1, %r8d
pushq %r12
pushq %r11
pushq 0x18(%rsp)
pushq %r10
callq 0x1352a
movq %rbx, %rdi
movq 0xa8(%rsp), %r11
addq $0x20, %rsp
movabsq $0x6db6db6db6db6db7, %r9 # imm = 0x6DB6DB6DB6DB6DB7
incq %r14
movq 0x60(%rsp), %rbx
movq 0x68(%rsp), %rcx
movq %rcx, %rdx
subq %rbx, %rdx
sarq $0x3, %rdx
imulq %r9, %rdx
cmpq %rdx, %r14
jae 0x1528f
imulq $0x38, %r14, %rdx
leaq (%rbx,%rdx), %rax
movl 0x28(%rbx,%rdx), %esi
movl 0x30(%rbx,%rdx), %edx
leal (%rsi,%rdx), %r8d
cmpl %r15d, %r8d
je 0x150af
movq %rcx, %rdx
subq %rbx, %rdx
sarq $0x3, %rdx
imulq %r9, %rdx
movl 0x1c(%rsp), %r10d
cmpq %rdx, %r14
jb 0x15017
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x152ba
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x72f0
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x152d7
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x72f0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x152ee
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x72f0
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1530a
jmp 0x1530a
jmp 0x15349
jmp 0x15349
jmp 0x1530a
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x1532a
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x72f0
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x1534c
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x72f0
jmp 0x1534c
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x15363
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x72f0
movq %rbx, %rdi
callq 0x7600
nop