#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;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ AddHuffmanCodeToTable(int /*<<< orphan*/ *, int,
                                         int /*<<< orphan*/, int, int);
int /*<<< orphan*/ InitializeHuffmanTable(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ ReverseBits(int);
bool BuildHuffmanTableFromLengths(uint16_t *table, int maxlength, int *lengths,
                                  int numcodes) {
  InitializeHuffmanTable(table, maxlength);
  uint32_t code = 0;
  for (int len = 1; len <= maxlength; len++) {
    for (int i = 0; i < numcodes; i++) {
      if (lengths[i] != len)
        continue;
      if (!AddHuffmanCodeToTable(table, maxlength, ReverseBits(code), len, i))
        return false;
      code += 1 << (16 - len);
    }
  }
  return true;
}


x86 assembly (clang, -O0):
<BuildHuffmanTableFromLengths>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <InitializeHuffmanTable@plt>
movl   $0x0,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     120e <BuildHuffmanTableFromLengths+0xde>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11fb <BuildHuffmanTableFromLengths+0xcb>
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x2c(%rbp),%eax
je     1198 <BuildHuffmanTableFromLengths+0x68>
jmp    11ed <BuildHuffmanTableFromLengths+0xbd>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <ReverseBits@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%r8d
call   1030 <AddHuffmanCodeToTable@plt>
cmp    $0x0,%eax
jne    11d8 <BuildHuffmanTableFromLengths+0xa8>
movl   $0x0,-0x4(%rbp)
jmp    1215 <BuildHuffmanTableFromLengths+0xe5>
mov    $0x10,%ecx
sub    -0x2c(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1173 <BuildHuffmanTableFromLengths+0x43>
jmp    1200 <BuildHuffmanTableFromLengths+0xd0>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1160 <BuildHuffmanTableFromLengths+0x30>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BuildHuffmanTableFromLengths>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %rdx,%r13
mov    %esi,%ebx
mov    %rdi,0x10(%rsp)
call   1040 <InitializeHuffmanTable@plt>
mov    %ebx,0x8(%rsp)
test   %ebx,%ebx
jle    11d9 <BuildHuffmanTableFromLengths+0xa9>
mov    %r14d,%r15d
xor    %ebp,%ebp
mov    $0x1,%ebx
mov    %r14d,0xc(%rsp)
jmp    1180 <BuildHuffmanTableFromLengths+0x50>
nopl   (%rax)
lea    0x1(%rbx),%eax
cmp    0x8(%rsp),%ebx
mov    %eax,%ebx
mov    0xc(%rsp),%r14d
je     11d9 <BuildHuffmanTableFromLengths+0xa9>
test   %r14d,%r14d
jle    1170 <BuildHuffmanTableFromLengths+0x40>
mov    $0x10,%cl
sub    %bl,%cl
mov    $0x1,%r12d
shl    %cl,%r12d
xor    %r14d,%r14d
jmp    11a9 <BuildHuffmanTableFromLengths+0x79>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r14
cmp    %r14,%r15
je     1170 <BuildHuffmanTableFromLengths+0x40>
cmp    %ebx,0x0(%r13,%r14,4)
jne    11a0 <BuildHuffmanTableFromLengths+0x70>
mov    %ebp,%edi
call   1050 <ReverseBits@plt>
mov    0x10(%rsp),%rdi
mov    0x8(%rsp),%esi
mov    %eax,%edx
mov    %ebx,%ecx
mov    %r14d,%r8d
call   1030 <AddHuffmanCodeToTable@plt>
test   %eax,%eax
je     11d5 <BuildHuffmanTableFromLengths+0xa5>
add    %r12d,%ebp
jmp    11a0 <BuildHuffmanTableFromLengths+0x70>
xor    %eax,%eax
jmp    11de <BuildHuffmanTableFromLengths+0xae>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @BuildHuffmanTableFromLengths(i32* %0, i32 %1, i32* nocapture %2, i32 %3) {
%5 = tail call i32 @InitializeHuffmanTable(i32* %0, i32 %1)
%6 = icmp slt i32 %1, 1
br i1 %6, label %37, label %7
7: ; preds = %4
%8 = icmp sgt i32 %3, 0
%9 = zext i32 %3 to i64
br label %10
10: ; preds = %7, %33
%11 = phi i32 [ 1, %7 ], [ %35, %33 ]
%12 = phi i32 [ 0, %7 ], [ %34, %33 ]
br i1 %8, label %13, label %33
13: ; preds = %10
%14 = sub nsw i32 16, %11
%15 = shl nuw i32 1, %14
br label %16
16: ; preds = %13, %29
%17 = phi i64 [ 0, %13 ], [ %31, %29 ]
%18 = phi i32 [ %12, %13 ], [ %30, %29 ]
%19 = getelementptr inbounds i32, i32* %2, i64 %17
%20 = load i32, i32* %19
%21 = icmp eq i32 %20, %11
br i1 %21, label %22, label %29
22: ; preds = %16
%23 = tail call i32 @ReverseBits(i32 %18)
%24 = trunc i64 %17 to i32
%25 = tail call i32 @AddHuffmanCodeToTable(i32* %0, i32 %1, i32 %23, i32 %11, i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %37, label %27
27: ; preds = %22
%28 = add nsw i32 %18, %15
br label %29
29: ; preds = %16, %27
%30 = phi i32 [ %18, %16 ], [ %28, %27 ]
%31 = add nuw nsw i64 %17, 1
%32 = icmp eq i64 %31, %9
br i1 %32, label %33, label %16
33: ; preds = %29, %10
%34 = phi i32 [ %12, %10 ], [ %30, %29 ]
%35 = add nuw i32 %11, 1
%36 = icmp eq i32 %11, %1
br i1 %36, label %37, label %10
37: ; preds = %33, %22, %4
%38 = phi i32 [ 1, %4 ], [ 0, %22 ], [ 1, %33 ]
ret i32 %38
}