The median American is not smart enough to do complex software engineering, just like they're not smart enough to be a doctor or college professor. All cognitively demanding jobs compete for the same (probably single-digit) percent of the workforce. Better education could certainly prepare
more people to do these jobs, but it's not a given that there are enough smart people in the domestic workforce to do all the cognitively-demanding jobs.
If thousands of the smartest people from the rest of the world want to move to the US and fill these gaps, doesn't that make America better off overall?