// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %r12
movq %rdi, 0x28(%rsp)
movl $0x202, 0x1c(%rsp)      # imm = 0x202
movl %esi, %eax
rolw $0x8, %ax
movw %ax, 0x1e(%rsp)
leal -0x8000(%rsi), %eax
leal -0x3(%rdx), %r15d
cmpl $0xffff8008, %eax       # imm = 0xFFFF8008
jae 0x1b4e7
movslq %edx, %rbx
xorl %r13d, %r13d
testl %esi, %esi
cmovgl %esi, %r13d
shlq $0x2, %rbx
leaq 0xc(%rsp), %r14
pushq $0x4
popq %rbp
subq $0x1, %r13
jb 0x1b6aa
cmpl $0x1, %r15d
ja 0x1b4ad
movsd 0x4(%r12), %xmm0
movsd %xmm0, 0x14(%rsp)
movss (%r12), %xmm0
jmp 0x1b4bf
movss (%r12), %xmm0
movss %xmm0, 0x18(%rsp)
movss %xmm0, 0x14(%rsp)
movss %xmm0, 0x10(%rsp)
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x1b2da
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdi
movq %r14, %rsi
movl %ebp, %edx
callq *(%rax)
addq %rbx, %r12
jmp 0x1b488
leal (%rsi,%rsi), %r13d
leal (%rsi,%rsi,2), %ebx
movslq %edx, %rbp
movq %rsi, 0x40(%rsp)
movl %esi, %eax
leaq (%rcx,%rax), %rdx
movq %rdx, 0x30(%rsp)
addq %rcx, %r13
movq %rcx, 0x38(%rsp)
addq %rcx, %rbx
shlq $0x2, %rbp
xorl %r14d, %r14d
movq %rax, 0x20(%rsp)
cmpq %r14, %rax
je 0x1b597
cmpl $0x1, %r15d
ja 0x1b538
movsd 0x4(%r12), %xmm0
movsd %xmm0, 0x14(%rsp)
movss (%r12), %xmm0
jmp 0x1b54a
movss (%r12), %xmm0
movss %xmm0, 0x18(%rsp)
movss %xmm0, 0x14(%rsp)
movss %xmm0, 0x10(%rsp)
leaq 0xc(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1b2da
movb 0xc(%rsp), %al
movq 0x38(%rsp), %rcx
movb %al, (%rcx,%r14)
movb 0xd(%rsp), %al
movq 0x30(%rsp), %rcx
movb %al, (%rcx,%r14)
movb 0xe(%rsp), %al
movb %al, (%r13,%r14)
movb 0xf(%rsp), %al
movb %al, (%rbx,%r14)
incq %r14
addq %rbp, %r12
movq 0x20(%rsp), %rax
jmp 0x1b518
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x1c(%rsp), %rsi
pushq $0x4
popq %rdx
callq *(%rax)
movq 0x20(%rsp), %rsi
xorl %r12d, %r12d
movq 0x38(%rsp), %r14
movq 0x40(%rsp), %rdx
cmpq $0x4, %r12
je 0x1b6aa
movq %r12, 0x30(%rsp)
imulq %rsi, %r12
addq 0x38(%rsp), %r12
xorl %r13d, %r13d
cmpl %edx, %r13d
jge 0x1b69a
movslq %r13d, %rax
addq $0x2, %rax
movl %r13d, %ebx
cmpq %rsi, %rax
jge 0x1b608
movb -0x2(%r14,%rax), %cl
cmpb -0x1(%r14,%rax), %cl
jne 0x1b601
cmpb (%r14,%rax), %cl
je 0x1b62c
incl %ebx
incq %rax
jmp 0x1b5ea
movl %edx, %ebx
jmp 0x1b62c
movl $0x80, %eax
cmpl %eax, %ebp
cmovgel %eax, %ebp
leaq (%r12,%r15), %rdx
movq 0x28(%rsp), %rdi
movl %ebp, %esi
callq 0x1b3d8
addl %ebp, %r15d
movl %r15d, %r13d
movl %ebx, %ebp
movslq %r13d, %r15
subl %r13d, %ebp
jg 0x1b60c
leal 0x2(%rbx), %eax
movq 0x40(%rsp), %rdx
cmpl %edx, %eax
movq 0x20(%rsp), %rsi
jge 0x1b5d7
movslq %ebx, %rbx
cmpq %rsi, %rbx
jge 0x1b65e
movb (%r14,%rbx), %al
cmpb (%r12,%r15), %al
jne 0x1b660
incq %rbx
jmp 0x1b64a
movl %edx, %ebx
pushq $0x7f
popq %r15
movl %ebx, %ebp
subl %r13d, %ebp
jle 0x1b68b
cmpl $0x7f, %ebp
cmovgel %r15d, %ebp
movslq %r13d, %rax
movzbl (%r12,%rax), %edx
movq 0x28(%rsp), %rdi
movl %ebp, %esi
callq 0x1b37c
addl %ebp, %r13d
jmp 0x1b664
movq 0x40(%rsp), %rdx
movq 0x20(%rsp), %rsi
jmp 0x1b5d7
movq 0x30(%rsp), %r12
incq %r12
addq %rsi, %r14
jmp 0x1b5bc
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq