// File: /workspace/github_c_makefile_star2_O0/imaya[P]zopfli-for-emscripten/tree.c
// Function: ZopfliLengthsToSymbols
void ZopfliLengthsToSymbols(const unsigned* lengths, size_t n, unsigned maxbits,
                            unsigned* symbols) {
  size_t* bl_count = (size_t*)malloc(sizeof(size_t) * (maxbits + 1));
  size_t* next_code = (size_t*)malloc(sizeof(size_t) * (maxbits + 1));
  unsigned bits, i;
  unsigned code;

  for (i = 0; i < n; i++) {
    symbols[i] = 0;
  }

  /* 1) Count the number of codes for each code length. Let bl_count[N] be the
  number of codes of length N, N >= 1. */
  for (bits = 0; bits <= maxbits; bits++) {
    bl_count[bits] = 0;
  }
  for (i = 0; i < n; i++) {
    assert(lengths[i] <= maxbits);
    bl_count[lengths[i]]++;
  }
  /* 2) Find the numerical value of the smallest code for each code length. */
  code = 0;
  bl_count[0] = 0;
  for (bits = 1; bits <= maxbits; bits++) {
    code = (code + bl_count[bits-1]) << 1;
    next_code[bits] = code;
  }
  /* 3) Assign numerical values to all codes, using consecutive values for all
  codes of the same length with the base values determined at step 2. */
  for (i = 0;  i < n; i++) {
    unsigned len = lengths[i];
    if (len != 0) {
      symbols[i] = next_code[len];
      next_code[len]++;
    }
  }

  free(bl_count);
  free(next_code);
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movl %edx, 0x34(%rsp)
movq %rcx, 0x28(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x1120
movq %rax, 0x20(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x1120
movq %rax, 0x18(%rsp)
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpq 0x38(%rsp), %rax
jae 0xaf59
movq 0x28(%rsp), %rax
movl 0x10(%rsp), %ecx
movl $0x0, (%rax,%rcx,4)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0xaf31
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x34(%rsp), %eax
ja 0xaf89
movq 0x20(%rsp), %rax
movl 0x14(%rsp), %ecx
movq $0x0, (%rax,%rcx,8)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0xaf61
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpq 0x38(%rsp), %rax
jae 0xaff9
movq 0x40(%rsp), %rax
movl 0x10(%rsp), %ecx
movl (%rax,%rcx,4), %eax
cmpl 0x34(%rsp), %eax
ja 0xafb0
jmp 0xafcf
leaq 0x3141(%rip), %rdi      # 0xe0f8
leaq 0x3150(%rip), %rsi      # 0xe10e
movl $0x2f, %edx
leaq 0x314b(%rip), %rcx      # 0xe115
callq 0x10b0
movq 0x20(%rsp), %rax
movq 0x40(%rsp), %rcx
movl 0x10(%rsp), %edx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0xaf91
movl $0x0, 0xc(%rsp)
movq 0x20(%rsp), %rax
movq $0x0, (%rax)
movl $0x1, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x34(%rsp), %eax
ja 0xb05c
movl 0xc(%rsp), %eax
movq 0x20(%rsp), %rcx
movl 0x14(%rsp), %edx
subl $0x1, %edx
movl %edx, %edx
addq (%rcx,%rdx,8), %rax
shlq %rax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, %edx
movq 0x18(%rsp), %rax
movl 0x14(%rsp), %ecx
movq %rdx, (%rax,%rcx,8)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0xb015
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpq 0x38(%rsp), %rax
jae 0xb0c5
movq 0x40(%rsp), %rax
movl 0x10(%rsp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, 0x8(%rsp)
cmpl $0x0, 0x8(%rsp)
je 0xb0b6
movq 0x18(%rsp), %rax
movl 0x8(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %edx
movq 0x28(%rsp), %rax
movl 0x10(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movq 0x18(%rsp), %rax
movl 0x8(%rsp), %ecx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0xb0b8
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0xb064
movq 0x20(%rsp), %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
callq 0x1030
addq $0x48, %rsp
retq
nop