EEPROM I2C driver hanged during ESD test?
It sounds like your test conditions are causing the I2C driver code, or the I2C module itself, to enter a state from which they can't recover.

Try putting a timeout on the loops to detect a failure; if a failure occurs then reset both I2C hardware and drivers.

The simplest timeout to implement would be to count the number of times you've tested the various flags and when that count expires, exit with an error code.

A more complicated (i.e. useful) approach would be to monitor a hardware timer that's running in the background.  When a loop starts, record the current value of the timer plus the timeout that you want; check the timer at the start of each loop and if it exceeds your pre-calculated value then exit with an error code.