def _binary_search(tb_start_addr, bbs):
    """
    Perform a binary search to find the index of a basic block that fits the
    given translation block.
    """
    num_bbs = len(bbs)
    lo = 1
    hi = num_bbs - 1

    if tb_start_addr <= bbs[0].end_addr:
        return 0
    if tb_start_addr > bbs[hi].end_addr:
        return num_bbs

    while lo < hi:
        mid = (lo + hi) // 2

        if bbs[mid - 1].end_addr < tb_start_addr <= bbs[mid].end_addr:
            return mid
        if tb_start_addr <= bbs[mid].end_addr:
            hi = mid
        else:
            lo = mid

    if bbs[lo - 1].end_addr < tb_start_addr <= bbs[lo].end_addr:
        return lo

    if bbs[hi - 1].end_addr < tb_start_addr <= bbs[hi].end_addr:
        return hi

    return num_bbs