convert endian.cpp/state.cpp to C