code: def _sqrtdenest_rec(expr):
      """Helper that denests the square root of three or more surds.
  
      Explanation
      ===========
  
      It returns the denested expression; if it cannot be denested it
      throws SqrtdenestStopIteration
  
      Algorithm: expr.base is in the extension Q_m = Q(sqrt(r_1),..,sqrt(r_k));
      split expr.base = a + b*sqrt(r_k), where `a` and `b` are on
      Q_(m-1) = Q(sqrt(r_1),..,sqrt(r_(k-1))); then a**2 - b**2*r_k is
      on Q_(m-1); denest sqrt(a**2 - b**2*r_k) and so on.
      See [1], section 6.
  
      Examples
      ========
  
      >>> from sympy import sqrt
      >>> from sympy.simplify.sqrtdenest import _sqrtdenest_rec
      >>> _sqrtdenest_rec(sqrt(-72*sqrt(2) + 158*sqrt(5) + 498))
      -sqrt(10) + sqrt(2) + 9 + 9*sqrt(5)
      >>> w=-6*sqrt(55)-6*sqrt(35)-2*sqrt(22)-2*sqrt(14)+2*sqrt(77)+6*sqrt(10)+65
      >>> _sqrtdenest_rec(sqrt(w))
      -sqrt(11) - sqrt(7) + sqrt(2) + 3*sqrt(5)
      """
      from sympy.simplify.radsimp import (
          radsimp,
          rad_rationalize,
          split_surds,
      )
  
      if not expr.is_Pow:
          return sqrtdenest(expr)
      if expr.base < 0:
          return sqrt(-1) * _sqrtdenest_rec(sqrt(-expr.base))
      g, a, b = split_surds(expr.base)
      a = a * sqrt(g)
      if a < b:
          a, b = b, a
      c2 = _mexpand(a**2 - b**2)
      if len(c2.args) > 2:
          g, a1, b1 = split_surds(c2)
          a1 = a1 * sqrt(g)
          if a1 < b1:
              a1, b1 = b1, a1
          c2_1 = _mexpand(a1**2 - b1**2)
          c_1 = _sqrtdenest_rec(sqrt(c2_1))
          d_1 = _sqrtdenest_rec(sqrt(a1 + c_1))
          num, den = rad_rationalize(b1, d_1)
          c = _mexpand(d_1 / sqrt(2) + num / (den * sqrt(2)))
      else:
          c = _sqrtdenest1(sqrt(c2))
  
      if sqrt_depth(c) > 1:
          raise SqrtdenestStopIteration
      ac = a + c
      if len(ac.args) >= len(expr.args):
          if count_ops(ac) >= count_ops(expr.base):
              raise SqrtdenestStopIteration
      d = sqrtdenest(sqrt(ac))
      if sqrt_depth(d) > 1:
          raise SqrtdenestStopIteration
      num, den = rad_rationalize(b, d)
      r = d / sqrt(2) + num / (den * sqrt(2))
      r = radsimp(r)
      return _mexpand(r)
  
imports: from sympy.core import Add, Expr, Mul, S, sympify
  from sympy.core.function import _mexpand, count_ops, expand_mul
  from sympy.core.sorting import default_sort_key
  from sympy.core.symbol import Dummy
  from sympy.functions import root, sign, sqrt
  from sympy.polys import Poly, PolynomialError
label_desc: Use image node_id 8 for calling a global function with example usage: _sqrtdenest_rec(expr) and returns: _mexpand, sqrtdenest, unknown