C code version of the python loop.