D: Nature's approximation of a short-term utility function for genetic replication.

Ultimately all common behaviors of organisms are explained by Selfish Gene Theory. Certain behaviors improve the chances of reproduction of the organism, such as running away from dangerous situations, or attacking other animals which threaten close relatives. The purpose of the brain is to control the behavior of the organism, and emotions are the mechanism control this. Certain things are desirable (or even necessary) for genetic replication.

The concept 'maximal genetic replication' is too abstract for an entity without a high degree rational thought. Hence this is expressed as short-term goals which approximate the behavior. A good analogy is with the static evaluation of computer chess. The utility function is actually producing many offspring, but there are shorter term goals (realized as drives) which are likely to achieve that.

The primary drives are

The first three are necessary for the organism to survive, the last is highly desirable for genetic replication (but not actually necessary if there are close relatives).

Emotions are expressions of these drives.

and arguably

There are others which seem to be unique to human emotions.