> The first "m" is changed because of the labial "p", but the second m
> seems simply changed arbitrarily, I think, for the way it sounds.

If I may respond to my own post, here is what Duroiselle has to say on
page 15 of his Pali Grammar:

"42. When precedeng a vowel, niggahita becomes m:
(i) ta.m+attha.m=tamattha.m
(ii) ya.m+aahu=yam aahu
(iii) ki.m+eta.m=kim eta.m"

Then there is a note:

"Rules 39 and 42 are not strictly adhered to in texts edited in Roman
characters; in prose above all, niggahita is allowed to remain
unchanged before a vowel or a consonant, even in the middle of a word
sometimes; in poetry, the retention of niggahita or its change to m
before a vowel, is regulated by the exigencies of the metres."

Yuttadhammo