#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint32_t;
typedef int /*<<< orphan*/ masks;

/* Variables and functions */
uint32_t reverse_bits(uint32_t val) {
  int s;
  const uint32_t masks[] = {0x55555555, 0x33333333, 0xF0F0F0F, 0xFF00FF,
                            0xFFFF};
  for (s = 0; s < sizeof(masks) / sizeof(masks[0]); ++s)
    val = ((val >> (1 << s)) & masks[s]) | ((val & masks[s]) << (1 << s));
  return val;
}


x86 assembly (clang, -O0):
<reverse_bits>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    0xef2(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xeef(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xeed(%rip),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
movslq -0x8(%rbp),%rax
cmp    $0x5,%rax
jae    1182 <reverse_bits+0x82>
mov    -0x4(%rbp),%eax
mov    -0x8(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
sar    %cl,%eax
movslq -0x8(%rbp),%rcx
and    -0x20(%rbp,%rcx,4),%eax
mov    -0x4(%rbp),%edx
movslq -0x8(%rbp),%rcx
and    -0x20(%rbp,%rcx,4),%edx
mov    -0x8(%rbp),%ecx
mov    $0x1,%esi
shl    %cl,%esi
mov    %esi,%ecx
shl    %cl,%edx
mov    %edx,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    112d <reverse_bits+0x2d>
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_bits>:
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):
@__const.reverse_bits.masks = [5 x i32] [i32 1431655765, i32 858993459, i32 252645135, i32 16711935, i32 65535]
define i32 @reverse_bits(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [5 x i32]
store i32 %0, i32* %2
%5 = bitcast [5 x i32]* %4 to i8*
store i32 0, i32* %3
br label %6
6: ; preds = %30, %1
%7 = load i32, i32* %3
%8 = sext i32 %7 to i64
%9 = icmp ult i64 %8, 5
br i1 %9, label %10, label %33
10: ; preds = %6
%11 = load i32, i32* %2
%12 = load i32, i32* %3
%13 = shl i32 1, %12
%14 = ashr i32 %11, %13
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [5 x i32], [5 x i32]* %4, i64 0, i64 %16
%18 = load i32, i32* %17
%19 = and i32 %14, %18
%20 = load i32, i32* %2
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [5 x i32], [5 x i32]* %4, i64 0, i64 %22
%24 = load i32, i32* %23
%25 = and i32 %20, %24
%26 = load i32, i32* %3
%27 = shl i32 1, %26
%28 = shl i32 %25, %27
%29 = or i32 %19, %28
store i32 %29, i32* %2
br label %30
30: ; preds = %10
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 1
store i32 %32, i32* %3
br label %6
33: ; preds = %6
%34 = load i32, i32* %2
ret i32 %34
}

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