20.3.2.2 Intermixing C and Assembly

If your code has any inline assembly, ensure that the code is written in such a way that the compiler knows about register access and data flow.

For example, the following code will likely cause a failure when optimized.

int foo;

int bar() {
  asm("mov _foo,w7");
  asm("inc w7,w0");
  asm("return");
}

Though the example could be written in pure C as return foo+1;, the correct way to write this in inline assembly requires the use of extended Asm syntax:

int bar() {
  int result;

  asm("inc %1,%0" : "=r"(result) : "r"(foo));
  return result;
}

This allows the compiler to connect C variables to registers. The compiler will pick an appropriate register and arrange to save the values as needed.

Ensure any assembly function that your write satisfies the rules specified in Mixing C and Assembly Code.