Two minor things:
1. I don't think ret can be non-zero here so this is not needed and slightly simplifies the code.
2.  A comment describing the long conditional would be helpful.