Your implementation does not behave correctly in minor timesteps. This is not obvious in the test model that you have attached, because the scope will only evaluate the output in major timesteps.
The difference becomes obvious if you feed the output of the C-Scripts to an integrator. With your implementation there is an integration error (the output of the integrator does not become 0 after one period), whereas the reference implementation works correctly (see attached model).