Brazil’s top electoral court has given embattled President Michel Temer a big victory, voting to reject allegations of campaign finance violations that would 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 Mr Temer could remain in office amid a growing corruption scandal and single-digit popularity.

Last month, a recording emerged that apparently captured Mr Temer endorsing hush money to ex-House Speaker Eduardo Cunha, a former Temer ally serving 15 years in prison for corruption and money laundering.

Napoleao Nunes Maia FilhoSuperior Electoral Court Minister Napoleao Nunes Maia Filho speaks during a session of the court in Brasilia (Eraldo Peres/AP)

Soon after that, another bombshell came out: that Mr Temer was being investigated for allegedly receiving bribes.

Mr Temer has denied wrongdoing and vowed to stay in office.

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

Superior Electoral Court Minister Luiz FuxSuperior Electoral Court Minister Luiz Fux voted to remove Mr Temer (Eraldo Peres/AP)

Judge Gilmar Mendes, who has called Mr Temer “a friend of many years”, cast the decisive vote to keep Mr Temer in office.

Mr Mendes, also a justice on the Supreme Federal Tribunal, the country’s highest court, argued that electoral laws needed reform, suggesting that politicians should not pay the price for a broken system.

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

Gilmar MendesSuperior Electoral Court President Gilmar Mendes cast the decisive vote to keep Mr Temer in office (Eraldo Peres/AP)

The campaign finance case was filed shortly after the 2014 presidential election by one of the losing parties. It alleged that the ticket of President Dilma Rousseff and running mate Mr Temer, then the vice presidential candidate, gained an unfair advantage through illegal campaign contributions.

Mr Temer took over the presidency last year after Ms Rousseff was impeached and removed for illegally managing the federal budget.

The campaign finance allegations were bolstered in recent months by stunning testimony from plea bargains signed by current and former executives at the construction giant Odebrecht, a company at the centre of a colossal investigation into billions of dollars in inflated contracts and kickbacks to politicians.

The executives provided shocking details of tens of millions of dollars in bribes and illegal campaign contributions, including to the Rousseff-Temer ticket.

Herman Benjamin Superior Electoral Court Minister Herman Benjamin feels the heat (Eraldo Peres/AP)

Over four days of deliberations, the judges argued about whether those plea bargains should be considered in their decision. They also clashed over the strength of the original evidence and whether punishments should be doled out when illegal campaign finance was widespread.

A guilty verdict would have annulled the 2014 victory, thus stripping Mr Temer of the rest of his mandate. It could also have stripped both Ms Rousseff and Mr Temer of political rights for eight years.