On the CNF encoding of cardinality constraints and beyond