int reverse(int x) {
  x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
  x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
  x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
  x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
  return ((x >> 16) | (x << 16));
}


x86 assembly (clang, -O0):
<reverse>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0xaaaaaaaa,%eax
shr    $0x1,%eax
mov    -0x4(%rbp),%ecx
and    $0x55555555,%ecx
shl    $0x1,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0xcccccccc,%eax
shr    $0x2,%eax
mov    -0x4(%rbp),%ecx
and    $0x33333333,%ecx
shl    $0x2,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0xf0f0f0f0,%eax
shr    $0x4,%eax
mov    -0x4(%rbp),%ecx
and    $0xf0f0f0f,%ecx
shl    $0x4,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0xff00ff00,%eax
shr    $0x8,%eax
mov    -0x4(%rbp),%ecx
and    $0xff00ff,%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x10,%eax
mov    -0x4(%rbp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse>:
mov    %edi,%eax
shr    %eax
and    $0x55555555,%eax
and    $0xd5555555,%edi
lea    (%rax,%rdi,2),%eax
mov    %eax,%ecx
shr    $0x2,%ecx
and    $0x33333333,%ecx
and    $0xf3333333,%eax
lea    (%rcx,%rax,4),%eax
mov    %eax,%ecx
shr    $0x4,%ecx
and    $0xf0f0f0f,%ecx
shl    $0x4,%eax
and    $0xf0f0f0f0,%eax
or     %ecx,%eax
bswap  %eax
ror    $0x10,%eax
mov    %eax,%ecx
sar    $0x10,%ecx
shl    $0x10,%eax
or     %ecx,%eax
ret

LLVM IR (-O0):
define i32 @reverse(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* %2
%4 = and i32 %3, -1431655766
%5 = lshr i32 %4, 1
%6 = load i32, i32* %2
%7 = and i32 %6, 1431655765
%8 = shl i32 %7, 1
%9 = or i32 %5, %8
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = and i32 %10, -858993460
%12 = lshr i32 %11, 2
%13 = load i32, i32* %2
%14 = and i32 %13, 858993459
%15 = shl i32 %14, 2
%16 = or i32 %12, %15
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = and i32 %17, -252645136
%19 = lshr i32 %18, 4
%20 = load i32, i32* %2
%21 = and i32 %20, 252645135
%22 = shl i32 %21, 4
%23 = or i32 %19, %22
store i32 %23, i32* %2
%24 = load i32, i32* %2
%25 = and i32 %24, -16711936
%26 = lshr i32 %25, 8
%27 = load i32, i32* %2
%28 = and i32 %27, 16711935
%29 = shl i32 %28, 8
%30 = or i32 %26, %29
store i32 %30, i32* %2
%31 = load i32, i32* %2
%32 = ashr i32 %31, 16
%33 = load i32, i32* %2
%34 = shl i32 %33, 16
%35 = or i32 %32, %34
ret i32 %35
}

LLVM IR (-O2):
define i32 @reverse(i32 %0) {
%2 = lshr i32 %0, 1
%3 = and i32 %2, 1431655765
%4 = shl i32 %0, 1
%5 = and i32 %4, -1431655766
%6 = or i32 %3, %5
%7 = lshr i32 %6, 2
%8 = and i32 %7, 858993459
%9 = shl i32 %6, 2
%10 = and i32 %9, -858993460
%11 = or i32 %8, %10
%12 = lshr i32 %11, 4
%13 = and i32 %12, 252645135
%14 = shl i32 %11, 4
%15 = and i32 %14, -252645136
%16 = or i32 %13, %15
%17 = lshr i32 %16, 8
%18 = and i32 %17, 16711935
%19 = shl i32 %16, 8
%20 = and i32 %19, -16711936
%21 = or i32 %18, %20
%22 = ashr i32 %21, 16
%23 = shl i32 %21, 16
%24 = or i32 %22, %23
ret i32 %24
}