#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 int *aligned_calloc(unsigned int, int, int);
int ilog2(int);
unsigned int revbin(unsigned int);
unsigned int *calculate_permutation_vector(int fft_length) {
  unsigned int *perm =
      aligned_calloc((16U), fft_length / 2, sizeof(unsigned int));
  if (perm == NULL) {
    return NULL;
  }
  unsigned int *p = perm;
  unsigned int rshift = 31 - ilog2(fft_length);
  for (unsigned int i = 0; i < fft_length; i += 2) {
    *p++ = revbin(i) >> rshift;
  }
  return perm;
}


x86 assembly (clang, -O0):
<calculate_permutation_vector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
mov    $0x10,%edi
mov    $0x4,%edx
call   1050 <aligned_calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1173 <calculate_permutation_vector+0x43>
movq   $0x0,-0x8(%rbp)
jmp    11d8 <calculate_permutation_vector+0xa8>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <ilog2@plt>
mov    %eax,%ecx
mov    $0x1f,%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    11d0 <calculate_permutation_vector+0xa0>
mov    -0x28(%rbp),%edi
call   1040 <revbin@plt>
mov    -0x24(%rbp),%ecx
shr    %cl,%eax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x20(%rbp)
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x28(%rbp)
jmp    1196 <calculate_permutation_vector+0x66>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_permutation_vector>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    %edi,%esi
shr    $0x1f,%esi
add    %edi,%esi
sar    %esi
mov    $0x10,%edi
mov    $0x4,%edx
call   1050 <aligned_calloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1188 <calculate_permutation_vector+0x58>
mov    %r14d,%edi
call   1030 <ilog2@plt>
test   %r14d,%r14d
je     1188 <calculate_permutation_vector+0x58>
mov    $0x1f,%ebp
sub    %eax,%ebp
xor    %ebx,%ebx
mov    %ebx,%edi
call   1040 <revbin@plt>
mov    %ebp,%ecx
shr    %cl,%eax
mov    %eax,(%r15,%rbx,2)
add    $0x2,%rbx
cmp    %r14d,%ebx
jb     1170 <calculate_permutation_vector+0x40>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @calculate_permutation_vector(i32 %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = sdiv i32 %8, 2
%10 = call i32* @aligned_calloc(i32 16, i32 %9, i32 4)
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %1
store i32* null, i32** %2
br label %35
14: ; preds = %1
%15 = load i32*, i32** %4
store i32* %15, i32** %5
%16 = load i32, i32* %3
%17 = call i32 @ilog2(i32 %16)
%18 = sub nsw i32 31, %17
store i32 %18, i32* %6
store i32 0, i32* %7
br label %19
19: ; preds = %30, %14
%20 = load i32, i32* %7
%21 = load i32, i32* %3
%22 = icmp ult i32 %20, %21
br i1 %22, label %23, label %33
23: ; preds = %19
%24 = load i32, i32* %7
%25 = call i32 @revbin(i32 %24)
%26 = load i32, i32* %6
%27 = lshr i32 %25, %26
%28 = load i32*, i32** %5
%29 = getelementptr inbounds i32, i32* %28, i32 1
store i32* %29, i32** %5
store i32 %27, i32* %28
br label %30
30: ; preds = %23
%31 = load i32, i32* %7
%32 = add i32 %31, 2
store i32 %32, i32* %7
br label %19
33: ; preds = %19
%34 = load i32*, i32** %4
store i32* %34, i32** %2
br label %35
35: ; preds = %33, %13
%36 = load i32*, i32** %2
ret i32* %36
}

LLVM IR (-O2):
define i32* @calculate_permutation_vector(i32 %0) {
%2 = sdiv i32 %0, 2
%3 = tail call i32* @aligned_calloc(i32 16, i32 %2, i32 4)
%4 = icmp eq i32* %3, null
br i1 %4, label %17, label %5
5: ; preds = %1
%6 = tail call i32 @ilog2(i32 %0)
%7 = sub nsw i32 31, %6
%8 = icmp eq i32 %0, 0
br i1 %8, label %17, label %9
9: ; preds = %5, %9
%10 = phi i32 [ %15, %9 ], [ 0, %5 ]
%11 = phi i32* [ %14, %9 ], [ %3, %5 ]
%12 = tail call i32 @revbin(i32 %10)
%13 = lshr i32 %12, %7
%14 = getelementptr inbounds i32, i32* %11, i64 1
store i32 %13, i32* %11
%15 = add i32 %10, 2
%16 = icmp ult i32 %15, %0
br i1 %16, label %9, label %17
17: ; preds = %9, %5, %1
ret i32* %3
}