Making 100 microseconds delay for STM32L0
You are adding 50,000 iterations which contain a check, and increase for a counter. If this would take e.g. 5 clock cycli per iteration, this results is 250,000 cycli. With a 12 MHz clock this is 20,8 ms, which is anyway above your requirement.

I don't see the 5 seconds extra delay, but even 20,8 ms is too much. It might be related to stack handling if you call a separate function, still than 5 seconds is a lot, but it will be (much) longer than 20,8 ms.

The solution would be to put the right value directly in the timer  prescaler/period and remove the counter/loop.