tored with the return to power of the victorious Sultan Barquq.
During this period, Ibn Khaldun devoted his time to lecturing and studying, as well as to completing his Universal History. After Yulbugha’s revolt, he wrote about asabiyah and its role in the rise and fall of states. He applied his theory to the Egyptian theater since the time of Salah ad-Din. 
Fourteen years after leaving the position of Chief Maliki Judge, Ibn Khaldun was reassigned to the post upon the death of the presiding judge. The state again fell into disarray upon Sultan Barquq’s death and his son’s ascension. Ibn Khaldun was not a party to these revolts and asked permission to visit Jerusalem. He joined the Sultan Faraj’s caravan on its way back from Damascus, and was relieved of his duties as judge for the second time, again due to political intrigue. However, this did not matter because he was called upon to accompany the sultan on a perilous journey with fate to Damascus. 
During Ibn Khaldun’s stay in Egypt he was asked by Sultan Faraj of Egypt to accompany him on his expedition to Damascus. News reports had confirmed the movement of Tamerlane’s war party toward Damascus. Sultan Faraj and his army were on their way to Damascus, and it seems that Ibn Khaldun was asked, firmly, to accompany the sultan to Damascus. 
The sultan stayed in Damascus just two weeks, as he had to leave because of rumors that a revolt was in the works back in Cairo. Ibn Khaldun and several notables were left behind in Damascus. It was up to the leaders of Damascus to deal with Tamerlane. Ibn Khaldun had suggested that they consider Tamerlane’s terms. It was the task of another judge, Ibn Muflih, to discuss the terms with Tamerlane. When Ibn Muflih returned from Tamerlane’s camp, the terms were not agreeable to the residents of Damascus.
Since it was Ibn Khaldun’s suggestion to come to terms with Tamerlane, Ibn Khaldun felt obliged to meet with Tamerlane personally, and so he left Damascus and went to Tamerlane’s camp. It is not clear whether he went on his own or in an official capacity. Ibn Khaldun took gifts with him for Tamerlane and they were well received; he stayed in Tamerlane’s camp for thirty-five days.
Over this period, Ibn Khaldun had many meetings with Tamerlane, conversing through an interpreter, Abd al-Jabbar al-Khwarizmi (d. 1403 A.C.E. / A.H.). Ibn Khaldun’s account is the only detailed account available; the subjects they discussed were varied and some were unrecorded. Walter Fischel lists six specific topics about which they talked:
1. The Maghrib and Ibn Khaldun’s land of origin;
2. Heroes in history;
3. Predictions of things to come;
4. The Abbasid Caliphate;
5. Amnesty and security “for Ibn Khaldun and his companion;”
6. Ibn Khaldun’s intention to stay with Tamerlane. 
Ibn Khaldun impressed the conqueror enough that he was asked to join Tamerlane’s court. Some biographers have suggested that he did plan to join Tamerlane’s court and that he wrote his eloquent appeal to return to Egypt to settle his affairs, get his books and family and join Tamerlane. However, it is more likely that Ibn Khaldun left on good terms with Tamerlane, and accomplished his mission of extracting favorable terms for the people of Damascus. 
Ibn Khaldun’s departing words lend credence to the fact that he would not be returning to his service:
Is there any generosity left beyond that which you have already shown me? You have heaped favors upon me, accorded me a place in your council among your intimate followers, and shown me kindness and generosity which I hope Allah will repay to you in like measures. 
Upon Ibn Khaldun’s return to Egypt, he was restored to his position as Maliki judge. Due to the political situation within the community of Maliki judges, Ibn Khaldun was dismissed and reinstated three times during the five-year period. He died while in office on Wednesday, March 17 1406 A.C.E. (25 of Ramadan 808 A.H.). He was buried in the sufi cemetery outside Bab an-Nasr, Cairo at the age of seventy-four. 
Ibn Khaldun’s works can be classified in the categories of historica