// File: adamnovak[P]rlcsa/suffixarray.cpp
// Function: CSA::SuffixArray::count(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) const
pair_type
SuffixArray::count(const std::string& pattern) const
{
  pair_type range;
  for(uint i = 0; i < pattern.length(); i++)
  {
    if(pattern[i] == '\0')
    {
      std::cerr << "Error: Pattern must not contain \\0!" << std::endl;
      return EMPTY_PAIR;
    }
  }

  // Lower bound for the range.
  sint low = 0, high = this->getSize() - 1;
  uint last_high = high;
  while(low < high)
  {
    uint mid = low + (high - low) / 2;
    uint matched = this->match(pattern, mid);
    if(matched >= pattern.length()) { high = mid; }
    else if((uchar)(pattern[matched]) < this->data[this->sa[mid] + matched])
    {
      high = ((sint)mid) - 1; last_high = high;
    }
    else { low = mid + 1; }
  }
  if(this->match(pattern, low) == pattern.length()) { range.first = low; }
  else { return EMPTY_PAIR; }

  // Upper bound for the range.
  high = last_high;
  while(low < high)
  {
    uint mid = low + (high - low + 1) / 2;
    uint matched = this->match(pattern, mid);
    if(matched >= pattern.length()) { low = mid; }
    else                            { high = mid - 1; }
  }
  range.second = high;

  return range;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0x20(%rbp), %rdi
callq 0xe530
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3470
movq %rax, %rcx
movq -0x50(%rbp), %rax
cmpq %rcx, %rax
jae 0x1bbcc
movq -0x18(%rbp), %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
callq 0x3350
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x1bbbf
movq 0x1c45c(%rip), %rdi     # 0x37ff0
leaq 0xdd2b(%rip), %rsi      # 0x298c6
callq 0x32a0
movq %rax, %rdi
movq 0x1c406(%rip), %rsi     # 0x37fb0
callq 0x3300
movq 0xdd6a(%rip), %rax      # 0x29920
movq %rax, -0x8(%rbp)
jmp 0x1bd59
jmp 0x1bbc1
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1bb5b
movq -0x48(%rbp), %rdi
movl $0x0, -0x28(%rbp)
callq 0x1c3f0
subl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x1bc9a
movq -0x48(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x2c(%rbp), %eax
subl -0x28(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x5c(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rsi
movl -0x34(%rbp), %edx
callq 0x1bd70
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3470
movq %rax, %rcx
movq -0x58(%rbp), %rax
cmpq %rcx, %rax
jb 0x1bc49
movl -0x34(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x1bc95
movq -0x18(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x3350
movq -0x48(%rbp), %rdx
movzbl (%rax), %eax
movq 0x8(%rdx), %rcx
movq 0x10(%rdx), %rdx
movl -0x34(%rbp), %esi
movl (%rdx,%rsi,4), %edx
addl -0x38(%rbp), %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jge 0x1bc8a
movl -0x34(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x1bc93
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1bc95
jmp 0x1bbe8
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %edx
callq 0x1bd70
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3470
movq %rax, %rcx
movq -0x68(%rbp), %rax
cmpq %rcx, %rax
jne 0x1bccd
movl -0x28(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x1bcda
movq 0xdc4c(%rip), %rax      # 0x29920
movq %rax, -0x8(%rbp)
jmp 0x1bd59
movl -0x30(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x1bd4b
movq -0x48(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x2c(%rbp), %eax
subl -0x28(%rbp), %eax
addl $0x1, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x74(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rsi
movl -0x3c(%rbp), %edx
callq 0x1bd70
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3470
movq %rax, %rcx
movq -0x70(%rbp), %rax
cmpq %rcx, %rax
jb 0x1bd40
movl -0x3c(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x1bd49
movl -0x3c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1bce0
movl -0x2c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)