All 3 of my dogs do that too. I don't think it has anything to do with pain. They lick each other's penises and do the same thing. It's quite disturbing. I do seriously doubt it's pain tho...I'd look further into it.
I am concerned that both these responses are related to pain. Have your dog examined by a veterinarian to rule out a urinary tract infection and bladder stones. Licking the penis can be a response to irritation and now we have to figure out where the irritation is coming from. Teeth chattering can also be a sign of discomfort; it also may be an incidental finding.