Cse rewrite

A rewrite of common-subexpression-elimination. The old version was pretty flaky.
Does this function need to early return if result is not Equal? It directly sets result here.