[Profile] fix overhead counts in format=:flat (#49824)
Regression caused by #41742, which inverted the loop without inverting
the logic. And fix a number of related formatting mistakes.
Fix #49732
(cherry picked from commit 45748b84ac74b01eb6080d92f79948a817a50005)