// File: srajangarg[P]raytracer/src/core/stb_image.hpp
// Function: stbi__zbuild_huffman(stbi__zhuffman*, unsigned char*, int)
static int stbi__zbuild_huffman(stbi__zhuffman *z, stbi_uc *sizelist, int num)
{
   int i,k=0;
   int code, next_code[16], sizes[17];

   // DEFLATE spec for generating codes
   memset(sizes, 0, sizeof(sizes));
   memset(z->fast, 0, sizeof(z->fast));
   for (i=0; i < num; ++i)
      ++sizes[sizelist[i]];
   sizes[0] = 0;
   for (i=1; i < 16; ++i)
      if (sizes[i] > (1 << i))
         return stbi__err("bad sizes", "Corrupt PNG");
   code = 0;
   for (i=1; i < 16; ++i) {
      next_code[i] = code;
      z->firstcode[i] = (stbi__uint16) code;
      z->firstsymbol[i] = (stbi__uint16) k;
      code = (code + sizes[i]);
      if (sizes[i])
         if (code-1 >= (1 << i)) return stbi__err("bad codelengths","Corrupt PNG");
      z->maxcode[i] = code << (16-i); // preshift for inner loop
      code <<= 1;
      k += sizes[i];
   }
   z->maxcode[16] = 0x10000; // sentinel
   for (i=0; i < num; ++i) {
      int s = sizelist[i];
      if (s) {
         int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
         stbi__uint16 fastv = (stbi__uint16) ((s << 9) | i);
         z->size [c] = (stbi_uc     ) s;
         z->value[c] = (stbi__uint16) i;
         if (s <= STBI__ZFAST_BITS) {
            int j = stbi__bit_reverse(next_code[s],s);
            while (j < (1 << STBI__ZFAST_BITS)) {
               z->fast[j] = fastv;
               j += (1 << s);
            }
         }
         ++next_code[s];
      }
   }
   return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl $0x0, -0x24(%rbp)
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
movl $0x44, %edx
callq 0x5230
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x5230
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x35f37
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl -0xc0(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0xc0(%rbp,%rax,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x35f07
movl $0x0, -0xc0(%rbp)
movl $0x1, -0x20(%rbp)
cmpl $0x10, -0x20(%rbp)
jge 0x35f8a
movslq -0x20(%rbp), %rax
movl -0xc0(%rbp,%rax,4), %eax
movl -0x20(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jle 0x35f7d
leaq 0x12808(%rip), %rdi     # 0x48778
callq 0x275b0
movl %eax, -0x4(%rbp)
jmp 0x361b5
jmp 0x35f7f
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x35f48
movl $0x0, -0x28(%rbp)
movl $0x1, -0x20(%rbp)
cmpl $0x10, -0x20(%rbp)
jge 0x36067
movl -0x28(%rbp), %ecx
movslq -0x20(%rbp), %rax
movl %ecx, -0x70(%rbp,%rax,4)
movl -0x28(%rbp), %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movw %dx, 0x400(%rax,%rcx,2)
movl -0x24(%rbp), %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movw %dx, 0x464(%rax,%rcx,2)
movl -0x28(%rbp), %eax
movslq -0x20(%rbp), %rcx
addl -0xc0(%rbp,%rcx,4), %eax
movl %eax, -0x28(%rbp)
movslq -0x20(%rbp), %rax
cmpl $0x0, -0xc0(%rbp,%rax,4)
je 0x36024
movl -0x28(%rbp), %eax
subl $0x1, %eax
movl -0x20(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jl 0x36022
leaq 0x1276d(%rip), %rdi     # 0x48782
callq 0x275b0
movl %eax, -0x4(%rbp)
jmp 0x361b5
jmp 0x36024
movl -0x28(%rbp), %edx
movl $0x10, %ecx
subl -0x20(%rbp), %ecx
shll %cl, %edx
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl %edx, 0x420(%rax,%rcx,4)
movl -0x28(%rbp), %eax
shll %eax
movl %eax, -0x28(%rbp)
movslq -0x20(%rbp), %rax
movl -0xc0(%rbp,%rax,4), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x35f98
movq -0x10(%rbp), %rax
movl $0x10000, 0x460(%rax)   # imm = 0x10000
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x361ae
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xc4(%rbp)
je 0x3619e
movslq -0xc4(%rbp), %rax
movl -0x70(%rbp,%rax,4), %eax
movq -0x10(%rbp), %rcx
movslq -0xc4(%rbp), %rdx
movzwl 0x400(%rcx,%rdx,2), %ecx
subl %ecx, %eax
movq -0x10(%rbp), %rcx
movslq -0xc4(%rbp), %rdx
movzwl 0x464(%rcx,%rdx,2), %ecx
addl %ecx, %eax
movl %eax, -0xc8(%rbp)
movl -0xc4(%rbp), %eax
shll $0x9, %eax
orl -0x20(%rbp), %eax
movw %ax, -0xca(%rbp)
movl -0xc4(%rbp), %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movslq -0xc8(%rbp), %rcx
movb %dl, 0x484(%rax,%rcx)
movl -0x20(%rbp), %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0xc8(%rbp), %rcx
movw %dx, 0x5a4(%rax,%rcx,2)
cmpl $0x9, -0xc4(%rbp)
jg 0x3618c
movslq -0xc4(%rbp), %rax
movl -0x70(%rbp,%rax,4), %edi
movl -0xc4(%rbp), %esi
callq 0x36970
movl %eax, -0xd0(%rbp)
cmpl $0x200, -0xd0(%rbp)     # imm = 0x200
jge 0x3618a
movw -0xca(%rbp), %dx
movq -0x10(%rbp), %rax
movslq -0xd0(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0xc4(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
addl -0xd0(%rbp), %eax
movl %eax, -0xd0(%rbp)
jmp 0x3614d
jmp 0x3618c
movslq -0xc4(%rbp), %rax
movl -0x70(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x70(%rbp,%rax,4)
jmp 0x361a0
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x3607c
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)