Common algorithm implementation in C