Merge.hs: cleanup: move optimizer code to separate helper

Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>