// File: srajangarg[P]raytracer/src/core/stb_image.hpp
// Function: stbi__build_huffman(stbi__huffman*, int*)
static int stbi__build_huffman(stbi__huffman *h, int *count)
{
   int i,j,k=0,code;
   // build size list for each symbol (from JPEG spec)
   for (i=0; i < 16; ++i)
      for (j=0; j < count[i]; ++j)
         h->size[k++] = (stbi_uc) (i+1);
   h->size[k] = 0;

   // compute actual symbols (from jpeg spec)
   code = 0;
   k = 0;
   for(j=1; j <= 16; ++j) {
      // compute delta to add to code to compute symbol id
      h->delta[j] = k - code;
      if (h->size[k] == j) {
         while (h->size[k] == j)
            h->code[k++] = (stbi__uint16) (code++);
         if (code-1 >= (1 << j)) return stbi__err("bad code lengths","Corrupt JPEG");
      }
      // compute largest code + 1 for this size, preshifted as needed later
      h->maxcode[j] = code << (16-j);
      code <<= 1;
   }
   h->maxcode[j] = 0xffffffff;

   // build non-spec acceleration table; 255 is flag for not-accelerated
   memset(h->fast, 255, 1 << FAST_BITS);
   for (i=0; i < k; ++i) {
      int s = h->size[i];
      if (s <= FAST_BITS) {
         int c = h->code[i] << (FAST_BITS-s);
         int m = 1 << (FAST_BITS-s);
         for (j=0; j < m; ++j) {
            h->fast[c+j] = (stbi_uc) i;
         }
      }
   }
   return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x1c(%rbp)
cmpl $0x10, -0x1c(%rbp)
jge 0x2d044
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
movslq -0x1c(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x2d037
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x24(%rbp)
movslq %ecx, %rcx
movb %dl, 0x500(%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2cffb
jmp 0x2d039
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2cfee
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movb $0x0, 0x500(%rax,%rcx)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x1, -0x20(%rbp)
cmpl $0x10, -0x20(%rbp)
jg 0x2d13c
movl -0x24(%rbp), %edx
subl -0x28(%rbp), %edx
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl %edx, 0x64c(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movzbl 0x500(%rax,%rcx), %eax
cmpl -0x20(%rbp), %eax
jne 0x2d10a
jmp 0x2d09f
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movzbl 0x500(%rax,%rcx), %eax
cmpl -0x20(%rbp), %eax
jne 0x2d0de
movl -0x28(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x28(%rbp)
movw %ax, %dx
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x24(%rbp)
movslq %ecx, %rcx
movw %dx, 0x200(%rax,%rcx,2)
jmp 0x2d09f
movl -0x28(%rbp), %eax
subl $0x1, %eax
movl -0x20(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jl 0x2d108
leaq 0x1ae67(%rip), %rdi     # 0x47f62
callq 0x275b0
movl %eax, -0x4(%rbp)
jmp 0x2d205
jmp 0x2d10a
movl -0x28(%rbp), %edx
movl $0x10, %ecx
subl -0x20(%rbp), %ecx
shll %cl, %edx
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl %edx, 0x604(%rax,%rcx,4)
movl -0x28(%rbp), %eax
shll %eax
movl %eax, -0x28(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2d069
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl $0xffffffff, 0x604(%rax,%rcx,4) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
movl $0xff, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x5230
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x2d1fe
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movzbl 0x500(%rax,%rcx), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x9, -0x2c(%rbp)
jg 0x2d1ee
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movzwl 0x200(%rax,%rcx,2), %eax
movl $0x9, %ecx
subl -0x2c(%rbp), %ecx
shll %cl, %eax
movl %eax, -0x30(%rbp)
movl $0x9, %ecx
subl -0x2c(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x2d1ec
movl -0x1c(%rbp), %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
addl -0x20(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2d1c4
jmp 0x2d1ee
jmp 0x2d1f0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2d169
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop