def _rename_node_in_fn(node, new_name, fn):
    """
    Rename a node and all it's connections.

    Parameters
    ----------
    node: ParsedTFNode
        Node to rename.
    new_name: str
        New name of the node.
    fn: SSAFunction
        Function that contains graph to operate on.
    """
    old_name = node.name
    node.name = new_name
    for i in node.inputs:
        idx = fn.graph[i].outputs.index(old_name)
        fn.graph[i].outputs[idx] = new_name
        if old_name in fn.graph[i].control_outputs:
            idx = fn.graph[i].control_outputs.index(old_name)
            fn.graph[i].control_outputs[idx] = new_name

    for o in node.outputs:
        idx = fn.graph[o].inputs.index(old_name)
        fn.graph[o].inputs[idx] = new_name
        if old_name in fn.graph[o].control_inputs:
            idx = fn.graph[o].control_inputs.index(old_name)
            fn.graph[o].control_inputs[idx] = new_name

    for i in node.control_inputs:
        if old_name in fn.graph[i].control_outputs:
            idx = fn.graph[i].control_outputs.index(old_name)
            fn.graph[i].control_outputs[idx] = new_name

    for o in node.control_outputs:
        if old_name in fn.graph[o].control_inputs:
            idx = fn.graph[o].control_inputs.index(old_name)
            fn.graph[o].control_inputs[idx] = new_name

    fn.graph[new_name] = fn.graph.pop(old_name)

    Rename a node and all it's connections.

    Parameters
    ----------
    node: ParsedTFNode
        Node to rename.
    new_name: str
        New name of the node.
    fn: SSAFunction
        Function that contains graph to operate on.
    
coremltools/converters/mil/frontend/tensorflow2/tf_graph_pass/rewrite_control_flow_functions.py