Robin Williams’ two messy divorces gave him plenty of material for bitter alimony jokes, but the state of his finances was no laughing matter.

The comic shelled out $20 million to $30 million to ex-wives Valerie Velardi and Marsha Garces, according to reports.

Although Velardi signed a prenup, she scored at least $50,000 a month for life when they split in 1988, a Hollywood insider told The Post. Court records also show Williams paid Velardi more than $518,000 in 1989 from a profit-sharing plan administered by accounting firm Joel Faden and Co. of Manhattan.

While Williams’ net worth has been estimated at $130 million, TMZ reported that he plowed a sizeable chunk of his money into a 2009 trust for his three kids: Zachary, now 31, and half-siblings Zelda, 25, and Cody, 22.

Terms of the trust gave them each one-third of their shares when they turned 21, another third at 25 and the remainder at age 30.

It’s unclear how much cash Williams had left to himself, but last year he lamented that he had to put his ranch in Napa up for sale. “I just can’t afford it anymore,” he said.