from pathlib import Path


def label_malformed(path: Path) -> Path:
    """
    Renames the file at the given location to <original_filename>_MALFORMED.
    If such a file already exists, an incremented number is appended to the
    name until it can be created. The new file name is returned.

    Raises: `OSError`
    """
    assert(path.is_file())

    malformed_file_path = list(path.parts)
    malformed_file_path[-1] += "_MALFORMED_CONTENTS"
    malformed_file = Path(*malformed_file_path)

    # Avoid naming collision
    i = 1
    while malformed_file.is_file():
        malformed_file_path[-1] += str(i)
        malformed_file = Path(*malformed_file_path)
        i += 1

    path.rename(malformed_file)
    return malformed_file