// File: /workspace/github_cpp_makefile_star2_O2/npazosmendez[P]branchsearch/branchsearch.cpp
// Function: best_match(std::vector<branch_t, std::allocator<branch_t>>&, char*, branch_t&)
bool best_match(vector<branch_t> &all_branches, char* pattern, branch_t& res){
    // pattern to lowercase
    transform(pattern, pattern+strlen(pattern), pattern, ::tolower);

    auto better_match = [pattern]( const string& a, const string& b) {
        // the one starting with the pattern is prefered
        if(a.find(pattern) == 0 and b.find(pattern) != 0) return true;
        if(b.find(pattern) == 0 and a.find(pattern) != 0) return false;

        // the one closer in length to the pattern is prefered
        if(a.size()-strlen(pattern) < b.size()-strlen(pattern)) return true;
        if(b.size()-strlen(pattern) < a.size()-strlen(pattern)) return false;

        // tiebreaker
        return a < b;
    };

    int index = -1;
    for (int i = 0; i< all_branches.size(); i++){
        string b = all_branches[i].name;
        // branch to lowercase
        transform(b.begin(), b.end(), b.begin(), ::tolower);
        if (b.find(pattern) != string::npos) {
            if(index == -1 or better_match(b, res.name)){
                index = i;
                res = all_branches[i];
            }
        }
    }

    if(index != -1) res = all_branches[index];
    return index != -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, -0x38(%rbp)
movq %rsi, %r14
movq %rdi, -0x40(%rbp)
movq %rsi, %rdi
callq 0x4180
movq %rax, %r12
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x5015
movsbl (%r14,%rbx), %edi
callq 0x44e0
movb %al, (%r14,%rbx)
incq %rbx
jmp 0x4ffd
pushq $-0x1
popq %rax
movl %eax, -0x2c(%rbp)
xorl %r15d, %r15d
leaq -0x68(%rbp), %rbx
movq -0x40(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rax
subq %rsi, %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cmpq %r15, %rax
jbe 0x5153
movq %r14, %r12
movq %r15, %r13
imulq $0x28, %r15, %rax
movq %rax, -0x48(%rbp)
addq %rax, %rsi
movq %rbx, %rdi
callq 0x4490
movq -0x68(%rbp), %rbx
movq -0x60(%rbp), %r15
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x507d
movsbl (%rbx,%r14), %edi
callq 0x44e0
movb %al, (%rbx,%r14)
incq %r14
jmp 0x5065
leaq -0x68(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %r14
movq %r12, %rsi
xorl %edx, %edx
callq 0x4620
cmpq $-0x1, %rax
je 0x50fc
cmpl $-0x1, -0x2c(%rbp)
movq %r13, %r15
je 0x5114
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x4620
testq %rax, %rax
je 0x5101
movq -0x38(%rbp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x4620
testq %rax, %rax
je 0x513c
movq -0x60(%rbp), %rbx
movq %r14, %rdi
callq 0x4180
subq %rax, %rbx
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq %rax, %rcx
cmpq %rcx, %rbx
leaq -0x68(%rbp), %rbx
jb 0x5114
ja 0x512c
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0x5ad5
testb %al, %al
jne 0x5114
jmp 0x512c
movq %r13, %r15
jmp 0x512c
movq -0x38(%rbp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x4620
testq %rax, %rax
je 0x50b2
movq -0x40(%rbp), %rax
movq -0x48(%rbp), %rsi
addq (%rax), %rsi
movq -0x38(%rbp), %rdi
callq 0x575c
movl %r15d, -0x2c(%rbp)
movq %rbx, %rdi
callq 0x41a0
incq %r15
jmp 0x5022
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x4620
testq %rax, %rax
jne 0x512c
jmp 0x50c5
movl -0x2c(%rbp), %ebx
cmpl $-0x1, %ebx
je 0x516e
movslq %ebx, %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movq -0x38(%rbp), %rdi
callq 0x575c
cmpl $-0x1, %ebx
setne %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r15d, %eax
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x41a0
movq %rbx, %rdi
callq 0x45a0