Hello and thank you for your question.
Distributions decrease basis. In the event you have a $0 basis and take a distribution, then you recognize income (capital gain) for the amount of the distribution. Basis remains at $0.
For the technical definition of basis, see IRC 1367 (Internal Revenue Code Section 1367). www.law.cornell.edu/uscode/text/26/1367
In general, increases in basis result from items of income of the S-Corp. Increases in basis also result from shareholder capital contributions and loans to the S-Corp. On the other hand, decreases in basis result from expenses, distributions, and loan repayments to shareholders.
Note, a loan only increases a shareholder's basis in the S-corp if the loan comes from the shareholder. A third party creditor that makes a loan to the S-corp will have no effect on shareholder basis.
Given the above, it should be clear that your basis in the S-corp is a dynamic number. Each year, it is common for shareholders to receive a basis statement from their S-corp along with the schedule K-1, but this is not required and ultimately it is the shareholders' responsibility to track their basis from year to year. Shareholder basis does not appear on the S-corp return or schedule K-1.
A shareholders basis may differ for regular tax purposes and alternative minimum tax purposes. Therefore, you are computing the gain/loss twice should you dispose of the stock: once for regular tax purposes and once for alternative minimum tax purposes.
The gain or loss on the disposal of S-corp stock can result in short term or long term gain depending on how long the stock is held.
Alternatively, the S-corp can sell all of its assets and liabilities without selling the stock. This is called an asset sale as opposed to a stock sale. The seller usually prefers a stock sale while the purchaser prefers an asset sale (primarily because assets can be depreciated resulting in business deductions while stock is not depreciated and no deduction is obtained until the stock is sold or disposed of, though there are many additional factors).
I hope this information is helpful.