def escape_verilog_name(name):
    """ Transform net names into escaped id and bus selection (if any)

    Args:
        name (str): Net name

    Returns:
        Escape verilog name

    >>> escape_verilog_name(
    ...     '$abc$6513$auto$alumacc.cc:474:replace_alu$1259.B_buf[4]')
    '\\\\$abc$6513$auto$alumacc.cc:474:replace_alu$1259.B_buf [4]'
    >>> escape_verilog_name(
    ...     '$abc$6513$auto$alumacc.cc:474:replace_alu$1259.B_buf[4:0]')
    '\\\\$abc$6513$auto$alumacc.cc:474:replace_alu$1259.B_buf[4:0] '
    >>> escape_verilog_name(
    ...     '$abc$6513$auto$alumacc.cc:474:replace_alu$1259.B_buf[4:0][0]')
    '\\\\$abc$6513$auto$alumacc.cc:474:replace_alu$1259.B_buf[4:0] [0]'
    >>> escape_verilog_name(
    ...     'test')
    '\\\\test '
    """

    idx = name.rfind('[')
    bus_idx = None
    if idx != -1 and name[-1] == ']':
        try:
            bus_idx = int(name[idx + 1:-1])
        except ValueError:
            pass

    if bus_idx is None:
        # Escape whole name
        return '\\' + name + ' '

    return '\\' + name[:idx] + ' ' + name[idx:]