As the bug occurs only when malloc is in the tested function,
Note that gcc 'knows' that memory obtained by malloc does not alias other memory. You can use a differently named wrapper function for malloc, or use the malloc attribute for another function, to experiment how this affects code generation.