The patch deduplicates all the code with a set of common macros.
I don't know what to do with pre-existing comments, they interfere with current macro use.
I don't know if there is a machine which provides smap but does not provide erms. I generated a routine just in case.
There will be further changes to slightly optimize this code.