This is pseudocode.
post
has been retrieved from the API.
Be sure to make substrings and other string operations by "multibyte" divisions.
text = post.text;
entities = post.entities;
diff = 0;
entities.links = reverse entities.links;
for entities.links as link
if link.amended_len exists
text = (substring of text from position 0 to (link.pos + diff)) and '[' and link.text and '](' and link.link and ')' and (substring of text from (link.pos+diff+link.amended_len) until the end)
diff += link.amended_len - link.len;
text is the original, now.