Preparation of hydrocarbons or halogenated hydrocarbons