EDIT: Thaddy missed, that the "original" code also sets Result to i if Result<0
Saw that at once ...
You saw that wrong..... My intention was to fix the logic and I did.
Simply recompile Lazarus for proof. The OR was incorrect and what broke it.
If you do not recompile you will never see it is correct.
The reverse logic from Zvoni also compiles and renders the same result.
The second error was passing implicitly -1 to the call, because -1 was used as index where it should have been the index, not [result], but i.