First, the procedure. The purchase contract is notarized. One clause states that for payment of the price, among other things, besides a clear land registry etc., also the waiver of the right of first refusal by the city must be present. The notary will therefore not request payment from the buyer before this is provided. If the city now exercises its right of first refusal, then the contract is null and void anyway.
But that does not happen. Something else might happen, depending on the contract that the original owner has with the municipality or city. It could be that it states that if xy is sold within z years and a profit beyond the originally paid price is achieved, then he has to share it with the municipality with x percent, unless the municipality explicitly waives this. Such a construct can only be circumvented if the purchase contract between you becomes null and void, should the municipality not waive it. For the municipality to waive it, the council must know the situation; the seller should therefore be completely open, tell the mayor or head of the council, depending on the municipal constitution, "Mr. Sundso, I want to sell at this price because I am backed into a corner. And if I achieve this price, I will be free of my debts and can try a fresh start, but if you want sum x from me now, it won't work. Then I probably won’t be able to prevent a forced sale by the bank...."
With some luck, it will go through. But if not, as a buyer you just have to wait until the bank acts and then step in. K.
The right of first refusal works a little differently. Every municipality has a right of first refusal on all properties without anything being entered or having to write anything extra into the purchase contract. If the right of first refusal is exercised, the municipality steps into the place of the original buyer without changes to the purchase contract. But a municipality only does this if it wants to influence the further development of the area. Building a road or renovating a district, adding a kindergarten, etc. A municipality does not buy a single house. The local resident discount is something completely different and is reclaimed by the seller after the sale.