Rewriting: implemented template AIG generation.