This is an energy balance. All change in energy is due to work done by compressor.
Note: all Work, enthalpy and mass terms are "dot" (differential with time).
Work = delta h3 + delta h2
Work = m3 * cp * delta T3 + m2 * cp * delta T2
Work = (m1 - m2) * cp * (T3 - T1) + m2 * cp * (T2 - T1)
P&C