// File: /workspace/github_cpp_makefile_star2_O2/ridiculousfish[P]docopt_fish/docopt_matching.cpp
// Function: docopt_fish::match_usages(std::vector<docopt_fish::usage_t, std::allocator<docopt_fish::usage_t>> const&, unsigned int, std::vector<docopt_fish::option_t, std::allocator<docopt_fish::option_t>> const&, docopt_fish::arg_classification_t const&, std::vector<std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>, std::allocator<std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>>> const&)
match_results_t match_usages(const std::vector<usage_t> &usages,
                             parse_flags_t flags,
                             const option_list_t &shortcut_options,
                             const arg_classification_t &aclass,
                             const string_list_t &argv) {
    
    match_context_t ctx(flags, shortcut_options, aclass, argv);
    size_t opt_count = ctx.aclass.resolved_options.size();
    match_state_list_t states;
    match(usages, match_state_t(opt_count), ctx, &states);
    
    // Illustration of some logging to help debug matching
    const bool log_stuff = false;
    if (log_stuff) {
        errstream() << "Matched " << states.size() << " way(s)\n";
        for (size_t i = 0; i < states.size(); i++) {
            const match_state_t &state = states.at(i);
            bool is_incomplete = !ctx.unused_arguments(&state).empty();
            errstream() << "Result " << i << (is_incomplete ? " (INCOMPLETE)" : "") << ":\n";
            for (const auto &kv : *state.argument_values) {
                const rstring_t &name = kv.first;
                const argument_t &arg = kv.second;
                errstream() << "\t" << name.std_string() << ": ";
                for (size_t j = 0; j < arg.values.size(); j++) {
                    if (j > 0) {
                        errstream() << ", ";
                    }
                    errstream() << arg.values.at(j);
                }
                errstream() << '\n';
            }
        }
    }
    
    match_results_t r;
    if (states.empty()) {
        // We had no states
        // Every arg is unused
        for (size_t i = 0; i < argv.size(); i++) {
            r.unused_args.push_back(i);
        }
    } else {
        // We got at least one state
        // Find the ones with the fewest unused arguments
        size_t fewest_unused_args = SIZE_MAX;
        for (match_state_t &st: states) {
            index_list_t unused_args = ctx.unused_arguments(&st);
            if (unused_args.size() < fewest_unused_args) {
                // Fewer unused args
                // Replace the suggestions
                fewest_unused_args = unused_args.size();
                r.option_map = std::move(st.argument_values.mut());
                r.unused_args = std::move(unused_args);
                r.suggestions = std::move(st.suggested_next_arguments);
            } else if (unused_args.size() == fewest_unused_args) {
                // Same number of unused args
                // Incorporate these suggestions too
                r.suggestions.insert(st.suggested_next_arguments.cbegin(),
                                     st.suggested_next_arguments.cend());
            }
        }
    }
    
    return r;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %r9, %r14
movq %rsi, %r15
movq %rdi, 0x8(%rsp)
movl %edx, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movq %r8, 0x48(%rsp)
movq %r9, 0x50(%rsp)
movq 0x20(%r8), %rax
subq 0x18(%r8), %rax
movl $0xa8, %ecx
cqto
idivq %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0xb8(%rsp), %rdi
movq %rax, %rsi
callq 0x12ffc
movq 0x8(%r15), %rcx
movq (%r15), %rsi
movq %rcx, %rax
subq %rsi, %rax
pushq $0x30
popq %rdi
cqto
idivq %rdi
cmpq %rsi, %rcx
je 0x11ece
movq %rax, %r13
xorl %esi, %esi
leaq 0x58(%rsp), %r12
xorl %eax, %eax
testb $0x1, %al
jne 0x11e7f
leaq 0x1(%rsi), %rbp
cmpq %r13, %rbp
jae 0x11e7f
movq %r15, %rdi
callq 0x133fc
movq %rax, %rbx
movq %r12, %rdi
leaq 0xb8(%rsp), %rsi
callq 0x14754
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x38(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq 0x1204f
movq %r12, %rdi
callq 0x130ce
xorl %eax, %eax
testb $0x2, 0x3a(%rsp)
movq %rbp, %rsi
je 0x11e0d
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
cmpq %rcx, %rdx
setne %al
je 0x11e7a
cmpb $0x0, 0x58(%rdx)
leaq 0x60(%rdx), %rdx
je 0x11e68
movq %rbp, %rsi
jmp 0x11e0d
testb $0x1, %al
jne 0x11ece
decq %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x133fc
movq %rax, %rbx
leaq 0x118(%rsp), %r12
leaq 0xb8(%rsp), %rsi
movq %r12, %rdi
callq 0x1341a
leaq 0x38(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1204f
leaq 0x118(%rsp), %rdi
callq 0x130ce
leaq 0xb8(%rsp), %rdi
callq 0x130ce
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x130f2
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r12
cmpq %r12, %r15
je 0x11fa3
leaq 0x48(%rbx), %rax
movq %rax, 0x28(%rsp)
leaq 0x18(%rbx), %rax
movq %rax, 0x30(%rsp)
addq $0x28, %r15
pushq $-0x1
popq %r14
leaq 0x58(%rsp), %r13
leaq -0x28(%r15), %rbx
cmpq %r12, %rbx
je 0x11fd6
movq %r13, %rdi
leaq 0x38(%rsp), %rsi
movq %rbx, %rdx
callq 0x13152
movq 0x60(%rsp), %rbp
subq 0x58(%rsp), %rbp
sarq $0x3, %rbp
cmpq %r14, %rbp
jae 0x11f7e
movq %rbx, %rdi
callq 0x1338e
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0xafc6
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0xaf74
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x14dfe
movq %rbp, %r14
jmp 0x11f92
jne 0x11f92
movq 0x18(%r15), %rsi
leaq 0x8(%r15), %rdx
movq 0x28(%rsp), %rdi
callq 0x14fda
movq %r13, %rdi
callq 0xf208
addq $0x60, %r15
jmp 0x11f1a
xorl %eax, %eax
leaq 0x58(%rsp), %rbx
movq %rax, 0x58(%rsp)
movq 0x8(%r14), %rcx
subq (%r14), %rcx
sarq $0x5, %rcx
cmpq %rcx, %rax
jae 0x11fd6
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x13132
movq 0x58(%rsp), %rax
incq %rax
jmp 0x11faa
leaq 0x10(%rsp), %rdi
callq 0x133da
movq 0x8(%rsp), %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x12020
jmp 0x1202d
movq %rax, %rbx
jmp 0x1203d
jmp 0x12011
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0xf208
jmp 0x12014
movq %rax, %rbx
movq 0x8(%rsp), %rdi
callq 0xaf54
jmp 0x1203d
movq %rax, %rbx
movq %r12, %rdi
callq 0x130ce
jmp 0x12030
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x130ce
leaq 0x10(%rsp), %rdi
callq 0x133da
movq %rbx, %rdi
callq 0x7390