Being a good coder in such a company could be problematic, because they won't be able to recognize ability or benefit from the difference in skill to an average coder. These companies will treat programmers poorly, will try to save costs, outsource, etc.
I don't get why you claim that younger people will replace 40-somethings. It's not like the older devs will break outside of the warranty period.
Companies in EU countries have the opposite problem, they can't easily dismiss employees, even when perhaps justified.
I wouldn't want to work in the US, it's too darwinian. A company shouldn't be able to dismiss an empoyee just because there's someone cheaper available.
Or: this argument is meaningless because it's assuming more supply (of someone like you) than jobs (and perfectly informed employers for that matter).
Is there a way out? Sure:
1. If you actually are that good of a software engineer, start a My Code As Service company - it is the "402 PAY_ME_A_TRUCKLOAD_OF_MONEY" response. There are gobs of companies out there that pay gobs of money for rock star performers because there are just not that many of them.
2. Create - or buy - your own lifestyle business. You will never make millions of dollars but you should be comfortable.
3. Stop pretending that coding matters. Start managing those that produce code. Being a technically competent manager is a great way not to get fired.