// File: maxgodfrey2004[P]gss/src/rabin_karp.cc
// Function: gss::rabin_karp::FindMatches(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
std::vector<int> FindMatches(std::string search, std::string pattern) {
  int search_length = static_cast<int>(search.length());
  int pattern_length = static_cast<int>(pattern.length());
  std::vector<int> occurrences;

  // Precompute (MAX_CHAR ^ (M - 1) mod HASH_KEY) where ^ is an exponent.
  int mc_precomp = 1;
  for (int i = 0; i + 1 < pattern_length; ++i) {
    mc_precomp = (MAX_CHAR * mc_precomp) % HASH_KEY;
  }

  // Hash the search string and the pattern according to a modular hash
  // function. The hash value is computed for the first "window", or text
  // segment whose length is that of pattern.
  int search_hash = 0;
  int pattern_hash = 0;
  for (int i = 0; i < pattern_length; ++i) {
    search_hash = (MAX_CHAR * search_hash + search[i]) % HASH_KEY;
    pattern_hash = (MAX_CHAR * pattern_hash + pattern[i]) % HASH_KEY;
  }

  // Start searching...
  for (int i = 0; i <= search_length - pattern_length; ++i) {
    if (search_hash == pattern_hash) {
      int current_idx = 0;
      for (; current_idx < pattern_length; ++current_idx) {
        if (search[i + current_idx] != pattern[current_idx]) {
          break;
        }
      }

      if (current_idx == pattern_length) {
        // We have found an occurrence of pattern in our search string!
        occurrences.push_back(i);
      }
    }

    // Hash the next "window".
    if (i < search_length - pattern_length) {
      search_hash = (MAX_CHAR * (search_hash - search[i] * mc_precomp) +
                      search[i + pattern_length]) % HASH_KEY;
      if (search_hash < 0) {
        search_hash += HASH_KEY;
      }
    }
  }

  return occurrences;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdx, -0x60(%rbp)
movq %rsi, -0x70(%rbp)
movq %rdi, %rax
movq -0x70(%rbp), %rdi
movq %rax, -0x58(%rbp)
movq %rax, %rcx
movq %rcx, -0x68(%rbp)
movq %rax, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
callq 0x2260
movq -0x60(%rbp), %rdi
movl %eax, -0x1c(%rbp)
callq 0x2260
movq -0x58(%rbp), %rdi
movl %eax, -0x20(%rbp)
movb $0x0, -0x21(%rbp)
callq 0x35b0
movl $0x1, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
cmpl -0x20(%rbp), %eax
jge 0x4ea7
movl -0x28(%rbp), %eax
shll $0x8, %eax
movl $0x65, %ecx
cltd
idivl %ecx
movl %edx, -0x28(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x4e80
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x4f76
movq -0x70(%rbp), %rdi
movl -0x30(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x7c(%rbp)
movslq -0x38(%rbp), %rsi
callq 0x22f0
movq %rax, -0x78(%rbp)
jmp 0x4ee4
movq -0x60(%rbp), %rdi
movl -0x7c(%rbp), %eax
movq -0x78(%rbp), %rcx
movsbl (%rcx), %ecx
addl %ecx, %eax
movslq %eax, %rcx
imulq $0x288df0cb, %rcx, %rcx # imm = 0x288DF0CB
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x24, %rcx
addl %edx, %ecx
imull $0x65, %ecx, %ecx
subl %ecx, %eax
movl %eax, -0x30(%rbp)
movl -0x34(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x8c(%rbp)
movslq -0x38(%rbp), %rsi
callq 0x22f0
movq %rax, -0x88(%rbp)
jmp 0x4f31
movl -0x8c(%rbp), %eax
movq -0x88(%rbp), %rcx
movsbl (%rcx), %ecx
addl %ecx, %eax
movl $0x65, %ecx
cltd
idivl %ecx
movl %edx, -0x34(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x4ebc
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x3630
jmp 0x50f6
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl -0x1c(%rbp), %ecx
subl -0x20(%rbp), %ecx
cmpl %ecx, %eax
jg 0x50d6
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jne 0x502a
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x500f
movq -0x70(%rbp), %rdi
movl -0x48(%rbp), %eax
movl -0x4c(%rbp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0x22f0
movq %rax, -0x98(%rbp)
jmp 0x4fc6
movq -0x60(%rbp), %rdi
movq -0x98(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0xa4(%rbp)
movslq -0x4c(%rbp), %rsi
callq 0x22f0
movq %rax, -0xa0(%rbp)
jmp 0x4fec
movl -0xa4(%rbp), %eax
movq -0xa0(%rbp), %rcx
movsbl (%rcx), %ecx
cmpl %ecx, %eax
je 0x5002
jmp 0x500f
jmp 0x5004
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x4fa1
movl -0x4c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jne 0x5028
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x5100
jmp 0x5026
jmp 0x5028
jmp 0x502a
movl -0x48(%rbp), %eax
movl -0x1c(%rbp), %ecx
subl -0x20(%rbp), %ecx
cmpl %ecx, %eax
jge 0x50c6
movq -0x70(%rbp), %rdi
movl -0x30(%rbp), %eax
movl %eax, -0xb4(%rbp)
movslq -0x48(%rbp), %rsi
callq 0x22f0
movq %rax, -0xb0(%rbp)
jmp 0x505a
movq -0x70(%rbp), %rdi
movl -0xb4(%rbp), %eax
movq -0xb0(%rbp), %rcx
movsbl (%rcx), %ecx
movl -0x28(%rbp), %edx
imull %edx, %ecx
subl %ecx, %eax
shll $0x8, %eax
movl %eax, -0xc4(%rbp)
movl -0x48(%rbp), %eax
movl -0x20(%rbp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0x22f0
movq %rax, -0xc0(%rbp)
jmp 0x5098
movl -0xc4(%rbp), %eax
movq -0xc0(%rbp), %rcx
movsbl (%rcx), %ecx
addl %ecx, %eax
movl $0x65, %ecx
cltd
idivl %ecx
movl %edx, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jge 0x50c4
movl -0x30(%rbp), %eax
addl $0x65, %eax
movl %eax, -0x30(%rbp)
jmp 0x50c6
jmp 0x50c8
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x4f7d
movb $0x1, -0x21(%rbp)
testb $0x1, -0x21(%rbp)
jne 0x50e9
movq -0x58(%rbp), %rdi
callq 0x3630
movq -0x68(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x22b0
nop