More or less copied from here, this seems to make the most sense to me:
int64_t muldiv64(const int64_t x, const int64_t n, const int64_t d)
{
/* find the integer and remainder portions of x/d */
const int64_t div = x / d;
const int64_t mod = x % d;
return div * n + (mod * n) / d;
}