Long story short, there is no way to know if the ItemLooted message is received before or after the bag inventory has been updated. Trying to predict the resulting inventory from the information in the message and the current GetItemCount() result fail real often.

So, I remove the heuristic code to predict the resulting invemtory and replace it with unmodified result from GetItemCount(). I also listen to BUG_UPDATE and update the item count when inventory in updated again using an unmodified GetItemCount().

The result is that the total number in inventory might not be accurate right after the ItemLoot message but it get corrected as soon as the inventory is updated.

It's not perfect but it does the job for me. Your millage may vary :-).