sanitycheck: autodetect errors in optimize.c
Fix stupid error in optimization code, add test case
document earlier error