The mechanism of corrosion inhibition and transition to underdeposit corrosion