(CNN) It's 3 a.m. and you're wide awake for the second night in a row. You get up and stumble to the kitchen for some warm milk mom swears will put you to sleep.

So why are you eating a bowl of ice cream with a side of cookies?

"When you're sleep deprived, you're not like, 'Oh, you know what, I want some carrots,' " said behavioral neuroscientist Erin Hanlon, who studies the connection between brain systems and behavior at the University of Chicago.

"You're craving sweets and salty and starchy things," she added. "You want those chips, you want a cookie, you want some candy, you know?"

An ancient system

What is it about sleep exhaustion and junk food? The answer lies in history, back when we dug in the dirt for starchy tubers, foraged for sweet berries and gorged on fatty fish.

Simply put, a lack of sleep triggers ancient instincts that yearn for rich, sweet, fatty foods.

"Evolutionarily speaking, it was a big deal to have a high carb, high fat meal, because you didn't necessarily have those all of the time," explained Hanlon, also a research assistant professor.

"If you think back to feast or famine times, having a meal with lots of carbs or fat was something that your brain would say, 'Hey, we want to have that.'

"It's only the last bit of humanity that we have been able to have food around the clock or food that is high in saturated fats and high in carbs," Hanlon continued. "And our brain has not evolved as quickly as the food choices that have become available."

Sleep and endocannabinoids

You may have heard about two hormones that control our urge to eat: leptin and ghrelin.

"I always tell my patients to think about them by their first letter," said sleep specialist Dr. Raj Dasgupta, the associate program director of the Sleep Medicine Fellowship at Keck Medicine of USC.

"The 'l' in leptin stands for lose: It suppresses appetite and therefore contributes to weight loss," he said. "The 'g' in ghrelin stands for gain: This fast-acting hormone increases hunger and leads to weight gain."

When you're sleep deprived, research shows , ghrelin levels spike while leptin takes a nose dive. The result is an increase in hunger.

But food is food, so that doesn't explain why we crave candy instead of carrots.

To answer that, you have to look at another system of the body: The endocannabinoid system. Its job is to keep our body in homeostasis, or equilibrium and it regulates everything from sleep to appetite to inflammation to pain and more.

Science only discovered the first cannabinoid receptor in 1988, in the brain of a rat. Within a few years, they found two receptors, CB1 and CB2, and discovered it was present in all vertebrates -- mammals, birds, reptiles and fish -- and possibly some invertebrates.

Now we know it's an ancient system, evolving eons ago. In fact, the most primitive animal known to have cannabinoid receptors is the sea squirt, which lived more than 600 million years ago.

What does this system have to do with eating? Endocannabinoids bind to the same receptors as the active ingredient in marijuana, which as we know, often triggers the "munchies."

"We know that you can infuse endocannabinoids into specific regions of animals' brains and make them eat," said Hanlon. "They will eat specific things, more palatable things. They will choose sucrose over saccharin, despite the fact that in theory both taste the same, but the sucrose has more carbs."

Researchers now believe it could be the endocannabinoid system that triggers our hedonistic cravings for fatty, starchy and sugary foods.

And just like with leptin and ghrelin, sleep deprivation seems to make it worse.

In a study published in 2016 , Hanlon compared the circulating levels of 2-AG, one of the most abundant endocannabinoids, in people who got four nights of normal sleep (more than eight hours) to people who only got 4.5 hours.

People who were sleep-deprived reported greater increases in hunger and appetite and had higher afternoon concentrations of 2-AG than those who slept well. They also had a rough time controlling their urges for high-carb, high-calorie snacks.

Why? "We don't know," Hanlon said.

All of this research is so new that science doesn't yet know how it all fits together or what can be done about it.

And that brings us to the bottom line: There's not gonna be a pill any time soon for the sleep-deprived junk-food junkies that we are.

Instead, you'll have to do what the doctor says to reduce your illicit cravings in the middle of the night: Get more sleep.