cleanup: port to memory wrapper API + coding style
simplify code leveraging memory_asprintf
more cleanup: coding style and hide local symbols