def split_region(contents, region_name):
    """Split `contents` by region with `region_name`.

    Returns:
        Tuple[str, str, str]:
            A tuple containing the start block, region block, and end block.
            Start block contains the start region line. End block contains
            the end region line.

    Raises:
        ValueError:
            If could not find region with `region_name` or found duplicate
            region definitions of `region_name`.
    """
    start_line = f'<!--START {region_name}-->'
    end_line = f'<!--END {region_name}-->'
    start_lines = []
    region_lines = []
    end_lines = []
    found_start = False
    found_end = False
    for line_no, line in enumerate(contents.split('\n')):
        line_stripped = line.strip()
        if line_stripped == end_line:
            if found_end:
                raise ValueError(
                    f'Found duplicate end line "{end_line}" at line {line_no + 1}.'
                    )
            found_end = True
        if not found_start:
            start_lines.append(line)
        elif not found_end:
            region_lines.append(line)
        else:
            end_lines.append(line)
        if line_stripped == start_line:
            if found_start:
                raise ValueError(
                    f'Found duplicate start line "{start_line}" at line {line_no + 1}.'
                    )
            found_start = True
    if not found_start:
        raise ValueError(f'Could not find start line "{start_line}".')
    if not found_end:
        raise ValueError(f'Could not find end line "{end_line}".')
    start_block = '\n'.join(start_lines) + '\n'
    region_block = '\n'.join(region_lines) + '\n' if region_lines else ''
    end_block = '\n'.join(end_lines)
    return start_block, region_block, end_block
