import re


def ireplace(text, old, new, count=None):
    """
    A case-insensitive replace() clone. Return a copy of text with all occurrences of substring
    old replaced by new. If the optional argument count is given, only the first count
    occurrences are replaced.
    """
    pattern = re.compile(re.escape(old), re.IGNORECASE)

    if count:
        return pattern.sub(new, text, count=count)
    else:
        return pattern.sub(new, text)