On the origin of O2 and other volatile species in comets