added C version for comparison