// File: fpavetic[P]lcskpp/lcskpp.cpp
// Function: get_matches(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>*)
static void get_matches(
    const string& a, const string& b,
    const int k, vector<pair<int, int> >* matches) {
  assert(matches != NULL);
  matches->clear();

  vector<char> aid;
  int alphabet_size;
  prepare_alphabet(a, b, aid, alphabet_size);

  // We assume: alphabet_size ** k < 2 ** 64,
  // in the case this does not hold, the entire
  // get_matches function probably has to be
  // reimplemented using non-perfect hashing or
  // suffix arrays.
  if (k * log(alphabet_size) >= 64 * log(2)) {
    fprintf(stderr, "We assume that alphabet_size ** k <\
2 ** 64.\nPlease see lcskpp.cpp for more information.");
    exit(1);
  }

  typedef unordered_multimap<uint64_t, int> MatchIndexType;
  unique_ptr<MatchIndexType> match_index =
    unique_ptr<MatchIndexType>(new MatchIndexType());

  uint64_t hash_mod = 1;
  for (int i = 0; i < k; ++i) hash_mod *= alphabet_size;

  if (alphabet_size == 4) {
    assert(hash_mod == (1LL<<(2*k)));
  }

  uint64_t rolling_hash = 0;
  for (int i = 0; i < a.size(); ++i) {
    rolling_hash = rolling_hash * alphabet_size + aid[a[i]];
    rolling_hash %= hash_mod;

    if (i+1 >= k) {
      match_index->insert(
        MatchIndexType::value_type(rolling_hash, i-k+1));
    }
  }

  rolling_hash = 0;
  for (int i = 0; i < b.size(); ++i) {
    rolling_hash = rolling_hash * alphabet_size + aid[b[i]];
    rolling_hash %= hash_mod;

    if (i+1 >= k) {
      auto positions_in_a = match_index->equal_range(rolling_hash);
      for (auto it = positions_in_a.first; 
           it != positions_in_a.second; ++it) {
        matches->push_back(make_pair(it->second, i-k+1));
      }
    }
  }

  sort(matches->begin(), matches->end());
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x150(%rsp)
movq %rsi, 0x148(%rsp)
movl %edx, 0x144(%rsp)
movq %rcx, 0x138(%rsp)
cmpq $0x0, 0x138(%rsp)
je 0x4aa3
jmp 0x4ac2
leaq 0xd7fa(%rip), %rdi      # 0x122a4
leaq 0xd607(%rip), %rsi      # 0x120b8
movl $0x36, %edx
leaq 0xd7f7(%rip), %rcx      # 0x122b4
callq 0x20f0
movq 0x138(%rsp), %rdi
callq 0x76e0
leaq 0x120(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0x7700
movq 0x78(%rsp), %rdx
movq 0x150(%rsp), %rdi
movq 0x148(%rsp), %rsi
leaq 0x11c(%rsp), %rcx
callq 0x6d60
jmp 0x4b05
cvtsi2sdl 0x144(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movl 0x11c(%rsp), %edi
callq 0x7710
movsd %xmm0, 0x70(%rsp)
jmp 0x4b28
movsd 0x70(%rsp), %xmm1
movsd 0x68(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movl $0x2, %edi
callq 0x7710
movsd %xmm0, 0x60(%rsp)
jmp 0x4b50
movsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm2
movsd 0xd734(%rip), %xmm1     # 0x12298
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x4ba9
movq 0x15463(%rip), %rax     # 0x19fd8
movq (%rax), %rdi
leaq 0xd78b(%rip), %rsi      # 0x1230a
movb $0x0, %al
callq 0x2210
movl $0x1, %edi
callq 0x21b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
jmp 0x5045
movl $0x38, %edi
callq 0x2170
movq %rax, 0x50(%rsp)
jmp 0x4bba
movq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0x38, %edx
callq 0x20c0
movq 0x50(%rsp), %rdi
callq 0x7730
movq 0x50(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0x7740
movq $0x1, 0xf8(%rsp)
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
cmpl 0x144(%rsp), %eax
jge 0x4c3a
movslq 0x11c(%rsp), %rax
imulq 0xf8(%rsp), %rax
movq %rax, 0xf8(%rsp)
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x4bfe
cmpl $0x4, 0x11c(%rsp)
jne 0x4c8a
movq 0xf8(%rsp), %rax
movl 0x144(%rsp), %ecx
shll %ecx
movl %ecx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
cmpq %rcx, %rax
jne 0x4c69
jmp 0x4c88
leaq 0xd6f2(%rip), %rdi      # 0x12362
leaq 0xd441(%rip), %rsi      # 0x120b8
movl $0x50, %edx
leaq 0xd631(%rip), %rcx      # 0x122b4
callq 0x20f0
jmp 0x4c8a
movq $0x0, 0xe8(%rsp)
movl $0x0, 0xe4(%rsp)
movslq 0xe4(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x150(%rsp), %rdi
callq 0x2110
movq %rax, %rcx
movq 0x48(%rsp), %rax
cmpq %rcx, %rax
jae 0x4e06
movq 0xe8(%rsp), %rax
movslq 0x11c(%rsp), %rcx
imulq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x150(%rsp), %rdi
movslq 0xe4(%rsp), %rsi
callq 0x21a0
movsbq (%rax), %rsi
leaq 0x120(%rsp), %rdi
callq 0x7770
movq %rax, %rcx
movq 0x40(%rsp), %rax
movsbq (%rcx), %rcx
addq %rcx, %rax
movq %rax, 0xe8(%rsp)
movq 0xf8(%rsp), %rcx
movq 0xe8(%rsp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, 0xe8(%rsp)
movl 0xe4(%rsp), %eax
addl $0x1, %eax
cmpl 0x144(%rsp), %eax
jl 0x4dee
leaq 0x100(%rsp), %rdi
callq 0x7790
movq %rax, 0x38(%rsp)
movl 0xe4(%rsp), %eax
movl 0x144(%rsp), %ecx
subl %ecx, %eax
incl %eax
movl %eax, 0xcc(%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
leaq 0xcc(%rsp), %rdx
callq 0x77d0
jmp 0x4da0
movq 0x38(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x77a0
movq %rax, 0x30(%rsp)
jmp 0x4db9
movq 0x30(%rsp), %rax
movq %rax, 0xc0(%rsp)
jmp 0x4dee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x79b0
jmp 0x5045
jmp 0x4df0
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x4ca1
movq $0x0, 0xe8(%rsp)
movl $0x0, 0xbc(%rsp)
movslq 0xbc(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x148(%rsp), %rdi
callq 0x2110
movq %rax, %rcx
movq 0x28(%rsp), %rax
cmpq %rcx, %rax
jae 0x4fe2
movq 0xe8(%rsp), %rax
movslq 0x11c(%rsp), %rcx
imulq %rcx, %rax
movq %rax, 0x20(%rsp)
movq 0x148(%rsp), %rdi
movslq 0xbc(%rsp), %rsi
callq 0x21a0
movsbq (%rax), %rsi
leaq 0x120(%rsp), %rdi
callq 0x7770
movq %rax, %rcx
movq 0x20(%rsp), %rax
movsbq (%rcx), %rcx
addq %rcx, %rax
movq %rax, 0xe8(%rsp)
movq 0xf8(%rsp), %rcx
movq 0xe8(%rsp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, 0xe8(%rsp)
movl 0xbc(%rsp), %eax
addl $0x1, %eax
cmpl 0x144(%rsp), %eax
jl 0x4fca
leaq 0x100(%rsp), %rdi
callq 0x7790
movq %rax, %rdi
leaq 0xe8(%rsp), %rsi
callq 0x7800
movq %rdx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
jmp 0x4efb
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0xa8(%rsp)
movq %rax, 0xb0(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0xa8(%rsp), %rsi
addq $0x8, %rsi
leaq 0xa0(%rsp), %rdi
callq 0x7840
testb $0x1, %al
jne 0x4f47
jmp 0x4fc8
movq 0x138(%rsp), %rax
movq %rax, (%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x78c0
movq %rax, %rdi
addq $0x8, %rdi
movl 0xbc(%rsp), %eax
movl 0x144(%rsp), %ecx
subl %ecx, %eax
incl %eax
movl %eax, 0x94(%rsp)
leaq 0x94(%rsp), %rsi
callq 0x7890
movq %rax, 0x8(%rsp)
jmp 0x4f94
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0x98(%rsp), %rsi
callq 0x7860
jmp 0x4fb4
jmp 0x4fb6
leaq 0xa0(%rsp), %rdi
callq 0x78e0
jmp 0x4f25
jmp 0x4fca
jmp 0x4fcc
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x4e1d
movq 0x138(%rsp), %rdi
callq 0x7950
movq %rax, 0x88(%rsp)
movq 0x138(%rsp), %rdi
callq 0x7980
movq %rax, 0x80(%rsp)
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x7910
jmp 0x5023
leaq 0x100(%rsp), %rdi
callq 0x79b0
leaq 0x120(%rsp), %rdi
callq 0x7a10
addq $0x158, %rsp            # imm = 0x158
retq
leaq 0x120(%rsp), %rdi
callq 0x7a10
movq 0x110(%rsp), %rdi
callq 0x2270
nop