The second-order version of Karamata's theorem with applications