RIO DE JANEIRO — Brazil’s top electoral court gave embattled President Michel Temer a big victory late Friday, voting to reject allegations of campaign finance violations that could have removed him from office.

After four days of deliberations, judges voted 4-3 in a case that many viewed as a measure of whether Temer could remain in office amid a ballooning corruption scandal and single-digit popularity.

Last month, a recording emerged that apparently captured Temer endorsing hush money to ex-House Speaker Eduardo Cunha, a former Temer ally serving 15 years in prison for corruption and money laundering. Soon after that, details of another bombshell came out: that Temer was being investigated for allegedly receiving bribes.

Temer has denied wrongdoing and vowed to stay in office.

“The facts are very serious, unbearable,” said Judge Luiz Fux, who voted to remove Temer, adding the campaign finance case was about “very serious crimes.”

Judge Gilmar Mendes, who has called Temer “a friend of many years,” cast the decisive vote to keep Temer in office. Mendes, also a justice on the Supreme Federal Tribunal, the country’s highest court, argued that electoral laws need reform.

“The system needs stability. It is very easy to talk about morality, fighting against corruption. I want that too,” said Mendes, who in the past has come to the aid of other politicians facing legal trouble. “A president can’t just be replaced at any time, even if the desire is there.”

The political turmoil in Brazil has reached a fever pitch. There are near weekly protests calling for Temer’s ouster, frequent shouting matches in Congress, and a simmering debate in the media over whether Temer will manage to finish out his term.