def count_prefixes(sequences):
    """
    Count how often each sequence occurs in the given list of
    sequences. If one sequence is the prefix of another one,
    they are considered to be 'identical'.

    Return a dictionary that maps sequence to count.

    >>> r = count_prefixes(['A', 'BB', 'CD', 'CDE', 'CDEF'])
    >>> r == {'A': 1, 'BB': 1, 'CD': 3, 'CDE': 3, 'CDEF': 3}
    True
    """
    sequences = sorted(sequences)
    sequences.append('GUARD')
    prev = 'X'
    start = 0
    count = dict()
    for i, s in enumerate(sequences):
        if not s.startswith(prev):
            # end of a run
            for j in range(start, i):
                count[sequences[j]] = i - start
            start = i
        prev = s
    return count