// File: ridiculousfish[P]docopt_fish/docopt_matching.cpp
// Function: docopt_fish::match_options(std::vector<docopt_fish::option_t, std::allocator<docopt_fish::option_t>> const&, docopt_fish::match_state_t*, docopt_fish::match_context_t const&)
static bool match_options(const option_list_t &options_in_doc, match_state_t *state,
                          const match_context_t &ctx) {
    bool successful_match = false;
    bool made_suggestion = false;

    // Collect potential suggestions in here. We squelch them if we find that a
    // later matched option
    // has the same corresponding long name; we need to remove those from the
    // suggestions
    option_list_t potential_suggestions;

    for (const option_t &opt_in_doc : options_in_doc) {
        // Find the matching option from the resolved option list (i.e. argv)
        size_t resolved_opt_idx = npos;
        for (size_t i = 0; i < ctx.aclass.resolved_options.size(); i++) {
            // Skip ones that have already been consumed
            if (! state->consumed_options->at(i)) {
                // See if the option from argv has the same key range as the option in
                // the document
                if (ctx.aclass.resolved_options.at(i).option.has_same_name(opt_in_doc)) {
                    resolved_opt_idx = i;
                    break;
                }
            }
        }

        if (resolved_opt_idx != npos) {
            // We found a matching option in argv. Set it in the argument_values for
            // this state and mark its index as used
            // We have two things to set:
            //  - The option name, like -foo
            //  - The option's argument value (if any)
            const resolved_option_t &resolved_opt = ctx.aclass.resolved_options.at(resolved_opt_idx);
            const rstring_t &name = opt_in_doc.best_name();

            // Update the option value, creating it if necessary
            state->argument_values.mut()[name].count += 1;

            // Update the option argument vlaue
            if (opt_in_doc.has_value() && resolved_opt.value_idx_in_argv != npos) {
                const rstring_t &variable_name = opt_in_doc.value;

                const rstring_t &value = resolved_opt.value_in_arg;
                state->argument_values.mut()[variable_name].values.push_back(value.std_string());
            }

            successful_match = true;
            state->consumed_options.mut()[resolved_opt_idx] = true;
        } else {
            // This was an option that was not specified in argv
            // It can be a suggestion
            if (ctx.flags & flag_generate_suggestions) {
                potential_suggestions.push_back(opt_in_doc);
            }
        }
    }

    // Now go through and handle potential suggestions
    if (ctx.flags & flag_generate_suggestions) {
        for (const option_t &suggestion : potential_suggestions) {
            size_t type_idx = option_t::NAME_TYPE_COUNT;
            while (type_idx--) {
                option_t::name_type_t type = static_cast<option_t::name_type_t>(type_idx);
                if (suggestion.has_type(type)) {
                    state->suggested_next_arguments.insert(suggestion.names[type]);
                }
            }
            made_suggestion = true;
        }
    }

    bool matched_something = successful_match || made_suggestion;
    if (matched_something) {
        ctx.try_mark_fully_consumed(state);
    }
    return matched_something;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
movb $0x0, -0x1a(%rbp)
leaq -0x38(%rbp), %rdi
callq 0xca90
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x14c00
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
callq 0x14c30
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x14c60
testb $0x1, %al
jne 0x31370
jmp 0x3166b
leaq -0x48(%rbp), %rdi
callq 0xc8c0
movq %rax, -0x58(%rbp)
movq $-0x1, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x18, %rdi
callq 0x25030
movq %rax, %rcx
movq -0x118(%rbp), %rax
cmpq %rcx, %rax
jae 0x3145b
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0x355e0
movq %rax, %rdi
movq -0x68(%rbp), %rsi
callq 0x36040
movb %al, -0x119(%rbp)
jmp 0x313dd
movb -0x119(%rbp), %al
testb $0x1, %al
jne 0x31448
jmp 0x313e9
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x18, %rdi
movq -0x68(%rbp), %rsi
callq 0x36080
movq %rax, -0x128(%rbp)
jmp 0x31407
movq -0x128(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1f300
movb %al, -0x129(%rbp)
jmp 0x3141f
movb -0x129(%rbp), %al
testb $0x1, %al
jne 0x3142b
jmp 0x31446
movq -0x68(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x3145b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x31817
jmp 0x31448
jmp 0x3144a
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x3138d
cmpq $-0x1, -0x60(%rbp)
je 0x31638
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x18, %rdi
movq -0x60(%rbp), %rsi
callq 0x36080
movq %rax, -0x138(%rbp)
jmp 0x31484
movq -0x138(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x58(%rbp), %rdi
callq 0x1f390
movq %rax, -0x140(%rbp)
jmp 0x314a1
movq -0x140(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
callq 0x32840
movq %rax, -0x148(%rbp)
jmp 0x314c1
movq -0x148(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x33ac0
movq %rax, -0x150(%rbp)
jmp 0x314dd
movq -0x150(%rbp), %rax
movl 0x18(%rax), %ecx
incl %ecx
movl %ecx, 0x18(%rax)
movq -0x58(%rbp), %rdi
callq 0xc8e0
movb %al, -0x151(%rbp)
jmp 0x314fd
movb -0x151(%rbp), %al
testb $0x1, %al
jne 0x3150c
jmp 0x315cf
movq -0x80(%rbp), %rax
cmpq $-0x1, 0x88(%rax)
je 0x315cf
movq -0x58(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x90(%rbp)
movq -0x80(%rbp), %rax
addq $0x90, %rax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rdi
callq 0x32840
movq %rax, -0x160(%rbp)
jmp 0x31550
movq -0x160(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x33ac0
movq %rax, -0x168(%rbp)
jmp 0x3156c
movq -0x168(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x98(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x11390
jmp 0x3158f
movq -0x170(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x18c20
jmp 0x315a4
leaq -0xb8(%rbp), %rdi
callq 0x8590
jmp 0x315cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x8590
jmp 0x31817
movb $0x1, -0x19(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0x360c0
movq %rax, -0x178(%rbp)
jmp 0x315e9
movq -0x178(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x36120
movq %rdx, -0x188(%rbp)
movq %rax, -0x180(%rbp)
jmp 0x31609
movq -0x188(%rbp), %rax
movq -0x180(%rbp), %rcx
movq %rcx, -0xc8(%rbp)
movq %rax, -0xc0(%rbp)
leaq -0xc8(%rbp), %rdi
movl $0x1, %esi
callq 0x36180
jmp 0x3165b
movq -0x18(%rbp), %rax
movl (%rax), %eax
andl $0x10000, %eax          # imm = 0x10000
cmpl $0x0, %eax
je 0x31659
movq -0x58(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0xcad0
jmp 0x31657
jmp 0x31659
jmp 0x3165b
jmp 0x3165d
leaq -0x48(%rbp), %rdi
callq 0x14d30
jmp 0x3135a
movq -0x18(%rbp), %rax
movl (%rax), %eax
andl $0x10000, %eax          # imm = 0x10000
cmpl $0x0, %eax
je 0x317b2
leaq -0x38(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rdi
callq 0x1cb80
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rdi
callq 0x1ca00
movq %rax, -0xe0(%rbp)
leaq -0xd8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x21a30
testb $0x1, %al
jne 0x316cc
jmp 0x317b0
leaq -0xd8(%rbp), %rdi
callq 0x224f0
movq %rax, -0xe8(%rbp)
movq $0x3, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0xf0(%rbp)
cmpq $0x0, %rax
je 0x3179b
movl -0xf0(%rbp), %eax
movl %eax, -0xf4(%rbp)
movq -0xe8(%rbp), %rdi
movl -0xf4(%rbp), %esi
callq 0x361d0
movb %al, -0x189(%rbp)
jmp 0x3172f
movb -0x189(%rbp), %al
testb $0x1, %al
jne 0x3173b
jmp 0x31796
movq -0x10(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x1a8(%rbp)
movq -0xe8(%rbp), %rdi
movl -0xf4(%rbp), %eax
movl %eax, %esi
callq 0x14d00
movq -0x1a8(%rbp), %rdi
movq %rax, %rsi
callq 0x18bd0
movb %dl, -0x199(%rbp)
movq %rax, -0x198(%rbp)
jmp 0x3177c
movb -0x199(%rbp), %al
movq -0x198(%rbp), %rcx
movq %rcx, -0x108(%rbp)
movb %al, -0x100(%rbp)
jmp 0x316ea
movb $0x1, -0x1a(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x22350
jmp 0x316b0
jmp 0x317b2
movb $0x1, %al
testb $0x1, -0x19(%rbp)
movb %al, -0x1a9(%rbp)
jne 0x317c9
movb -0x1a(%rbp), %al
movb %al, -0x1a9(%rbp)
movb -0x1a9(%rbp), %al
andb $0x1, %al
movb %al, -0x109(%rbp)
testb $0x1, -0x109(%rbp)
je 0x317f1
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x33ba0
jmp 0x317ef
jmp 0x317f1
movb -0x109(%rbp), %al
movb %al, -0x1aa(%rbp)
leaq -0x38(%rbp), %rdi
callq 0xcb80
movb -0x1aa(%rbp), %al
andb $0x1, %al
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0xcb80
movq -0x70(%rbp), %rdi
callq 0x85d0
nopl (%rax)