// File: srajangarg[P]raytracer/src/core/stb_image.hpp
// Function: load_jpeg_image(stbi__jpeg*, int*, int*, int*, int)
static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)
{
   int n, decode_n;
   z->s->img_n = 0; // make stbi__cleanup_jpeg safe

   // validate req_comp
   if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");

   // load a jpeg image from whichever source, but leave in YCbCr format
   if (!stbi__decode_jpeg_image(z)) { stbi__cleanup_jpeg(z); return NULL; }

   // determine actual number of components to generate
   n = req_comp ? req_comp : z->s->img_n;

   if (z->s->img_n == 3 && n < 3)
      decode_n = 1;
   else
      decode_n = z->s->img_n;

   // resample and color-convert
   {
      int k;
      unsigned int i,j;
      stbi_uc *output;
      stbi_uc *coutput[4];

      stbi__resample res_comp[4];

      for (k=0; k < decode_n; ++k) {
         stbi__resample *r = &res_comp[k];

         // allocate line buffer big enough for upsampling off the edges
         // with upsample factor of 4
         z->img_comp[k].linebuf = (stbi_uc *) stbi__malloc(z->s->img_x + 3);
         if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }

         r->hs      = z->img_h_max / z->img_comp[k].h;
         r->vs      = z->img_v_max / z->img_comp[k].v;
         r->ystep   = r->vs >> 1;
         r->w_lores = (z->s->img_x + r->hs-1) / r->hs;
         r->ypos    = 0;
         r->line0   = r->line1 = z->img_comp[k].data;

         if      (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
         else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2;
         else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2;
         else if (r->hs == 2 && r->vs == 2) r->resample = z->resample_row_hv_2_kernel;
         else                               r->resample = stbi__resample_row_generic;
      }

      // can't error after this so, this is safe
      output = (stbi_uc *) stbi__malloc(n * z->s->img_x * z->s->img_y + 1);
      if (!output) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }

      // now go ahead and resample
      for (j=0; j < z->s->img_y; ++j) {
         stbi_uc *out = output + n * z->s->img_x * j;
         for (k=0; k < decode_n; ++k) {
            stbi__resample *r = &res_comp[k];
            int y_bot = r->ystep >= (r->vs >> 1);
            coutput[k] = r->resample(z->img_comp[k].linebuf,
                                     y_bot ? r->line1 : r->line0,
                                     y_bot ? r->line0 : r->line1,
                                     r->w_lores, r->hs);
            if (++r->ystep >= r->vs) {
               r->ystep = 0;
               r->line0 = r->line1;
               if (++r->ypos < z->img_comp[k].y)
                  r->line1 += z->img_comp[k].w2;
            }
         }
         if (n >= 3) {
            stbi_uc *y = coutput[0];
            if (z->s->img_n == 3) {
               z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
            } else
               for (i=0; i < z->s->img_x; ++i) {
                  out[0] = out[1] = out[2] = y[i];
                  out[3] = 255; // not used if n==3
                  out += n;
               }
         } else {
            stbi_uc *y = coutput[0];
            if (n == 1)
               for (i=0; i < z->s->img_x; ++i) out[i] = y[i];
            else
               for (i=0; i < z->s->img_x; ++i) *out++ = y[i], *out++ = 255;
         }
      }
      stbi__cleanup_jpeg(z);
      *out_x = z->s->img_x;
      *out_y = z->s->img_y;
      if (comp) *comp  = z->s->img_n; // report original components, not output
      return output;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl $0x0, 0x8(%rax)
cmpl $0x0, -0x2c(%rbp)
jl 0x2d429
cmpl $0x4, -0x2c(%rbp)
jle 0x2d449
leaq 0x1abbe(%rip), %rdi     # 0x47fee
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x2db5c
movq -0x10(%rbp), %rdi
callq 0x2db70
cmpl $0x0, %eax
jne 0x2d46d
movq -0x10(%rbp), %rdi
callq 0x2dd40
movq $0x0, -0x8(%rbp)
jmp 0x2db5c
cmpl $0x0, -0x2c(%rbp)
je 0x2d47e
movl -0x2c(%rbp), %eax
movl %eax, -0x164(%rbp)
jmp 0x2d48e
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl 0x8(%rax), %eax
movl %eax, -0x164(%rbp)
movl -0x164(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpl $0x3, 0x8(%rax)
jne 0x2d4b3
cmpl $0x3, -0x30(%rbp)
jge 0x2d4b3
movl $0x1, -0x34(%rbp)
jmp 0x2d4c0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl 0x8(%rax), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x2d725
movslq -0x38(%rbp), %rcx
leaq -0x130(%rbp), %rax
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x138(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
addl $0x3, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x27f20
movq %rax, %rcx
movq -0x10(%rbp), %rax
addq $0x45a0, %rax           # imm = 0x45A0
movslq -0x38(%rbp), %rdx
imulq $0x60, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
addq $0x45a0, %rax           # imm = 0x45A0
movslq -0x38(%rbp), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x48(%rax)
jne 0x2d562
movq -0x10(%rbp), %rdi
callq 0x2dd40
leaq 0x1aa9c(%rip), %rdi     # 0x47fe5
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x2db5c
movq -0x10(%rbp), %rax
movl 0x4588(%rax), %eax
movq -0x10(%rbp), %rcx
addq $0x45a0, %rcx           # imm = 0x45A0
movslq -0x38(%rbp), %rdx
imulq $0x60, %rdx, %rdx
addq %rdx, %rcx
cltd
idivl 0x4(%rcx)
movl %eax, %ecx
movq -0x138(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rax
movl 0x458c(%rax), %eax
movq -0x10(%rbp), %rcx
addq $0x45a0, %rcx           # imm = 0x45A0
movslq -0x38(%rbp), %rdx
imulq $0x60, %rdx, %rdx
addq %rdx, %rcx
cltd
idivl 0x8(%rcx)
movl %eax, %ecx
movq -0x138(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x138(%rbp), %rax
movl 0x1c(%rax), %ecx
sarl %ecx
movq -0x138(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
movq -0x138(%rbp), %rcx
addl 0x18(%rcx), %eax
subl $0x1, %eax
movq -0x138(%rbp), %rcx
xorl %edx, %edx
divl 0x18(%rcx)
movl %eax, %ecx
movq -0x138(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x138(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x10(%rbp), %rax
addq $0x45a0, %rax           # imm = 0x45A0
movslq -0x38(%rbp), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movq 0x30(%rax), %rcx
movq -0x138(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x138(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x138(%rbp), %rax
cmpl $0x1, 0x18(%rax)
jne 0x2d673
movq -0x138(%rbp), %rax
cmpl $0x1, 0x1c(%rax)
jne 0x2d673
movq -0x138(%rbp), %rax
leaq 0x855(%rip), %rcx       # 0x2dec0
movq %rcx, (%rax)
jmp 0x2d715
movq -0x138(%rbp), %rax
cmpl $0x1, 0x18(%rax)
jne 0x2d6a0
movq -0x138(%rbp), %rax
cmpl $0x2, 0x1c(%rax)
jne 0x2d6a0
movq -0x138(%rbp), %rax
leaq 0x845(%rip), %rcx       # 0x2dee0
movq %rcx, (%rax)
jmp 0x2d713
movq -0x138(%rbp), %rax
cmpl $0x2, 0x18(%rax)
jne 0x2d6cd
movq -0x138(%rbp), %rax
cmpl $0x1, 0x1c(%rax)
jne 0x2d6cd
movq -0x138(%rbp), %rax
leaq 0x888(%rip), %rcx       # 0x2df50
movq %rcx, (%rax)
jmp 0x2d711
movq -0x138(%rbp), %rax
cmpl $0x2, 0x18(%rax)
jne 0x2d6fe
movq -0x138(%rbp), %rax
cmpl $0x2, 0x1c(%rax)
jne 0x2d6fe
movq -0x10(%rbp), %rax
movq 0x4778(%rax), %rcx
movq -0x138(%rbp), %rax
movq %rcx, (%rax)
jmp 0x2d70f
movq -0x138(%rbp), %rax
leaq 0x9c4(%rip), %rcx       # 0x2e0d0
movq %rcx, (%rax)
jmp 0x2d711
jmp 0x2d713
jmp 0x2d715
jmp 0x2d717
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2d4c7
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
imull (%rcx), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
imull 0x4(%rcx), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x27f20
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x2d77d
movq -0x10(%rbp), %rdi
callq 0x2dd40
leaq 0x1a881(%rip), %rdi     # 0x47fe5
callq 0x275b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x2db5c
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
cmpl 0x4(%rcx), %eax
jae 0x2db15
movq -0x48(%rbp), %rax
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rdx
movq (%rdx), %rdx
imull (%rdx), %ecx
imull -0x40(%rbp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x140(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x2d990
movslq -0x38(%rbp), %rcx
leaq -0x130(%rbp), %rax
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rax
movl 0x24(%rax), %eax
movq -0x148(%rbp), %rcx
movl 0x1c(%rcx), %ecx
sarl %ecx
cmpl %ecx, %eax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x14c(%rbp)
movq -0x148(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x178(%rbp)
movq -0x10(%rbp), %rax
addq $0x45a0, %rax           # imm = 0x45A0
movslq -0x38(%rbp), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movq 0x48(%rax), %rax
movq %rax, -0x170(%rbp)
cmpl $0x0, -0x14c(%rbp)
je 0x2d858
movq -0x148(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x180(%rbp)
jmp 0x2d86a
movq -0x148(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x188(%rbp)
cmpl $0x0, -0x14c(%rbp)
je 0x2d895
movq -0x148(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x190(%rbp)
jmp 0x2d8a7
movq -0x148(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x190(%rbp)
movq -0x178(%rbp), %rax
movq -0x188(%rbp), %rsi
movq -0x170(%rbp), %rdi
movq -0x190(%rbp), %rdx
movq -0x148(%rbp), %rcx
movl 0x20(%rcx), %ecx
movq -0x148(%rbp), %r8
movl 0x18(%r8), %r8d
callq *%rax
movq %rax, %rcx
movslq -0x38(%rbp), %rax
movq %rcx, -0x70(%rbp,%rax,8)
movq -0x148(%rbp), %rcx
movl 0x24(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x24(%rcx)
movq -0x148(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jl 0x2d980
movq -0x148(%rbp), %rax
movl $0x0, 0x24(%rax)
movq -0x148(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x148(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x148(%rbp), %rcx
movl 0x28(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x28(%rcx)
movq -0x10(%rbp), %rcx
addq $0x45a0, %rcx           # imm = 0x45A0
movslq -0x38(%rbp), %rdx
imulq $0x60, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x20(%rcx), %eax
jge 0x2d97e
movq -0x10(%rbp), %rax
addq $0x45a0, %rax           # imm = 0x45A0
movslq -0x38(%rbp), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movl 0x24(%rax), %edx
movq -0x148(%rbp), %rax
movq 0x10(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x10(%rax)
jmp 0x2d980
jmp 0x2d982
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2d7bf
cmpl $0x3, -0x30(%rbp)
jl 0x2da58
movq -0x70(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpl $0x3, 0x8(%rax)
jne 0x2d9e5
movq -0x10(%rbp), %rax
movq 0x4770(%rax), %rax
movq -0x140(%rbp), %rdi
movq -0x158(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %r8
movq (%r8), %r8
movl (%r8), %r8d
movl -0x30(%rbp), %r9d
callq *%rax
jmp 0x2da53
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
cmpl (%rcx), %eax
jae 0x2da51
movq -0x158(%rbp), %rax
movl -0x3c(%rbp), %ecx
movb (%rax,%rcx), %cl
movq -0x140(%rbp), %rax
movb %cl, 0x2(%rax)
movq -0x140(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x140(%rbp), %rax
movb %cl, (%rax)
movq -0x140(%rbp), %rax
movb $-0x1, 0x3(%rax)
movl -0x30(%rbp), %ecx
movq -0x140(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x140(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2d9ec
jmp 0x2da53
jmp 0x2db05
movq -0x70(%rbp), %rax
movq %rax, -0x160(%rbp)
cmpl $0x1, -0x30(%rbp)
jne 0x2daa5
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
cmpl (%rcx), %eax
jae 0x2daa3
movq -0x160(%rbp), %rax
movl -0x3c(%rbp), %ecx
movb (%rax,%rcx), %dl
movq -0x140(%rbp), %rax
movl -0x3c(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2da70
jmp 0x2db03
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
cmpl (%rcx), %eax
jae 0x2db01
movq -0x160(%rbp), %rax
movl -0x3c(%rbp), %ecx
movb (%rax,%rcx), %cl
movq -0x140(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x140(%rbp)
movb %cl, (%rax)
movq -0x140(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x140(%rbp)
movb $-0x1, (%rax)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2daac
jmp 0x2db03
jmp 0x2db05
jmp 0x2db07
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x2d784
movq -0x10(%rbp), %rdi
callq 0x2dd40
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl (%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl 0x4(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x28(%rbp)
je 0x2db54
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl 0x8(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x48(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
nopl (%rax)