def order_rewrite(node, name, expr, assumed_result):
    # Get the literal and static compare values
    static_value = expr.right.value
    numeric = isinstance(expr.right, ast.Number)

    # Based on the assumed result get the upper/lower bounds
    less_than = "<" in expr.type
    maybe_equals = "=" in expr.type
    if less_than:
        if assumed_result:
            min_bound = float("-inf")
            min_incl = False
            max_bound = static_value
            max_incl = maybe_equals
        else:
            min_bound = static_value
            min_incl = not maybe_equals
            max_bound = float("inf")
            max_incl = False
    else:
        if assumed_result:
            min_bound = static_value
            min_incl = maybe_equals
            max_bound = float("inf")
            max_incl = False
        else:
            min_bound = float("-inf")
            min_incl = False
            max_bound = static_value
            max_incl = not maybe_equals

    if not assumed_result:
        less_than = not less_than
        maybe_equals = not maybe_equals

    # Replace function to handle AST re-writes
    def replace_func(pattern, node):
        node_val = node.right.value
        if not numeric and node_val != static_value:
            return None

        # Check what this node is asserting
        assert_less = "<" in node.type
        assert_equals = "=" in node.type

        # For literals, check that assertions match
        if not numeric:
            const = (less_than == assert_less)
            if maybe_equals:
                const = const and assert_equals

        # For numerics we can do static analysis
        else:
            # Some cases cannot be re-written
            const = None
            if assert_less:
                # a < c, c > b iff a < b
                if node_val > max_bound:
                    const = True

                # a <= c, c = b iff a <= b
                # a < c, c = b iff a <= b UNKNOWN!
                elif node_val == max_bound and max_incl and assert_equals:
                    const = True

                # a < c, c == b, a < b
                # a <= c, c == b, a < b
                elif node_val == max_bound and not max_incl:
                    const = True

                # a < 5, a > 6
                elif node_val < min_bound:
                    const = False

                # a < 5, a > 5
                # a < 5, a >= 5
                elif node_val == min_bound and not assert_equals:
                    const = False

                # a <= 5, a > 5
                # a <= 5, a >= 5 UNKNOWN!
                elif node_val == min_bound and assert_equals and \
                    not min_incl:
                    const = False

            else:
                # a > c, c < b iff a > b
                if node_val < min_bound:
                    const = True

                # a >= c, c = b iff a > b
                # a > c, c = b iff a > b
                elif node_val == min_bound and not min_incl:
                    const = True

                # a >= c, c = b iff a >= b
                # a > c, c = b iff a >= b UNKNOWN!
                elif node_val == min_bound and min_incl and assert_equals:
                    const = True

                # a > c, c > b iff a < b
                elif node_val > max_bound:
                    const = False

                # a > 5, a < 5
                # a > 5, a <= 5
                elif node_val == max_bound and not assert_equals:
                    const = False

                # a >= 5, a < 5
                # a >= 5, a <= 5 UNKNOWN!
                elif node_val == max_bound and assert_equals and \
                    not max_incl:
                    const = False

        # No replacement in some situations
        if const is None:
            return None
        return ast.Constant(const)


    # Tile to replace
    pattern = SimplePattern("types:CompareOperator AND ops:<,<=,>,>=",
            ASTPattern(expr.left), "types:Number" if numeric else "types:Literal")
    return tile(node, [pattern], replace_func)
dataset/ETHPy150Open armon/pypred/pypred/compare.py/order_rewrite/original