// File: srajangarg[P]raytracer/src/core/stb_image_write.hpp
// Function: stbiw__write_hdr_scanline(stbi__write_context*, int, int, unsigned char*, float*)
void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int ncomp, unsigned char *scratch, float *scanline)
{
   unsigned char scanlineheader[4] = { 2, 2, 0, 0 };
   unsigned char rgbe[4];
   float linear[3];
   int x;

   scanlineheader[2] = (width&0xff00)>>8;
   scanlineheader[3] = (width&0x00ff);

   /* skip RLE for images too small or large */
   if (width < 8 || width >= 32768) {
      for (x=0; x < width; x++) {
         switch (ncomp) {
            case 4: /* fallthrough */
            case 3: linear[2] = scanline[x*ncomp + 2];
                    linear[1] = scanline[x*ncomp + 1];
                    linear[0] = scanline[x*ncomp + 0];
                    break;
            default:
                    linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0];
                    break;
         }
         stbiw__linear_to_rgbe(rgbe, linear);
         s->func(s->context, rgbe, 4);
      }
   } else {
      int c,r;
      /* encode into scratch buffer */
      for (x=0; x < width; x++) {
         switch(ncomp) {
            case 4: /* fallthrough */
            case 3: linear[2] = scanline[x*ncomp + 2];
                    linear[1] = scanline[x*ncomp + 1];
                    linear[0] = scanline[x*ncomp + 0];
                    break;
            default:
                    linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0];
                    break;
         }
         stbiw__linear_to_rgbe(rgbe, linear);
         scratch[x + width*0] = rgbe[0];
         scratch[x + width*1] = rgbe[1];
         scratch[x + width*2] = rgbe[2];
         scratch[x + width*3] = rgbe[3];
      }

      s->func(s->context, scanlineheader, 4);

      /* RLE each component separately */
      for (c=0; c < 4; c++) {
         unsigned char *comp = &scratch[width*c];

         x = 0;
         while (x < width) {
            // find first run
            r = x;
            while (r+2 < width) {
               if (comp[r] == comp[r+1] && comp[r] == comp[r+2])
                  break;
               ++r;
            }
            if (r+2 >= width)
               r = width;
            // dump up to first run
            while (x < r) {
               int len = r-x;
               if (len > 128) len = 128;
               stbiw__write_dump_data(s, len, &comp[x]);
               x += len;
            }
            // if there's a run, output it
            if (r+2 < width) { // same test as what we break out of in search loop, so only true if we break'd
               // find next byte after run
               while (r < width && comp[r] == comp[x])
                  ++r;
               // output run up to r
               while (x < r) {
                  int len = r-x;
                  if (len > 127) len = 127;
                  stbiw__write_run_data(s, len, comp[x]);
                  x += len;
               }
            }
         }
      }
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movl 0x10684(%rip), %eax     # 0x48864
movl %eax, -0x24(%rbp)
movl -0xc(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, -0x22(%rbp)
movl -0xc(%rbp), %eax
andl $0xff, %eax
movb %al, -0x21(%rbp)
cmpl $0x8, -0xc(%rbp)
jl 0x3820f
cmpl $0x8000, -0xc(%rbp)     # imm = 0x8000
jl 0x382e1
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x382dc
movl -0x10(%rbp), %eax
addl $-0x3, %eax
subl $0x1, %eax
ja 0x38282
jmp 0x3822f
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
imull -0x10(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
imull -0x10(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x30(%rbp)
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
imull -0x10(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x34(%rbp)
jmp 0x382a7
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
imull -0x10(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x2c(%rbp)
movss %xmm0, -0x30(%rbp)
movss %xmm0, -0x34(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x37f30
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rdi
leaq -0x28(%rbp), %rsi
movl $0x4, %edx
callq *%rax
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x38216
jmp 0x385a6
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x383ef
movl -0x10(%rbp), %eax
addl $-0x3, %eax
subl $0x1, %eax
ja 0x38354
jmp 0x38301
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
imull -0x10(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
imull -0x10(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x30(%rbp)
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
imull -0x10(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x34(%rbp)
jmp 0x38379
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
imull -0x10(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x2c(%rbp)
movss %xmm0, -0x30(%rbp)
movss %xmm0, -0x34(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x37f30
movb -0x28(%rbp), %dl
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
imull $0x0, -0xc(%rbp), %esi
addl %esi, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movb -0x27(%rbp), %dl
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
movl -0xc(%rbp), %esi
shll $0x0, %esi
addl %esi, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movb -0x26(%rbp), %dl
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
movl -0xc(%rbp), %esi
shll %esi
addl %esi, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movb -0x25(%rbp), %dl
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
imull $0x3, -0xc(%rbp), %esi
addl %esi, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x382e8
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rdi
leaq -0x24(%rbp), %rsi
movl $0x4, %edx
callq *%rax
movl $0x0, -0x3c(%rbp)
cmpl $0x4, -0x3c(%rbp)
jge 0x385a4
movq -0x18(%rbp), %rax
movl -0xc(%rbp), %ecx
imull -0x3c(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x38594
movl -0x38(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
addl $0x2, %eax
cmpl -0xc(%rbp), %eax
jge 0x384a2
movq -0x48(%rbp), %rax
movslq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq -0x48(%rbp), %rcx
movl -0x40(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x38497
movq -0x48(%rbp), %rax
movslq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq -0x48(%rbp), %rcx
movl -0x40(%rbp), %edx
addl $0x2, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x38497
jmp 0x384a2
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x38448
movl -0x40(%rbp), %eax
addl $0x2, %eax
cmpl -0xc(%rbp), %eax
jl 0x384b3
movl -0xc(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x384b5
movl -0x38(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x384f8
movl -0x40(%rbp), %eax
subl -0x38(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x80, -0x4c(%rbp)
jle 0x384d6
movl $0x80, -0x4c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x4c(%rbp), %esi
movq -0x48(%rbp), %rdx
movslq -0x38(%rbp), %rax
addq %rax, %rdx
callq 0x38140
movl -0x4c(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x384b5
movl -0x40(%rbp), %eax
addl $0x2, %eax
cmpl -0xc(%rbp), %eax
jge 0x3858f
jmp 0x38509
movl -0x40(%rbp), %ecx
xorl %eax, %eax
cmpl -0xc(%rbp), %ecx
movb %al, -0x51(%rbp)
jge 0x38536
movq -0x48(%rbp), %rax
movslq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq -0x48(%rbp), %rcx
movslq -0x38(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0x3853f
jmp 0x3854a
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x38509
jmp 0x3854c
movl -0x38(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x3858d
movl -0x40(%rbp), %eax
subl -0x38(%rbp), %eax
movl %eax, -0x50(%rbp)
cmpl $0x7f, -0x50(%rbp)
jle 0x3856a
movl $0x7f, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movl -0x50(%rbp), %esi
movq -0x48(%rbp), %rax
movslq -0x38(%rbp), %rcx
movzbl (%rax,%rcx), %edx
callq 0x380b0
movl -0x50(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x3854c
jmp 0x3858f
jmp 0x38436
jmp 0x38596
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x38410
jmp 0x385a6
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)