diff --git a/bin/mktreeopt.pike b/bin/mktreeopt.pike
index 89f85295a6fd1984e85ae7cc96475a8492b8fe46..64035a8022d1519d047825ba2c3d314bbaf5a91e 100644
--- a/bin/mktreeopt.pike
+++ b/bin/mktreeopt.pike
@@ -1,5 +1,5 @@
 /*
- * $Id: mktreeopt.pike,v 1.29 2000/05/15 21:24:39 grubba Exp $
+ * $Id: mktreeopt.pike,v 1.30 2000/05/15 21:26:40 grubba Exp $
  *
  * Generates tree-transformation code from a specification.
  *
@@ -236,7 +236,7 @@ constant header =
 "/* Tree transformation code.\n"
 " *\n"
 " * This file was generated from %O by\n"
-" * $Id: mktreeopt.pike,v 1.29 2000/05/15 21:24:39 grubba Exp $\n"
+" * $Id: mktreeopt.pike,v 1.30 2000/05/15 21:26:40 grubba Exp $\n"
 " *\n"
 " * Do NOT edit!\n"
 " */\n"
@@ -1031,7 +1031,7 @@ string generate_match(array(object(node)) rule_set, string indent)
 		"      ((CA%sR(n)->master?CA%sR(n)->master:CA%sR(n))==\n" +
 		indent + "       (%s->master?%s->master:%s)))\n"
 		"#endif /* SHARED_NODES_MK2 */\n" +
-		indent + ") {\n",
+		indent + "  ) {\n",
 		tpos, expr,
 		tpos, expr,
 		tpos, tpos, tpos,
@@ -1095,7 +1095,17 @@ string generate_match(array(object(node)) rule_set, string indent)
       } else {
 	res += indent;
       }
-      res += sprintf("if (CD%sR(n) == %s) {\n", tpos, expr);
+      sprintf("if ((CD%sR(n) == %s)\n"
+	      "#ifdef SHARED_NODES_MK2\n" + indent +
+	      "  || (CD%sR(n) && %s &&\n" + indent +
+	      "      ((CD%sR(n)->master?CD%sR(n)->master:CD%sR(n))==\n" +
+	      indent + "       (%s->master?%s->master:%s)))\n"
+	      "#endif /* SHARED_NODES_MK2 */\n" +
+	      indent + "  ) {\n",
+	      tpos, expr,
+	      tpos, expr,
+	      tpos, tpos, tpos,
+	      expr, expr, expr);
       res += generate_match(exacts[expr], indent + "  ");
       res += indent + "}";
     }