Use GCC builtins instead of inline asm
Fix memory leak in new counter implementation

Since we're allocating memory for use outside Haskell, we're
responsible for freeing it. Switched to ForeignPtr to automate this.