r == 0 means EOF or that you passed zero-length input.  This check looks bogus though because at the first iteration r == 0, hence r > 0 is automatically false.