If you think Donald Trump’s immigration ban is dead in the water, think again.

Yes, President Trump’s controversial executive order has been temporarily blocked by a federal judge — and a U.S. appeals court heard arguments from both sides on Tuesday. (Legal experts say the case almost certainly will go to the Supreme Court.)

Yes, Trump’s directive has failed to win the popular support he had hoped for. It also has faced tremendous corporate backlash, including from the leaders of Google GOOG, -1.03% , AAPL, -0.50% Microsoft MSFT, -0.55% , Netflix NFLX, -1.88% , Levi Strauss and more than 90 other U.S. companies.

But if the courts follow statutory and case law, Trump is likely to get his way in the end. That, at least, is the clear implication of a nonpartisan report recently circulated to members of Congress.

Trump stunned the world on Jan. 27 when he signed a sudden executive order suspending immigration for 90 days from seven mostly Muslim countries he deemed to have ties to Islamic terrorists, from Iran to Libya.

But the Immigration and Nationality Act of 1952 grants “the President broad authority to bar or impose conditions upon the entry of aliens,” reports the CRS, a federal organization that provides authoritative, impartial and nonpartisan analysis of major issues for members of Congress.

The law’s main limit on his powers is simply that “the President must have found that the entry of any aliens or class of aliens would be ‘detrimental to the interests of the United States.’” However, that’s not much of a limit at all, the CRS notes, because the law does not specify what is, or isn’t, detrimental. It’s up to the president to decide.

John Kelly: Travel ban rolled out too quickly

Furthermore, the case law to date, while limited, “also supports the view that this provision of the INA confers broad authority to suspend or restrict the entry of aliens.” A 1993 ruling on the topic by the U.S. Supreme Court favored the president’s authority. The U.S. District Court for Northern California ruled in 1996 that “The exclusion of aliens is a fundamental act of sovereignty. The right to do so stems not alone from legislative power but is inherent in the executive power to control the foreign affairs of the nation. When Congress prescribes a procedure concerning the admissibility of aliens, it is not dealing alone with a legislative power. It is implementing an inherent executive power.”

The CRS report was published on Jan. 23, before Trump’s executive order, and is an overview of the principles involved rather than the specifics of this order or any other.

Over the weekend close to 100 U.S. companies, most of them in the technology industry, filed a legal motion opposing the president’s immigration proposal. They argued it would harm their businesses. The companies’ amicus brief is long on political rhetoric and short on solid legal fundamentals. Yes, we are a nation of immigrants, and yes, many people are appalled by Trump’s executive order on political, moral, and economic grounds. But that cuts little ice with the law.

Some parts of the brief are stronger than others. Will suspending immigration from, say, Yemen, or Sudan, or Somalia really hurt the ability of, for example, Apple, or Salesforce.com CRM, -1.46% , to recruit talent? Will it interfere with top global technology conferences?

The brief’s main legal argument may rest on a flawed premise. It argues that denying immigration visas based on “nationality” is unlawful. However, the CRS notes, distinctions between aliens based on nationality have usually been allowed by the courts subject only to a “deferential ‘rational basis’ review.” (In other words, the courts will defer to a president if he can argue some sort of rational basis for excluding certain nationals. He might have trouble banning immigrants from Finland, say, on the basis “I just don’t like them.”)

“ Trump’s weakest terrain is on the subject of competence. ”

Trump’s weakest terrain is on the subject of competence. The executive order was badly bungled. I doubt even Kellyanne Conway could make up a set of “alternative facts” to make it look otherwise. It wasn’t reviewed properly and the Keystone Cops in the White House apparently gave little thought to implementation. It brought chaos at airports. As some wit said, this was essentially a Tweet turned into a presidential order. The courts may decide it is overly broad or slapdash.

Amazingly, Trump and his team either forgot to exclude U.S. green card holders from the ban, or couldn’t decide what to do about them and just left the issue up in the air for two days. That’s like forgetting to put gas in the car. It’s not a matter of debate, it’s just a screw-up. (There’s no plausible case for a blanket ban on green-card holders. And indeed, as permanent residents whose status is governed by statute, they may actually be immune from arbitrary presidential authority.)

It would be a brave or foolish person who tried to predict with absolute certainty what the courts will do. But if the CRS is right, the betting would be on Trump to get his way.