one function ported to c++ from asm