The tax code in most countries, despite being tens of thousands of pages of legalese, are extremely subjective. This subjective and loose language allow for innumerable loopholes and tax dodges.
If you're using these creative methods, the chances are that you'll get audited every single year by the IRS or local equivalent. Trump famously gets audited every year. Your tax lawyers & accountants will then negotiate a tax payment with the tax office. I used the word negotiate because there is no one true formula for tax owed. It doesn't exist, especially not for corporations.
When you add in 'creative' intellectual property ownership and royalty payments, then the tax office basically has no idea where money is flowing. This is why EU countries commonly lob these huge penalties. Facebook tries to pay X in tax, and the tax office says they should pay X2, and then after negotiation they pay X1.5.