#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 */
unsigned long long BitMatMultOr(unsigned long long, unsigned long long);
unsigned long long bitRotLeft(unsigned long long, unsigned long long);
int log(int);
double *malloc(int);
int /*<<< orphan*/ setupBitIndices();
double *bit_reverse(int n, double *w) {
  unsigned long i;
  unsigned long long shift;
  double *v = malloc(sizeof(double) * (2 * n));
  unsigned long long mask = 0x0102040810204080LL;
  shift = (unsigned long long)(log(n) / log(2));
  setupBitIndices();
  for (i = 0; i < n; i++) {
    unsigned long long ndx = BitMatMultOr(mask, BitMatMultOr(i, mask));
    ndx = bitRotLeft(ndx, shift);
    v[2 * ndx] = w[2 * i];
    v[2 * ndx + 1] = w[2 * i + 1];
  }
  return (v);
}


x86 assembly (clang, -O0):
<bit_reverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
movabs $0x102040810204080,%rax
04 02 01
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
call   1070 <log@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x2,%edi
call   1070 <log@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
cltd
idiv   %ecx
cltq
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <setupBitIndices@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x4(%rbp),%rcx
cmp    %rcx,%rax
jae    1254 <bit_reverse+0x104>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <BitMatMultOr@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
call   1040 <BitMatMultOr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <bitRotLeft@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x1,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
shl    $0x1,%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x1,%rcx
movsd  0x8(%rax,%rcx,8),%xmm0
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
shl    $0x1,%rcx
movsd  %xmm0,0x8(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11b6 <bit_reverse+0x66>
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bit_reverse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %edi,%ebx
shl    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %ebx,%edi
call   1070 <log@plt>
mov    %eax,%ebp
mov    $0x2,%edi
call   1070 <log@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <setupBitIndices@plt>
test   %ebx,%ebx
je     11ec <bit_reverse+0x9c>
movslq %ebp,%r15
movabs $0x102040810204080,%r12
04 02 01
movslq %ebx,%rbp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <BitMatMultOr@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1040 <BitMatMultOr@plt>
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <bitRotLeft@plt>
shl    $0x4,%rax
movups 0x0(%r13),%xmm0
movups %xmm0,(%r14,%rax,1)
add    $0x1,%rbx
add    $0x10,%r13
cmp    %rbx,%rbp
jne    11b0 <bit_reverse+0x60>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double* @bit_reverse(i32 %0, double* %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca i64
%6 = alloca i64
%7 = alloca double*
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %3
store double* %1, double** %4
%10 = load i32, i32* %3
%11 = mul nsw i32 2, %10
%12 = sext i32 %11 to i64
%13 = mul i64 8, %12
%14 = trunc i64 %13 to i32
%15 = call double* @malloc(i32 %14)
store double* %15, double** %7
store i64 72624976668147840, i64* %8
%16 = load i32, i32* %3
%17 = call i32 @log(i32 %16)
%18 = call i32 @log(i32 2)
%19 = sdiv i32 %17, %18
%20 = sext i32 %19 to i64
store i64 %20, i64* %6
%21 = call i32 (...) @setupBitIndices()
store i64 0, i64* %5
br label %22
22: ; preds = %56, %2
%23 = load i64, i64* %5
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = icmp ult i64 %23, %25
br i1 %26, label %27, label %59
27: ; preds = %22
%28 = load i64, i64* %8
%29 = load i64, i64* %5
%30 = load i64, i64* %8
%31 = call i64 @BitMatMultOr(i64 %29, i64 %30)
%32 = call i64 @BitMatMultOr(i64 %28, i64 %31)
store i64 %32, i64* %9
%33 = load i64, i64* %9
%34 = load i64, i64* %6
%35 = call i64 @bitRotLeft(i64 %33, i64 %34)
store i64 %35, i64* %9
%36 = load double*, double** %4
%37 = load i64, i64* %5
%38 = mul i64 2, %37
%39 = getelementptr inbounds double, double* %36, i64 %38
%40 = load double, double* %39
%41 = load double*, double** %7
%42 = load i64, i64* %9
%43 = mul i64 2, %42
%44 = getelementptr inbounds double, double* %41, i64 %43
store double %40, double* %44
%45 = load double*, double** %4
%46 = load i64, i64* %5
%47 = mul i64 2, %46
%48 = add i64 %47, 1
%49 = getelementptr inbounds double, double* %45, i64 %48
%50 = load double, double* %49
%51 = load double*, double** %7
%52 = load i64, i64* %9
%53 = mul i64 2, %52
%54 = add i64 %53, 1
%55 = getelementptr inbounds double, double* %51, i64 %54
store double %50, double* %55
br label %56
56: ; preds = %27
%57 = load i64, i64* %5
%58 = add i64 %57, 1
store i64 %58, i64* %5
br label %22
59: ; preds = %22
%60 = load double*, double** %7
ret double* %60
}

LLVM IR (-O2):
define noalias double* @bit_reverse(i32 %0, double* nocapture %1) {
%3 = shl i32 %0, 4
%4 = tail call double* @malloc(i32 %3)
%5 = tail call i32 @log(i32 %0)
%6 = tail call i32 @log(i32 2)
%7 = sdiv i32 %5, %6
%8 = sext i32 %7 to i64
%9 = tail call i32 (...) @setupBitIndices()
%10 = icmp eq i32 %0, 0
br i1 %10, label %27, label %11
11: ; preds = %2
%12 = sext i32 %0 to i64
br label %13
13: ; preds = %11, %13
%14 = phi i64 [ %25, %13 ], [ 0, %11 ]
%15 = tail call i64 @BitMatMultOr(i64 %14, i64 72624976668147840)
%16 = tail call i64 @BitMatMultOr(i64 72624976668147840, i64 %15)
%17 = tail call i64 @bitRotLeft(i64 %16, i64 %8)
%18 = shl i64 %14, 1
%19 = getelementptr inbounds double, double* %1, i64 %18
%20 = shl i64 %17, 1
%21 = getelementptr inbounds double, double* %4, i64 %20
%22 = bitcast double* %19 to <2 x double>*
%23 = load <2 x double>, <2 x double>* %22
%24 = bitcast double* %21 to <2 x double>*
store <2 x double> %23, <2 x double>* %24
%25 = add nuw i64 %14, 1
%26 = icmp eq i64 %25, %12
br i1 %26, label %27, label %13
27: ; preds = %13, %2
ret double* %4
}