was executed. Based on the laws of the time, Wang Gui was to be executed as well, but he fled into the Qinling Mountains and remained in hiding there for more than a decade. In 617, the general Li Yuan rose against Emperor Yang, and later that year captured the capital Chang'an, declaring Emperor Yang's grandson Yang You the Prince of Dai emperor (as Emperor Gong) but retaining power himself as regent. Li Yuan's staff member Li Gang (李綱) recommended Wang Gui, as he knew Wang Gui's capabilities. Li Yuan made Wang Gui a military advisor to his heir apparent Li Jiancheng. During Emperor Gaozu's reign In 618, after news arrived at Chang'an that Emperor Yang had been killed in a coup at Jiangdu (江都, in modern Yangzhou, Jiangsu) led by the general Yuwen Huaji, Li Yuan had Yang You yield the throne to him, establishing Tang Dynasty as its Emperor Gaozu. He created Li Jiancheng crown prince, and Wang Gui continued to serve on Li Jiancheng's staff as a trusted advisor. In 622, with Tang having mostly reunited the former Sui territory under its rule due to the contributions of Li Jiancheng's younger brother Li Shimin the Prince of Qin, Li Shimin's fame and power thus threatened Li Jiancheng's position. Wang and Wei Zheng, another advisor to Li Jiancheng, suggested that he should also try to establish some military achievement of his own and also recruit capable people to serve on staff during the campaign. Li Jiancheng agreed, and volunteered to attack one of the remaining threats against Tang rule, Liu Heita the Prince of Handong. Li Jiancheng subsequently defeated Liu, who was then betrayed by his own subordinate Zhuge Dewei (諸葛德威) in 623 and delivered to Li Jiancheng, who executed Liu. Subsequently, in 624, when Li Jiancheng was suspected of having recruited his personal guards against regulations and having encouraged his guard commander Yang Wen'gan (楊文幹) into rebelling, Emperor Gaozu briefly considered deposing Li Jiancheng and replacing him with Li Shimin, but changed his mind after intercession by the chancellor Feng Deyi, another son Li Yuanji the Prince of Qi, and his concubines (who were on much better relations with Li Jiancheng than with Li Shimin). However, he blamed Li Jiancheng's and Li Shimin's staff members for encouraging their rivalry, and he exiled Wang Gui, Wei Ting (韋挺, another of Li Jiancheng's advisors), and Du Yan (an advisor