// File: srajangarg[P]raytracer/src/core/stb_image.hpp
// Function: stbi__hdr_load(stbi__context*, int*, int*, int*, int)
static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
{
   char buffer[STBI__HDR_BUFLEN];
   char *token;
   int valid = 0;
   int width, height;
   stbi_uc *scanline;
   float *hdr_data;
   int len;
   unsigned char count, value;
   int i, j, k, c1,c2, z;


   // Check identifier
   if (strcmp(stbi__hdr_gettoken(s,buffer), "#?RADIANCE") != 0)
      return stbi__errpf("not HDR", "Corrupt HDR image");

   // Parse header
   for(;;) {
      token = stbi__hdr_gettoken(s,buffer);
      if (token[0] == 0) break;
      if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
   }

   if (!valid)    return stbi__errpf("unsupported format", "Unsupported HDR format");

   // Parse width and height
   // can't use sscanf() if we're not using stdio!
   token = stbi__hdr_gettoken(s,buffer);
   if (strncmp(token, "-Y ", 3))  return stbi__errpf("unsupported data layout", "Unsupported HDR format");
   token += 3;
   height = (int) strtol(token, &token, 10);
   while (*token == ' ') ++token;
   if (strncmp(token, "+X ", 3))  return stbi__errpf("unsupported data layout", "Unsupported HDR format");
   token += 3;
   width = (int) strtol(token, NULL, 10);

   *x = width;
   *y = height;

   if (comp) *comp = 3;
   if (req_comp == 0) req_comp = 3;

   // Read data
   hdr_data = (float *) stbi__malloc(height * width * req_comp * sizeof(float));

   // Load image data
   // image data is stored as some number of sca
   if ( width < 8 || width >= 32768) {
      // Read flat data
      for (j=0; j < height; ++j) {
         for (i=0; i < width; ++i) {
            stbi_uc rgbe[4];
           main_decode_loop:
            stbi__getn(s, rgbe, 4);
            stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
         }
      }
   } else {
      // Read RLE-encoded data
      scanline = NULL;

      for (j = 0; j < height; ++j) {
         c1 = stbi__get8(s);
         c2 = stbi__get8(s);
         len = stbi__get8(s);
         if (c1 != 2 || c2 != 2 || (len & 0x80)) {
            // not run-length encoded, so we have to actually use THIS data as a decoded
            // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
            stbi_uc rgbe[4];
            rgbe[0] = (stbi_uc) c1;
            rgbe[1] = (stbi_uc) c2;
            rgbe[2] = (stbi_uc) len;
            rgbe[3] = (stbi_uc) stbi__get8(s);
            stbi__hdr_convert(hdr_data, rgbe, req_comp);
            i = 1;
            j = 0;
            STBI_FREE(scanline);
            goto main_decode_loop; // yes, this makes no sense
         }
         len <<= 8;
         len |= stbi__get8(s);
         if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); }
         if (scanline == NULL) scanline = (stbi_uc *) stbi__malloc(width * 4);

         for (k = 0; k < 4; ++k) {
            i = 0;
            while (i < width) {
               count = stbi__get8(s);
               if (count > 128) {
                  // Run
                  value = stbi__get8(s);
                  count -= 128;
                  for (z = 0; z < count; ++z)
                     scanline[i++ * 4 + k] = value;
               } else {
                  // Dump
                  for (z = 0; z < count; ++z)
                     scanline[i++ * 4 + k] = stbi__get8(s);
               }
            }
         }
         for (i=0; i < width; ++i)
            stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
      }
      STBI_FREE(scanline);
   }

   return hdr_data;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x480, %rsp            # imm = 0x480
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl $0x0, -0x43c(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x430(%rbp), %rsi
callq 0x353e0
movq %rax, %rdi
leaq 0x1e449(%rip), %rsi     # 0x485bc
callq 0x55b0
cmpl $0x0, %eax
je 0x2a19d
leaq 0x1e443(%rip), %rdi     # 0x485c7
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x2a7af
jmp 0x2a19f
movq -0x10(%rbp), %rdi
leaq -0x430(%rbp), %rsi
callq 0x353e0
movq %rax, -0x438(%rbp)
movq -0x438(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x2a1c7
jmp 0x2a1eb
movq -0x438(%rbp), %rdi
leaq 0x1e3fa(%rip), %rsi     # 0x485cf
callq 0x55b0
cmpl $0x0, %eax
jne 0x2a1e9
movl $0x1, -0x43c(%rbp)
jmp 0x2a19f
cmpl $0x0, -0x43c(%rbp)
jne 0x2a214
leaq 0x1e3eb(%rip), %rdi     # 0x485e6
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x2a7af
movq -0x10(%rbp), %rdi
leaq -0x430(%rbp), %rsi
callq 0x353e0
movq %rax, -0x438(%rbp)
movq -0x438(%rbp), %rdi
leaq 0x1e3c0(%rip), %rsi     # 0x485f9
movl $0x3, %edx
callq 0x5200
cmpl $0x0, %eax
je 0x2a268
leaq 0x1e3ae(%rip), %rdi     # 0x485fd
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x2a7af
movq -0x438(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x438(%rbp)
movq -0x438(%rbp), %rdi
leaq -0x438(%rbp), %rsi
movl $0xa, %edx
callq 0x55c0
movl %eax, -0x444(%rbp)
movq -0x438(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x2a2bb
movq -0x438(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x438(%rbp)
jmp 0x2a298
movq -0x438(%rbp), %rdi
leaq 0x1e34c(%rip), %rsi     # 0x48615
movl $0x3, %edx
callq 0x5200
cmpl $0x0, %eax
je 0x2a2f8
leaq 0x1e31e(%rip), %rdi     # 0x485fd
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x2a7af
movq -0x438(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x438(%rbp)
movq -0x438(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x55c0
movl %eax, -0x440(%rbp)
movl -0x440(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x444(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x28(%rbp)
je 0x2a34e
movq -0x28(%rbp), %rax
movl $0x3, (%rax)
cmpl $0x0, -0x2c(%rbp)
jne 0x2a35b
movl $0x3, -0x2c(%rbp)
movl -0x444(%rbp), %eax
imull -0x440(%rbp), %eax
imull -0x2c(%rbp), %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x27f20
movq %rax, -0x458(%rbp)
cmpl $0x8, -0x440(%rbp)
jl 0x2a398
cmpl $0x8000, -0x440(%rbp)   # imm = 0x8000
jl 0x2a452
movl $0x0, -0x468(%rbp)
movl -0x468(%rbp), %eax
cmpl -0x444(%rbp), %eax
jge 0x2a44d
movl $0x0, -0x464(%rbp)
movl -0x464(%rbp), %eax
cmpl -0x440(%rbp), %eax
jge 0x2a437
jmp 0x2a3ce
movq -0x10(%rbp), %rdi
leaq -0x47c(%rbp), %rsi
movl $0x4, %edx
callq 0x31550
movq -0x458(%rbp), %rdi
movl -0x468(%rbp), %eax
imull -0x440(%rbp), %eax
imull -0x2c(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
movl -0x464(%rbp), %eax
imull -0x2c(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
leaq -0x47c(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0x354c0
movl -0x464(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x464(%rbp)
jmp 0x2a3be
jmp 0x2a439
movl -0x468(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x468(%rbp)
jmp 0x2a3a2
jmp 0x2a7a4
movq $0x0, -0x450(%rbp)
movl $0x0, -0x468(%rbp)
movl -0x468(%rbp), %eax
cmpl -0x444(%rbp), %eax
jge 0x2a798
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0x470(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0x474(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
movl %eax, -0x45c(%rbp)
cmpl $0x2, -0x470(%rbp)
jne 0x2a4d1
cmpl $0x2, -0x474(%rbp)
jne 0x2a4d1
movl -0x45c(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x2a53f
movl -0x470(%rbp), %eax
movb %al, -0x480(%rbp)
movl -0x474(%rbp), %eax
movb %al, -0x47f(%rbp)
movl -0x45c(%rbp), %eax
movb %al, -0x47e(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movb %al, -0x47d(%rbp)
movq -0x458(%rbp), %rdi
leaq -0x480(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0x354c0
movl $0x1, -0x464(%rbp)
movl $0x0, -0x468(%rbp)
movq -0x450(%rbp), %rdi
callq 0x54c0
jmp 0x2a3ce
movl -0x45c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x45c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ce50
movzbl %al, %eax
orl -0x45c(%rbp), %eax
movl %eax, -0x45c(%rbp)
movl -0x45c(%rbp), %eax
cmpl -0x440(%rbp), %eax
je 0x2a5ac
movq -0x458(%rbp), %rdi
callq 0x54c0
movq -0x450(%rbp), %rdi
callq 0x54c0
leaq 0x1e086(%rip), %rdi     # 0x48619
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x2a7af
cmpq $0x0, -0x450(%rbp)
jne 0x2a5ce
movl -0x440(%rbp), %eax
shll $0x2, %eax
movslq %eax, %rdi
callq 0x27f20
movq %rax, -0x450(%rbp)
movl $0x0, -0x46c(%rbp)
cmpl $0x4, -0x46c(%rbp)
jge 0x2a715
movl $0x0, -0x464(%rbp)
movl -0x464(%rbp), %eax
cmpl -0x440(%rbp), %eax
jge 0x2a6ff
movq -0x10(%rbp), %rdi
callq 0x2ce50
movb %al, -0x45d(%rbp)
movzbl -0x45d(%rbp), %eax
cmpl $0x80, %eax
jle 0x2a69a
movq -0x10(%rbp), %rdi
callq 0x2ce50
movb %al, -0x45e(%rbp)
movzbl -0x45d(%rbp), %eax
subl $0x80, %eax
movb %al, -0x45d(%rbp)
movl $0x0, -0x478(%rbp)
movl -0x478(%rbp), %eax
movzbl -0x45d(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2a698
movb -0x45e(%rbp), %dl
movq -0x450(%rbp), %rax
movl -0x464(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x464(%rbp)
shll $0x2, %ecx
addl -0x46c(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x478(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x478(%rbp)
jmp 0x2a649
jmp 0x2a6fa
movl $0x0, -0x478(%rbp)
movl -0x478(%rbp), %eax
movzbl -0x45d(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2a6f8
movq -0x10(%rbp), %rdi
callq 0x2ce50
movb %al, %dl
movq -0x450(%rbp), %rax
movl -0x464(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x464(%rbp)
shll $0x2, %ecx
addl -0x46c(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x478(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x478(%rbp)
jmp 0x2a6a4
jmp 0x2a6fa
jmp 0x2a5ef
jmp 0x2a701
movl -0x46c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x46c(%rbp)
jmp 0x2a5d8
movl $0x0, -0x464(%rbp)
movl -0x464(%rbp), %eax
cmpl -0x440(%rbp), %eax
jge 0x2a782
movq -0x458(%rbp), %rdi
movl -0x468(%rbp), %eax
imull -0x440(%rbp), %eax
addl -0x464(%rbp), %eax
imull -0x2c(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
movq -0x450(%rbp), %rsi
movl -0x464(%rbp), %eax
shll $0x2, %eax
cltq
addq %rax, %rsi
movl -0x2c(%rbp), %edx
callq 0x354c0
movl -0x464(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x464(%rbp)
jmp 0x2a71f
jmp 0x2a784
movl -0x468(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x468(%rbp)
jmp 0x2a467
movq -0x450(%rbp), %rdi
callq 0x54c0
movq -0x458(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x480, %rsp            # imm = 0x480
popq %rbp
retq
nopl (%rax)