title: Analysis of simple ARM assembly code
body: <p>I began to study the ARM assembly language with the GNU toolchains and create a very simple function example in C  with the following code:</p>
  
  <pre><code>#include &lt;stdint.h&gt;
  
      uint32_t *a;
      uint32_t *b;
      uint32_t *c;
  
       __attribute__((naked)) void f() {
  
               *a += *c;
               *b += *c;
         }
  </code></pre>
  
  <p>After I used this commands in the terminal to see the assembly code:</p>
  
  <pre><code>arm-none-eabi-gcc -O1 -S -std=c99 example.c -o -
  </code></pre>
  
  <p>And that's the result:</p>
  
  <pre><code>    @ Function supports interworking.
      @ Naked Function: prologue and epilogue provided by programmer.
      @ args = 0, pretend = 0, frame = 0
      @ frame_needed = 0, uses_anonymous_args = 0
      ldr r3, .L2
      ldr r2, .L2+4
      ldr r3, [r3]
      ldr r2, [r2] ; r2 &lt;- &amp;(*c)
      ldr ip, [r3]
      ldr r0, [r2] ; r0 &lt;- *c
      ldr r1, .L2+8
      add r0, ip, r0
      str r0, [r3]
      ldr r3, [r1]
      ldr r2, [r2] ; why make the same thing 
      ldr r1, [r3]
      add r2, r1, r2
      str r2, [r3]
  .L3:
      .align  2
  .L2:
      .word   a
      .word   c
      .word   b
      .size   f, .-f
      .comm   c,4,4
      .comm   b,4,4
      .comm   a,4,4
  </code></pre>
  
  <p>My question is why the compiler load two times the address of the pointer c, if i well understand, that is the line </p>
  
  <pre><code>ldr  r2, [r2] 
  </code></pre>
  
  <p>I can't find a good reason for the compiler replicate this code.
  Thanks in advance.</p>