Compiling with x86-64 gcc -Og -std=gnu99 -xc.
In the second line of .L3 (addl (%rdi,%rcx,4), %eax), why not just use the register %edx when adding to the sum?
addl (%rdi,%edx,4), %eax
int sum_arr(int arr[], int nelems) {
int sum = 0;
for (int i = 0; i < nelems; i++) {
sum += arr[i];
}
return sum;
}
sum_arr:
movl $0, %edx
movl $0, %eax
jmp .L2
.L3:
movslq %edx, %rcx
addl (%rdi,%rcx,4), %eax
addl $1, %edx
.L2:
cmpl %esi, %edx
jl .L3
rep ret