#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 */

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
void ReverseBits(unsigned int *bits, int numbits) {
  if (sizeof(int) == 4) {
    *bits = (((*bits) & 0x0000ffff) << 16) + (((*bits) & 0xffff0000) >> 16);
    *bits = (((*bits) & 0x00ff00ff) << 8) + (((*bits) & 0xff00ff00) >> 8);
    *bits = (((*bits) & 0x0f0f0f0f) << 4) + (((*bits) & 0xf0f0f0f0) >> 4);
    *bits = (((*bits) & 0x33333333) << 2) + (((*bits) & 0xCCCCCCCC) >> 2);
    *bits = (((*bits) & 0x55555555) << 1) + (((*bits) & 0xAAAAAAAA) >> 1);
    *bits >>= (32 - numbits);
  } else {
    fprintf(stderr, "error: int has not 32 bits\n");
  }
}


x86 assembly (clang, -O0):
<ReverseBits>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
and    $0xffff,%ecx
shl    $0x10,%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
and    $0xffff0000,%eax
shr    $0x10,%eax
add    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
and    $0xff00ff,%ecx
shl    $0x8,%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
and    $0xff00ff00,%eax
shr    $0x8,%eax
add    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
and    $0xf0f0f0f,%ecx
shl    $0x4,%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
and    $0xf0f0f0f0,%eax
shr    $0x4,%eax
add    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
and    $0x33333333,%ecx
shl    $0x2,%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
and    $0xcccccccc,%eax
shr    $0x2,%eax
add    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
and    $0x55555555,%ecx
shl    $0x1,%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
and    $0xaaaaaaaa,%eax
shr    $0x1,%eax
add    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    $0x20,%ecx
sub    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
shr    %cl,%edx
mov    %edx,%ecx
mov    %ecx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReverseBits>:
mov    %esi,%ecx
mov    (%rdi),%eax
bswap  %eax
mov    %eax,%edx
and    $0xf0f0f0f,%edx
shl    $0x4,%edx
shr    $0x4,%eax
and    $0xf0f0f0f,%eax
or     %edx,%eax
mov    %eax,%edx
and    $0x33333333,%edx
shr    $0x2,%eax
and    $0x33333333,%eax
lea    (%rax,%rdx,4),%eax
mov    %eax,%edx
and    $0x55555555,%edx
shr    %eax
and    $0x55555555,%eax
lea    (%rax,%rdx,2),%eax
neg    %cl
shr    %cl,%eax
mov    %eax,(%rdi)
ret

LLVM IR (-O0):
@stderr = global i32 0
define void @ReverseBits(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%5 = load i32*, i32** %3
%6 = load i32, i32* %5
%7 = and i32 %6, 65535
%8 = shl i32 %7, 16
%9 = load i32*, i32** %3
%10 = load i32, i32* %9
%11 = and i32 %10, -65536
%12 = lshr i32 %11, 16
%13 = add i32 %8, %12
%14 = load i32*, i32** %3
store i32 %13, i32* %14
%15 = load i32*, i32** %3
%16 = load i32, i32* %15
%17 = and i32 %16, 16711935
%18 = shl i32 %17, 8
%19 = load i32*, i32** %3
%20 = load i32, i32* %19
%21 = and i32 %20, -16711936
%22 = lshr i32 %21, 8
%23 = add i32 %18, %22
%24 = load i32*, i32** %3
store i32 %23, i32* %24
%25 = load i32*, i32** %3
%26 = load i32, i32* %25
%27 = and i32 %26, 252645135
%28 = shl i32 %27, 4
%29 = load i32*, i32** %3
%30 = load i32, i32* %29
%31 = and i32 %30, -252645136
%32 = lshr i32 %31, 4
%33 = add i32 %28, %32
%34 = load i32*, i32** %3
store i32 %33, i32* %34
%35 = load i32*, i32** %3
%36 = load i32, i32* %35
%37 = and i32 %36, 858993459
%38 = shl i32 %37, 2
%39 = load i32*, i32** %3
%40 = load i32, i32* %39
%41 = and i32 %40, -858993460
%42 = lshr i32 %41, 2
%43 = add i32 %38, %42
%44 = load i32*, i32** %3
store i32 %43, i32* %44
%45 = load i32*, i32** %3
%46 = load i32, i32* %45
%47 = and i32 %46, 1431655765
%48 = shl i32 %47, 1
%49 = load i32*, i32** %3
%50 = load i32, i32* %49
%51 = and i32 %50, -1431655766
%52 = lshr i32 %51, 1
%53 = add i32 %48, %52
%54 = load i32*, i32** %3
store i32 %53, i32* %54
%55 = load i32, i32* %4
%56 = sub nsw i32 32, %55
%57 = load i32*, i32** %3
%58 = load i32, i32* %57
%59 = lshr i32 %58, %56
store i32 %59, i32* %57
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
define void @ReverseBits(i32* nocapture %0, i32 %1) {
%3 = load i32, i32* %0
%4 = tail call i32 @llvm.bitreverse.i32(i32 %3)
%5 = sub nsw i32 32, %1
%6 = lshr i32 %4, %5
store i32 %6, i32* %0
ret void
}