S&P 500 caps off dismal year with worst loss since 2008 crash