Well, even if he didn't intervene, megatron still would have shot and killed prime. Plus, he did what anyone else would do if they saw that megatron had a gun.
Yeah the first shot didn’t kill him, in fact none of the shots killed him immediately. Megatron was in worse shape than Prime, Optimus would’ve been able to get a few shots in and Megatron probably would’ve died then and there.