unsigned int reverseBits(unsigned int num) {
  unsigned int BITS = sizeof(num) * 8;
  unsigned int reverse_num = 0, i;
  for (int i = 0; i < BITS; i++)
    reverse_num |= ((2 ^ i & num) << (BITS - 1 - i));
  return reverse_num;
}


x86 assembly (clang, -O0):
<reverseBits>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x20,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    1150 <reverseBits+0x50>
mov    -0x14(%rbp),%eax
and    -0x4(%rbp),%eax
xor    $0x2,%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x14(%rbp),%ecx
shl    %cl,%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    111c <reverseBits+0x1c>
mov    -0xc(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverseBits>:
movd   %edi,%xmm0
pshufd $0x0,%xmm0,%xmm0
movdqa 0xeef(%rip),%xmm2
pand   %xmm0,%xmm2
movdqa 0xef3(%rip),%xmm1
pxor   %xmm1,%xmm2
pshufd $0xf5,%xmm2,%xmm3
pmuludq 0xef2(%rip),%xmm2
pmuludq 0xefa(%rip),%xmm3
pshufd $0xe8,%xmm2,%xmm2
pshufd $0xe8,%xmm3,%xmm3
movdqa 0xef8(%rip),%xmm4
pand   %xmm0,%xmm4
pxor   %xmm1,%xmm4
pshufd $0xf5,%xmm4,%xmm5
pmuludq 0xef3(%rip),%xmm4
punpckldq %xmm3,%xmm2
pshufd $0xe8,%xmm4,%xmm3
pmuludq 0xef2(%rip),%xmm5
pshufd $0xe8,%xmm5,%xmm4
punpckldq %xmm4,%xmm3
movdqa 0xef1(%rip),%xmm4
pand   %xmm0,%xmm4
pxor   %xmm1,%xmm4
pshufd $0xf5,%xmm4,%xmm5
pmuludq 0xeec(%rip),%xmm4
por    %xmm2,%xmm3
pshufd $0xe8,%xmm4,%xmm4
pmuludq 0xeeb(%rip),%xmm5
pshufd $0xe8,%xmm5,%xmm2
punpckldq %xmm2,%xmm4
movdqa 0xeea(%rip),%xmm2
pand   %xmm0,%xmm2
pxor   %xmm1,%xmm2
pshufd $0xf5,%xmm2,%xmm5
pmuludq 0xee5(%rip),%xmm2
pshufd $0xe8,%xmm2,%xmm2
pmuludq 0xee8(%rip),%xmm5
pshufd $0xe8,%xmm5,%xmm5
punpckldq %xmm5,%xmm2
por    %xmm4,%xmm2
movdqa 0xee3(%rip),%xmm4
pand   %xmm0,%xmm4
pxor   %xmm1,%xmm4
pshufd $0xf5,%xmm4,%xmm5
pmuludq 0xede(%rip),%xmm4
por    %xmm3,%xmm2
pshufd $0xe8,%xmm4,%xmm3
pmuludq 0xedd(%rip),%xmm5
pshufd $0xe8,%xmm5,%xmm4
punpckldq %xmm4,%xmm3
movdqa 0xedc(%rip),%xmm4
pand   %xmm0,%xmm4
pxor   %xmm1,%xmm4
pshufd $0xf5,%xmm4,%xmm5
pmuludq 0xed7(%rip),%xmm4
pshufd $0xe8,%xmm4,%xmm4
pmuludq 0xeda(%rip),%xmm5
pshufd $0xe8,%xmm5,%xmm5
punpckldq %xmm5,%xmm4
por    %xmm3,%xmm4
movdqa 0xed5(%rip),%xmm3
pand   %xmm0,%xmm3
pxor   %xmm1,%xmm3
pshufd $0xf5,%xmm3,%xmm5
pmuludq 0xed0(%rip),%xmm3
pshufd $0xe8,%xmm3,%xmm3
pmuludq 0xed3(%rip),%xmm5
pshufd $0xe8,%xmm5,%xmm5
punpckldq %xmm5,%xmm3
por    %xmm4,%xmm3
pand   0xece(%rip),%xmm0
pxor   %xmm1,%xmm0
pshufd $0xf5,%xmm0,%xmm1
pmuludq 0xecd(%rip),%xmm0
por    %xmm2,%xmm3
pshufd $0xe8,%xmm0,%xmm0
pmuludq 0xecc(%rip),%xmm1
pshufd $0xe8,%xmm1,%xmm1
punpckldq %xmm1,%xmm0
por    %xmm3,%xmm0
pshufd $0xee,%xmm0,%xmm1
por    %xmm0,%xmm1
pshufd $0x55,%xmm1,%xmm0
por    %xmm1,%xmm0
movd   %xmm0,%eax
ret

LLVM IR (-O0):
define i32 @reverseBits(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
store i32 32, i32* %3
store i32 0, i32* %4
store i32 0, i32* %6
br label %7
7: ; preds = %23, %1
%8 = load i32, i32* %6
%9 = load i32, i32* %3
%10 = icmp ult i32 %8, %9
br i1 %10, label %11, label %26
11: ; preds = %7
%12 = load i32, i32* %6
%13 = load i32, i32* %2
%14 = and i32 %12, %13
%15 = xor i32 2, %14
%16 = load i32, i32* %3
%17 = sub i32 %16, 1
%18 = load i32, i32* %6
%19 = sub i32 %17, %18
%20 = shl i32 %15, %19
%21 = load i32, i32* %4
%22 = or i32 %21, %20
store i32 %22, i32* %4
br label %23
23: ; preds = %11
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
store i32 %25, i32* %6
br label %7
26: ; preds = %7
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
define i32 @reverseBits(i32 %0) {
%2 = insertelement <4 x i32> poison, i32 %0, i64 0
%3 = shufflevector <4 x i32> %2, <4 x i32> poison, <4 x i32> zeroinitializer
%4 = and <4 x i32> %3, <i32 0, i32 1, i32 2, i32 3>
%5 = xor <4 x i32> %4, <i32 2, i32 2, i32 2, i32 2>
%6 = shl <4 x i32> %5, <i32 31, i32 30, i32 29, i32 28>
%7 = and <4 x i32> %3, <i32 4, i32 5, i32 6, i32 7>
%8 = xor <4 x i32> %7, <i32 2, i32 2, i32 2, i32 2>
%9 = shl <4 x i32> %8, <i32 27, i32 26, i32 25, i32 24>
%10 = or <4 x i32> %9, %6
%11 = and <4 x i32> %3, <i32 8, i32 9, i32 10, i32 11>
%12 = xor <4 x i32> %11, <i32 2, i32 2, i32 2, i32 2>
%13 = shl <4 x i32> %12, <i32 23, i32 22, i32 21, i32 20>
%14 = or <4 x i32> %13, %10
%15 = and <4 x i32> %3, <i32 12, i32 13, i32 14, i32 15>
%16 = xor <4 x i32> %15, <i32 2, i32 2, i32 2, i32 2>
%17 = shl <4 x i32> %16, <i32 19, i32 18, i32 17, i32 16>
%18 = or <4 x i32> %17, %14
%19 = and <4 x i32> %3, <i32 16, i32 17, i32 18, i32 19>
%20 = xor <4 x i32> %19, <i32 2, i32 2, i32 2, i32 2>
%21 = shl <4 x i32> %20, <i32 15, i32 14, i32 13, i32 12>
%22 = or <4 x i32> %21, %18
%23 = and <4 x i32> %3, <i32 20, i32 21, i32 22, i32 23>
%24 = xor <4 x i32> %23, <i32 2, i32 2, i32 2, i32 2>
%25 = shl <4 x i32> %24, <i32 11, i32 10, i32 9, i32 8>
%26 = or <4 x i32> %25, %22
%27 = and <4 x i32> %3, <i32 24, i32 25, i32 26, i32 27>
%28 = xor <4 x i32> %27, <i32 2, i32 2, i32 2, i32 2>
%29 = shl <4 x i32> %28, <i32 7, i32 6, i32 5, i32 4>
%30 = or <4 x i32> %29, %26
%31 = and <4 x i32> %3, <i32 28, i32 29, i32 30, i32 31>
%32 = xor <4 x i32> %31, <i32 2, i32 2, i32 2, i32 2>
%33 = shl <4 x i32> %32, <i32 3, i32 2, i32 1, i32 0>
%34 = or <4 x i32> %33, %30
%35 = call i32 @llvm.vector.reduce.or.v4i32(<4 x i32> %34)
ret i32 %35
}