[msandr] Add check-before-write optimization.

Replace blind store with check-before-store to avoid unnecessary memory stores.

Patch by Qin Zhao.