Pull in commit a769e80 from upstream (with some fixes to make it apply) to fix build on 32bit architectures.