Masochism (enjoying pain) was probably selected by a combination of rape (it could be incredibly painful, and so enjoying pain reduced the resistance to it), as well as it's function in combat (it allows you to take a beating without having to lose sensitivity to pain, which makes it quite useful if an untreated scratch or trying to escape a fight can kill you). It likely developed into pleasure because it was easier to use the existing pleasure framework than to create a completely new system and was thus a more common adaptation.
Sadism likely developed as from the desire to test for combat related traits in both your allies and your mates (if your husband or wife can withstand pain, then your sons will be better fighters and your daughters will produce better sons). It's vary similar to an unhealthy form of typical testing behaviour, such as that which is seen in monosex friendships (male testing seems to be less cruel than female testing) and in unisex outgroup relationships, though this is often seen as harassment by those who fail such testing.
Humiliation and name calling are likely tied to the same testing, though looking for "strength of will" and cool-headedness rather than masochism/physical tolerance.
These are just guesses, it could be that masochism is just the same function as an adrenaline addiction, that is to say, a coping mechanism for an extreme stressor becomes a method to get a high.