Even a little more complicated than that. IBM didn't randomly try and fail to license CP/M. They were sent there by Gates himself. Gates called Kildall and told him he was sending some important people to meet him, to hear them out. He couldn't say more due to the NDA he'd signed. Kindall responding by snubbing the meeting to fly in his private jet, having his wife be quite rude to the IBM dudes who had shown up ready to make them billionaires. Gates only made a deal with IBM after the Kindall's embarrassed him. It's like when you vouch for a friend to your boss but they end up getting fired for being incompetent.
Gates tried to help Kindall, but Kindall just didn't see the writing on the wall. Gates saw it. And his brilliance wasn't in creating ms-dos, it was the deal with IBM. He didn't "give it to them". He licensed it to them, while keeping the rights to sell it to their competitors, the IBM clones.