int reverse_bits_v2(int num) {
  int num_copy = num;
  num_copy = (((num_copy & 0xff000000) >> 24) & 0x000000ff) |
             (((num_copy & 0x00ff0000) >> 8) & 0x0000ff00) |
             (((num_copy & 0x0000ff00) << 8) & 0x00ff0000) |
             (((num_copy & 0x000000ff) << 24) & 0xff000000);
  num_copy = (((num_copy & 0xf0f0f0f0) >> 4) & 0x0f0f0f0f) |
             (((num_copy & 0x0f0f0f0f) << 4) & 0xf0f0f0f0);
  num_copy = (((num_copy & 0xcccccccc) >> 2) & 0x33333333) |
             (((num_copy & 0x33333333) << 2) & 0xcccccccc);
  num_copy = (((num_copy & 0xaaaaaaaa) >> 1) & 0x55555555) |
             (((num_copy & 0x55555555) << 1) & 0xaaaaaaaa);
  return (num_copy);
}


x86 assembly (clang, -O0):
<reverse_bits_v2>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0xff000000,%eax
shr    $0x18,%eax
and    $0xff,%eax
mov    -0x8(%rbp),%ecx
and    $0xff0000,%ecx
sar    $0x8,%ecx
and    $0xff00,%ecx
or     %ecx,%eax
mov    -0x8(%rbp),%ecx
and    $0xff00,%ecx
shl    $0x8,%ecx
and    $0xff0000,%ecx
or     %ecx,%eax
mov    -0x8(%rbp),%ecx
and    $0xff,%ecx
shl    $0x18,%ecx
and    $0xff000000,%ecx
or     %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0xf0f0f0f0,%eax
shr    $0x4,%eax
and    $0xf0f0f0f,%eax
mov    -0x8(%rbp),%ecx
and    $0xf0f0f0f,%ecx
shl    $0x4,%ecx
and    $0xf0f0f0f0,%ecx
or     %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0xcccccccc,%eax
shr    $0x2,%eax
and    $0x33333333,%eax
mov    -0x8(%rbp),%ecx
and    $0x33333333,%ecx
shl    $0x2,%ecx
and    $0xcccccccc,%ecx
or     %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0xaaaaaaaa,%eax
shr    $0x1,%eax
and    $0x55555555,%eax
mov    -0x8(%rbp),%ecx
and    $0x55555555,%ecx
shl    $0x1,%ecx
and    $0xaaaaaaaa,%ecx
or     %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
pop    %rbp
ret

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

LLVM IR (-O0):
define i32 @reverse_bits_v2(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = and i32 %5, -16777216
%7 = lshr i32 %6, 24
%8 = and i32 %7, 255
%9 = load i32, i32* %3
%10 = and i32 %9, 16711680
%11 = ashr i32 %10, 8
%12 = and i32 %11, 65280
%13 = or i32 %8, %12
%14 = load i32, i32* %3
%15 = and i32 %14, 65280
%16 = shl i32 %15, 8
%17 = and i32 %16, 16711680
%18 = or i32 %13, %17
%19 = load i32, i32* %3
%20 = and i32 %19, 255
%21 = shl i32 %20, 24
%22 = and i32 %21, -16777216
%23 = or i32 %18, %22
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = and i32 %24, -252645136
%26 = lshr i32 %25, 4
%27 = and i32 %26, 252645135
%28 = load i32, i32* %3
%29 = and i32 %28, 252645135
%30 = shl i32 %29, 4
%31 = and i32 %30, -252645136
%32 = or i32 %27, %31
store i32 %32, i32* %3
%33 = load i32, i32* %3
%34 = and i32 %33, -858993460
%35 = lshr i32 %34, 2
%36 = and i32 %35, 858993459
%37 = load i32, i32* %3
%38 = and i32 %37, 858993459
%39 = shl i32 %38, 2
%40 = and i32 %39, -858993460
%41 = or i32 %36, %40
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = and i32 %42, -1431655766
%44 = lshr i32 %43, 1
%45 = and i32 %44, 1431655765
%46 = load i32, i32* %3
%47 = and i32 %46, 1431655765
%48 = shl i32 %47, 1
%49 = and i32 %48, -1431655766
%50 = or i32 %45, %49
store i32 %50, i32* %3
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
define i32 @reverse_bits_v2(i32 %0) {
%2 = tail call i32 @llvm.bitreverse.i32(i32 %0)
ret i32 %2
}