#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 uint16_t;

/* Variables and functions */
void rev16(uint16_t *x) {
  *x = (*x & 0x5555) << 1 | (*x & 0xAAAA) >> 1;
  *x = (*x & 0x3333) << 2 | (*x & 0xCCCC) >> 2;
  *x = (*x & 0x0F0F) << 4 | (*x & 0xF0F0) >> 4;
  *x = (*x & 0x00FF) << 8 | (*x & 0xFF00) >> 8;
}


x86 assembly (clang, -O0):
<rev16>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
and    $0x5555,%ecx
shl    $0x1,%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
and    $0xaaaa,%eax
sar    $0x1,%eax
or     %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
and    $0x3333,%ecx
shl    $0x2,%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
and    $0xcccc,%eax
sar    $0x2,%eax
or     %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
and    $0xf0f,%ecx
shl    $0x4,%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
and    $0xf0f0,%eax
sar    $0x4,%eax
or     %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
and    $0xff,%ecx
shl    $0x8,%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
and    $0xff00,%eax
sar    $0x8,%eax
or     %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<rev16>:
movzwl (%rdi),%eax
rol    $0x8,%ax
mov    %eax,%ecx
and    $0xf0f,%ecx
shl    $0x4,%ecx
shr    $0x4,%eax
and    $0xf0f,%eax
or     %ecx,%eax
mov    %eax,%ecx
and    $0x3333,%ecx
shr    $0x2,%eax
and    $0x3333,%eax
lea    (%rax,%rcx,4),%eax
mov    %eax,%ecx
and    $0x5555,%ecx
shr    %eax
and    $0x5555,%eax
lea    (%rax,%rcx,2),%eax
mov    %eax,(%rdi)
ret

LLVM IR (-O0):
define void @rev16(i32* %0) {
%2 = alloca i32*
store i32* %0, i32** %2
%3 = load i32*, i32** %2
%4 = load i32, i32* %3
%5 = and i32 %4, 21845
%6 = shl i32 %5, 1
%7 = load i32*, i32** %2
%8 = load i32, i32* %7
%9 = and i32 %8, 43690
%10 = ashr i32 %9, 1
%11 = or i32 %6, %10
%12 = load i32*, i32** %2
store i32 %11, i32* %12
%13 = load i32*, i32** %2
%14 = load i32, i32* %13
%15 = and i32 %14, 13107
%16 = shl i32 %15, 2
%17 = load i32*, i32** %2
%18 = load i32, i32* %17
%19 = and i32 %18, 52428
%20 = ashr i32 %19, 2
%21 = or i32 %16, %20
%22 = load i32*, i32** %2
store i32 %21, i32* %22
%23 = load i32*, i32** %2
%24 = load i32, i32* %23
%25 = and i32 %24, 3855
%26 = shl i32 %25, 4
%27 = load i32*, i32** %2
%28 = load i32, i32* %27
%29 = and i32 %28, 61680
%30 = ashr i32 %29, 4
%31 = or i32 %26, %30
%32 = load i32*, i32** %2
store i32 %31, i32* %32
%33 = load i32*, i32** %2
%34 = load i32, i32* %33
%35 = and i32 %34, 255
%36 = shl i32 %35, 8
%37 = load i32*, i32** %2
%38 = load i32, i32* %37
%39 = and i32 %38, 65280
%40 = ashr i32 %39, 8
%41 = or i32 %36, %40
%42 = load i32*, i32** %2
store i32 %41, i32* %42
ret void
}

LLVM IR (-O2):
define void @rev16(i32* nocapture %0) {
%2 = load i32, i32* %0
%3 = trunc i32 %2 to i16
%4 = tail call i16 @llvm.bitreverse.i16(i16 %3)
%5 = zext i16 %4 to i32
store i32 %5, i32* %0
ret void
}