title: gcc replaces loops with memcpy and memset
body: <p>I have the following simple program:</p>
  
  <pre><code>#define N 20
  long c[N];
  long a[N + N];
  
  void f(void)
  {
      long *s = c;
      long *p = a;
      while (p != a + N) *p++ = *s++;
      while (p != a + N + N) *p++ = 0;
  }
  </code></pre>
  
  <p>I compile it with:</p>
  
  <pre><code>/usr/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi-gcc -mthumb -O3 -o main.o -c main.c
  </code></pre>
  
  <p>gcc conveniently replaces the loops with <code>memcpy</code> and <code>memset</code> respectively:</p>
  
  <pre><code>00000000 &lt;f&gt;:
     0:   b570            push    {r4, r5, r6, lr}
     2:   4d07            ldr     r5, [pc, #28]   ; (20 &lt;f+0x20&gt;)
     4:   4c07            ldr     r4, [pc, #28]   ; (24 &lt;f+0x24&gt;)
     6:   002a            movs    r2, r5
     8:   4907            ldr     r1, [pc, #28]   ; (28 &lt;f+0x28&gt;)
     a:   0020            movs    r0, r4
     c:   f7ff fffe       bl      0 &lt;memcpy&gt;
    10:   1960            adds    r0, r4, r5
    12:   002a            movs    r2, r5
    14:   2100            movs    r1, #0
    16:   f7ff fffe       bl      0 &lt;memset&gt;
    1a:   bc70            pop     {r4, r5, r6}
    1c:   bc01            pop     {r0}
    1e:   4700            bx      r0
  </code></pre>
  
  <p>Obviously, gcc is smart and decides that library implementation is more efficient, which may or may not be the case in each particular situation. I am wondering how this behavior can be avoided when, for example, speed is not important and library calls are not desirable.</p>