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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int nlz16(int);
unsigned int calc_index(uint32_t alloc_size) {
  if ((alloc_size >> (9 + 3 + 4)) != 0) {
    return ((9 + 1) * (1 << 3)) - 1;
  }
  unsigned int fli = 16 - nlz16(alloc_size >> (3 + 4));
  unsigned int shift = (fli == 0) ? 4 : (4 - 1 + fli);
  unsigned int sli = (alloc_size >> shift) & ((1 << 3) - 1);
  unsigned int index = (fli << 3) + sli;
  assert(fli <= 9);
  assert(sli <= (1 << 3) - 1);
  return index;
}


x86 assembly (clang, -O0):
<calc_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x10,%eax
cmp    $0x0,%eax
je     1146 <calc_index+0x26>
movl   $0x4f,-0x4(%rbp)
jmp    11c5 <calc_index+0xa5>
mov    -0x8(%rbp),%edi
sar    $0x7,%edi
call   1040 <nlz16@plt>
mov    %eax,%ecx
mov    $0x10,%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1174 <calc_index+0x54>
mov    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    117d <calc_index+0x5d>
mov    -0xc(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x10(%rbp),%ecx
sar    %cl,%eax
and    $0x7,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x3,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x9,-0xc(%rbp)
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0x7,-0x14(%rbp)
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_index>:
mov    $0x4f,%eax
cmp    $0xffff,%edi
ja     1173 <calc_index+0x53>
push   %rbx
mov    %edi,%ebx
shr    $0x7,%edi
call   1040 <nlz16@plt>
mov    $0x13,%cl
sub    %al,%cl
sub    $0x10,%eax
movzbl %cl,%edx
mov    $0x4,%ecx
cmovne %edx,%ecx
shr    %cl,%ebx
mov    %eax,%ecx
neg    %ecx
and    $0x7,%ebx
shl    $0x3,%eax
sub    %eax,%ebx
xor    %edi,%edi
cmp    $0xa,%ecx
setb   %dil
call   1030 <assert@plt>
mov    $0x1,%edi
call   1030 <assert@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @calc_index(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 = ashr i32 %8, 16
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 79, i32* %2
br label %42
12: ; preds = %1
%13 = load i32, i32* %3
%14 = ashr i32 %13, 7
%15 = call i32 @nlz16(i32 %14)
%16 = sub nsw i32 16, %15
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %12
br label %23
20: ; preds = %12
%21 = load i32, i32* %4
%22 = add i32 3, %21
br label %23
23: ; preds = %20, %19
%24 = phi i32 [ 4, %19 ], [ %22, %20 ]
store i32 %24, i32* %5
%25 = load i32, i32* %3
%26 = load i32, i32* %5
%27 = ashr i32 %25, %26
%28 = and i32 %27, 7
store i32 %28, i32* %6
%29 = load i32, i32* %4
%30 = shl i32 %29, 3
%31 = load i32, i32* %6
%32 = add i32 %30, %31
store i32 %32, i32* %7
%33 = load i32, i32* %4
%34 = icmp ule i32 %33, 9
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = load i32, i32* %6
%38 = icmp ule i32 %37, 7
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i32, i32* %7
store i32 %41, i32* %2
br label %42
42: ; preds = %23, %11
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
define i32 @calc_index(i32 %0) {
%2 = icmp ult i32 %0, 65536
br i1 %2, label %3, label %18
3: ; preds = %1
%4 = lshr i32 %0, 7
%5 = tail call i32 @nlz16(i32 %4)
%6 = sub nsw i32 16, %5
%7 = icmp eq i32 %5, 16
%8 = sub i32 19, %5
%9 = select i1 %7, i32 4, i32 %8
%10 = lshr i32 %0, %9
%11 = and i32 %10, 7
%12 = shl i32 %6, 3
%13 = or i32 %11, %12
%14 = icmp ult i32 %6, 10
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = tail call i32 @assert(i32 1)
br label %18
18: ; preds = %1, %3
%19 = phi i32 [ %13, %3 ], [ 79, %1 ]
ret i32 %19
}