Do I lose if I have an empty library and cast Brainstorm?
You will still lose, even though your library will not be empty when you lose.
You will fully resolve Brainstorm, including drawing as many as you can from your library (none in this case), and then putting 2 cards onto your library. After Brainstorm resolves, state-based actions will be checked, including this one:

704.5b If a player attempted to draw a card from a library with no cards in it since the last time state-based actions were checked, he or she loses the game.

So because you had made an attempt to draw a card from a library with no cards in it, you will lose, even though your library now has cards in it.