// File: ahupp[P]mural/search.cpp
// Function: find_best_fuzzy_matches(std::vector<TagInfo, std::allocator<TagInfo>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned long)
vector<TagInfo> find_best_fuzzy_matches(const vector<TagInfo>& tags,
                                        const string& query,
                                        const size_t max_items) {

  bool ignore_case = query == lowercase(query);

  vector<FuzzyMatch> matches = find_all_fuzzy_matches(tags, query, ignore_case);
  if (matches.size() == 0 && !ignore_case) {
    matches = find_all_fuzzy_matches(tags, lowercase(query), true);
  }
  size_t limit = min(max_items, matches.size());
  partial_sort(matches.begin(), matches.begin()+limit, matches.end(),
               is_better_match);

  vector<TagInfo> result;
  for (size_t i = 0; i < limit; ++i) {
    result.push_back(*(matches[i].first));
  }

  return result;
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x20(%rsp)
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movq %rdi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
movq %rdx, 0x110(%rsp)
movq %rcx, 0x108(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x110(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x6ff0
movq 0x30(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x4c70
movb %al, 0x3f(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x3170
movb 0x3f(%rsp), %al
andb $0x1, %al
movb %al, 0x107(%rsp)
movq 0x118(%rsp), %rsi
movq 0x110(%rsp), %rdx
movb 0x107(%rsp), %al
leaq 0xc8(%rsp), %rdi
andb $0x1, %al
movzbl %al, %ecx
callq 0x46b0
leaq 0xc8(%rsp), %rdi
callq 0x4d10
cmpq $0x0, %rax
jne 0x4a19
testb $0x1, 0x107(%rsp)
jne 0x4a19
movq 0x118(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x110(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x6ff0
jmp 0x4988
movq 0x18(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
leaq 0x90(%rsp), %rdx
movl $0x1, %ecx
callq 0x46b0
jmp 0x49a9
leaq 0xc8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x4d30
leaq 0xb0(%rsp), %rdi
callq 0x4c10
leaq 0x90(%rsp), %rdi
callq 0x3170
jmp 0x4a19
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
jmp 0x4b5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x3170
jmp 0x4b5e
leaq 0xc8(%rsp), %rdi
callq 0x4d10
movq %rax, 0x70(%rsp)
leaq 0x108(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x4d70
movq %rax, 0x10(%rsp)
jmp 0x4a44
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x78(%rsp)
leaq 0xc8(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x4e20
movq 0x8(%rsp), %rdi
movq %rax, 0x68(%rsp)
callq 0x4e20
movq %rax, 0x58(%rsp)
movq 0x78(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0x4e50
movq 0x8(%rsp), %rdi
movq %rax, 0x60(%rsp)
callq 0x4e90
movq %rax, 0x50(%rsp)
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x50(%rsp), %rdx
leaq -0x480(%rip), %rcx      # 0x4630
callq 0x4db0
jmp 0x4ab7
movq 0x20(%rsp), %rdi
movb $0x0, 0x4f(%rsp)
callq 0x3f80
movq $0x0, 0x40(%rsp)
movq 0x40(%rsp), %rax
cmpq 0x78(%rsp), %rax
jae 0x4b2e
movq 0x40(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x4f30
movq 0x20(%rsp), %rdi
movq (%rax), %rsi
callq 0x4ec0
jmp 0x4afc
jmp 0x4afe
movq 0x40(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x40(%rsp)
jmp 0x4acf
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
callq 0x3e60
jmp 0x4b5e
movb $0x1, 0x4f(%rsp)
testb $0x1, 0x4f(%rsp)
jne 0x4b44
movq 0x20(%rsp), %rdi
callq 0x3e60
leaq 0xc8(%rsp), %rdi
callq 0x4c10
movq 0x28(%rsp), %rax
addq $0x128, %rsp            # imm = 0x128
retq
leaq 0xc8(%rsp), %rdi
callq 0x4c10
movq 0x88(%rsp), %rdi
callq 0x33f0
nopl (%rax,%rax)