rewrite in c version