// File: srajangarg[P]raytracer/src/core/stb_image.hpp
// Function: stbi__pic_load_core(stbi__context*, int, int, int*, unsigned char*)
static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result)
{
   int act_comp=0,num_packets=0,y,chained;
   stbi__pic_packet packets[10];

   // this will (should...) cater for even some bizarre stuff like having data
    // for the same channel in multiple packets.
   do {
      stbi__pic_packet *packet;

      if (num_packets==sizeof(packets)/sizeof(packets[0]))
         return stbi__errpuc("bad format","too many packets");

      packet = &packets[num_packets++];

      chained = stbi__get8(s);
      packet->size    = stbi__get8(s);
      packet->type    = stbi__get8(s);
      packet->channel = stbi__get8(s);

      act_comp |= packet->channel;

      if (stbi__at_eof(s))          return stbi__errpuc("bad file","file too short (reading packets)");
      if (packet->size != 8)  return stbi__errpuc("bad format","packet isn't 8bpp");
   } while (chained);

   *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel?

   for(y=0; y<height; ++y) {
      int packet_idx;

      for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
         stbi__pic_packet *packet = &packets[packet_idx];
         stbi_uc *dest = result+y*width*4;

         switch (packet->type) {
            default:
               return stbi__errpuc("bad format","packet has bad compression type");

            case 0: {//uncompressed
               int x;

               for(x=0;x<width;++x, dest+=4)
                  if (!stbi__readval(s,packet->channel,dest))
                     return 0;
               break;
            }

            case 1://Pure RLE
               {
                  int left=width, i;

                  while (left>0) {
                     stbi_uc count,value[4];

                     count=stbi__get8(s);
                     if (stbi__at_eof(s))   return stbi__errpuc("bad file","file too short (pure read count)");

                     if (count > left)
                        count = (stbi_uc) left;

                     if (!stbi__readval(s,packet->channel,value))  return 0;

                     for(i=0; i<count; ++i,dest+=4)
                        stbi__copyval(packet->channel,dest,value);
                     left -= count;
                  }
               }
               break;

            case 2: {//Mixed RLE
               int left=width;
               while (left>0) {
                  int count = stbi__get8(s), i;
                  if (stbi__at_eof(s))  return stbi__errpuc("bad file","file too short (mixed read count)");

                  if (count >= 128) { // Repeated
                     stbi_uc value[4];

                     if (count==128)
                        count = stbi__get16be(s);
                     else
                        count -= 127;
                     if (count > left)
                        return stbi__errpuc("bad file","scanline overrun");

                     if (!stbi__readval(s,packet->channel,value))
                        return 0;

                     for(i=0;i<count;++i, dest += 4)
                        stbi__copyval(packet->channel,dest,value);
                  } else { // Raw
                     ++count;
                     if (count>left) return stbi__errpuc("bad file","scanline overrun");

                     for(i=0;i<count;++i, dest+=4)
                        if (!stbi__readval(s,packet->channel,dest))
                           return 0;
                  }
                  left-=count;
               }
               break;
            }
         }
      }
   }

   return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movslq -0x30(%rbp), %rax
cmpq $0xa, %rax
jne 0x34a95
leaq 0x13b25(%rip), %rdi     # 0x485a1
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x34fb1
movl -0x30(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x30(%rbp)
movslq %eax, %rcx
leaq -0x60(%rbp), %rax
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movb %al, %cl
movq -0x68(%rbp), %rax
movb %cl, (%rax)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movb %al, %cl
movq -0x68(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movb %al, %cl
movq -0x68(%rbp), %rax
movb %cl, 0x2(%rax)
movq -0x68(%rbp), %rax
movzbl 0x2(%rax), %eax
orl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2c540
cmpl $0x0, %eax
je 0x34b32
leaq 0x13a7f(%rip), %rdi     # 0x48598
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x34fb1
movq -0x68(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x8, %eax
je 0x34b5e
leaq 0x13a5c(%rip), %rdi     # 0x485a1
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x34fb1
jmp 0x34b60
cmpl $0x0, -0x38(%rbp)
jne 0x34a6b
movl -0x2c(%rbp), %edx
andl $0x10, %edx
movl $0x3, %ecx
movl $0x4, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x34fa9
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x34f99
movslq -0x6c(%rbp), %rax
leaq (%rax,%rax,2), %rax
leaq -0x60(%rbp,%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
movl -0x14(%rbp), %edx
imull %edx, %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rax
movzbl 0x1(%rax), %eax
movl %eax, -0xa8(%rbp)
testl %eax, %eax
je 0x34c2d
jmp 0x34beb
movl -0xa8(%rbp), %eax
subl $0x1, %eax
je 0x34c8e
jmp 0x34bfc
movl -0xa8(%rbp), %eax
subl $0x2, %eax
je 0x34d96
jmp 0x34c0d
leaq 0x1398d(%rip), %rdi     # 0x485a1
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x34fb1
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x34c89
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rax
movzbl 0x2(%rax), %esi
movq -0x80(%rbp), %rdx
callq 0x34fc0
cmpq $0x0, %rax
jne 0x34c6a
movq $0x0, -0x8(%rbp)
jmp 0x34fb1
jmp 0x34c6c
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
movq -0x80(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x80(%rbp)
jmp 0x34c37
jmp 0x34f89
movl -0x14(%rbp), %eax
movl %eax, -0x88(%rbp)
cmpl $0x0, -0x88(%rbp)
jle 0x34d91
movq -0x10(%rbp), %rdi
callq 0x2ce50
movb %al, -0x8d(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2c540
cmpl $0x0, %eax
je 0x34ce1
leaq 0x138d0(%rip), %rdi     # 0x48598
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x34fb1
movzbl -0x8d(%rbp), %eax
cmpl -0x88(%rbp), %eax
jle 0x34cfc
movl -0x88(%rbp), %eax
movb %al, -0x8d(%rbp)
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rax
movzbl 0x2(%rax), %esi
leaq -0x91(%rbp), %rdx
callq 0x34fc0
cmpq $0x0, %rax
jne 0x34d27
movq $0x0, -0x8(%rbp)
jmp 0x34fb1
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movzbl -0x8d(%rbp), %ecx
cmpl %ecx, %eax
jge 0x34d77
movq -0x78(%rbp), %rax
movzbl 0x2(%rax), %edi
movq -0x80(%rbp), %rsi
leaq -0x91(%rbp), %rdx
callq 0x35060
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
movq -0x80(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x80(%rbp)
jmp 0x34d31
movzbl -0x8d(%rbp), %ecx
movl -0x88(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x88(%rbp)
jmp 0x34c97
jmp 0x34f89
movl -0x14(%rbp), %eax
movl %eax, -0x98(%rbp)
cmpl $0x0, -0x98(%rbp)
jle 0x34f87
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0x9c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2c540
cmpl $0x0, %eax
je 0x34dec
leaq 0x137c5(%rip), %rdi     # 0x48598
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x34fb1
cmpl $0x80, -0x9c(%rbp)
jl 0x34ed3
cmpl $0x80, -0x9c(%rbp)
jne 0x34e19
movq -0x10(%rbp), %rdi
callq 0x2cf90
movl %eax, -0x9c(%rbp)
jmp 0x34e28
movl -0x9c(%rbp), %eax
subl $0x7f, %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x98(%rbp), %eax
jle 0x34e56
leaq 0x1375b(%rip), %rdi     # 0x48598
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x34fb1
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rax
movzbl 0x2(%rax), %esi
leaq -0xa4(%rbp), %rdx
callq 0x34fc0
cmpq $0x0, %rax
jne 0x34e81
movq $0x0, -0x8(%rbp)
jmp 0x34fb1
movl $0x0, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jge 0x34ece
movq -0x78(%rbp), %rax
movzbl 0x2(%rax), %edi
movq -0x80(%rbp), %rsi
leaq -0xa4(%rbp), %rdx
callq 0x35060
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
movq -0x80(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x80(%rbp)
jmp 0x34e8b
jmp 0x34f6e
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x98(%rbp), %eax
jle 0x34f10
leaq 0x136a1(%rip), %rdi     # 0x48598
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x34fb1
movl $0x0, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jge 0x34f6c
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rax
movzbl 0x2(%rax), %esi
movq -0x80(%rbp), %rdx
callq 0x34fc0
cmpq $0x0, %rax
jne 0x34f4d
movq $0x0, -0x8(%rbp)
jmp 0x34fb1
jmp 0x34f4f
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
movq -0x80(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x80(%rbp)
jmp 0x34f1a
jmp 0x34f6e
movl -0x9c(%rbp), %ecx
movl -0x98(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x98(%rbp)
jmp 0x34d9f
jmp 0x34f89
jmp 0x34f8b
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x34ba0
jmp 0x34f9b
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x34b8d
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nop