#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*/ CalculateBitLengths(size_t *, int, int, unsigned int *);
scalar_t__ CalculateBlockSymbolSize(unsigned int *, unsigned int *,
                                    unsigned short const *,
                                    unsigned short const *, size_t, size_t);
scalar_t__ CalculateTreeSize(unsigned int *, unsigned int *, size_t *,
                             size_t *);
int /*<<< orphan*/ GetFixedTree(unsigned int *, unsigned int *);
int /*<<< orphan*/ GetLZ77Counts(unsigned short const *, unsigned short const *,
                                 size_t, size_t, size_t *, size_t *);
int /*<<< orphan*/ PatchDistanceCodesForBuggyDecoders(unsigned int *);
int /*<<< orphan*/ assert(int);
double CalculateBlockSize(const unsigned short *litlens,
                          const unsigned short *dists, size_t lstart,
                          size_t lend, int btype) {
  size_t ll_counts[288];
  size_t d_counts[32];
  unsigned ll_lengths[288];
  unsigned d_lengths[32];
  double result = 3;
  assert(btype == 1 || btype == 2);
  if (btype == 1) {
    GetFixedTree(ll_lengths, d_lengths);
  } else {
    GetLZ77Counts(litlens, dists, lstart, lend, ll_counts, d_counts);
    CalculateBitLengths(ll_counts, 288, 15, ll_lengths);
    CalculateBitLengths(d_counts, 32, 15, d_lengths);
    PatchDistanceCodesForBuggyDecoders(d_lengths);
    result += CalculateTreeSize(ll_lengths, d_lengths, ll_counts, d_counts);
  }
  result += CalculateBlockSymbolSize(ll_lengths, d_lengths, litlens, dists,
                                     lstart, lend);
  return result;
}


x86 assembly (clang, -O0):
<CalculateBlockSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movsd  0xe69(%rip),%xmm0
movsd  %xmm0,-0xf38(%rbp)
ff
mov    $0x1,%al
cmpl   $0x1,-0x24(%rbp)
mov    %al,-0xf39(%rbp)
je     11be <CalculateBlockSize+0x4e>
cmpl   $0x2,-0x24(%rbp)
sete   %al
mov    %al,-0xf39(%rbp)
mov    -0xf39(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpl   $0x1,-0x24(%rbp)
jne    11f0 <CalculateBlockSize+0x80>
lea    -0xeb0(%rbp),%rdi
lea    -0xf30(%rbp),%rsi
call   1030 <GetFixedTree@plt>
jmp    128f <CalculateBlockSize+0x11f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x930(%rbp),%r8
lea    -0xa30(%rbp),%r9
call   1060 <GetLZ77Counts@plt>
lea    -0x930(%rbp),%rdi
lea    -0xeb0(%rbp),%rcx
mov    $0x120,%esi
mov    $0xf,%edx
call   1090 <CalculateBitLengths@plt>
lea    -0xa30(%rbp),%rdi
lea    -0xf30(%rbp),%rcx
mov    $0x20,%esi
mov    $0xf,%edx
call   1090 <CalculateBitLengths@plt>
lea    -0xf30(%rbp),%rdi
call   1080 <PatchDistanceCodesForBuggyDecoders@plt>
lea    -0xeb0(%rbp),%rdi
lea    -0xf30(%rbp),%rsi
lea    -0x930(%rbp),%rdx
lea    -0xa30(%rbp),%rcx
call   1040 <CalculateTreeSize@plt>
cvtsi2sd %rax,%xmm0
addsd  -0xf38(%rbp),%xmm0
ff
movsd  %xmm0,-0xf38(%rbp)
ff
lea    -0xeb0(%rbp),%rdi
lea    -0xf30(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
call   1070 <CalculateBlockSymbolSize@plt>
cvtsi2sd %rax,%xmm0
addsd  -0xf38(%rbp),%xmm0
ff
movsd  %xmm0,-0xf38(%rbp)
ff
movsd  -0xf38(%rbp),%xmm0
ff
add    $0xf40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CalculateBlockSize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xf18,%rsp
mov    %r8d,%ebp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
lea    -0x1(%rbp),%eax
xor    %edi,%edi
cmp    $0x2,%eax
setb   %dil
call   1050 <assert@plt>
cmp    $0x1,%ebp
jne    11c5 <CalculateBlockSize+0x55>
lea    0x190(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <GetFixedTree@plt>
movsd  0xe40(%rip),%xmm0
jmp    1255 <CalculateBlockSize+0xe5>
lea    0x610(%rsp),%rbp
lea    0x90(%rsp),%r9
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rbp,%r8
call   1060 <GetLZ77Counts@plt>
lea    0x190(%rsp),%rcx
mov    %rbp,%rdi
mov    $0x120,%esi
mov    $0xf,%edx
mov    %rcx,%rbp
call   1090 <CalculateBitLengths@plt>
lea    0x10(%rsp),%r13
lea    0x90(%rsp),%rdi
mov    $0x20,%esi
mov    $0xf,%edx
mov    %r13,%rcx
call   1090 <CalculateBitLengths@plt>
mov    %r13,%rdi
call   1080 <PatchDistanceCodesForBuggyDecoders@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
lea    0x610(%rsp),%rdx
lea    0x90(%rsp),%rcx
call   1040 <CalculateTreeSize@plt>
cvtsi2sd %rax,%xmm0
addsd  0xdab(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
lea    0x190(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
mov    %r15,%r8
mov    %r14,%r9
call   1070 <CalculateBlockSymbolSize@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
addsd  0x8(%rsp),%xmm0
add    $0xf18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @CalculateBlockSize(i16* %0, i16* %1, i64 %2, i64 %3, i32 %4) {
%6 = alloca i16*
%7 = alloca i16*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca [288 x i64]
%12 = alloca [32 x i64]
%13 = alloca [288 x i32]
%14 = alloca [32 x i32]
%15 = alloca double
store i16* %0, i16** %6
store i16* %1, i16** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
store i32 %4, i32* %10
store double 3.000000e+00, double* %15
%16 = load i32, i32* %10
%17 = icmp eq i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %5
%19 = load i32, i32* %10
%20 = icmp eq i32 %19, 2
br label %21
21: ; preds = %18, %5
%22 = phi i1 [ true, %5 ], [ %20, %18 ]
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load i32, i32* %10
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = getelementptr inbounds [288 x i32], [288 x i32]* %13, i64 0, i64 0
%29 = getelementptr inbounds [32 x i32], [32 x i32]* %14, i64 0, i64 0
%30 = call i32 @GetFixedTree(i32* %28, i32* %29)
br label %55
31: ; preds = %21
%32 = load i16*, i16** %6
%33 = load i16*, i16** %7
%34 = load i64, i64* %8
%35 = load i64, i64* %9
%36 = getelementptr inbounds [288 x i64], [288 x i64]* %11, i64 0, i64 0
%37 = getelementptr inbounds [32 x i64], [32 x i64]* %12, i64 0, i64 0
%38 = call i32 @GetLZ77Counts(i16* %32, i16* %33, i64 %34, i64 %35, i64* %36, i64* %37)
%39 = getelementptr inbounds [288 x i64], [288 x i64]* %11, i64 0, i64 0
%40 = getelementptr inbounds [288 x i32], [288 x i32]* %13, i64 0, i64 0
%41 = call i32 @CalculateBitLengths(i64* %39, i32 288, i32 15, i32* %40)
%42 = getelementptr inbounds [32 x i64], [32 x i64]* %12, i64 0, i64 0
%43 = getelementptr inbounds [32 x i32], [32 x i32]* %14, i64 0, i64 0
%44 = call i32 @CalculateBitLengths(i64* %42, i32 32, i32 15, i32* %43)
%45 = getelementptr inbounds [32 x i32], [32 x i32]* %14, i64 0, i64 0
%46 = call i32 @PatchDistanceCodesForBuggyDecoders(i32* %45)
%47 = getelementptr inbounds [288 x i32], [288 x i32]* %13, i64 0, i64 0
%48 = getelementptr inbounds [32 x i32], [32 x i32]* %14, i64 0, i64 0
%49 = getelementptr inbounds [288 x i64], [288 x i64]* %11, i64 0, i64 0
%50 = getelementptr inbounds [32 x i64], [32 x i64]* %12, i64 0, i64 0
%51 = call i64 @CalculateTreeSize(i32* %47, i32* %48, i64* %49, i64* %50)
%52 = sitofp i64 %51 to double
%53 = load double, double* %15
%54 = fadd double %53, %52
store double %54, double* %15
br label %55
55: ; preds = %31, %27
%56 = getelementptr inbounds [288 x i32], [288 x i32]* %13, i64 0, i64 0
%57 = getelementptr inbounds [32 x i32], [32 x i32]* %14, i64 0, i64 0
%58 = load i16*, i16** %6
%59 = load i16*, i16** %7
%60 = load i64, i64* %8
%61 = load i64, i64* %9
%62 = call i64 @CalculateBlockSymbolSize(i32* %56, i32* %57, i16* %58, i16* %59, i64 %60, i64 %61)
%63 = sitofp i64 %62 to double
%64 = load double, double* %15
%65 = fadd double %64, %63
store double %65, double* %15
%66 = load double, double* %15
ret double %66
}

LLVM IR (-O2):
define double @CalculateBlockSize(i16* %0, i16* %1, i64 %2, i64 %3, i32 %4) {
%6 = alloca [288 x i64]
%7 = alloca [32 x i64]
%8 = alloca [288 x i32]
%9 = alloca [32 x i32]
%10 = bitcast [288 x i64]* %6 to i8*
%11 = bitcast [32 x i64]* %7 to i8*
%12 = bitcast [288 x i32]* %8 to i8*
%13 = bitcast [32 x i32]* %9 to i8*
%14 = icmp eq i32 %4, 1
%15 = add i32 %4, -1
%16 = icmp ult i32 %15, 2
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
br i1 %14, label %19, label %23
19: ; preds = %5
%20 = getelementptr inbounds [288 x i32], [288 x i32]* %8, i64 0, i64 0
%21 = getelementptr inbounds [32 x i32], [32 x i32]* %9, i64 0, i64 0
%22 = call i32 @GetFixedTree(i32* %20, i32* %21)
br label %35
23: ; preds = %5
%24 = getelementptr inbounds [288 x i64], [288 x i64]* %6, i64 0, i64 0
%25 = getelementptr inbounds [32 x i64], [32 x i64]* %7, i64 0, i64 0
%26 = call i32 @GetLZ77Counts(i16* %0, i16* %1, i64 %2, i64 %3, i64* %24, i64* %25)
%27 = getelementptr inbounds [288 x i32], [288 x i32]* %8, i64 0, i64 0
%28 = call i32 @CalculateBitLengths(i64* %24, i32 288, i32 15, i32* %27)
%29 = getelementptr inbounds [32 x i32], [32 x i32]* %9, i64 0, i64 0
%30 = call i32 @CalculateBitLengths(i64* %25, i32 32, i32 15, i32* %29)
%31 = call i32 @PatchDistanceCodesForBuggyDecoders(i32* %29)
%32 = call i64 @CalculateTreeSize(i32* %27, i32* %29, i64* %24, i64* %25)
%33 = sitofp i64 %32 to double
%34 = fadd double %33, 3.000000e+00
br label %35
35: ; preds = %23, %19
%36 = phi double [ 3.000000e+00, %19 ], [ %34, %23 ]
%37 = getelementptr inbounds [288 x i32], [288 x i32]* %8, i64 0, i64 0
%38 = getelementptr inbounds [32 x i32], [32 x i32]* %9, i64 0, i64 0
%39 = call i64 @CalculateBlockSymbolSize(i32* %37, i32* %38, i16* %0, i16* %1, i64 %2, i64 %3)
%40 = sitofp i64 %39 to double
%41 = fadd double %36, %40
ret double %41
}