// File: srajangarg[P]raytracer/src/core/stb_image.hpp
// Function: stbi__jpeg_huff_decode(stbi__jpeg*, stbi__huffman*)
stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)
{
   unsigned int temp;
   int c,k;

   if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);

   // look at the top FAST_BITS and determine what symbol ID it is,
   // if the code is <= FAST_BITS
   c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
   k = h->fast[c];
   if (k < 255) {
      int s = h->size[k];
      if (s > j->code_bits)
         return -1;
      j->code_buffer <<= s;
      j->code_bits -= s;
      return h->values[k];
   }

   // naive test is to shift the code_buffer down so k bits are
   // valid, then test against maxcode. To speed this up, we've
   // preshifted maxcode left so that it has (16-k) 0s at the
   // end; in other words, regardless of the number of bits, it
   // wants to be compared against something shifted to have 16;
   // that way we don't need to shift inside the loop.
   temp = j->code_buffer >> 16;
   for (k=FAST_BITS+1 ; ; ++k)
      if (temp < h->maxcode[k])
         break;
   if (k == 17) {
      // error! code not found
      j->code_bits -= 16;
      return -1;
   }

   if (k > j->code_bits)
      return -1;

   // convert the huffman code to the symbol id
   c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k];
   STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]);

   // convert the id to a symbol
   j->code_bits -= k;
   j->code_buffer <<= k;
   return h->values[c];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x10, 0x4724(%rax)
jge 0x2fc36
movq -0x10(%rbp), %rdi
callq 0x2f500
movq -0x10(%rbp), %rax
movl 0x4720(%rax), %eax
shrl $0x17, %eax
andl $0x1ff, %eax            # imm = 0x1FF
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x24(%rbp)
cmpl $0xff, -0x24(%rbp)
jge 0x2fcd5
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movzbl 0x500(%rax,%rcx), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4724(%rcx), %eax
jle 0x2fc91
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2fe36
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movl 0x4720(%rax), %edx
shll %cl, %edx
movl %edx, %ecx
movl %ecx, 0x4720(%rax)
movl -0x28(%rbp), %edx
movq -0x10(%rbp), %rax
movl 0x4724(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x4724(%rax)
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movzbl 0x400(%rax,%rcx), %eax
movl %eax, -0x4(%rbp)
jmp 0x2fe36
movq -0x10(%rbp), %rax
movl 0x4720(%rax), %eax
shrl $0x10, %eax
movl %eax, -0x1c(%rbp)
movl $0xa, -0x24(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
movslq -0x24(%rbp), %rdx
cmpl 0x604(%rcx,%rdx,4), %eax
jae 0x2fd02
jmp 0x2fd0f
jmp 0x2fd04
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x2fcec
cmpl $0x11, -0x24(%rbp)
jne 0x2fd34
movq -0x10(%rbp), %rax
movl 0x4724(%rax), %ecx
subl $0x10, %ecx
movl %ecx, 0x4724(%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2fe36
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4724(%rcx), %eax
jle 0x2fd4f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2fe36
movq -0x10(%rbp), %rax
movl 0x4720(%rax), %eax
movl $0x20, %ecx
subl -0x24(%rbp), %ecx
shrl %cl, %eax
movslq -0x24(%rbp), %rdx
leaq 0x2c732(%rip), %rcx     # 0x5c4a0
andl (%rcx,%rdx,4), %eax
movq -0x18(%rbp), %rcx
movslq -0x24(%rbp), %rdx
addl 0x64c(%rcx,%rdx,4), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0x4720(%rax), %eax
movq -0x18(%rbp), %rcx
movslq -0x20(%rbp), %rdx
movzbl 0x500(%rcx,%rdx), %edx
movl $0x20, %ecx
subl %edx, %ecx
shrl %cl, %eax
movq -0x18(%rbp), %rcx
movslq -0x20(%rbp), %rdx
movzbl 0x500(%rcx,%rdx), %ecx
movl %ecx, %edx
leaq 0x2c6e1(%rip), %rcx     # 0x5c4a0
andl (%rcx,%rdx,4), %eax
movq -0x18(%rbp), %rcx
movslq -0x20(%rbp), %rdx
movzwl 0x200(%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
jne 0x2fdd8
jmp 0x2fdf7
leaq 0x18284(%rip), %rdi     # 0x48063
leaq 0x182cf(%rip), %rsi     # 0x480b5
movl $0x671, %edx            # imm = 0x671
leaq 0x182da(%rip), %rcx     # 0x480cc
callq 0x52c0
movl -0x24(%rbp), %edx
movq -0x10(%rbp), %rax
movl 0x4724(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x4724(%rax)
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movl 0x4720(%rax), %edx
shll %cl, %edx
movl %edx, %ecx
movl %ecx, 0x4720(%rax)
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl 0x400(%rax,%rcx), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop