// File: 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
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
callq 0x51f0
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdi
addq %rax, %rsi
movq -0x10(%rbp), %rdx
movq 0x4b65c(%rip), %rcx     # 0x51fc8
callq 0x7ed0
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x28(%rbp)
movslq -0x28(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x8(%rbp), %rdi
callq 0x7de0
movq %rax, %rcx
movq -0x90(%rbp), %rax
cmpq %rcx, %rax
jae 0x6abf
movq -0x8(%rbp), %rdi
movslq -0x28(%rbp), %rsi
callq 0x7e60
movq %rax, %rsi
leaq -0x48(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
callq 0x51e0
movq -0xa0(%rbp), %rdi
callq 0x53e0
movq -0xa0(%rbp), %rdi
movq %rax, -0x50(%rbp)
callq 0x5280
movq -0xa0(%rbp), %rdi
movq %rax, -0x58(%rbp)
callq 0x53e0
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq 0x4b5b7(%rip), %rcx     # 0x51fc8
callq 0x7f30
movq %rax, -0x98(%rbp)
jmp 0x6a1f
movq -0x98(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
callq 0x5830
cmpq $-0x1, %rax
je 0x6aa8
cmpl $-0x1, -0x24(%rbp)
je 0x6a6c
movq -0x18(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x6b00
movb %al, -0xa1(%rbp)
jmp 0x6a60
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0x6a6c
jmp 0x6aa6
movl -0x28(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x28(%rbp), %rsi
callq 0x7e60
movq %rax, %rsi
movq -0x18(%rbp), %rdi
callq 0x7990
jmp 0x6a8d
jmp 0x6aa6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x52c0
jmp 0x6af0
jmp 0x6aa8
leaq -0x48(%rbp), %rdi
callq 0x52c0
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x6987
cmpl $-0x1, -0x24(%rbp)
je 0x6ade
movq -0x8(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x7e60
movq %rax, %rsi
movq -0x18(%rbp), %rdi
callq 0x7990
cmpl $-0x1, -0x24(%rbp)
setne %al
andb $0x1, %al
addq $0xb0, %rsp
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x5780
nopl (%rax)