Yes, better to avoid all the conversions between int and uint. Nim compiler does not really like uint, but can use it.
Note that your Nim arithmetics are not identical to C arithmetic. For example
in Nim code you have
uint(k-Kn)
So you use signed values for substraction, and convert result to unsigned. C
code does unsigned substraction. You may try
uint(k) - uint(Kn)
