More general comments about porting C code to Pascal...
Before I even start porting the C code, I compile it and ensure it works correctly. There is no point in porting something that doesn't work.
One thing as a result of the above step is that I always have debuggable C code. That way if the Pascal port isn't working, I can place breakpoints in strategic places in both executables and verify values match. A mismatch gives an idea of where things are going wrong.
If the code relies on some random numbers, I will hard code matching "random number" values in the C and Pascal code. Repeatability is important and, often a necessity when things aren't working as expected. In extreme cases, even memory addresses can be made repeatable and synchronized between the two apps by inserting a custom memory manager in both executables. This is rarely needed but, can be extremely useful when it is needed to find the cause of one or more discrepancies.
Summary: try as much as possible to make the "environment" where the C and Pascal executables run to be as close as possible to being the same. That can save a lot of time and aggravation.
HTH.