Re-apply customizations from the libffi 3.1

Re-apply customizations from the libffi 3.1