// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rsi, %r14
movq %rdi, -0x38(%rbp)
movq %rsi, %rdi
callq 0x4190
testq %rax, %rax
je 0x5470
movq %rax, %r12
xorl %ebx, %ebx
movsbl (%r14,%rbx), %edi
callq 0x44c0
movb %al, (%r14,%rbx)
incq %rbx
cmpq %rbx, %r12
jne 0x545a
movq -0x38(%rbp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x5631
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
xorl %r12d, %r12d
leaq -0x58(%rbp), %rbx
movq %r15, -0x60(%rbp)
movq %r14, -0x68(%rbp)
leaq (%r12,%r12,4), %r13
leaq -0x48(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movq (%rax,%r13,8), %rsi
movq 0x8(%rax,%r13,8), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x6214
movq -0x50(%rbp), %rbx
testq %rbx, %rbx
je 0x54dd
movq -0x58(%rbp), %r15
xorl %r14d, %r14d
movsbl (%r15,%r14), %edi
callq 0x44c0
movb %al, (%r15,%r14)
incq %r14
cmpq %r14, %rbx
jne 0x54c7
movq -0x68(%rbp), %r14
movq %r14, %rdi
callq 0x4190
leaq -0x58(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4570
cmpq $-0x1, %rax
je 0x5581
cmpl $-0x1, -0x2c(%rbp)
movq -0x60(%rbp), %r15
je 0x55a4
movq %r14, %rdi
callq 0x4190
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4570
testq %rax, %rax
je 0x5587
movq %r14, %rdi
callq 0x4190
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4570
testq %rax, %rax
je 0x560f
movq -0x50(%rbp), %rbx
movq %r14, %rdi
callq 0x4190
subq %rax, %rbx
movq 0x8(%r15), %rcx
subq %rax, %rcx
cmpq %rcx, %rbx
leaq -0x58(%rbp), %rbx
jb 0x55a4
ja 0x55c8
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4040
testl %eax, %eax
jns 0x55c8
jmp 0x55a4
movq -0x60(%rbp), %r15
jmp 0x55c8
movq %r14, %rdi
callq 0x4190
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4570
testq %rax, %rax
je 0x552e
shlq $0x3, %r13
movq -0x38(%rbp), %rax
addq (%rax), %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x41e0
movzwl 0x20(%r13), %eax
movw %ax, 0x20(%r15)
movl %r12d, -0x2c(%rbp)
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x55e1
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x4300
incq %r12
movq -0x38(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rdx, %rcx
cmpq %r12, %rcx
ja 0x5497
jmp 0x5638
movq %r14, %rdi
callq 0x4190
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4570
testq %rax, %rax
jne 0x55c8
jmp 0x554f
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl -0x2c(%rbp), %ebx
cmpl $-0x1, %ebx
je 0x5660
movslq %ebx, %rcx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x41e0
movzwl 0x20(%r14), %eax
movw %ax, 0x20(%r15)
cmpl $-0x1, %ebx
setne %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0x610a
movq %rax, %rbx
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x5699
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x4300
movq %rbx, %rdi
callq 0x4580