Write Python code to read and write files, parse a string to extract values, and store the extracted data in a dictionary.
Implement parse_file(input_path, output_path, file_contents). The function should write file_contents to input_path, read and parse the file, write the resulting dictionary as formatted JSON to output_path, and return the dictionary.
Each non-empty line contains whitespace-separated key=value tokens. Values may be quoted and may contain spaces or =. If a key appears more than once, the last value wins. Raise ValueError for malformed tokens.
Example: name="Ada Lovelace" role=engineer returns {"name": "Ada Lovelace", "role": "engineer"}.
def parse_file(input_path, output_path, file_contents):