human
Complete the below
def _find_bad_optimizations2(order, reasons, r_vals):

    ''''Use a simple algorithm to find broken optimizations.
    This algorithm is simple to understand, but sometimes when there\'s
    a problem it identifies the wrong optimization as the culprit.
    The problem stems from the fact that results are not evaluated in
    chronological order (looking at when they were introduced to the
    graph).'
    '''

What is a Python program to find broken optimizations?
assistant
#Here is Python program to find broken optimizations.
    checked_variables = set()
    def check_variable_norec(new_r):
        '\n                                Verify    that    `r`    has    the    same    value    as    the    results    it    replaces.\n\n                                '
        for (reason, r, old_graph_str, new_graph_str) in reasons[new_r]:
            new_r_val = r_vals[new_r]
            r_val = r_vals[r]
            if ((r.type != new_r.type) or (not r.type.values_eq_approx(r_val, new_r_val))):
                raise BadOptimization(old_r=r, new_r=new_r, old_r_val=r_val, new_r_val=new_r_val, reason=reason, old_graph=old_graph_str, new_graph=new_graph_str)
    def check_variable(r):
        if (r in checked_variables):
            return
        checked_variables.add(r)
        list_of_vars = [old_r for (reason, old_r, olds, news) in reasons[r]]
        if (None is not r.owner):
            list_of_vars += r.owner.inputs
        for var_that_could_make_r_look_bad in list_of_vars:
            check_variable(var_that_could_make_r_look_bad)
        check_variable_norec(r)
    for (i, node) in enumerate(order):
        for new_r in node.outputs:
            check_variable(new_r)