Let's say local variable b is at -8(%rbp), and local variable c is at -12(%rbp).
.L3: movq -8(%rbp), %rax
andl $1, %eax
Set the value of eax to the value of the least significant bit of b.
xorl %eax, -12(%rbp)
Perform an exclusive-or with c and the least significant bit of b, storing the result in c.
sarq -8(%rbp)
Divide b by 2.
cmpq $0, -8(%rbp)
jg .L3
Go back to start of loop if b is greater than 0, otherwise continue.
So the corresponding C code is:
do {
c ^= (b & 1);
b /= 2; // Or: b >>= 1;
} while ( b > 0 );
although the existence of the .L2 label suggests there may be a jmp .L2 immediately before, which you're not showing us, in which case it would be a while loop:
while ( b > 0 ) {
c ^= (b & 1);
b /= 2; // Or: b >>= 1;
}
A working demonstration (using gas assembler on OS X):
asm_func.S:
.globl _asm_func
.text
_asm_func:
push %rbp
mov %rsp, %rbp
sub $16, %rsp
movq %rdi, -8(%rbp)
movl %esi, -12(%rbp)
jmp .L2
.L3:
movq -8(%rbp), %rax
andl $1, %eax
xorl %eax, -12(%rbp)
sarq -8(%rbp)
.L2:
cmpq $0, -8(%rbp)
jg .L3
movl -12(%rbp), %eax
leave
ret
main.c:
#include <stdio.h>
int asm_func(int b, int c);
int c_func(int b, int c)
{
while ( b > 0 ) {
c ^= (b & 1);
b >>= 1;
}
return c;
}
int main(void)
{
for ( int i = 112; i < 127; i += 7 ) {
for ( int j = 203; j > 182; j -= 9 ) {
printf("C function (%d, %d): %d\n", i, j, c_func(i, j));
printf("Asm function(%d, %d): %d\n", i, j, asm_func(i, j));
}
}
return 0;
}
Makefile:
prog: main.o asm_func.o
cc -o prog main.o asm_func.o
main.o: main.c
cc -o main.o main.c -c -std=c99 -pedantic -Wall -Wextra
asm_func.o: asm_func.S
as -o asm_func.o asm_func.S
with output:
paul@horus:~/src/sandbox/so_asm$ ./prog
C function (112, 203): 202
Asm function(112, 203): 202
C function (112, 194): 195
Asm function(112, 194): 195
C function (112, 185): 184
Asm function(112, 185): 184
C function (119, 203): 203
Asm function(119, 203): 203
C function (119, 194): 194
Asm function(119, 194): 194
C function (119, 185): 185
Asm function(119, 185): 185
C function (126, 203): 203
Asm function(126, 203): 203
C function (126, 194): 194
Asm function(126, 194): 194
C function (126, 185): 185
Asm function(126, 185): 185
paul@horus:~/src/sandbox/so_asm$