// File: /workspace/github_cpp_makefile_star2_O1/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 %r8, 0x90(%rsp)
movq %rdx, 0xb8(%rsp)
movq %rsi, %rbx
movslq %ecx, %rax
movq 0xe8(%rdi), %rsi
cmpq %rax, %rsi
jg 0x15472
movl %r9d, %r15d
movl %esi, %eax
imull %r9d, %eax
movq %rcx, 0x30(%rsp)
movl %ecx, %r12d
subl %eax, %r12d
incl %r12d
movq %rdi, 0x8(%rsp)
movq 0x38(%rdi), %rax
cqto
idivq %rsi
decq %rax
xorl %ecx, %ecx
movq %rcx, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movl %ecx, 0x70(%rsp)
movq %rcx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movl %ecx, 0x50(%rsp)
leaq 0x10(%rsp), %rdi
movq %rcx, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movq 0x8(%rbx), %rsi
movslq %r12d, %rax
subq %rax, %rsi
callq 0x16460
cmpl 0x8(%rbx), %r12d
movq %rbx, %rsi
movq %rbx, 0x38(%rsp)
jg 0x14fc7
leal -0x1(%r15), %ebp
xorl %r14d, %r14d
leaq 0x60(%rsp), %r13
xorl %ebx, %ebx
movq 0x8(%rsp), %rdi
movl %ebx, %edx
movq %r13, %rcx
movl %r12d, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x15cf4
movl 0x70(%rsp), %eax
cmpl 0x50(%rsp), %eax
jle 0x14ead
movl 0x70(%rsp), %ecx
movl %ecx, 0x50(%rsp)
movaps 0x60(%rsp), %xmm0
movaps %xmm0, 0x40(%rsp)
cmpl $0x1, %eax
jg 0x14eea
movq 0x8(%rsp), %rcx
movq 0x38(%rcx), %rax
cqto
idivq 0xe8(%rcx)
decq %rax
movq %r14, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movl %r14d, 0x70(%rsp)
movq %r14, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movl %r14d, 0x50(%rsp)
jmp 0x14faf
cmpl %r12d, %eax
jl 0x14f61
movq 0x38(%rsp), %rsi
movl 0x8(%rsi), %r8d
movq 0x8(%rsp), %rdi
movl %ebx, %edx
leaq 0x40(%rsp), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x15cf4
movl 0x50(%rsp), %eax
leal (%rax,%rbx), %ecx
cmpl 0x30(%rsp), %ecx
jl 0x14f61
movaps 0x60(%rsp), %xmm0
movaps %xmm0, 0xc0(%rsp)
movl 0x70(%rsp), %ecx
movl %ecx, 0xd0(%rsp)
movaps 0x40(%rsp), %xmm0
movups %xmm0, 0xd8(%rsp)
movl %eax, 0xe8(%rsp)
movl %ebx, 0xf0(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x189f2
movq 0x8(%rsp), %rcx
movq 0x38(%rcx), %rax
movq 0xe8(%rcx), %rcx
cqto
idivq %rcx
decq %rax
movq %r14, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movl %r14d, 0x70(%rsp)
movq %r14, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movl %r14d, 0x50(%rsp)
movslq %ebx, %rax
cqto
idivq %rcx
leaq -0x1(%rcx), %rax
cmpq %rax, %rdx
jne 0x14faf
imull %ebp, %ecx
addl %ebx, %ecx
movl %ecx, %ebx
leal 0x1(%rbx), %eax
movq 0x38(%rsp), %rsi
movl 0x8(%rsi), %ecx
subl %r12d, %ecx
cmpl %ecx, %ebx
movl %eax, %ebx
jl 0x14e79
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r12
cmpq %r12, %rbx
movq %rsi, %r14
je 0x15022
movq %r12, %rax
subq %rbx, %rax
sarq $0x3, %rax
movabsq $0x6db6db6db6db6db7, %rcx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rcx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0x539e(%rip), %rcx     # 0xfc67
movq %rbx, %rdi
movq %r12, %rsi
callq 0x18bd9
leaq -0x53b0(%rip), %rdx     # 0xfc67
movq %rbx, %rdi
movq %r12, %rsi
callq 0x18cb0
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)
movq 0x8(%r14), %rsi
incq %rsi
leaq 0x9c(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
callq 0x19432
movq 0x8(%rsp), %r12
movq %r15, 0x80(%rsp)
movq 0x8(%r14), %rsi
incq %rsi
leaq 0x9c(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0xa0(%rsp), %rdi
callq 0x19432
movl 0x1b0(%rsp), %ebx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
je 0x15173
sarq $0x3, %rcx
movabsq $0x6db6db6db6db6db7, %rdx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rdx, %rcx
movslq %ebx, %rdx
movq 0xa0(%rsp), %rsi
movq %rsi, 0x88(%rsp)
xorl %edi, %edi
imulq $0x38, %rdi, %r8
addq %rax, %r8
addq $0x30, %r8
movslq (%r8), %r9
movslq -0x8(%r8), %r10
addq %r9, %r10
movq 0xc0(%rsp), %r11
movq 0xe8(%r12), %r14
xorl %r15d, %r15d
xorl %r9d, %r9d
movl (%r8), %ebp
movslq %ebp, %r13
movslq -0x8(%r8), %r12
addq %r13, %r12
cmpl %r10d, %r12d
jne 0x15148
testb $0x1, %r15b
je 0x1511f
movslq (%r11,%r10,4), %rsi
addq %r14, %rsi
cmpq %r13, %rsi
jl 0x1512a
addq -0x10(%r8), %r9
subq -0x18(%r8), %r9
incq %r9
testb $0x1, %r15b
jne 0x1513c
cmpq %rdx, %r9
jl 0x1513c
movl %ebp, (%r11,%r12,4)
movb $0x1, %r15b
incq %rdi
addq $0x38, %r8
cmpq %rcx, %rdi
jb 0x150fb
imulq $0x38, %rdi, %rsi
movslq -0x8(%rax,%rsi), %r8
movslq -0x10(%rax,%rsi), %rsi
addq %r8, %rsi
movq 0x88(%rsp), %r8
movl %r9d, (%r8,%rsi,4)
cmpq %rcx, %rdi
movq 0x8(%rsp), %r12
jb 0x150d0
xorl %ecx, %ecx
cmpl $0x2, 0x80(%rsp)
jl 0x15187
movl 0xe8(%r12), %ecx
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rdi
je 0x15421
subl %ecx, %edi
movq 0x38(%rsp), %rcx
movl 0x8(%rcx), %r8d
xorl %r14d, %r14d
movq %rdi, 0x30(%rsp)
imulq $0x38, %r14, %rcx
movslq 0x28(%rax,%rcx), %r13
movslq 0x30(%rax,%rcx), %rax
addq %rax, %r13
movq 0xa0(%rsp), %rcx
movl (%rcx,%r13,4), %ecx
cmpl 0x90(%rsp), %ecx
setge %dl
movslq %r8d, %rsi
addq 0xe8(%r12), %rsi
cmpq %rax, %rsi
setl %r15b
orb %dl, %r15b
cmpl %ebx, %ecx
jl 0x15207
testb %r15b, %r15b
jne 0x15207
movq 0xc0(%rsp), %rax
movl (%rax,%r13,4), %eax
cmpl %r8d, %eax
cmovll %eax, %r8d
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $0x6db6db6db6db6db7, %rdx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rdx, %rcx
cmpq %rcx, %r14
movl %r8d, 0x88(%rsp)
jae 0x153ec
movslq %r8d, %r12
movl %r13d, %ebp
imulq $0x38, %r14, %rsi
movl 0x28(%rax,%rsi), %edx
movl 0x30(%rax,%rsi), %ecx
leal (%rdx,%rcx), %edi
cmpl %r13d, %edi
jne 0x153ec
testb %r15b, %r15b
jne 0x153c1
movslq %ecx, %rdi
movq 0x8(%rsp), %r8
movq 0xe8(%r8), %r10
leaq (%r10,%r12), %r8
cmpq %rdi, %r8
jl 0x153c1
addq %rsi, %rax
movq 0xa0(%rsp), %rsi
movl (%rsi,%r13,4), %r8d
movq %rbp, %r11
cmpl %ebx, %r8d
jge 0x152b9
movq %r11, %r9
testl %r9d, %r9d
jle 0x1534b
leaq -0x1(%r9), %r11
cmpl %ebx, -0x4(%rsi,%r9,4)
jl 0x15291
movq 0xc0(%rsp), %rsi
movl (%rsi,%r11,4), %r11d
jmp 0x1534e
movq 0xc0(%rsp), %r9
movslq (%r9,%r13,4), %r9
addq %r10, %r9
cmpq %rdi, %r9
jl 0x153c1
movq %rbp, %r11
cmpl %ebx, %r8d
jl 0x15291
movups (%rax), %xmm0
movaps %xmm0, 0x140(%rsp)
movl 0x10(%rax), %esi
movl %esi, 0x150(%rsp)
movups 0x18(%rax), %xmm0
movups %xmm0, 0x158(%rsp)
movl %edx, 0x168(%rsp)
movl %ecx, 0x170(%rsp)
imull 0x80(%rsp), %r10d
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq 0x140(%rsp), %rdx
movq 0xb8(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r10
pushq 0x98(%rsp)
pushq 0x40(%rsp)
pushq $0x0
callq 0x1367a
addq $0x20, %rsp
jmp 0x153c1
movl %r13d, %r11d
decl %r9d
movups (%rax), %xmm0
movaps %xmm0, 0x100(%rsp)
movl 0x10(%rax), %esi
movl %esi, 0x110(%rsp)
movups 0x18(%rax), %xmm0
movups %xmm0, 0x118(%rsp)
movl %edx, 0x128(%rsp)
movl %ecx, 0x130(%rsp)
imull 0x80(%rsp), %r10d
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq 0x100(%rsp), %rdx
movq 0xb8(%rsp), %rcx
movl $0x1, %r8d
pushq %r10
pushq 0x98(%rsp)
pushq 0x40(%rsp)
pushq %r11
callq 0x1367a
addq $0x20, %rsp
incq %r14
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $0x6db6db6db6db6db7, %rdx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rdx, %rcx
cmpq %rcx, %r14
jb 0x1523d
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $0x6db6db6db6db6db7, %rdx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rdx, %rcx
cmpq %rcx, %r14
movq 0x8(%rsp), %r12
movl 0x88(%rsp), %r8d
jb 0x151af
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x1543e
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x72f0
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x1545b
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x72f0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x15472
movq 0x20(%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 0x15493
jmp 0x15493
jmp 0x1548e
jmp 0x1548e
jmp 0x15493
movq %rax, %rbx
jmp 0x154d0
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x154b3
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x72f0
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x154d0
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x72f0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x154e7
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x72f0
movq %rbx, %rdi
callq 0x7600
nop