When Bill Clinton recently criticized ObamaCare as “the craziest thing in the world,” it looked to everyone like he had given Donald Trump a stick to beat Hillary with at their second presidential debate this Sunday at Washington University in St. Louis.

But as usual, the Big Dog was crazy like a fox.

According to multiple sources both in the White House and the Clinton campaign, Bill was following a carefully crafted script that was poll tested and endorsed by his wife and Barack Obama.

“Barack warned Hillary that she was going to have to confront an attack from Trump or [the debate moderators] Anderson Cooper and Martha Raddatz on the problems with ObamaCare,” said a source with intimate knowledge of a conversation between the president and Hillary Clinton.

“Barack said he was aware of the widespread unhappiness with ObamaCare’s massive premium increases,” the source continued, “and that Hillary had to be ready during the presidential town hall debate to defang the attack that was sure to come.”

This source, who was in the room when Obama spoke to Hillary about the upcoming debate, reconstructed the conversation for me as follows:

“The president told Hillary, ‘You and Bill have to go out there and say there’s plenty wrong with ObamaCare, but that we know how to fix it if I’m elected president. I’m not running for anything, but I want to make sure my signature achievement isn’t dismantled by Donald Trump.’ ”

(Even Chelsea has gotten in on the act, saying this week that “we need to work on the ‘affordable’ part of the Affordable Care Act.”)

During the campaign, Obama has been in constant phone contact with Clinton, offering her tips and advice on how to bait Donald Trump and beat the Republicans.

Unlike most politicians who are eager to give the Democrat nominee their advice, Obama doesn’t have to go through layers of aides to reach her; he just picks up the phone in the Oval Office and calls her himself.

According to a source who spoke with Valerie Jarrett, Obama and Clinton — onetime bitter political adversaries — “are getting along fabulously.”

Obama’s No. 1 goal is to defeat Trump and save ObamaCare from repeal. He and Clinton understand that the Affordable Care Act has become unaffordable and is heading for a meltdown.

Clinton has developed a lot of respect for Obama’s political smarts, and she listens to his advice. During her preparation for the second debate, she passed on Obama’s advice to her campaign team and surrogates.

“Barack was a little taken aback when Bill called ObamaCare crazy,” said a source close to the president. “But Barack laughed it off, saying, ‘You never know what that guy is going to say.’ ”

“Calling ObamaCare crazy was a shocking thing to say,” agreed a source close to Bill Clinton. “But Bill’s comments were meant to assure the Democratic base that he and Hillary understand what their concerns about the spiraling costs of ObamaCare.

“Obama and his advisers knew in advance that Bill was going to tackle the subject,” the source continued. “It was coordinated three ways — the White House, Hillary’s campaign, and Bill. It was his job to help disarm the Republican opposition by vowing to change ObamaCare and make it truly affordable in the next Clinton administration.”

Edward Klein’s new book, “Guilty as Sin: Uncovering New Evidence of Corruption and How Hillary Clinton and the Democrats Derailed the FBI Investigation” (Regnery Publishing), is out now.