div(numer,denom) and ldiv(numer,denom), where numer>0 and denom<0, return
negative remainders, whereas the remainders should be positive in such a
situation. ANSI specifies that quot*denom + rem shall equal numer, which it
therefore does not.

The existing source code tries too hard to handle the various sign possibilities
for the two arguments. The second conditional in the existing code is intended
to fix up the result in case the machine truncates the quotient towards
-infinity, instead of towards zero, but this conditional is never taken in
DJGPP, since DJGPP always returns an integer quotient truncated toward zero.
Thus, it appears that no tests at all are needed when running the code under
DJGPP. Here is my suggested solution for div.c:
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <stdlib.h>
div_t
div(int num, int denom)
{
div_t r;
r.rem = num % denom;
r.quot = num / denom;
return r;
}
The fix for ldiv.c is similar. Note that I also took the liberty of re-ordering
the statements -- this seemed to please the optimizer, which otherwise
generated much useless data motion. The optimizer is, however, smart enough to
do only one idiv, so assembly language is probably not needed here.