#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*/ _huff_fatal_error(char *);
int /*<<< orphan*/ _huff_out_of_mem(char *);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ build_heap(int *, int *, int);
int /*<<< orphan*/ build_huffman_tree(int *, int);
int /*<<< orphan*/ compute_code_len(int *, int *, int);
int /*<<< orphan*/ compute_code_values(int *, int *, int);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
void compute_huffman_code(int *freq, int n, int *code_len, int *code_value) {
  int i, *heap;
  for (i = 0; i < n; i++)
    assert(freq[i] >= 0);
  if (n > 2) {
    heap = (int *)malloc((2 * n + 1) * sizeof(int));
    if (heap == NULL)
      _huff_out_of_mem("compute_huffman_code");
    build_heap(heap, freq, n);
    build_huffman_tree(heap, n);
    compute_code_len(code_len, heap, n);
    compute_code_values(code_value, code_len, n);
    free(heap);
  } else if (n == 2) {
    code_len[0] = code_len[1] = 1;
    code_value[0] = 0;
    code_value[1] = 1;
  } else if (n == 1)
    code_len[0] = code_value[0] = 0;
  else
    _huff_fatal_error("Illegal number of symbols! (compute_huffman_code)");
}


x86 assembly (clang, -O0):
<compute_huffman_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e1 <compute_huffman_code+0x51>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ae <compute_huffman_code+0x1e>
cmpl   $0x2,-0xc(%rbp)
jle    1266 <compute_huffman_code+0xd6>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    121c <compute_huffman_code+0x8c>
lea    0xde9(%rip),%rdi
call   10a0 <_huff_out_of_mem@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1070 <build_heap@plt>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <build_huffman_tree@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1080 <compute_code_len@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <compute_code_values@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    12d8 <compute_huffman_code+0x148>
cmpl   $0x2,-0xc(%rbp)
jne    129f <compute_huffman_code+0x10f>
mov    -0x18(%rbp),%rax
movl   $0x1,0x4(%rax)
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x1,0x4(%rax)
jmp    12d3 <compute_huffman_code+0x143>
cmpl   $0x1,-0xc(%rbp)
jne    12c2 <compute_huffman_code+0x132>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
jmp    12ce <compute_huffman_code+0x13e>
lea    0xd4c(%rip),%rdi
call   10b0 <_huff_fatal_error@plt>
jmp    12d3 <compute_huffman_code+0x143>
jmp    12d8 <compute_huffman_code+0x148>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_huffman_code>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %rdx,%r15
mov    %esi,%r12d
test   %esi,%esi
jle    124c <compute_huffman_code+0xbc>
mov    %rdi,%r14
mov    %r12d,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14,%rbp,4),%edi
not    %edi
shr    $0x1f,%edi
call   1060 <assert@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11c0 <compute_huffman_code+0x30>
cmp    $0x3,%r12d
jl     124c <compute_huffman_code+0xbc>
mov    %r13,%rbx
lea    0x4(,%r12,8),%edi
call   1090 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1201 <compute_huffman_code+0x71>
lea    0xe04(%rip),%rdi
call   10a0 <_huff_out_of_mem@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1070 <build_heap@plt>
mov    %r13,%rdi
mov    %r12d,%esi
call   1050 <build_huffman_tree@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1080 <compute_code_len@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1040 <compute_code_values@plt>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
cmp    $0x1,%r12d
je     1275 <compute_huffman_code+0xe5>
cmp    $0x2,%r12d
jne    1293 <compute_huffman_code+0x103>
movabs $0x100000001,%rax
mov    %rax,(%r15)
movabs $0x100000000,%rax
mov    %rax,0x0(%r13)
jmp    1284 <compute_huffman_code+0xf4>
movl   $0x0,0x0(%r13)
movl   $0x0,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7b(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <_huff_fatal_error@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"compute_huffman_code\00"
@.str.1 = [50 x i8] c"Illegal number of symbols! (compute_huffman_code)\00"
define void @compute_huffman_code(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*
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32* %3, i32** %8
store i32 0, i32* %9
br label %11
11: ; preds = %24, %4
%12 = load i32, i32* %9
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %27
15: ; preds = %11
%16 = load i32*, i32** %5
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %16, i64 %18
%20 = load i32, i32* %19
%21 = icmp sge i32 %20, 0
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
br label %24
24: ; preds = %15
%25 = load i32, i32* %9
%26 = add nsw i32 %25, 1
store i32 %26, i32* %9
br label %11
27: ; preds = %11
%28 = load i32, i32* %6
%29 = icmp sgt i32 %28, 2
br i1 %29, label %30, label %61
30: ; preds = %27
%31 = load i32, i32* %6
%32 = mul nsw i32 2, %31
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = mul i64 %34, 4
%36 = trunc i64 %35 to i32
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to i32*
store i32* %38, i32** %10
%39 = load i32*, i32** %10
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %43
41: ; preds = %30
%42 = call i32 @_huff_out_of_mem(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %41, %30
%44 = load i32*, i32** %10
%45 = load i32*, i32** %5
%46 = load i32, i32* %6
%47 = call i32 @build_heap(i32* %44, i32* %45, i32 %46)
%48 = load i32*, i32** %10
%49 = load i32, i32* %6
%50 = call i32 @build_huffman_tree(i32* %48, i32 %49)
%51 = load i32*, i32** %7
%52 = load i32*, i32** %10
%53 = load i32, i32* %6
%54 = call i32 @compute_code_len(i32* %51, i32* %52, i32 %53)
%55 = load i32*, i32** %8
%56 = load i32*, i32** %7
%57 = load i32, i32* %6
%58 = call i32 @compute_code_values(i32* %55, i32* %56, i32 %57)
%59 = load i32*, i32** %10
%60 = call i32 @free(i32* %59)
br label %85
61: ; preds = %27
%62 = load i32, i32* %6
%63 = icmp eq i32 %62, 2
br i1 %63, label %64, label %73
64: ; preds = %61
%65 = load i32*, i32** %7
%66 = getelementptr inbounds i32, i32* %65, i64 1
store i32 1, i32* %66
%67 = load i32*, i32** %7
%68 = getelementptr inbounds i32, i32* %67, i64 0
store i32 1, i32* %68
%69 = load i32*, i32** %8
%70 = getelementptr inbounds i32, i32* %69, i64 0
store i32 0, i32* %70
%71 = load i32*, i32** %8
%72 = getelementptr inbounds i32, i32* %71, i64 1
store i32 1, i32* %72
br label %84
73: ; preds = %61
%74 = load i32, i32* %6
%75 = icmp eq i32 %74, 1
br i1 %75, label %76, label %81
76: ; preds = %73
%77 = load i32*, i32** %8
%78 = getelementptr inbounds i32, i32* %77, i64 0
store i32 0, i32* %78
%79 = load i32*, i32** %7
%80 = getelementptr inbounds i32, i32* %79, i64 0
store i32 0, i32* %80
br label %83
81: ; preds = %73
%82 = call i32 @_huff_fatal_error(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %83
83: ; preds = %81, %76
br label %84
84: ; preds = %83, %64
br label %85
85: ; preds = %84, %43
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"compute_huffman_code\00"
@.str.1 = [50 x i8] c"Illegal number of symbols! (compute_huffman_code)\00"
define void @compute_huffman_code(i32* %0, i32 %1, i32* %2, i32* %3) {
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %33
6: ; preds = %4
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ 0, %6 ], [ %15, %8 ]
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = load i32, i32* %10
%12 = xor i32 %11, -1
%13 = lshr i32 %12, 31
%14 = tail call i32 @assert(i32 %13)
%15 = add nuw nsw i64 %9, 1
%16 = icmp eq i64 %15, %7
br i1 %16, label %17, label %8
17: ; preds = %8
%18 = icmp sgt i32 %1, 2
br i1 %18, label %19, label %33
19: ; preds = %17
%20 = shl i32 %1, 3
%21 = or i32 %20, 4
%22 = tail call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i32*
%24 = icmp eq i64 %22, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = tail call i32 @_huff_out_of_mem(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %19
%28 = tail call i32 @build_heap(i32* %23, i32* %0, i32 %1)
%29 = tail call i32 @build_huffman_tree(i32* %23, i32 %1)
%30 = tail call i32 @compute_code_len(i32* %2, i32* %23, i32 %1)
%31 = tail call i32 @compute_code_values(i32* %3, i32* %2, i32 %1)
%32 = tail call i32 @free(i32* %23)
br label %40
33: ; preds = %4, %17
switch i32 %1, label %38 [
i32 2, label %34
i32 1, label %37
]
34: ; preds = %33
%35 = getelementptr inbounds i32, i32* %2, i64 1
store i32 1, i32* %35
store i32 1, i32* %2
store i32 0, i32* %3
%36 = getelementptr inbounds i32, i32* %3, i64 1
store i32 1, i32* %36
br label %40
37: ; preds = %33
store i32 0, i32* %3
store i32 0, i32* %2
br label %40
38: ; preds = %33
%39 = tail call i32 @_huff_fatal_error(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %34, %38, %37, %27
ret void
}