int calc_pcm_bits(int num_val, int num_levels) {
  int num_complete_chunks = 0, rest_chunk_size = 0;
  int max_grp_len = 0, bits_pcm = 0;
  float ld_nlev = 0.f;
  switch (num_levels) {
  case 3:
    max_grp_len = 5;
    break;
  case 7:
    max_grp_len = 6;
    break;
  case 11:
    max_grp_len = 2;
    break;
  case 13:
    max_grp_len = 4;
    break;
  case 19:
    max_grp_len = 4;
    break;
  case 25:
    max_grp_len = 3;
    break;
  case 51:
    max_grp_len = 4;
    break;
  case 4:
  case 8:
  case 15:
  case 26:
  case 31:
    max_grp_len = 1;
    break;
  default:
    assert(0);
  }
  ld_nlev = (float)(log((float)num_levels) / log(2.f));
  num_complete_chunks = num_val / max_grp_len;
  rest_chunk_size = num_val % max_grp_len;
  bits_pcm = ((int)ceil((float)(max_grp_len)*ld_nlev)) * num_complete_chunks;
  bits_pcm += (int)ceil((float)(rest_chunk_size)*ld_nlev);
  return bits_pcm;
}


x86 assembly (clang, -O0):
<calc_pcm_bits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
add    $0xfffffffd,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x30,%eax
ja     11ea <calc_pcm_bits+0xba>
mov    -0x28(%rbp),%rax
lea    0xe87(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x5,-0x14(%rbp)
jmp    11f3 <calc_pcm_bits+0xc3>
movl   $0x6,-0x14(%rbp)
jmp    11f3 <calc_pcm_bits+0xc3>
movl   $0x2,-0x14(%rbp)
jmp    11f3 <calc_pcm_bits+0xc3>
movl   $0x4,-0x14(%rbp)
jmp    11f3 <calc_pcm_bits+0xc3>
movl   $0x4,-0x14(%rbp)
jmp    11f3 <calc_pcm_bits+0xc3>
movl   $0x3,-0x14(%rbp)
jmp    11f3 <calc_pcm_bits+0xc3>
movl   $0x4,-0x14(%rbp)
jmp    11f3 <calc_pcm_bits+0xc3>
movl   $0x1,-0x14(%rbp)
jmp    11f3 <calc_pcm_bits+0xc3>
xor    %edi,%edi
mov    $0x0,%al
call   1040 <assert@plt>
cvtsi2ssl -0x8(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
call   1050 <log@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  0xdf2(%rip),%xmm0
call   1050 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %edx,-0x10(%rbp)
cvtsi2ssl -0x14(%rbp),%xmm0
mulss  -0x1c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
call   1030 <ceil@plt>
cvttsd2si %xmm0,%eax
imul   -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
cvtsi2ssl -0x10(%rbp),%xmm0
mulss  -0x1c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
call   1030 <ceil@plt>
cvttsd2si %xmm0,%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_pcm_bits>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
lea    -0x3(%rbx),%eax
cmp    $0x30,%eax
ja     1169 <calc_pcm_bits+0x39>
mov    $0x5,%r14d
lea    0xeb8(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0x1,%r14d
jmp    118d <calc_pcm_bits+0x5d>
mov    $0x4,%r14d
jmp    118d <calc_pcm_bits+0x5d>
xor    %r14d,%r14d
xor    %edi,%edi
xor    %eax,%eax
call   1030 <assert@plt>
jmp    118d <calc_pcm_bits+0x5d>
mov    $0x6,%r14d
jmp    118d <calc_pcm_bits+0x5d>
mov    $0x2,%r14d
jmp    118d <calc_pcm_bits+0x5d>
mov    $0x3,%r14d
cvtsi2ss %ebx,%xmm0
cvtss2sd %xmm0,%xmm0
call   1050 <log@plt>
mov    %ebp,%eax
cltd
idiv   %r14d
mov    %eax,%ebx
mov    %edx,%r15d
divsd  0xe53(%rip),%xmm0
cvtsd2ss %xmm0,%xmm1
movss  %xmm1,0x4(%rsp)
xorps  %xmm0,%xmm0
cvtsi2ss %r14d,%xmm0
mulss  %xmm1,%xmm0
call   1040 <ceilf@plt>
cvttss2si %xmm0,%ebp
imul   %ebx,%ebp
xorps  %xmm0,%xmm0
cvtsi2ss %r15d,%xmm0
mulss  0x4(%rsp),%xmm0
call   1040 <ceilf@plt>
cvttss2si %xmm0,%eax
add    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @calc_pcm_bits(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca float
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store float 0.000000e+00, float* %9
%10 = load i32, i32* %4
switch i32 %10, label %19 [
i32 3, label %11
i32 7, label %12
i32 11, label %13
i32 13, label %14
i32 19, label %15
i32 25, label %16
i32 51, label %17
i32 4, label %18
i32 8, label %18
i32 15, label %18
i32 26, label %18
i32 31, label %18
]
11: ; preds = %2
store i32 5, i32* %7
br label %21
12: ; preds = %2
store i32 6, i32* %7
br label %21
13: ; preds = %2
store i32 2, i32* %7
br label %21
14: ; preds = %2
store i32 4, i32* %7
br label %21
15: ; preds = %2
store i32 4, i32* %7
br label %21
16: ; preds = %2
store i32 3, i32* %7
br label %21
17: ; preds = %2
store i32 4, i32* %7
br label %21
18: ; preds = %2, %2, %2, %2, %2
store i32 1, i32* %7
br label %21
19: ; preds = %2
%20 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 0)
br label %21
21: ; preds = %19, %18, %17, %16, %15, %14, %13, %12, %11
%22 = load i32, i32* %4
%23 = sitofp i32 %22 to float
%24 = fpext float %23 to double
%25 = call double @log(double %24)
%26 = call double @log(double 2.000000e+00)
%27 = fdiv double %25, %26
%28 = fptrunc double %27 to float
store float %28, float* %9
%29 = load i32, i32* %3
%30 = load i32, i32* %7
%31 = sdiv i32 %29, %30
store i32 %31, i32* %5
%32 = load i32, i32* %3
%33 = load i32, i32* %7
%34 = srem i32 %32, %33
store i32 %34, i32* %6
%35 = load i32, i32* %7
%36 = sitofp i32 %35 to float
%37 = load float, float* %9
%38 = fmul float %36, %37
%39 = fpext float %38 to double
%40 = call double @llvm.ceil.f64(double %39)
%41 = fptosi double %40 to i32
%42 = load i32, i32* %5
%43 = mul nsw i32 %41, %42
store i32 %43, i32* %8
%44 = load i32, i32* %6
%45 = sitofp i32 %44 to float
%46 = load float, float* %9
%47 = fmul float %45, %46
%48 = fpext float %47 to double
%49 = call double @llvm.ceil.f64(double %48)
%50 = fptosi double %49 to i32
%51 = load i32, i32* %8
%52 = add nsw i32 %51, %50
store i32 %52, i32* %8
%53 = load i32, i32* %8
ret i32 %53
}

LLVM IR (-O2):
define i32 @calc_pcm_bits(i32 %0, i32 %1) {
switch i32 %1, label %10 [
i32 3, label %12
i32 7, label %3
i32 11, label %4
i32 13, label %5
i32 19, label %6
i32 25, label %7
i32 51, label %8
i32 4, label %9
i32 8, label %9
i32 15, label %9
i32 26, label %9
i32 31, label %9
]
3: ; preds = %2
br label %12
4: ; preds = %2
br label %12
5: ; preds = %2
br label %12
6: ; preds = %2
br label %12
7: ; preds = %2
br label %12
8: ; preds = %2
br label %12
9: ; preds = %2, %2, %2, %2, %2
br label %12
10: ; preds = %2
%11 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 0)
br label %12
12: ; preds = %2, %10, %9, %8, %7, %6, %5, %4, %3
%13 = phi i32 [ 0, %10 ], [ 1, %9 ], [ 4, %8 ], [ 3, %7 ], [ 4, %6 ], [ 4, %5 ], [ 2, %4 ], [ 6, %3 ], [ 5, %2 ]
%14 = sitofp i32 %1 to float
%15 = fpext float %14 to double
%16 = tail call double @log(double %15)
%17 = fdiv double %16, 0x3FE62E42FEFA39EF
%18 = fptrunc double %17 to float
%19 = sdiv i32 %0, %13
%20 = srem i32 %0, %13
%21 = sitofp i32 %13 to float
%22 = fmul float %21, %18
%23 = tail call float @llvm.ceil.f32(float %22)
%24 = fptosi float %23 to i32
%25 = mul nsw i32 %19, %24
%26 = sitofp i32 %20 to float
%27 = fmul float %18, %26
%28 = tail call float @llvm.ceil.f32(float %27)
%29 = fptosi float %28 to i32
%30 = add nsw i32 %25, %29
ret i32 %30
}