Guy, you make it complex, I am well in finance, but know a little, below is my opinion, if wrong, correct me:
1.what's stock market: Stock is a market, that let company collecting money for company developing through stock exchanging, first, stock itself is not unvalued, but because it can exchange, it has price, like currency.
2.The company which IPO get the first benifit from the stock market: When a company start IPO, the instituation(A) and public(B) can buy the share through Primary and secondary market that you said( Normal public can't buy it through Primary market), if the company have 1 million shares, assume that, each share's price is $20(In fact, in primary, you can get it cheaper, for simple, let's set it $20), and in fact, in the 1 million shares, many are owned by company owner, assume about 0.5 million, so the company get about 10 million$, instituation and public lost 10 million$, but they have shares.
then instituation and public exchange the share in the secondary market, if the share price up to 30$, C buy them, so till now: Company get 10 million, the A and B earn 5 million, the C spend 15 million on these, the C's wealth is distributed to Company, A, B, the whole wealth is 15 million( If you want caculate the 0.5 million shares owned by company owner, the whole value is from 20 million to 30 million).
Then if the share price to 10$, C sell it to D, C lost 5 million$,the price of 0.5 million shares is down from 15 million to 5 million right. is 10 million disappear, no one get it? I don't thing so, from IP, Company earn 10 million, A and B earn 5 million, D lost 10 million, and value of 0.5 million is 5 million: 10(Company earned) +5( A and B earned) = 10( D lost) + 5 (Value of 0.5 million). it is wealth transfer, not disappear.
The $3.7 trillion has been took by others before falling.