#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 uint8_t;
typedef int uint64_t;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ build_tree(int *, int *, int);
int /*<<< orphan*/ compute_depths(int *, int *, int);
int /*<<< orphan*/ sort_alphabet(int *, int *);
void make_code_lengths(uint8_t length[], uint32_t frequency[], uint32_t as) {
  uint32_t i;
  uint32_t k;
  uint32_t d;
  uint32_t c;
  uint64_t weight[(2 + 255 + 1)];
  uint32_t V[(2 + 255 + 1)];
  uint32_t count[30 + 2];
  assert(as >= (2 + 0 + 1));
  assert(as <= (2 + 255 + 1));
  for (i = 0; i < as; i++) {
    weight[i] =
        (((uint64_t)((frequency[i]) < (1u) ? (1u) : (frequency[i])) << 32) |
         0x10000 | ((2 + 255 + 1) - i));
  }
  sort_alphabet(weight, weight + as);
  build_tree(V, weight, as);
  compute_depths(count, V, as);
  i = 0;
  c = 0;
  for (d = 0; d <= 30; d++) {
    k = count[d];
    c = (c + k) << 1;
    while (k != 0) {
      assert(i < as);
      length[(2 + 255 + 1) - (weight[i] & 0xFFFF)] = d;
      i++;
      k--;
    }
  }
  assert(c == (1UL << (30 + 1)));
  assert(i == as);
}


x86 assembly (clang, -O0):
<make_code_lengths>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8d0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x102,-0x14(%rbp)
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11f9 <make_code_lengths+0xb9>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmpl   $0x1,(%rax,%rcx,4)
jae    11b0 <make_code_lengths+0x70>
mov    $0x1,%eax
mov    %eax,-0x8c4(%rbp)
jmp    11c1 <make_code_lengths+0x81>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x8c4(%rbp)
mov    -0x8c4(%rbp),%eax
mov    $0x20,%ecx
shl    %cl,%eax
mov    %eax,%ecx
or     $0x10000,%ecx
mov    $0x102,%eax
sub    -0x18(%rbp),%eax
or     %eax,%ecx
movslq -0x18(%rbp),%rax
mov    %ecx,-0x430(%rbp,%rax,4)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1182 <make_code_lengths+0x42>
lea    -0x430(%rbp),%rdi
lea    -0x430(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1050 <sort_alphabet@plt>
lea    -0x840(%rbp),%rdi
lea    -0x430(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <build_tree@plt>
lea    -0x8c0(%rbp),%rdi
lea    -0x840(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <compute_depths@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x1e,-0x20(%rbp)
jg     12e8 <make_code_lengths+0x1a8>
movslq -0x20(%rbp),%rax
mov    -0x8c0(%rbp,%rax,4),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
add    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12d5 <make_code_lengths+0x195>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x20(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    -0x430(%rbp,%rcx,4),%esi
and    $0xffff,%esi
mov    $0x102,%ecx
sub    %esi,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    127c <make_code_lengths+0x13c>
jmp    12da <make_code_lengths+0x19a>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1258 <make_code_lengths+0x118>
movslq -0x24(%rbp),%rax
mov    $0x80000000,%ecx
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
add    $0x8d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_code_lengths>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x8a8,%rsp
mov    %edx,%ebx
mov    %rdi,%r13
xor    %edi,%edi
cmp    $0x3,%edx
setge  %dil
call   1040 <assert@plt>
xor    %edi,%edi
cmp    $0x103,%ebx
setl   %dil
call   1040 <assert@plt>
mov    %ebx,0x8(%rsp)
movslq %ebx,%r14
lea    (%rsp,%r14,4),%rsi
add    $0x90,%rsi
lea    0x90(%rsp),%r15
mov    %r15,%rdi
call   1050 <sort_alphabet@plt>
lea    0x4a0(%rsp),%r12
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1060 <build_tree@plt>
lea    0x10(%rsp),%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1030 <compute_depths@plt>
xor    %r12d,%r12d
xor    %ebx,%ebx
jmp    11da <make_code_lengths+0x9a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r12
cmp    $0x1f,%r12
je     1229 <make_code_lengths+0xe9>
mov    0x10(%rsp,%r12,4),%ebp
test   %ebp,%ebp
je     11d0 <make_code_lengths+0x90>
mov    %ebx,0xc(%rsp)
movslq %ebx,%rbx
neg    %ebp
xor    %r15d,%r15d
nop
xor    %edi,%edi
cmp    %r14,%rbx
setl   %dil
call   1040 <assert@plt>
movzwl 0x90(%rsp,%rbx,4),%eax
mov    $0x102,%ecx
sub    %rax,%rcx
mov    %r12d,0x0(%r13,%rcx,4)
add    $0x1,%rbx
add    $0xffffffff,%r15d
cmp    %r15d,%ebp
jne    11f0 <make_code_lengths+0xb0>
mov    0xc(%rsp),%ebx
sub    %r15d,%ebx
jmp    11d0 <make_code_lengths+0x90>
xor    %edi,%edi
call   1040 <assert@plt>
xor    %edi,%edi
cmp    0x8(%rsp),%ebx
sete   %dil
call   1040 <assert@plt>
add    $0x8a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @make_code_lengths(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [258 x i32]
%12 = alloca [258 x i32]
%13 = alloca [32 x i32]
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%14 = load i32, i32* %6
%15 = icmp sge i32 %14, 3
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i32, i32* %6
%19 = icmp sle i32 %18, 258
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
store i32 0, i32* %7
br label %22
22: ; preds = %50, %3
%23 = load i32, i32* %7
%24 = load i32, i32* %6
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %53
26: ; preds = %22
%27 = load i32*, i32** %5
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = load i32, i32* %30
%32 = icmp ult i32 %31, 1
br i1 %32, label %33, label %34
33: ; preds = %26
br label %40
34: ; preds = %26
%35 = load i32*, i32** %5
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = load i32, i32* %38
br label %40
40: ; preds = %34, %33
%41 = phi i32 [ 1, %33 ], [ %39, %34 ]
%42 = shl i32 %41, 32
%43 = or i32 %42, 65536
%44 = load i32, i32* %7
%45 = sub nsw i32 258, %44
%46 = or i32 %43, %45
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [258 x i32], [258 x i32]* %11, i64 0, i64 %48
store i32 %46, i32* %49
br label %50
50: ; preds = %40
%51 = load i32, i32* %7
%52 = add nsw i32 %51, 1
store i32 %52, i32* %7
br label %22
53: ; preds = %22
%54 = getelementptr inbounds [258 x i32], [258 x i32]* %11, i64 0, i64 0
%55 = getelementptr inbounds [258 x i32], [258 x i32]* %11, i64 0, i64 0
%56 = load i32, i32* %6
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %55, i64 %57
%59 = call i32 @sort_alphabet(i32* %54, i32* %58)
%60 = getelementptr inbounds [258 x i32], [258 x i32]* %12, i64 0, i64 0
%61 = getelementptr inbounds [258 x i32], [258 x i32]* %11, i64 0, i64 0
%62 = load i32, i32* %6
%63 = call i32 @build_tree(i32* %60, i32* %61, i32 %62)
%64 = getelementptr inbounds [32 x i32], [32 x i32]* %13, i64 0, i64 0
%65 = getelementptr inbounds [258 x i32], [258 x i32]* %12, i64 0, i64 0
%66 = load i32, i32* %6
%67 = call i32 @compute_depths(i32* %64, i32* %65, i32 %66)
store i32 0, i32* %7
store i32 0, i32* %10
store i32 0, i32* %9
br label %68
68: ; preds = %104, %53
%69 = load i32, i32* %9
%70 = icmp sle i32 %69, 30
br i1 %70, label %71, label %107
71: ; preds = %68
%72 = load i32, i32* %9
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [32 x i32], [32 x i32]* %13, i64 0, i64 %73
%75 = load i32, i32* %74
store i32 %75, i32* %8
%76 = load i32, i32* %10
%77 = load i32, i32* %8
%78 = add nsw i32 %76, %77
%79 = shl i32 %78, 1
store i32 %79, i32* %10
br label %80
80: ; preds = %83, %71
%81 = load i32, i32* %8
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %103
83: ; preds = %80
%84 = load i32, i32* %7
%85 = load i32, i32* %6
%86 = icmp slt i32 %84, %85
%87 = zext i1 %86 to i32
%88 = call i32 @assert(i32 %87)
%89 = load i32, i32* %9
%90 = load i32*, i32** %4
%91 = load i32, i32* %7
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds [258 x i32], [258 x i32]* %11, i64 0, i64 %92
%94 = load i32, i32* %93
%95 = and i32 %94, 65535
%96 = sub nsw i32 258, %95
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i32, i32* %90, i64 %97
store i32 %89, i32* %98
%99 = load i32, i32* %7
%100 = add nsw i32 %99, 1
store i32 %100, i32* %7
%101 = load i32, i32* %8
%102 = add nsw i32 %101, -1
store i32 %102, i32* %8
br label %80
103: ; preds = %80
br label %104
104: ; preds = %103
%105 = load i32, i32* %9
%106 = add nsw i32 %105, 1
store i32 %106, i32* %9
br label %68
107: ; preds = %68
%108 = load i32, i32* %10
%109 = sext i32 %108 to i64
%110 = icmp eq i64 %109, 2147483648
%111 = zext i1 %110 to i32
%112 = call i32 @assert(i32 %111)
%113 = load i32, i32* %7
%114 = load i32, i32* %6
%115 = icmp eq i32 %113, %114
%116 = zext i1 %115 to i32
%117 = call i32 @assert(i32 %116)
ret void
}

LLVM IR (-O2):
define void @make_code_lengths(i32* nocapture writeonly %0, i32* nocapture %1, i32 %2) {
%4 = alloca [258 x i32]
%5 = alloca [258 x i32]
%6 = alloca [32 x i32]
%7 = bitcast [258 x i32]* %4 to i8*
%8 = bitcast [258 x i32]* %5 to i8*
%9 = bitcast [32 x i32]* %6 to i8*
%10 = icmp sgt i32 %2, 2
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = icmp slt i32 %2, 259
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = getelementptr inbounds [258 x i32], [258 x i32]* %4, i64 0, i64 0
%17 = sext i32 %2 to i64
%18 = getelementptr inbounds [258 x i32], [258 x i32]* %4, i64 0, i64 %17
%19 = call i32 @sort_alphabet(i32* %16, i32* %18)
%20 = getelementptr inbounds [258 x i32], [258 x i32]* %5, i64 0, i64 0
%21 = call i32 @build_tree(i32* %20, i32* %16, i32 %2)
%22 = getelementptr inbounds [32 x i32], [32 x i32]* %6, i64 0, i64 0
%23 = call i32 @compute_depths(i32* %22, i32* %20, i32 %2)
br label %24
24: ; preds = %3, %50
%25 = phi i64 [ 0, %3 ], [ %52, %50 ]
%26 = phi i32 [ 0, %3 ], [ %51, %50 ]
%27 = getelementptr inbounds [32 x i32], [32 x i32]* %6, i64 0, i64 %25
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, 0
br i1 %29, label %50, label %30
30: ; preds = %24
%31 = sext i32 %26 to i64
%32 = trunc i64 %25 to i32
br label %33
33: ; preds = %30, %33
%34 = phi i64 [ %31, %30 ], [ %45, %33 ]
%35 = phi i32 [ %28, %30 ], [ %46, %33 ]
%36 = icmp slt i64 %34, %17
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = getelementptr inbounds [258 x i32], [258 x i32]* %4, i64 0, i64 %34
%40 = load i32, i32* %39
%41 = and i32 %40, 65535
%42 = sub nsw i32 258, %41
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %0, i64 %43
store i32 %32, i32* %44
%45 = add nsw i64 %34, 1
%46 = add nsw i32 %35, -1
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %33
48: ; preds = %33
%49 = trunc i64 %45 to i32
br label %50
50: ; preds = %48, %24
%51 = phi i32 [ %26, %24 ], [ %49, %48 ]
%52 = add nuw nsw i64 %25, 1
%53 = icmp eq i64 %52, 31
br i1 %53, label %54, label %24
54: ; preds = %50
%55 = call i32 @assert(i32 0)
%56 = icmp eq i32 %51, %2
%57 = zext i1 %56 to i32
%58 = call i32 @assert(i32 %57)
ret void
}