– Brazil for years reveled in its image as a post-racial, left-leaning society. Now Jair Bolsonaro — a far-right outsider who says he “loves” President Donald Trump — has surged to the front of the pack in Sunday’s presidential elections, sharply dividing Latin America’s largest nation.

To tackle runaway crime, Bolsonaro says, police should gun down more criminals. “A gay son is the result of a lack of beatings,” he once quipped, and insists that homosexuality should not be discussed in elementary schools.

He once said that women — because they get pregnant — deserve less pay than men. He has called some minorities fat and lazy.

Like Trump, he embraces social media to reach legions of loyal followers. His rallies have become outlets for white men rattled by social and economic change. He has vowed to drain the swamp in the capital and make Brazil “great.”

“It’s going to be beautiful,” the front-runner’s 34-year-old son, Eduardo Bolsonaro, said while envisioning his father’s presidency at a rally last week. “It will be just like Trump in the United States.”

As this nation of 208 million goes to the polls Sunday, Brazil is a polarized place. The economy is foundering. Huge swaths of its political class are tainted by corruption.

Bolsonaro, 63, has advanced with a Brazil-first campaign that attacks the traditional press while grabbing headlines with tough talk and his own set of alternative facts. His popularity, some say, reflects the globalization of the anti-globalists and the rise of Western candidates who seek to exploit racial and social fault lines.

In Europe and the U.S., far-right and populist movements have aimed their anger at immigrants and minorities. In Brazil, the mostly white upper-middle class has felt threatened by the rise of a largely black lower-middle class whose income shot up faster than that of the wealthiest segment of society during more than a decade of leftist Workers’ Party rule starting in 2003. During those years, liberal programs sent many black Brazilians to college for the first time.

With the economy languishing, however, all segments of society are hurting, and Bolsonaro’s impassioned speeches are touching a raw nerve. His rallies attract not only ultraconservatives in T-shirts with skulls but also angry professionals who feel they have lost ground.

“Today, if you are a white, heterosexual man of means, you must be punished,” said Marcio Ferraz, 47, a Sao Paulo doctor who attended a Bolsonaro rally Sunday. “We don’t have a social debt to blacks. We need to be responsible for ourselves.”

Anti-crime message resonates

Yet much like Trump in 2016, Bolsonaro is winning support from many voters who are turning to him despite his words on minorities and women, not because of them. His pledge to crack down on crime and political corruption and prevent the return of a leftist government are resounding with millions who see him as the last, best chance for law and order. He has managed to portray himself as an outsider despite his 27 years in Congress.

Bolsonaro in recent months has sought to tone down his most controversial rhetoric, praising Brazil’s cultural diversity in a tweet Thursday and softening his stance on gender inequality, in what analysts say is an attempt to increase his ­electability.

“He’s not the savior of the country, but to beat the communists in the Workers’ Party, he is the best option,” said Danilo Monato, 30, a black physical-education teacher who attended the recent ­Bolsonaro rally.

In much of Latin America, polls show, Trump is desperately unpopular. But Bolsonaro and his supporters — who sometimes fly U.S. flags at rallies — view the U.S. president as a political touchstone.

Bolsonaro’s similarities with Trump are “conscious and deliberate,” said Guilherme Casarões, a comparative politics professor at the Getulio Vargas Foundation, a university in Sao Paulo. “Five years ago, he was just another congressman with anti-gay views. Now Bolsonaro, like Trump, has become a larger-than-life figure.”

He continued: “Bolsonaro uses well-crafted rhetoric — his slogan is ‘Brazil before everything, and God above all.’ He has been able to reach out to different groups of voters who feel abandoned by the political class. They are now blind to the negative things he says and does, and tend to overlook any aspect that speaks against their ­candidate.”

Bolsonaro has embraced the politics of fear. He demonized his opponents on the left as corrupt, inept radicals and his foes on the right as weak centrists unable to “strongly” represent the conservative cause.

Conspiracy theories rule his world: If he loses, it will be only because of a plot to steal the presidency from him, he says. “I will not accept an election result that is not my own victory,” Bolsonaro said in a TV interview last month.

His talk of bringing back family values has been music to the ears of conservative Catholics and evangelicals, even though he is now on his third wife. After an unsuccessful assassination attempt last month some Brazilians even tweeted Bible verses and proclaimed his survival a divine sign.

Like Trump, Bolsonaro was initially underestimated by Brazilian pundits, who laughed off his candidacy. They aren’t laughing now. A former army captain whose running mate is a retired general, Bolsonaro has lavished praise on the 21-year, right-wing military dictatorship that ruled Brazil until 1985.

If he wins and finds himself unable to cobble together a majority in Congress, some observers fear he may back military intervention and seize authoritarian powers.