// File: srajangarg[P]raytracer/src/core/stb_image.hpp
// Function: stbi__tga_load(stbi__context*, int*, int*, int*, int)
static stbi_uc *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
{
   //   read in the TGA header stuff
   int tga_offset = stbi__get8(s);
   int tga_indexed = stbi__get8(s);
   int tga_image_type = stbi__get8(s);
   int tga_is_RLE = 0;
   int tga_palette_start = stbi__get16le(s);
   int tga_palette_len = stbi__get16le(s);
   int tga_palette_bits = stbi__get8(s);
   int tga_x_origin = stbi__get16le(s);
   int tga_y_origin = stbi__get16le(s);
   int tga_width = stbi__get16le(s);
   int tga_height = stbi__get16le(s);
   int tga_bits_per_pixel = stbi__get8(s);
   int tga_comp, tga_rgb16=0;
   int tga_inverted = stbi__get8(s);
   // int tga_alpha_bits = tga_inverted & 15; // the 4 lowest bits - unused (useless?)
   //   image data
   unsigned char *tga_data;
   unsigned char *tga_palette = NULL;
   int i, j;
   unsigned char raw_data[4];
   int RLE_count = 0;
   int RLE_repeating = 0;
   int read_next_pixel = 1;

   //   do a tiny bit of precessing
   if ( tga_image_type >= 8 )
   {
      tga_image_type -= 8;
      tga_is_RLE = 1;
   }
   tga_inverted = 1 - ((tga_inverted >> 5) & 1);

   //   If I'm paletted, then I'll use the number of bits from the palette
   if ( tga_indexed ) tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16);
   else tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3), &tga_rgb16);

   if(!tga_comp) // shouldn't really happen, stbi__tga_test() should have ensured basic consistency
      return stbi__errpuc("bad format", "Can't find out TGA pixelformat");

   //   tga info
   *x = tga_width;
   *y = tga_height;
   if (comp) *comp = tga_comp;

   tga_data = (unsigned char*)stbi__malloc( (size_t)tga_width * tga_height * tga_comp );
   if (!tga_data) return stbi__errpuc("outofmem", "Out of memory");

   // skip to the data's starting position (offset usually = 0)
   stbi__skip(s, tga_offset );

   if ( !tga_indexed && !tga_is_RLE && !tga_rgb16 ) {
      for (i=0; i < tga_height; ++i) {
         int row = tga_inverted ? tga_height -i - 1 : i;
         stbi_uc *tga_row = tga_data + row*tga_width*tga_comp;
         stbi__getn(s, tga_row, tga_width * tga_comp);
      }
   } else  {
      //   do I need to load a palette?
      if ( tga_indexed)
      {
         //   any data to skip? (offset usually = 0)
         stbi__skip(s, tga_palette_start );
         //   load the palette
         tga_palette = (unsigned char*)stbi__malloc( tga_palette_len * tga_comp );
         if (!tga_palette) {
            STBI_FREE(tga_data);
            return stbi__errpuc("outofmem", "Out of memory");
         }
         if (tga_rgb16) {
            stbi_uc *pal_entry = tga_palette;
            STBI_ASSERT(tga_comp == STBI_rgb);
            for (i=0; i < tga_palette_len; ++i) {
               stbi__tga_read_rgb16(s, pal_entry);
               pal_entry += tga_comp;
            }
         } else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) {
               STBI_FREE(tga_data);
               STBI_FREE(tga_palette);
               return stbi__errpuc("bad palette", "Corrupt TGA");
         }
      }
      //   load the data
      for (i=0; i < tga_width * tga_height; ++i)
      {
         //   if I'm in RLE mode, do I need to get a RLE stbi__pngchunk?
         if ( tga_is_RLE )
         {
            if ( RLE_count == 0 )
            {
               //   yep, get the next byte as a RLE command
               int RLE_cmd = stbi__get8(s);
               RLE_count = 1 + (RLE_cmd & 127);
               RLE_repeating = RLE_cmd >> 7;
               read_next_pixel = 1;
            } else if ( !RLE_repeating )
            {
               read_next_pixel = 1;
            }
         } else
         {
            read_next_pixel = 1;
         }
         //   OK, if I need to read a pixel, do it now
         if ( read_next_pixel )
         {
            //   load however much data we did have
            if ( tga_indexed )
            {
               // read in index, then perform the lookup
               int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s);
               if ( pal_idx >= tga_palette_len ) {
                  // invalid index
                  pal_idx = 0;
               }
               pal_idx *= tga_comp;
               for (j = 0; j < tga_comp; ++j) {
                  raw_data[j] = tga_palette[pal_idx+j];
               }
            } else if(tga_rgb16) {
               STBI_ASSERT(tga_comp == STBI_rgb);
               stbi__tga_read_rgb16(s, raw_data);
            } else {
               //   read in the data raw
               for (j = 0; j < tga_comp; ++j) {
                  raw_data[j] = stbi__get8(s);
               }
            }
            //   clear the reading flag for the next pixel
            read_next_pixel = 0;
         } // end of reading a pixel

         // copy data
         for (j = 0; j < tga_comp; ++j)
           tga_data[i*tga_comp+j] = raw_data[j];

         //   in case we're in RLE mode, keep counting down
         --RLE_count;
      }
      //   do I need to invert the image?
      if ( tga_inverted )
      {
         for (j = 0; j*2 < tga_height; ++j)
         {
            int index1 = j * tga_width * tga_comp;
            int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
            for (i = tga_width * tga_comp; i > 0; --i)
            {
               unsigned char temp = tga_data[index1];
               tga_data[index1] = tga_data[index2];
               tga_data[index2] = temp;
               ++index1;
               ++index2;
            }
         }
      }
      //   clear my palette, if I had one
      if ( tga_palette != NULL )
      {
         STBI_FREE( tga_palette );
      }
   }

   // swap RGB - if the source data was RGB16, it already is in the right order
   if (tga_comp >= 3 && !tga_rgb16)
   {
      unsigned char* tga_pixel = tga_data;
      for (i=0; i < tga_width * tga_height; ++i)
      {
         unsigned char temp = tga_pixel[0];
         tga_pixel[0] = tga_pixel[2];
         tga_pixel[2] = temp;
         tga_pixel += tga_comp;
      }
   }

   // convert to target component count
   if (req_comp && req_comp != tga_comp)
      tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);

   //   the things I do to get rid of an error message, and yet keep
   //   Microsoft's C compilers happy... [8^(
   tga_palette_start = tga_palette_len = tga_palette_bits =
         tga_x_origin = tga_y_origin = 0;
   //   OK, done
   return tga_data;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x28370
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
callq 0x28370
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0x28370
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x28370
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
callq 0x28370
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rdi
callq 0x28370
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x64(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0x68(%rbp)
movq $0x0, -0x78(%rbp)
movl $0x0, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl $0x1, -0x90(%rbp)
cmpl $0x8, -0x38(%rbp)
jl 0x2ac1b
movl -0x38(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movl -0x68(%rbp), %ecx
sarl $0x5, %ecx
andl $0x1, %ecx
movl $0x1, %eax
subl %ecx, %eax
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x2ac47
movl -0x48(%rbp), %edi
xorl %esi, %esi
leaq -0x64(%rbp), %rdx
callq 0x35640
movl %eax, -0x60(%rbp)
jmp 0x2ac62
movl -0x5c(%rbp), %edi
cmpl $0x3, -0x38(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %esi
leaq -0x64(%rbp), %rdx
callq 0x35640
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
jne 0x2ac88
leaq 0x1d932(%rip), %rdi     # 0x485a1
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x2b27f
movl -0x54(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x58(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x28(%rbp)
je 0x2acaa
movl -0x60(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movslq -0x54(%rbp), %rdi
movslq -0x58(%rbp), %rax
imulq %rax, %rdi
movslq -0x60(%rbp), %rax
imulq %rax, %rdi
callq 0x27f20
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0x2acee
leaq 0x1d310(%rip), %rdi     # 0x47fe5
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x2b27f
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x2d340
cmpl $0x0, -0x34(%rbp)
jne 0x2ad99
cmpl $0x0, -0x3c(%rbp)
jne 0x2ad99
cmpl $0x0, -0x64(%rbp)
jne 0x2ad99
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x2ad94
cmpl $0x0, -0x68(%rbp)
je 0x2ad3e
movl -0x58(%rbp), %eax
subl -0x7c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0x2ad47
movl -0x7c(%rbp), %eax
movl %eax, -0xd0(%rbp)
movl -0xd0(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x70(%rbp), %rax
movl -0x94(%rbp), %ecx
imull -0x54(%rbp), %ecx
imull -0x60(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movl -0x54(%rbp), %edx
imull -0x60(%rbp), %edx
callq 0x31550
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x2ad1f
jmp 0x2b1aa
cmpl $0x0, -0x34(%rbp)
je 0x2aebc
movq -0x10(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0x2d340
movl -0x44(%rbp), %eax
imull -0x60(%rbp), %eax
movslq %eax, %rdi
callq 0x27f20
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x2adf2
movq -0x70(%rbp), %rdi
callq 0x54c0
leaq 0x1d20c(%rip), %rdi     # 0x47fe5
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x2b27f
cmpl $0x0, -0x64(%rbp)
je 0x2ae6d
movq -0x78(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpl $0x3, -0x60(%rbp)
jne 0x2ae0b
jmp 0x2ae2a
leaq 0x1d827(%rip), %rdi     # 0x48639
leaq 0x1d29c(%rip), %rsi     # 0x480b5
movl $0x13bf, %edx           # imm = 0x13BF
leaq 0x1d829(%rip), %rcx     # 0x4864e
callq 0x52c0
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x2ae6b
movq -0x10(%rbp), %rdi
movq -0xa8(%rbp), %rsi
callq 0x282d0
movl -0x60(%rbp), %ecx
movq -0xa8(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x2ae31
jmp 0x2aeba
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl -0x44(%rbp), %edx
imull -0x60(%rbp), %edx
callq 0x31550
cmpl $0x0, %eax
jne 0x2aeb8
movq -0x70(%rbp), %rdi
callq 0x54c0
movq -0x78(%rbp), %rdi
callq 0x54c0
leaq 0x1d7f2(%rip), %rdi     # 0x48691
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x2b27f
jmp 0x2aeba
jmp 0x2aebc
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl -0x54(%rbp), %ecx
imull -0x58(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2b0c2
cmpl $0x0, -0x3c(%rbp)
je 0x2af3a
cmpl $0x0, -0x88(%rbp)
jne 0x2af23
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
movl -0xac(%rbp), %eax
sarl $0x7, %eax
movl %eax, -0x8c(%rbp)
movl $0x1, -0x90(%rbp)
jmp 0x2af38
cmpl $0x0, -0x8c(%rbp)
jne 0x2af36
movl $0x1, -0x90(%rbp)
jmp 0x2af38
jmp 0x2af44
movl $0x1, -0x90(%rbp)
cmpl $0x0, -0x90(%rbp)
je 0x2b06c
cmpl $0x0, -0x34(%rbp)
je 0x2afef
cmpl $0x8, -0x5c(%rbp)
jne 0x2af75
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0xd4(%rbp)
jmp 0x2af84
movq -0x10(%rbp), %rdi
callq 0x28370
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
cmpl -0x44(%rbp), %eax
jl 0x2afa5
movl $0x0, -0xb0(%rbp)
movl -0x60(%rbp), %eax
imull -0xb0(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x60(%rbp), %eax
jge 0x2afed
movq -0x78(%rbp), %rax
movl -0xb0(%rbp), %ecx
addl -0x80(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movslq -0x80(%rbp), %rax
movb %cl, -0x84(%rbp,%rax)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x2afbc
jmp 0x2b062
cmpl $0x0, -0x64(%rbp)
je 0x2b02e
cmpl $0x3, -0x60(%rbp)
jne 0x2affd
jmp 0x2b01c
leaq 0x1d635(%rip), %rdi     # 0x48639
leaq 0x1d0aa(%rip), %rsi     # 0x480b5
movl $0x13f0, %edx           # imm = 0x13F0
leaq 0x1d637(%rip), %rcx     # 0x4864e
callq 0x52c0
movq -0x10(%rbp), %rdi
leaq -0x84(%rbp), %rsi
callq 0x282d0
jmp 0x2b060
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x60(%rbp), %eax
jge 0x2b05e
movq -0x10(%rbp), %rdi
callq 0x2ce50
movb %al, %cl
movslq -0x80(%rbp), %rax
movb %cl, -0x84(%rbp,%rax)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x2b035
jmp 0x2b060
jmp 0x2b062
movl $0x0, -0x90(%rbp)
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x60(%rbp), %eax
jge 0x2b0a5
movslq -0x80(%rbp), %rax
movb -0x84(%rbp,%rax), %dl
movq -0x70(%rbp), %rax
movl -0x7c(%rbp), %ecx
imull -0x60(%rbp), %ecx
addl -0x80(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x2b073
movl -0x88(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x88(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x2aec3
cmpl $0x0, -0x68(%rbp)
je 0x2b198
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
shll %eax
cmpl -0x58(%rbp), %eax
jge 0x2b196
movl -0x80(%rbp), %eax
imull -0x54(%rbp), %eax
imull -0x60(%rbp), %eax
movl %eax, -0xb4(%rbp)
movl -0x58(%rbp), %eax
subl $0x1, %eax
subl -0x80(%rbp), %eax
imull -0x54(%rbp), %eax
imull -0x60(%rbp), %eax
movl %eax, -0xb8(%rbp)
movl -0x54(%rbp), %eax
imull -0x60(%rbp), %eax
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jle 0x2b186
movq -0x70(%rbp), %rax
movslq -0xb4(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0xb9(%rbp)
movq -0x70(%rbp), %rax
movslq -0xb8(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0x70(%rbp), %rax
movslq -0xb4(%rbp), %rcx
movb %dl, (%rax,%rcx)
movb -0xb9(%rbp), %dl
movq -0x70(%rbp), %rax
movslq -0xb8(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
movl -0x7c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x2b113
jmp 0x2b188
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x2b0d3
jmp 0x2b198
cmpq $0x0, -0x78(%rbp)
je 0x2b1a8
movq -0x78(%rbp), %rdi
callq 0x54c0
jmp 0x2b1aa
cmpl $0x3, -0x60(%rbp)
jl 0x2b22c
cmpl $0x0, -0x64(%rbp)
jne 0x2b22c
movq -0x70(%rbp), %rax
movq %rax, -0xc8(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl -0x54(%rbp), %ecx
imull -0x58(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2b22a
movq -0xc8(%rbp), %rax
movb (%rax), %al
movb %al, -0xc9(%rbp)
movq -0xc8(%rbp), %rax
movb 0x2(%rax), %cl
movq -0xc8(%rbp), %rax
movb %cl, (%rax)
movb -0xc9(%rbp), %cl
movq -0xc8(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0x60(%rbp), %ecx
movq -0xc8(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xc8(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x2b1c8
jmp 0x2b22c
cmpl $0x0, -0x2c(%rbp)
je 0x2b254
movl -0x2c(%rbp), %eax
cmpl -0x60(%rbp), %eax
je 0x2b254
movq -0x70(%rbp), %rdi
movl -0x60(%rbp), %esi
movl -0x2c(%rbp), %edx
movl -0x54(%rbp), %ecx
movl -0x58(%rbp), %r8d
callq 0x30f30
movq %rax, -0x70(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)