Rename toCBufferWithAttributes and move it into a free function