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

/* Variables and functions */
int MakeHuffmanTableEntry(int, int);
bool AddHuffmanCodeToTable(uint16_t *table, int maxlength, uint16_t code,
                           int length, int value) {
  for (int i = 0; i < 1 << (maxlength - length); i++) {
    if (table[code + (i << length)])
      return false;
    table[code + (i << length)] = MakeHuffmanTableEntry(value, length);
  }
  return true;
}


x86 assembly (clang, -O0):
<AddHuffmanCodeToTable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
cmp    %ecx,%eax
jge    11b2 <AddHuffmanCodeToTable+0xa2>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,-0x28(%rbp)
mov    -0x24(%rbp),%edx
mov    -0x1c(%rbp),%ecx
shl    %cl,%edx
mov    -0x28(%rbp),%ecx
add    %edx,%ecx
movslq %ecx,%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     117a <AddHuffmanCodeToTable+0x6a>
movl   $0x0,-0x4(%rbp)
jmp    11b9 <AddHuffmanCodeToTable+0xa9>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1030 <MakeHuffmanTableEntry@plt>
mov    %eax,%edx
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,-0x2c(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%ecx
shl    %cl,%esi
mov    -0x2c(%rbp),%ecx
add    %esi,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1130 <AddHuffmanCodeToTable+0x20>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AddHuffmanCodeToTable>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,0x4(%rsp)
mov    %ecx,%ebp
mov    %esi,%ecx
sub    %ebp,%ecx
mov    $0x1,%eax
mov    $0x1,%esi
shl    %cl,%esi
cmp    $0x1f,%ecx
je     11aa <AddHuffmanCodeToTable+0x9a>
mov    %edx,%r15d
mov    %rdi,%rbx
cmp    $0x2,%esi
mov    $0x1,%ecx
mov    $0x1,%r13d
cmovge %esi,%r13d
movslq %edx,%rdx
xor    %eax,%eax
cmpl   $0x0,(%rdi,%rdx,4)
jne    11aa <AddHuffmanCodeToTable+0x9a>
mov    %esi,(%rsp)
lea    (%rbx,%rdx,4),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ecx,%r14d
mov    0x4(%rsp),%edi
mov    %ebp,%esi
call   1030 <MakeHuffmanTableEntry@plt>
mov    %eax,(%r12)
cmp    %r14d,%r13d
je     11a1 <AddHuffmanCodeToTable+0x91>
mov    %r14d,%eax
mov    %ebp,%ecx
shl    %cl,%eax
add    %r15d,%eax
cltq
lea    (%rbx,%rax,4),%r12
lea    0x1(%r14),%ecx
cmpl   $0x0,(%rbx,%rax,4)
je     1170 <AddHuffmanCodeToTable+0x60>
xor    %eax,%eax
cmp    (%rsp),%r14d
setge  %al
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @AddHuffmanCodeToTable(i32* nocapture %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = sub nsw i32 %1, %3
%7 = shl nuw i32 1, %6
%8 = icmp eq i32 %6, 31
br i1 %8, label %31, label %9
9: ; preds = %5
%10 = call i32 @llvm.smax.i32(i32 %7, i32 1)
%11 = sext i32 %2 to i64
%12 = getelementptr inbounds i32, i32* %0, i64 %11
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %22, label %31
15: ; preds = %22
%16 = shl i32 %26, %3
%17 = add nsw i32 %16, %2
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %0, i64 %18
%20 = load i32, i32* %19
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %9, %15
%23 = phi i32* [ %19, %15 ], [ %12, %9 ]
%24 = phi i32 [ %26, %15 ], [ 0, %9 ]
%25 = tail call i32 @MakeHuffmanTableEntry(i32 %4, i32 %3)
store i32 %25, i32* %23
%26 = add nuw nsw i32 %24, 1
%27 = icmp eq i32 %26, %10
br i1 %27, label %28, label %15
28: ; preds = %22, %15
%29 = icmp sge i32 %26, %7
%30 = zext i1 %29 to i32
br label %31
31: ; preds = %28, %9, %5
%32 = phi i32 [ 1, %5 ], [ 0, %9 ], [ %30, %28 ]
ret i32 %32
}