// File: /workspace/github_c_makefile_star2_O1/stetre[P]moonnuklear/src/./nuklear/nuklear.h
// Function: nk_strmatch_fuzzy_text
NK_API int
nk_strmatch_fuzzy_text(const char *str, int str_len,
    const char *pattern, int *out_score)
{
    /* Returns true if each character in pattern is found sequentially within str
     * if found then out_score is also set. Score value has no intrinsic meaning.
     * Range varies with pattern. Can only compare scores with same search pattern. */

    /* bonus for adjacent matches */
    #define NK_ADJACENCY_BONUS 5
    /* bonus if match occurs after a separator */
    #define NK_SEPARATOR_BONUS 10
    /* bonus if match is uppercase and prev is lower */
    #define NK_CAMEL_BONUS 10
    /* penalty applied for every letter in str before the first match */
    #define NK_LEADING_LETTER_PENALTY (-3)
    /* maximum penalty for leading letters */
    #define NK_MAX_LEADING_LETTER_PENALTY (-9)
    /* penalty for every letter that doesn't matter */
    #define NK_UNMATCHED_LETTER_PENALTY (-1)

    /* loop variables */
    int score = 0;
    char const * pattern_iter = pattern;
    int str_iter = 0;
    int prev_matched = nk_false;
    int prev_lower = nk_false;
    /* true so if first letter match gets separator bonus*/
    int prev_separator = nk_true;

    /* use "best" matched letter if multiple string letters match the pattern */
    char const * best_letter = 0;
    int best_letter_score = 0;

    /* loop over strings */
    NK_ASSERT(str);
    NK_ASSERT(pattern);
    if (!str || !str_len || !pattern) return 0;
    while (str_iter < str_len)
    {
        const char pattern_letter = *pattern_iter;
        const char str_letter = str[str_iter];

        int next_match = *pattern_iter != '\0' &&
            nk_to_lower(pattern_letter) == nk_to_lower(str_letter);
        int rematch = best_letter && nk_to_upper(*best_letter) == nk_to_upper(str_letter);

        int advanced = next_match && best_letter;
        int pattern_repeat = best_letter && *pattern_iter != '\0';
        pattern_repeat = pattern_repeat &&
            nk_to_lower(*best_letter) == nk_to_lower(pattern_letter);

        if (advanced || pattern_repeat) {
            score += best_letter_score;
            best_letter = 0;
            best_letter_score = 0;
        }

        if (next_match || rematch)
        {
            int new_score = 0;
            /* Apply penalty for each letter before the first pattern match */
            if (pattern_iter == pattern) {
                int count = (int)(&str[str_iter] - str);
                int penalty = NK_LEADING_LETTER_PENALTY * count;
                if (penalty < NK_MAX_LEADING_LETTER_PENALTY)
                    penalty = NK_MAX_LEADING_LETTER_PENALTY;

                score += penalty;
            }

            /* apply bonus for consecutive bonuses */
            if (prev_matched)
                new_score += NK_ADJACENCY_BONUS;

            /* apply bonus for matches after a separator */
            if (prev_separator)
                new_score += NK_SEPARATOR_BONUS;

            /* apply bonus across camel case boundaries */
            if (prev_lower && nk_is_upper(str_letter))
                new_score += NK_CAMEL_BONUS;

            /* update pattern iter IFF the next pattern letter was matched */
            if (next_match)
                ++pattern_iter;

            /* update best letter in str which may be for a "next" letter or a rematch */
            if (new_score >= best_letter_score) {
                /* apply penalty for now skipped letter */
                if (best_letter != 0)
                    score += NK_UNMATCHED_LETTER_PENALTY;

                best_letter = &str[str_iter];
                best_letter_score = new_score;
            }
            prev_matched = nk_true;
        } else {
            score += NK_UNMATCHED_LETTER_PENALTY;
            prev_matched = nk_false;
        }

        /* separators should be more easily defined */
        prev_lower = nk_is_lower(str_letter) != 0;
        prev_separator = str_letter == '_' || str_letter == ' ';

        ++str_iter;
    }

    /* apply score for last match */
    if (best_letter)
        score += best_letter_score;

    /* did not match full pattern */
    if (*pattern_iter != '\0')
        return nk_false;

    if (out_score)
        *out_score = score;
    return nk_true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r14
movq %rdx, %r12
movl %esi, %ebp
movq %rdi, %r15
testq %rdi, %rdi
jne 0x23a5c
movq 0x645b9(%rip), %rbx     # 0x87ff0
movq (%rbx), %rdi
leaq 0x4e34b(%rip), %rsi     # 0x71d8c
leaq 0x4e362(%rip), %rdx     # 0x71daa
movl $0x1ace, %ecx           # imm = 0x1ACE
xorl %eax, %eax
callq 0x12d80
movq (%rbx), %rdi
callq 0x13780
testq %r12, %r12
jne 0x23a8d
movq 0x64588(%rip), %rbx     # 0x87ff0
movq (%rbx), %rdi
leaq 0x4e31a(%rip), %rsi     # 0x71d8c
leaq 0x4e331(%rip), %rdx     # 0x71daa
movl $0x1acf, %ecx           # imm = 0x1ACF
xorl %eax, %eax
callq 0x12d80
movq (%rbx), %rdi
callq 0x13780
testq %r12, %r12
sete %sil
testq %r15, %r15
sete %cl
testl %ebp, %ebp
sete %dl
orb %cl, %dl
xorl %eax, %eax
orb %sil, %dl
jne 0x23caf
xorl %eax, %eax
testl %ebp, %ebp
jle 0x23c97
movq %r14, 0x10(%rsp)
movl %ebp, %eax
leaq (%rax,%rax,2), %rbx
movl $0x1, %ecx
xorl %r9d, %r9d
xorl %r8d, %r8d
xorl %r14d, %r14d
movq %r12, 0x20(%rsp)
movq %r12, %rbp
xorl %esi, %esi
xorl %r13d, %r13d
xorl %r10d, %r10d
xorl %r11d, %r11d
movq %rbx, 0x18(%rsp)
movl %ecx, 0xc(%rsp)
movl %esi, 0x8(%rsp)
movsbl (%rbp), %edx
movsbl (%r15), %r12d
testl %edx, %edx
je 0x23b23
leal -0x41(%rdx), %eax
movl $0xffffff5e, %ecx       # imm = 0xFFFFFF5E
leal (%rdx,%rcx), %esi
cmpl $0x1a, %eax
cmovael %edx, %esi
leal -0x41(%r12), %eax
leal (%r12,%rcx), %edi
cmpl $0x1a, %eax
cmovael %r12d, %edi
cmpl %edi, %esi
sete %cl
jmp 0x23b25
xorl %ecx, %ecx
testq %r11, %r11
je 0x23b52
movsbl (%r11), %eax
leal -0x61(%rax), %esi
leal -0x20(%rax), %edi
cmpl $0x1a, %esi
cmovael %eax, %edi
leal -0x61(%r12), %eax
leal -0x20(%r12), %esi
cmpl $0x1a, %eax
cmovael %r12d, %esi
cmpl %esi, %edi
sete %al
jmp 0x23b54
xorl %eax, %eax
xorl %esi, %esi
testq %r11, %r11
setne %dil
je 0x23ba4
testb %dl, %dl
je 0x23ba4
movsbl (%r11), %esi
movq %rbp, %rbx
leal -0x41(%rsi), %ebp
movl %ecx, %r9d
movl $0xffffff5e, %ecx       # imm = 0xFFFFFF5E
addl %esi, %ecx
cmpl $0x1a, %ebp
cmovael %esi, %ecx
leal -0x41(%rdx), %esi
movl $0xffffff5e, %ebp       # imm = 0xFFFFFF5E
addl %edx, %ebp
cmpl $0x1a, %esi
cmovael %edx, %ebp
cmpl %ebp, %ecx
movl %r9d, %ecx
movq %rbx, %rbp
movq 0x18(%rsp), %rbx
movl $0x0, %r9d
sete %sil
andb %cl, %dil
orb %sil, %dil
testb $0x1, %dil
cmovneq %r9, %r11
movl %r10d, %edx
cmovnel %r9d, %edx
cmovel %r9d, %r10d
addl %r14d, %r10d
orb %cl, %al
testb $0x1, %al
je 0x23c3e
movl %ecx, %edi
cmpq 0x20(%rsp), %rbp
jne 0x23bdf
cmpl $-0x8, %r8d
movl $0xfffffff7, %eax       # imm = 0xFFFFFFF7
cmovgel %r8d, %eax
addl %eax, %r10d
movl 0x8(%rsp), %ecx
xorl %eax, %eax
testl %ecx, %ecx
setne %al
leal (%rax,%rax,4), %ecx
cmpl $0x0, 0xc(%rsp)
leal 0xa(%rax,%rax,4), %esi
cmovel %ecx, %esi
leal -0x5b(%r12), %ecx
leal 0xa(%rsi), %eax
cmpl $-0x1a, %ecx
cmovbl %esi, %eax
testl %r13d, %r13d
cmovel %esi, %eax
movl %r10d, %r14d
movzbl %dil, %ecx
addq %rcx, %rbp
testq %r11, %r11
setne %cl
cmpl %edx, %eax
cmovgeq %r15, %r11
setge %sil
cmovlel %edx, %eax
andb %cl, %sil
movzbl %sil, %ecx
subl %ecx, %r14d
movl $0x1, %esi
movl %eax, %r10d
jmp 0x23c49
movl %r10d, %r14d
decl %r14d
xorl %esi, %esi
movl %edx, %r10d
movl %r12d, %eax
addl $-0x61, %eax
xorl %r13d, %r13d
cmpl $0x1a, %eax
setb %r13b
cmpb $0x5f, %r12b
sete %al
cmpb $0x20, %r12b
sete %cl
orb %al, %cl
movzbl %cl, %ecx
addq $-0x3, %r8
incq %r15
movq %rbx, %rax
addq %r8, %rax
jne 0x23ae7
xorl %eax, %eax
testq %r11, %r11
cmovel %eax, %r10d
addl %r10d, %r14d
movl %r14d, %ecx
movq 0x10(%rsp), %r14
xorl %eax, %eax
jmp 0x23c9c
movq %r12, %rbp
xorl %ecx, %ecx
cmpb $0x0, (%rbp)
jne 0x23caf
movl $0x1, %eax
testq %r14, %r14
je 0x23caf
movl %ecx, (%r14)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq