It's not actually all that hard once you realize:
a) the ECMA-335 specification is incorrect, whether to sign- or zero-extend does not always depends on the source type
b) the C# compilator generates completely weird code when casting between (U)IntPtr and integers.
Map all IntPtr casts to a few semi-sane cases, and we can obtain the correct semantics without introducing any helper methods.