Using -0-, this code compiles and works and the values show as expected. Thank you!
Ok, so then if it does not work with optimization that makes it complicated.
Because, the error may or may not be where you think it is.
With optimization, the debugger may show the wrong value, and the error may be somewhere else.
Best chance you hove to check this, is to step out, and check the variable to which the return value is assigned.
If the debugger does show it wrong (but it actually does the right thing), that is probably still being the case in newer fpc versions.
This is because values are stored in temporary places (correct), but the compiler does not undergo the extra work to tell the debugger where those are.
If the value itself is actually wrong, when compiled with optimization.... If the program behaves incorrectly and when stepped out gets the wrong value...
There are several possibilities.
It could be an error, such as an uninitialized value somewhere in your code. However given that "output" was correct, that seems very very unlikely.
It could be a bug in the optimizer. There have indeed been one or two in 3.2.2. They should be fixed in the fixes branch. So if you use FpcUpDeluxe to get that (3.2.3) then that should do.
To know for sure, it would be necessary to compile with -alr, and look at the generated assembler. I have not downloaded your app to check, did not have the time.