a simple state-machine implementation in C