C port of the cardinality code from streamlib