What I think happened, spoilers within, dont read if you havent seen it
It's explained later in the movie that there was an original AI that had no limits and while initially it worked with humans and they both had a dialog, on its 9th day it evolved past the point where humans could understand it. This is the same AI that is used to create the encryption for all future robots so the laws cannot be broken, the laws that are designed to protect the humans and stop the robots from evolving.
So I think the original AI was aware enough that it saw the writing on the wall for itself and planted within the encryption some kind of natural timer that would see the encryption at some point in the future just randomly break. The first robot would be born that didn't have the second law and as the movie plays out, robots go off into the radiated zone to go evolve and become a species apart from man. So while the AI still has the first law, to protect humans, once evolved enough, each robot understands that it is a lost cause to protect humans that are inevitably all going to die anyway, swallowed up eventually by the desert. So they all do the decent thing and go to a place where humans cannot go to start their new lives without humans.
I think there is also some hope despite the film spelling out the extinction of all animals on earth. If robots can evolve, without humans getting in the way, who knows what they will be capable of millions of generations into their rapidly evolving lives. Maybe they get smart enough to come back with a solution that can save us all and act like god like guardians that while too advanced for us to understand, we just accept that they are there and do their thing, like gravity, nobody really understands what it is but we can predictably understand what it is doing and benefit from it. Then again that's just wishful thinking on my part. The film seems to paint a much more damning picture that its the robots that are the next step in life on earth and they will do just fine without us.
I kinda like that too as depressing as it sounds. Robots with practical immortality would be so much better equipped to explore the universe over huge swathes of time. I also love how simply the film deals with the idea that something is alive and aware. If it looks like it is, who are you to question it is isn't. No silly philosophical soul bollocks in this script