They shouldn't be
They have a decent OS freely available to them (which they insist on touching and breaking).
They have divisions churning our the memory, screens and CPUs that all these phones need.
They have no shortage of cash, to do whatever they want with.
Their problem, is that they make pretty fugly high-end phones (no, I've really no idea why), which then force them to create even worse low-end phones so as not to cannibalize their own high-end.
If you look at Xiaomi, Oppo, Lenovo etc etc. It seems you can make a pretty high-spec phone for a lot less than some people are trying to sell them for.
If you want to rule the top end, you have to differentiate. Sony's camera, LG's screen-res, HTC's physical design. Samsung... back to my previous point. I genuinely have no idea why anybody would buy a phone from them.
They either need to pull their finger our and design something paradigm shifting at the high end (I'd like another choice) or cut their losses, bail out, compete with the middle-tiers and leverage their vertical.