// File: /workspace/github_cpp_makefile_star2_O1/srajangarg[P]raytracer/src/core/stb_image.hpp
// Function: stbi__psd_load(stbi__context*, int*, int*, int*, int)
static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
{
   int   pixelCount;
   int channelCount, compression;
   int channel, i, count, len;
   int bitdepth;
   int w,h;
   stbi_uc *out;

   // Check identifier
   if (stbi__get32be(s) != 0x38425053)   // "8BPS"
      return stbi__errpuc("not PSD", "Corrupt PSD image");

   // Check file type version.
   if (stbi__get16be(s) != 1)
      return stbi__errpuc("wrong version", "Unsupported version of PSD image");

   // Skip 6 reserved bytes.
   stbi__skip(s, 6 );

   // Read the number of channels (R, G, B, A, etc).
   channelCount = stbi__get16be(s);
   if (channelCount < 0 || channelCount > 16)
      return stbi__errpuc("wrong channel count", "Unsupported number of channels in PSD image");

   // Read the rows and columns of the image.
   h = stbi__get32be(s);
   w = stbi__get32be(s);

   // Make sure the depth is 8 bits.
   bitdepth = stbi__get16be(s);
   if (bitdepth != 8 && bitdepth != 16)
      return stbi__errpuc("unsupported bit depth", "PSD bit depth is not 8 or 16 bit");

   // Make sure the color mode is RGB.
   // Valid options are:
   //   0: Bitmap
   //   1: Grayscale
   //   2: Indexed color
   //   3: RGB color
   //   4: CMYK color
   //   7: Multichannel
   //   8: Duotone
   //   9: Lab color
   if (stbi__get16be(s) != 3)
      return stbi__errpuc("wrong color format", "PSD is not in RGB color format");

   // Skip the Mode Data.  (It's the palette for indexed color; other info for other modes.)
   stbi__skip(s,stbi__get32be(s) );

   // Skip the image resources.  (resolution, pen tool paths, etc)
   stbi__skip(s, stbi__get32be(s) );

   // Skip the reserved data.
   stbi__skip(s, stbi__get32be(s) );

   // Find out if the data is compressed.
   // Known values:
   //   0: no compression
   //   1: RLE compressed
   compression = stbi__get16be(s);
   if (compression > 1)
      return stbi__errpuc("bad compression", "PSD has an unknown compression format");

   // Create the destination image.
   out = (stbi_uc *) stbi__malloc(4 * w*h);
   if (!out) return stbi__errpuc("outofmem", "Out of memory");
   pixelCount = w*h;

   // Initialize the data to zero.
   //memset( out, 0, pixelCount * 4 );

   // Finally, the image data.
   if (compression) {
      // RLE as used by .PSD and .TIFF
      // Loop until you get the number of unpacked bytes you are expecting:
      //     Read the next source byte into n.
      //     If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
      //     Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.
      //     Else if n is 128, noop.
      // Endloop

      // The RLE-compressed data is preceeded by a 2-byte data count for each row in the data,
      // which we're going to just skip.
      stbi__skip(s, h * channelCount * 2 );

      // Read the RLE data by channel.
      for (channel = 0; channel < 4; channel++) {
         stbi_uc *p;

         p = out+channel;
         if (channel >= channelCount) {
            // Fill this channel with default data.
            for (i = 0; i < pixelCount; i++, p += 4)
               *p = (channel == 3 ? 255 : 0);
         } else {
            // Read the RLE data.
            count = 0;
            while (count < pixelCount) {
               len = stbi__get8(s);
               if (len == 128) {
                  // No-op.
               } else if (len < 128) {
                  // Copy next len+1 bytes literally.
                  len++;
                  count += len;
                  while (len) {
                     *p = stbi__get8(s);
                     p += 4;
                     len--;
                  }
               } else if (len > 128) {
                  stbi_uc   val;
                  // Next -len+1 bytes in the dest are replicated from next source byte.
                  // (Interpret len as a negative 8-bit int.)
                  len ^= 0x0FF;
                  len += 2;
                  val = stbi__get8(s);
                  count += len;
                  while (len) {
                     *p = val;
                     p += 4;
                     len--;
                  }
               }
            }
         }
      }

   } else {
      // We're at the raw image data.  It's each channel in order (Red, Green, Blue, Alpha, ...)
      // where each channel consists of an 8-bit value for each pixel in the image.

      // Read the data by channel.
      for (channel = 0; channel < 4; channel++) {
         stbi_uc *p;

         p = out + channel;
         if (channel >= channelCount) {
            // Fill this channel with default data.
            stbi_uc val = channel == 3 ? 255 : 0;
            for (i = 0; i < pixelCount; i++, p += 4)
               *p = val;
         } else {
            // Read the data.
            if (bitdepth == 16) {
               for (i = 0; i < pixelCount; i++, p += 4)
                  *p = (stbi_uc) (stbi__get16be(s) >> 8);
            } else {
               for (i = 0; i < pixelCount; i++, p += 4)
                  *p = stbi__get8(s);
            }
         }
      }
   }

   if (req_comp && req_comp != 4) {
      out = stbi__convert_format(out, 4, req_comp, w, h);
      if (out == NULL) return out; // stbi__convert_format frees input on failure
   }

   if (comp) *comp = 4;
   *y = h;
   *x = w;

   return out;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r8d, %r14d
movq %rcx, %r15
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, %r12
callq 0x1e01c
movl %eax, %ebx
shll $0x10, %ebx
movq %r12, %rdi
callq 0x1e01c
orl %ebx, %eax
cmpl $0x38425053, %eax       # imm = 0x38425053
jne 0x1a1ce
movq %r12, %rdi
callq 0x1e01c
cmpl $0x1, %eax
jne 0x1a1da
cmpq $0x0, 0x10(%r12)
movl %r14d, 0x4c(%rsp)
je 0x1a1e6
movq 0xc0(%r12), %rcx
movl 0xb8(%r12), %edx
movl %ecx, %eax
subl %edx, %eax
cmpl $0x5, %eax
jg 0x1a1e6
movq %rcx, 0xb8(%r12)
movq 0x28(%r12), %rdi
movl $0x6, %esi
subl %eax, %esi
callq *0x18(%r12)
jmp 0x1a1ef
leaq 0x1357a(%rip), %rax     # 0x2d74f
jmp 0x1a2f9
leaq 0x13576(%rip), %rax     # 0x2d757
jmp 0x1a2f9
addq $0x6, 0xb8(%r12)
movq %r12, %rdi
callq 0x1e01c
cmpl $0x11, %eax
jb 0x1a208
leaq 0x13562(%rip), %rax     # 0x2d765
jmp 0x1a2f9
movl %eax, %r14d
movq %r12, %rdi
callq 0x1e01c
movq %rax, 0x8(%rsp)
movq %r12, %rdi
callq 0x1e01c
movq %rax, 0x20(%rsp)
movq %r12, %rdi
callq 0x1e01c
movq %rax, (%rsp)
movq %r12, %rdi
callq 0x1e01c
movq %rax, 0x40(%rsp)
movq %r12, %rdi
callq 0x1e01c
movl %eax, %ebx
cmpl $0x8, %eax
je 0x1a256
cmpl $0x10, %ebx
jne 0x1a2f2
movq %r12, %rdi
callq 0x1e01c
cmpl $0x3, %eax
jne 0x1a2e9
movl %ebx, 0x48(%rsp)
movq %r15, 0x60(%rsp)
movq %r12, %rdi
callq 0x1e01c
movl %eax, %ebx
shll $0x10, %ebx
movq %r12, %rdi
callq 0x1e01c
orl %eax, %ebx
movq %r12, %rdi
movl %ebx, %esi
callq 0x1e25e
movq %r12, %rdi
callq 0x1e01c
movl %eax, %ebx
shll $0x10, %ebx
movq %r12, %rdi
callq 0x1e01c
orl %eax, %ebx
movq %r12, %rdi
movl %ebx, %esi
callq 0x1e25e
movq %r12, %rdi
callq 0x1e01c
movl %eax, %ebx
shll $0x10, %ebx
movq %r12, %rdi
callq 0x1e01c
orl %eax, %ebx
movq %r12, %rdi
movl %ebx, %esi
callq 0x1e25e
movq %r12, %rdi
callq 0x1e01c
cmpl $0x2, %eax
jb 0x1a311
leaq 0x134bb(%rip), %rax     # 0x2d7a2
jmp 0x1a2f9
leaq 0x1349f(%rip), %rax     # 0x2d78f
jmp 0x1a2f9
leaq 0x13480(%rip), %rax     # 0x2d779
movq %rax, 0x1e0b0(%rip)     # 0x383b0
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %r15d
movq 0x8(%rsp), %rcx
shll $0x10, %ecx
movq 0x20(%rsp), %rax
movq %rcx, 0x8(%rsp)
addl %ecx, %eax
movq (%rsp), %rdx
shll $0x10, %edx
movq 0x40(%rsp), %rcx
movq %rdx, (%rsp)
addl %edx, %ecx
movl %eax, 0x14(%rsp)
movl %ecx, 0x34(%rsp)
imull %ecx, %eax
movq %rax, 0x18(%rsp)
leal (,%rax,4), %eax
movslq %eax, %rdi
callq 0x4400
testq %rax, %rax
je 0x1a621
movq %rax, %rbx
movq %rbp, 0x50(%rsp)
movq %r13, 0x58(%rsp)
movl %r14d, %eax
movq %rax, 0x68(%rsp)
testl %r15d, %r15d
movq %rbx, 0x38(%rsp)
je 0x1a62d
imull 0x14(%rsp), %r14d
addl %r14d, %r14d
movq %r12, %rdi
movl %r14d, %esi
callq 0x1e25e
leaq 0x38(%r12), %r14
leaq 0x39(%r12), %rax
movq %rax, 0x28(%rsp)
movq 0x40(%rsp), %rcx
addl (%rsp), %ecx
movq 0x20(%rsp), %rax
addl 0x8(%rsp), %eax
imull %ecx, %eax
movq %rax, 0x20(%rsp)
xorl %eax, %eax
movq %rbx, %rbp
movq %rax, (%rsp)
cmpq 0x68(%rsp), %rax
jae 0x1a5e5
movq 0x18(%rsp), %r15
testl %r15d, %r15d
jle 0x1a608
movq 0x38(%rsp), %rax
movq (%rsp), %rcx
leaq (%rax,%rcx), %rbx
xorl %r13d, %r13d
movq %rbp, 0x8(%rsp)
movq 0xb8(%r12), %rax
cmpq 0xc0(%r12), %rax
jb 0x1a46f
cmpl $0x0, 0x30(%r12)
je 0x1a43f
movq 0x28(%r12), %rdi
movl 0x34(%r12), %edx
movq %r14, %rsi
callq *0x10(%r12)
testl %eax, %eax
je 0x1a443
movq %r14, 0xb8(%r12)
cltq
addq %r14, %rax
movq %rax, 0xc0(%r12)
jmp 0x1a467
xorl %eax, %eax
jmp 0x1a47d
movl $0x0, 0x30(%r12)
movq %r14, 0xb8(%r12)
movq 0x28(%rsp), %rax
movq %rax, 0xc0(%r12)
movb $0x0, 0x38(%r12)
movq 0xb8(%r12), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0xb8(%r12)
movb (%rax), %al
cmpb $-0x80, %al
je 0x1a5da
movzbl %al, %ebp
testb %al, %al
js 0x1a536
incl %ebp
movl %ebp, %r15d
movq 0xb8(%r12), %rax
cmpq 0xc0(%r12), %rax
jb 0x1a50c
cmpl $0x0, 0x30(%r12)
je 0x1a4dc
movq 0x28(%r12), %rdi
movl 0x34(%r12), %edx
movq %r14, %rsi
callq *0x10(%r12)
testl %eax, %eax
je 0x1a4e0
movq %r14, 0xb8(%r12)
cltq
addq %r14, %rax
movq %rax, 0xc0(%r12)
jmp 0x1a504
xorl %eax, %eax
jmp 0x1a51a
movl $0x0, 0x30(%r12)
movq %r14, 0xb8(%r12)
movq 0x28(%rsp), %rax
movq %rax, 0xc0(%r12)
movb $0x0, 0x38(%r12)
movq 0xb8(%r12), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0xb8(%r12)
movb (%rax), %al
movb %al, (%rbx)
addq $0x4, %rbx
decl %r15d
jne 0x1a495
addl %ebp, %r13d
movq 0x18(%rsp), %r15
jmp 0x1a5d5
movq 0xb8(%r12), %rax
cmpq 0xc0(%r12), %rax
jb 0x1a5ad
cmpl $0x0, 0x30(%r12)
je 0x1a57d
movq 0x28(%r12), %rdi
movl 0x34(%r12), %edx
movq %r14, %rsi
callq *0x10(%r12)
testl %eax, %eax
je 0x1a581
movq %r14, 0xb8(%r12)
cltq
addq %r14, %rax
movq %rax, 0xc0(%r12)
jmp 0x1a5a5
xorl %eax, %eax
jmp 0x1a5bb
movl $0x0, 0x30(%r12)
movq %r14, 0xb8(%r12)
movq 0x28(%rsp), %rax
movq %rax, 0xc0(%r12)
movb $0x0, 0x38(%r12)
movq 0xb8(%r12), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0xb8(%r12)
movb (%rax), %al
movl $0x101, %ecx            # imm = 0x101
subl %ebp, %ecx
addl $0xfffffeff, %ebp       # imm = 0xFFFFFEFF
movb %al, (%rbx)
addq $0x4, %rbx
incl %ebp
jne 0x1a5c8
addl %ecx, %r13d
movq 0x8(%rsp), %rbp
cmpl %r15d, %r13d
jl 0x1a3f8
jmp 0x1a608
cmpl $0x0, 0x18(%rsp)
movq 0x20(%rsp), %rdx
jle 0x1a608
cmpq $0x3, (%rsp)
sete %al
negb %al
xorl %ecx, %ecx
movb %al, (%rbp,%rcx,4)
incq %rcx
cmpl %ecx, %edx
jne 0x1a5fd
movq (%rsp), %rax
incq %rax
incq %rbp
cmpq $0x4, %rax
jne 0x1a3c6
jmp 0x1a779
leaq 0x12be1(%rip), %rax     # 0x2d209
jmp 0x1a2f9
leaq 0x38(%r12), %rax
movq %rax, 0x28(%rsp)
leaq 0x39(%r12), %rax
movq %rax, 0x70(%rsp)
movq 0x40(%rsp), %rcx
addl (%rsp), %ecx
movq 0x20(%rsp), %r14
addl 0x8(%rsp), %r14d
imull %ecx, %r14d
xorl %r15d, %r15d
movq %rbx, %rbp
cmpq 0x68(%rsp), %r15
jae 0x1a694
cmpl $0x10, 0x48(%rsp)
jne 0x1a6bd
cmpl $0x0, 0x18(%rsp)
jle 0x1a769
movq %r14, %r13
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x1e01c
movb %ah, (%rbp,%rbx,4)
incq %rbx
cmpl %ebx, %r13d
jne 0x1a67b
jmp 0x1a769
cmpq $0x3, %r15
sete %al
cmpl $0x0, 0x18(%rsp)
jle 0x1a769
movq %r14, %rdx
negb %al
xorl %ecx, %ecx
movb %al, (%rbp,%rcx,4)
incq %rcx
cmpl %ecx, %edx
jne 0x1a6ad
jmp 0x1a769
cmpl $0x0, 0x18(%rsp)
jle 0x1a769
xorl %r13d, %r13d
movq 0xb8(%r12), %rax
cmpq 0xc0(%r12), %rax
jb 0x1a747
cmpl $0x0, 0x30(%r12)
je 0x1a717
movq 0x28(%r12), %rdi
movl 0x34(%r12), %edx
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
callq *0x10(%r12)
testl %eax, %eax
je 0x1a71b
movq %rbx, 0xb8(%r12)
cltq
addq %rbx, %rax
movq %rax, 0xc0(%r12)
jmp 0x1a73f
xorl %eax, %eax
jmp 0x1a755
movl $0x0, 0x30(%r12)
movq %rbx, 0xb8(%r12)
movq 0x70(%rsp), %rax
movq %rax, 0xc0(%r12)
movb $0x0, 0x38(%r12)
movq 0xb8(%r12), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0xb8(%r12)
movb (%rax), %al
movb %al, (%rbp,%r13,4)
incq %r13
movq %r14, %rax
cmpl %r13d, %eax
jne 0x1a6cb
incq %r15
incq %rbp
cmpq $0x4, %r15
jne 0x1a65d
movl 0x4c(%rsp), %edx
testl $0xfffffffb, %edx       # imm = 0xFFFFFFFB
je 0x1a7b8
movq 0x38(%rsp), %rdi
movl $0x4, %esi
movl 0x34(%rsp), %ebx
movl %ebx, %ecx
movl 0x14(%rsp), %r8d
callq 0x2003d
testq %rax, %rax
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rcx
movq 0x60(%rsp), %rdx
jne 0x1a7d0
jmp 0x1a300
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rcx
movq 0x60(%rsp), %rdx
movq 0x38(%rsp), %rax
movl 0x34(%rsp), %ebx
testq %rdx, %rdx
je 0x1a7db
movl $0x4, (%rdx)
movl 0x14(%rsp), %edx
movl %edx, (%rcx)
movl %ebx, (%rsi)
jmp 0x1a302