Merge pull request #3864 from 9rnsr/fix13187

[REG2.066a] Issue 13187 - Function wrongly deduced as pure
Merge pull request #3865 from 9rnsr/2.066

Cherry-picking commits from master to 2.066 branch