information:extraction, information:classification, linguistic:paraphrasing
Struggle against Li Jiancheng and Li Yuanji

By this point, Li Shimin and his older brother Li Jiancheng, who was created crown prince in 618, reportedly after Emperor Gaozu first offered the position to Li Shimin due to his contributions, were locked in an intense rivalry, as Li Shimin's accomplishments caused people to speculate that he would displace Li Jiancheng as crown prince, and Li Jiancheng, while an accomplished general himself, was overshadowed by his younger brother. The court became divided into a faction favoring the Crown Prince and a faction favoring the Prince of Qin. The rivalry was particularly causing problems within the capital, as the commands of the Crown Prince, the Prince of Qin, and the Prince of Qi Li Yuanji were said to have the same force as the emperor's edicts, and the officials had to carry conflicting orders out by acting on the ones that arrived first. Li Shimin's staff was full of talented men, but Li Jiancheng was supported by Li Yuanji, as well as Emperor Gaozu's concubines, who had better relationships with Li Jiancheng and Li Yuanji than they did with Li Shimin.

Late in 622, when Liu Heita returned east after receiving aid from the Eastern Turks, defeating and killing Li Shimin's cousin Li Daoxuan (李道玄), the Prince of Huaiyang, he again regained most of former Xia territory. Li Jiancheng's staff members Wang Gui and Wei Zheng suggested that Li Jiancheng needed to enhance his own reputation in battle, and so Li Jiancheng volunteered for the mission. Emperor Gaozu thus sent Li Jiancheng, assisted by Li Yuanji, to attack Liu. Li Jiancheng defeated Liu around the new year of 623, and Liu was subsequently betrayed by his own official Zhuge Dewei (諸葛德威) and delivered to Li Jiancheng. Li Jiancheng executed Liu in his former capital and returned to Chang'an in triumph. China was, by this point, roughly united under Tang rule.

For the next few years, the rivalry intensified, although during the meantime both Li Jiancheng and Li Shimin served as generals when the Eastern Göktürks made incursions. In 623, when the general Fu Gongshi rebelled at Danyang (丹楊, in modern Nanjing, Jiangsu), Emperor Gaozu briefly commissioned Li Shimin to attack Fu, but soon cancelled the order and sent Li Shimin's cousin Li Xiaogong the Prince of Zhao Commandery instead.

In 624, when Li Jiancheng was found to have, against regulations, tried to add soldiers to his guard corps, Emperor Gaozu was so angry that he put Li Jiancheng under arrest. In fear, Li Jiancheng's guard commander Yang Wen'gan (楊文幹) rebelled. Emperor Gaozu sent Li Shimin against Yang, offering to make him crown prince after he returned. After Li Shimin left, however, Feng Deyi (now a chancellor), Li Yuanji, and the concubines all spoke on Li Jiancheng's behalf, and after Li Shimin returned, Emperor Gaozu did not depose Li Jiancheng, but instead blamed the discord between him and Li Shimin on Li Jiancheng's staff members Wang Gui and Wei Ting (韋挺) and Li Shimin's staff member Du Yan, exiling them to Xi Prefecture (巂州, roughly modern Liangshan Yi Autonomous Prefecture, Sichuan).

Later that year, Emperor Gaozu, troubled by repeated Göktürk incursions, seriously considered burning Chang'an to the ground and moving the capital to Fancheng, a suggestion that Li Jiancheng, Li Yuanji, and Pei Ji agreed with. Li Shimin opposed, however, and the plan was not carried out. Meanwhile, Li Shimin himself was sending his confidants to Luoyang to build up personal control of the army there. After an incident in which Li Shimin suffered a severe case of food poisoning after feasting at Li Jiancheng's palace—an event that both Emperor Gaozu and Li Shimin apparently interpreted as an assassination attempt—Emperor Gaozu considered sending Li Shimin to guard Luoyang to prevent further conflict, but Li Jiancheng and Li Yuanji, after consulting each other, believed that this would only give Li Shimin an opportunity to build up his personal power there, and therefore opposed it. Emperor Gaozu therefore did not carry out the plan. Meanwhile, the rivalry continued. Traditional historical accounts also indicated that at one point, when Li Shimin visited Li Yuanji's mansion, Li Yuanji wanted to assassinate Li Shimin, but Li Jiancheng, who could not resolve to kill a brother, stopped the plot. There was yet another incident in which Li Jiancheng, knowing that a horse threw its rider easily, had Li Shimin ride it, causing Li Shimin to fall off from it several times.

By 626, Li Shimin was fearful that he would be killed by Li Jiancheng, and his staff members Fang Xuanling, Du Ruhui, and Zhangsun Wuji were repeatedly encouraging Li Shimin to attack Li Jiancheng and Li Yuanji first—while Wei Zheng was encouraging Li Jiancheng to attack Li Shimin first. Li Jiancheng persuaded Emperor Gaozu to remove Fang and Du, as well as Li Shimin's trusted guard generals Yuchi Jingde and Cheng Zhijie, from Li Shimin's staff. Zhangsun Wuji, who remained on Li Shimin's staff, continued to try to persuade Li Shimin to attack first.

In summer 626, the Göktürks were making another attack, and under Li Jiancheng's suggestion, Emperor Gaozu, instead of sending Li Shimin to resist the Göktürks as he first was inclined, decided to send Li Yuanji instead. Li Yuanji was given command of much of the army previously under Li Shimin's control, further troubling Li Shimin, who believed that with the army in Li Yuanji's hands, he would be unable to resist an attack. Li Shimin had Yuchi summon Fang and Du back to his mansion secretly, and then on one night submitted an accusation to Emperor Gaozu that Li Jiancheng and Li Yuanji were committing adultery with Emperor Gaozu's concubines. Emperor Gaozu, in response, issued summonses to Li Jiancheng and Li Yuanji for the next morning, convening the senior officials Pei Ji, Xiao Yu, and Chen Shuda to examine Li Shimin's accusations. As Li Jiancheng and Li Yuanji approached the central gate leading to Emperor Gaozu's palace, Xuanwu Gate (玄武門), Li Shimin carried out the ambush he had set. He personally fired an arrow that killed Li Jiancheng. Subsequently, Yuchi Jingde killed Li Yuanji. Li Shimin's forces entered the palace and, under the intimidation of Li Shimin's forces, Emperor Gaozu agreed to create Li Shimin crown prince. Li Jiancheng's and Li Yuanji's sons were killed, and Li Shimin took Li Yuanji's wife Princess Yang as a concubine. Two months later, with Li Shimin firmly in control of power, Emperor Gaozu yielded the throne to him (as Emperor Taizong).