// File: alan0201tw[P]RayTracer/include/stb_image.h
// Function: stbi__hdr_load(stbi__context*, int*, int*, int*, int, stbi__result_info*)
static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
{
   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;
   const char *headerToken;
   STBI_NOTUSED(ri);

   // Check identifier
   headerToken = stbi__hdr_gettoken(s,buffer);
   if (strcmp(headerToken, "#?RADIANCE") != 0 && strcmp(headerToken, "#?RGBE") != 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;

   if (!stbi__mad4sizes_valid(width, height, req_comp, sizeof(float), 0))
      return stbi__errpf("too large", "HDR image is too large");

   // Read data
   hdr_data = (float *) stbi__malloc_mad4(width, height, req_comp, sizeof(float), 0);
   if (!hdr_data)
      return stbi__errpf("outofmem", "Out of memory");

   // 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_mad2(width, 4, 0);
            if (!scanline) {
               STBI_FREE(hdr_data);
               return stbi__errpf("outofmem", "Out of memory");
            }
         }

         for (k = 0; k < 4; ++k) {
            int nleft;
            i = 0;
            while ((nleft = width - i) > 0) {
               count = stbi__get8(s);
               if (count > 128) {
                  // Run
                  value = stbi__get8(s);
                  count -= 128;
                  if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
                  for (z = 0; z < count; ++z)
                     scanline[i++ * 4 + k] = value;
               } else {
                  // Dump
                  if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
                  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);
      }
      if (scanline)
         STBI_FREE(scanline);
   }

   return hdr_data;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4a0, %rsp            # imm = 0x4A0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq %r9, -0x38(%rbp)
movl $0x0, -0x44c(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x6f900
movq %rax, -0x490(%rbp)
movq -0x490(%rbp), %rdi
leaq 0x24165(%rip), %rsi     # 0x817b7
callq 0x76a0
cmpl $0x0, %eax
je 0x5d694
movq -0x490(%rbp), %rdi
leaq 0x24158(%rip), %rsi     # 0x817c2
callq 0x76a0
cmpl $0x0, %eax
je 0x5d694
leaq 0x2414e(%rip), %rdi     # 0x817c9
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x5ddf3
jmp 0x5d696
movq -0x10(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x6f900
movq %rax, -0x448(%rbp)
movq -0x448(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x5d6be
jmp 0x5d6e2
movq -0x448(%rbp), %rdi
leaq 0x24105(%rip), %rsi     # 0x817d1
callq 0x76a0
cmpl $0x0, %eax
jne 0x5d6e0
movl $0x1, -0x44c(%rbp)
jmp 0x5d696
cmpl $0x0, -0x44c(%rbp)
jne 0x5d70b
leaq 0x240f6(%rip), %rdi     # 0x817e8
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x5ddf3
movq -0x10(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x6f900
movq %rax, -0x448(%rbp)
movq -0x448(%rbp), %rdi
leaq 0x240cb(%rip), %rsi     # 0x817fb
movl $0x3, %edx
callq 0x7240
cmpl $0x0, %eax
je 0x5d75f
leaq 0x240b9(%rip), %rdi     # 0x817ff
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x5ddf3
movq -0x448(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x448(%rbp)
movq -0x448(%rbp), %rdi
leaq -0x448(%rbp), %rsi
movl $0xa, %edx
callq 0x76c0
movl %eax, -0x454(%rbp)
movq -0x448(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x5d7b2
movq -0x448(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x448(%rbp)
jmp 0x5d78f
movq -0x448(%rbp), %rdi
leaq 0x24057(%rip), %rsi     # 0x81817
movl $0x3, %edx
callq 0x7240
cmpl $0x0, %eax
je 0x5d7ef
leaq 0x24029(%rip), %rdi     # 0x817ff
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x5ddf3
movq -0x448(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x448(%rbp)
movq -0x448(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x76c0
movl %eax, -0x450(%rbp)
movl -0x450(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x454(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x28(%rbp)
je 0x5d845
movq -0x28(%rbp), %rax
movl $0x3, (%rax)
cmpl $0x0, -0x2c(%rbp)
jne 0x5d852
movl $0x3, -0x2c(%rbp)
movl -0x450(%rbp), %edi
movl -0x454(%rbp), %esi
movl -0x2c(%rbp), %edx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0x6f9e0
cmpl $0x0, %eax
jne 0x5d893
leaq 0x2387e(%rip), %rdi     # 0x810f8
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x5ddf3
movl -0x450(%rbp), %edi
movl -0x454(%rbp), %esi
movl -0x2c(%rbp), %edx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0x6fa80
movq %rax, -0x468(%rbp)
cmpq $0x0, -0x468(%rbp)
jne 0x5d8e0
leaq 0x2383b(%rip), %rdi     # 0x81102
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x5ddf3
cmpl $0x8, -0x450(%rbp)
jl 0x5d8f9
cmpl $0x8000, -0x450(%rbp)   # imm = 0x8000
jl 0x5d9b3
movl $0x0, -0x478(%rbp)
movl -0x478(%rbp), %eax
cmpl -0x454(%rbp), %eax
jge 0x5d9ae
movl $0x0, -0x474(%rbp)
movl -0x474(%rbp), %eax
cmpl -0x450(%rbp), %eax
jge 0x5d998
jmp 0x5d92f
movq -0x10(%rbp), %rdi
leaq -0x494(%rbp), %rsi
movl $0x4, %edx
callq 0x6b450
movq -0x468(%rbp), %rdi
movl -0x478(%rbp), %eax
imull -0x450(%rbp), %eax
imull -0x2c(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
movl -0x474(%rbp), %eax
imull -0x2c(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
leaq -0x494(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0x6faf0
movl -0x474(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x474(%rbp)
jmp 0x5d91f
jmp 0x5d99a
movl -0x478(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x478(%rbp)
jmp 0x5d903
jmp 0x5dde8
movq $0x0, -0x460(%rbp)
movl $0x0, -0x478(%rbp)
movl -0x478(%rbp), %eax
cmpl -0x454(%rbp), %eax
jge 0x5ddd0
movq -0x10(%rbp), %rdi
callq 0x65d00
movzbl %al, %eax
movl %eax, -0x480(%rbp)
movq -0x10(%rbp), %rdi
callq 0x65d00
movzbl %al, %eax
movl %eax, -0x484(%rbp)
movq -0x10(%rbp), %rdi
callq 0x65d00
movzbl %al, %eax
movl %eax, -0x46c(%rbp)
cmpl $0x2, -0x480(%rbp)
jne 0x5da32
cmpl $0x2, -0x484(%rbp)
jne 0x5da32
movl -0x46c(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x5daa0
movl -0x480(%rbp), %eax
movb %al, -0x498(%rbp)
movl -0x484(%rbp), %eax
movb %al, -0x497(%rbp)
movl -0x46c(%rbp), %eax
movb %al, -0x496(%rbp)
movq -0x10(%rbp), %rdi
callq 0x65d00
movb %al, -0x495(%rbp)
movq -0x468(%rbp), %rdi
leaq -0x498(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0x6faf0
movl $0x1, -0x474(%rbp)
movl $0x0, -0x478(%rbp)
movq -0x460(%rbp), %rdi
callq 0x75e0
jmp 0x5d92f
movl -0x46c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x46c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x65d00
movzbl %al, %eax
orl -0x46c(%rbp), %eax
movl %eax, -0x46c(%rbp)
movl -0x46c(%rbp), %eax
cmpl -0x450(%rbp), %eax
je 0x5db0d
movq -0x468(%rbp), %rdi
callq 0x75e0
movq -0x460(%rbp), %rdi
callq 0x75e0
leaq 0x23d27(%rip), %rdi     # 0x8181b
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x5ddf3
cmpq $0x0, -0x460(%rbp)
jne 0x5db68
movl -0x450(%rbp), %edi
movl $0x4, %esi
xorl %edx, %edx
callq 0x66310
movq %rax, -0x460(%rbp)
cmpq $0x0, -0x460(%rbp)
jne 0x5db66
movq -0x468(%rbp), %rdi
callq 0x75e0
leaq 0x235b5(%rip), %rdi     # 0x81102
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x5ddf3
jmp 0x5db68
movl $0x0, -0x47c(%rbp)
cmpl $0x4, -0x47c(%rbp)
jge 0x5dd4d
movl $0x0, -0x474(%rbp)
movl -0x450(%rbp), %eax
subl -0x474(%rbp), %eax
movl %eax, -0x49c(%rbp)
cmpl $0x0, %eax
jle 0x5dd37
movq -0x10(%rbp), %rdi
callq 0x65d00
movb %al, -0x46d(%rbp)
movzbl -0x46d(%rbp), %eax
cmpl $0x80, %eax
jle 0x5dc8b
movq -0x10(%rbp), %rdi
callq 0x65d00
movb %al, -0x46e(%rbp)
movzbl -0x46d(%rbp), %eax
subl $0x80, %eax
movb %al, -0x46d(%rbp)
movzbl -0x46d(%rbp), %eax
cmpl -0x49c(%rbp), %eax
jle 0x5dc2d
movq -0x468(%rbp), %rdi
callq 0x75e0
movq -0x460(%rbp), %rdi
callq 0x75e0
leaq 0x23dbe(%rip), %rdi     # 0x819d2
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x5ddf3
movl $0x0, -0x488(%rbp)
movl -0x488(%rbp), %eax
movzbl -0x46d(%rbp), %ecx
cmpl %ecx, %eax
jge 0x5dc86
movb -0x46e(%rbp), %dl
movq -0x460(%rbp), %rax
movl -0x474(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x474(%rbp)
shll $0x2, %ecx
addl -0x47c(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x488(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x488(%rbp)
jmp 0x5dc37
jmp 0x5dd32
movzbl -0x46d(%rbp), %eax
cmpl -0x49c(%rbp), %eax
jle 0x5dcd2
movq -0x468(%rbp), %rdi
callq 0x75e0
movq -0x460(%rbp), %rdi
callq 0x75e0
leaq 0x23d19(%rip), %rdi     # 0x819d2
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x5ddf3
movl $0x0, -0x488(%rbp)
movl -0x488(%rbp), %eax
movzbl -0x46d(%rbp), %ecx
cmpl %ecx, %eax
jge 0x5dd30
movq -0x10(%rbp), %rdi
callq 0x65d00
movb %al, %dl
movq -0x460(%rbp), %rax
movl -0x474(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x474(%rbp)
shll $0x2, %ecx
addl -0x47c(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x488(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x488(%rbp)
jmp 0x5dcdc
jmp 0x5dd32
jmp 0x5db89
jmp 0x5dd39
movl -0x47c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x47c(%rbp)
jmp 0x5db72
movl $0x0, -0x474(%rbp)
movl -0x474(%rbp), %eax
cmpl -0x450(%rbp), %eax
jge 0x5ddba
movq -0x468(%rbp), %rdi
movl -0x478(%rbp), %eax
imull -0x450(%rbp), %eax
addl -0x474(%rbp), %eax
imull -0x2c(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
movq -0x460(%rbp), %rsi
movl -0x474(%rbp), %eax
shll $0x2, %eax
cltq
addq %rax, %rsi
movl -0x2c(%rbp), %edx
callq 0x6faf0
movl -0x474(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x474(%rbp)
jmp 0x5dd57
jmp 0x5ddbc
movl -0x478(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x478(%rbp)
jmp 0x5d9c8
cmpq $0x0, -0x460(%rbp)
je 0x5dde6
movq -0x460(%rbp), %rdi
callq 0x75e0
jmp 0x5dde8
movq -0x468(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x4a0, %rsp            # imm = 0x4A0
popq %rbp
retq