[3/4] reduce cookie tracking: count cookies

add an efficient cookie counting function

Fixes bootlop on PAC-Man rom

Thanks plaguedbypenguins for this!