code: def _smart_subs(expr, sub_dict):
      """Performs subs, checking for conditions that may result in `nan` or
      `oo`, and attempts to simplify them out.
  
      The expression tree is traversed twice, and the following steps are
      performed on each expression node:
      - First traverse:
          Replace all `tan` with `sin/cos`.
      - Second traverse:
          If node is a fraction, check if the denominator evaluates to 0.
          If so, attempt to simplify it out. Then if node is in sub_dict,
          sub in the corresponding value.
  
      """
      expr = _crawl(expr, _tan_repl_func)
  
      def _recurser(expr, sub_dict):
          # Decompose the expression into num, den
          num, den = _fraction_decomp(expr)
          if den != 1:
              # If there is a non trivial denominator, we need to handle it
              denom_subbed = _recurser(den, sub_dict)
              if denom_subbed.evalf() == 0:
                  # If denom is 0 after this, attempt to simplify the bad expr
                  expr = simplify(expr)
              else:
                  # Expression won't result in nan, find numerator
                  num_subbed = _recurser(num, sub_dict)
                  return num_subbed / denom_subbed
          # We have to crawl the tree manually, because `expr` may have been
          # modified in the simplify step. First, perform subs as normal:
          val = _sub_func(expr, sub_dict)
          if val is not None:
              return val
          new_args = (_recurser(arg, sub_dict) for arg in expr.args)
          return expr.func(*new_args)
  
      return _recurser(expr, sub_dict)
  
imports: from sympy.utilities import dict_merge
  from sympy.utilities.iterables import iterable
  from sympy.physics.vector import Dyadic, Vector, ReferenceFrame, Point, dynamicsymbols
  from sympy.physics.vector.printing import vprint, vsprint, vpprint, vlatex, init_vprinting
  from sympy.physics.mechanics.particle import Particle
  from sympy.physics.mechanics.rigidbody import RigidBody
  from sympy.simplify.simplify import simplify
  from sympy import Matrix, Mul, Derivative, sin, cos, tan, S
  from sympy.core.function import AppliedUndef
  from sympy.physics.mechanics.inertia import inertia, inertia_of_point_mass
  from sympy.utilities.exceptions import sympy_deprecation_warning
label_desc: Use image node_id 16 for calling a global function with example usage: _smart_subs(expr, sub_dict) and returns: _recurser, expr, val, unknown