Consider this C code:
extern volatile int hardware_reg;
void f(const void *src, size_t len)
{
void *dst = ;
hardware_reg = 1;
Here is a slightly modified example, compiled with gcc 7.2.1 on x86-64:
#include
static int temp;
extern volatile int hardware_reg;
int foo (int x)
{
hardware_reg = 0;
memcpy(&temp, &x, sizeof(int));
hardware_reg = 1;
return temp;
}
gcc knows that the memcpy() is the same as an assignment, and knows that temp is not accessed anywhere else, so temp and the memcpy() disappear completely from the generated code:
foo:
movl $0, hardware_reg(%rip)
movl %edi, %eax
movl $1, hardware_reg(%rip)
ret