NEW YORK (Fortune) -- Here's a scary, and relevant, question to ponder as the housing market continues to slide: What would it take for the government to step in and help Fannie Mae and Freddie Mac, and how would a rescue affect you, the taxpayer?

It's been a brutal week for Freddie (FRE, Fortune 500) and Fannie (FNM, Fortune 500). A Lehman analyst report Monday kicked off a stock rout that had shares in the mortgage finance giants hitting fresh multi-year lows Thursday. Freddie plummeted 22% Thursday. Fannie was down nearly 14%.

The beating continued Friday. Freddie and Fannie shares tumbled in early trading before recovering some of their lost ground. In afternoon trading, Freddie shares were down 14%; Fannie was off more than 26%.

The stock plunge, together with Fed Chairman Ben Bernanke's downbeat housing outlook on Tuesday, is forcing investors to consider what would happen if a bailout is needed - a prospect raised Thursday when William Poole, the former president of the St. Louis Federal Reserve, told Bloomberg the companies are already "insolvent."

Also on Thursday, The Wall Street Journal reported that officials at the U.S. Treasury Department have been monitoring the companies for months as part of its normal contingency planning, but that discussions about what to do should they collapse have picked up in recent weeks.

A grim outlook

Fannie Mae and Freddie Mac are government-sponsored enterprises that help the mortgage market function by purchasing pools of loans and packaging them into securities. If one or both couldn't function, the result would be chaos.

At the end of last year, Fannie alone had packaged and guaranteed about $2.8 trillion worth of mortgages, approximately 23% of all outstanding U.S. mortgage debt. And these securities are highly rated and sold to investors all over the world.

"If Fannie or Freddie failed, it would be far worse than the fall of [investment bank] Bear Stearns," says Sean Egan, head of credit ratings firm Egan Jones. "It could throw the economy into depression or something close to it."

Clearly, investors are concerned. Credit default swaps - a kind of insurance against the possibility of Fannie and Freddie defaulting on their corporate bonds, are at their most expensive levels in 14 weeks; both companies are expected to report steep losses for the second quarter; and their main business, mortgage securitization, is under pressure as home price values decline and foreclosure numbers rise.

"The major issue is that these are very leveraged financial institutions, leveraged much more than any other bank, and they have lots of mortgage assets. As real estate values decline every day, the value of [the mortgages that it bundles, guarantees, and sells] are called into question," says Dalton Investments co-founder Steve Persky, who has been focused on distressed mortgage assets.

The possibility of government aid looms because it's hard to see how the private market can help the companies. Their stock market values have dropped so low that it would be difficult for them to raise money. For example, Egan estimates that Freddie alone will need to raise $7 billion over the next two quarters due to writedowns and losses. But the company's market capitalization - the number of outstanding shares times the share price stands at $8.7 billion.

"An investment banker would be hard pressed to raise an amount of money nearly equal to the value of the entire company," Egan says.

What's more, both companies have already raised a total of $13 billion by issuing preferred stock at the end of 2007; and they reduced their dividend payments to conserve cash.

The disaster scenarios

The Federal Reserve and the Treasury have taken great pains to point out that the government is not obligated to bail out either Fannie or Freddie if they face insolvency.

It's debatable where the legal obligations lie, but as a practical matter, the government can't let these institutions fail because they are being counted up on to help fix the mortgage mess. If Fannie and Freddie were unable to buy and back loans, banks would stop originating them and the pool of homebuyers would shrink, causing home prices to fall even further.

"If the government believes the companies serve an essential role in the market, which they do, they cannot let them fail," says Joseph Mason, an economics professor with the University of Louisiana who focuses on the mortgage markets.

So what would force the Treasury and Fed to step in?

Fannie and Freddie are among the most highly-leveraged companies around, meaning the amount of capital they have on hand is nowhere close to the level of assets they control.

Fannie and Freddie must constantly borrow money in order to operate; if for any reason borrowing costs rose sharply they would not be able to make good on their guarantees or even fund their day to day operations. This is when the government would feel intense pressure to step in and, at the very least, pay contracts in a timely manner.

In an April report, Standard & Poor's said an Armageddon scenario whereby Fannie and Freddie are insolvent is unlikely, but that the mere possibility of failure at either is a greater threat to the economy than the actual collapse of any investment bank.

The bailout scenarios

So what might it look like if the government had to lend a hand? Outright nationalization is an unlikely option given that neither the current administration nor the presidential candidates could afford to support such a move in an election year.

More likely, the Treasury Department or the Federal Reserve would come in and provide a liquidity backstop, in the form of a loan or guarantee to bondholders that they will be paid. Fannie and Freddie could even do a preferred stock deal with the government, much like the deal forged by Citigroup with the Abu Dhabi Investment Authority, says Egan.

That would allow give officials the ability to argue that they weren't bailing out the companies, but rather making an investment that would pay off in the long run.

Mason has a diffferent twist on a possible intervention. If either were to face insolvency, he says the government should purchase a large voting block of equity in the institution and use that as a tool to eliminate any dividends, replace officers and manage the firms back to solvency.

"But [a rescue] would be a political situation, so it would be messy," says Mason. "Fannie and Freddie would fight against having officers replaced. They would want to keep the dividend."

The doomsday scenario could cost taxpayers more than $1 trillion, says the S&P report. The report went so far as to say that a government bailout of Fannie or Freddie could force the agency to lower its rating on the creditworthiness of the United States.