#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*/ printf(char *, int);
unsigned long long reverse_bits(unsigned long long val, int len) {
  if ((1 > len) || (len > 64)) {
    printf("ERROR: Cannot reverse %d bits. Returning zero\n", len);
    return 0;
  }
  val = (((val & 0xaaaaaaaaaaaaaaaaULL) >> 1) |
         ((val & 0x5555555555555555ULL) << 1));
  val = (((val & 0xccccccccccccccccULL) >> 2) |
         ((val & 0x3333333333333333ULL) << 2));
  val = (((val & 0xf0f0f0f0f0f0f0f0ULL) >> 4) |
         ((val & 0x0f0f0f0f0f0f0f0fULL) << 4));
  val = (((val & 0xff00ff00ff00ff00ULL) >> 8) |
         ((val & 0x00ff00ff00ff00ffULL) << 8));
  val = (((val & 0xffff0000ffff0000ULL) >> 16) |
         ((val & 0x0000ffff0000ffffULL) << 16));
  return ((val >> 32) | (val << 32)) >> (64 - len);
}


x86 assembly (clang, -O0):
<reverse_bits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x1,%eax
cmp    -0x14(%rbp),%eax
jg     1137 <reverse_bits+0x27>
cmpl   $0x40,-0x14(%rbp)
jle    1153 <reverse_bits+0x43>
mov    -0x14(%rbp),%esi
lea    0xebf(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    124e <reverse_bits+0x13e>
movabs $0xaaaaaaaaaaaaaaaa,%rax
aa aa aa
and    -0x10(%rbp),%rax
shr    $0x1,%rax
movabs $0x5555555555555555,%rcx
55 55 55
and    -0x10(%rbp),%rcx
shl    $0x1,%rcx
or     %rcx,%rax
mov    %rax,-0x10(%rbp)
movabs $0xcccccccccccccccc,%rax
cc cc cc
and    -0x10(%rbp),%rax
shr    $0x2,%rax
movabs $0x3333333333333333,%rcx
33 33 33
and    -0x10(%rbp),%rcx
shl    $0x2,%rcx
or     %rcx,%rax
mov    %rax,-0x10(%rbp)
movabs $0xf0f0f0f0f0f0f0f0,%rax
f0 f0 f0
and    -0x10(%rbp),%rax
shr    $0x4,%rax
movabs $0xf0f0f0f0f0f0f0f,%rcx
0f 0f 0f
and    -0x10(%rbp),%rcx
shl    $0x4,%rcx
or     %rcx,%rax
mov    %rax,-0x10(%rbp)
movabs $0xff00ff00ff00ff00,%rax
and    -0x10(%rbp),%rax
shr    $0x8,%rax
movabs $0xff00ff00ff00ff,%rcx
and    -0x10(%rbp),%rcx
shl    $0x8,%rcx
or     %rcx,%rax
mov    %rax,-0x10(%rbp)
movabs $0xffff0000ffff0000,%rax
and    -0x10(%rbp),%rax
shr    $0x10,%rax
movabs $0xffff0000ffff,%rcx
and    -0x10(%rbp),%rcx
shl    $0x10,%rcx
or     %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
shr    $0x20,%rax
mov    -0x10(%rbp),%rcx
shl    $0x20,%rcx
or     %rcx,%rax
mov    $0x40,%ecx
sub    -0x14(%rbp),%ecx
mov    %ecx,%ecx
shr    %cl,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_bits>:
lea    -0x41(%rsi),%eax
cmp    $0xffffffbf,%eax
ja     112c <reverse_bits+0x1c>
push   %rax
lea    0xee0(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
ret
bswap  %rdi
mov    %rdi,%rax
shr    $0x4,%rax
movabs $0xf0f0f0f0f0f0f0f,%rcx
0f 0f 0f
and    %rcx,%rax
and    %rcx,%rdi
shl    $0x4,%rdi
or     %rax,%rdi
movabs $0x3333333333333333,%rax
33 33 33
mov    %rdi,%rcx
and    %rax,%rcx
shr    $0x2,%rdi
and    %rax,%rdi
lea    (%rdi,%rcx,4),%rax
movabs $0x5555555555555555,%rcx
55 55 55
mov    %rax,%rdx
and    %rcx,%rdx
shr    %rax
and    %rcx,%rax
lea    (%rax,%rdx,2),%rax
neg    %sil
mov    %esi,%ecx
shr    %cl,%rax
ret

LLVM IR (-O0):
@.str = [47 x i8] c"ERROR: Cannot reverse %d bits. Returning zero\0A\00"
define i64 @reverse_bits(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
store i64 %0, i64* %4
store i32 %1, i32* %5
%6 = load i32, i32* %5
%7 = icmp sgt i32 1, %6
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* %5
%10 = icmp sgt i32 %9, 64
br i1 %10, label %11, label %14
11: ; preds = %8, %2
%12 = load i32, i32* %5
%13 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %12)
store i64 0, i64* %3
br label %59
14: ; preds = %8
%15 = load i64, i64* %4
%16 = and i64 %15, -6148914691236517206
%17 = lshr i64 %16, 1
%18 = load i64, i64* %4
%19 = and i64 %18, 6148914691236517205
%20 = shl i64 %19, 1
%21 = or i64 %17, %20
store i64 %21, i64* %4
%22 = load i64, i64* %4
%23 = and i64 %22, -3689348814741910324
%24 = lshr i64 %23, 2
%25 = load i64, i64* %4
%26 = and i64 %25, 3689348814741910323
%27 = shl i64 %26, 2
%28 = or i64 %24, %27
store i64 %28, i64* %4
%29 = load i64, i64* %4
%30 = and i64 %29, -1085102592571150096
%31 = lshr i64 %30, 4
%32 = load i64, i64* %4
%33 = and i64 %32, 1085102592571150095
%34 = shl i64 %33, 4
%35 = or i64 %31, %34
store i64 %35, i64* %4
%36 = load i64, i64* %4
%37 = and i64 %36, -71777214294589696
%38 = lshr i64 %37, 8
%39 = load i64, i64* %4
%40 = and i64 %39, 71777214294589695
%41 = shl i64 %40, 8
%42 = or i64 %38, %41
store i64 %42, i64* %4
%43 = load i64, i64* %4
%44 = and i64 %43, -281470681808896
%45 = lshr i64 %44, 16
%46 = load i64, i64* %4
%47 = and i64 %46, 281470681808895
%48 = shl i64 %47, 16
%49 = or i64 %45, %48
store i64 %49, i64* %4
%50 = load i64, i64* %4
%51 = lshr i64 %50, 32
%52 = load i64, i64* %4
%53 = shl i64 %52, 32
%54 = or i64 %51, %53
%55 = load i32, i32* %5
%56 = sub nsw i32 64, %55
%57 = zext i32 %56 to i64
%58 = lshr i64 %54, %57
store i64 %58, i64* %3
br label %59
59: ; preds = %14, %11
%60 = load i64, i64* %3
ret i64 %60
}

LLVM IR (-O2):
@.str = [47 x i8] c"ERROR: Cannot reverse %d bits. Returning zero\0A\00"
define i64 @reverse_bits(i64 %0, i32 %1) {
%3 = add i32 %1, -65
%4 = icmp ult i32 %3, -64
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %12
7: ; preds = %2
%8 = tail call i64 @llvm.bitreverse.i64(i64 %0)
%9 = sub nuw nsw i32 64, %1
%10 = zext i32 %9 to i64
%11 = lshr i64 %8, %10
br label %12
12: ; preds = %7, %5
%13 = phi i64 [ 0, %5 ], [ %11, %7 ]
ret i64 %13
}