// File: alan0201tw[P]RayTracer/include/stb_image.h
// Function: stbi__gif_load_next(stbi__context*, stbi__gif*, int*, int, unsigned char*)
static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back)
{
   int dispose; 
   int first_frame; 
   int pi; 
   int pcount; 
   STBI_NOTUSED(req_comp);

   // on first frame, any non-written pixels get the background colour (non-transparent)
   first_frame = 0; 
   if (g->out == 0) {
      if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header
      if (!stbi__mad3sizes_valid(4, g->w, g->h, 0))
         return stbi__errpuc("too large", "GIF image is too large");
      pcount = g->w * g->h;
      g->out = (stbi_uc *) stbi__malloc(4 * pcount);
      g->background = (stbi_uc *) stbi__malloc(4 * pcount);
      g->history = (stbi_uc *) stbi__malloc(pcount);
      if (!g->out || !g->background || !g->history)
         return stbi__errpuc("outofmem", "Out of memory");

      // image is treated as "transparent" at the start - ie, nothing overwrites the current background; 
      // background colour is only used for pixels that are not rendered first frame, after that "background"
      // color refers to the color that was there the previous frame. 
      memset(g->out, 0x00, 4 * pcount);
      memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent)
      memset(g->history, 0x00, pcount);        // pixels that were affected previous frame
      first_frame = 1; 
   } else {
      // second frame - how do we dispoase of the previous one?
      dispose = (g->eflags & 0x1C) >> 2; 
      pcount = g->w * g->h; 

      if ((dispose == 3) && (two_back == 0)) {
         dispose = 2; // if I don't have an image to revert back to, default to the old background
      }

      if (dispose == 3) { // use previous graphic
         for (pi = 0; pi < pcount; ++pi) {
            if (g->history[pi]) {
               memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 ); 
            }
         }
      } else if (dispose == 2) { 
         // restore what was changed last frame to background before that frame; 
         for (pi = 0; pi < pcount; ++pi) {
            if (g->history[pi]) {
               memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 ); 
            }
         }
      } else {
         // This is a non-disposal case eithe way, so just 
         // leave the pixels as is, and they will become the new background
         // 1: do not dispose
         // 0:  not specified.
      }

      // background is what out is after the undoing of the previou frame; 
      memcpy( g->background, g->out, 4 * g->w * g->h ); 
   }

   // clear my history; 
   memset( g->history, 0x00, g->w * g->h );        // pixels that were affected previous frame

   for (;;) {
      int tag = stbi__get8(s); 
      switch (tag) {
         case 0x2C: /* Image Descriptor */
         {
            stbi__int32 x, y, w, h;
            stbi_uc *o;

            x = stbi__get16le(s);
            y = stbi__get16le(s);
            w = stbi__get16le(s);
            h = stbi__get16le(s);
            if (((x + w) > (g->w)) || ((y + h) > (g->h)))
               return stbi__errpuc("bad Image Descriptor", "Corrupt GIF");

            g->line_size = g->w * 4;
            g->start_x = x * 4;
            g->start_y = y * g->line_size;
            g->max_x   = g->start_x + w * 4;
            g->max_y   = g->start_y + h * g->line_size;
            g->cur_x   = g->start_x;
            g->cur_y   = g->start_y;

            // if the width of the specified rectangle is 0, that means
            // we may not see *any* pixels or the image is malformed;
            // to make sure this is caught, move the current y down to
            // max_y (which is what out_gif_code checks).
            if (w == 0)
               g->cur_y = g->max_y;

            g->lflags = stbi__get8(s);

            if (g->lflags & 0x40) {
               g->step = 8 * g->line_size; // first interlaced spacing
               g->parse = 3;
            } else {
               g->step = g->line_size;
               g->parse = 0;
            }

            if (g->lflags & 0x80) {
               stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
               g->color_table = (stbi_uc *) g->lpal;
            } else if (g->flags & 0x80) {
               g->color_table = (stbi_uc *) g->pal;
            } else
               return stbi__errpuc("missing color table", "Corrupt GIF");            
            
            o = stbi__process_gif_raster(s, g);
            if (!o) return NULL;

            // if this was the first frame, 
            pcount = g->w * g->h; 
            if (first_frame && (g->bgindex > 0)) {
               // if first frame, any pixel not drawn to gets the background color
               for (pi = 0; pi < pcount; ++pi) {
                  if (g->history[pi] == 0) {
                     g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be; 
                     memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 ); 
                  }
               }
            }

            return o;
         }

         case 0x21: // Comment Extension.
         {
            int len;
            int ext = stbi__get8(s); 
            if (ext == 0xF9) { // Graphic Control Extension.
               len = stbi__get8(s);
               if (len == 4) {
                  g->eflags = stbi__get8(s);
                  g->delay = 10 * stbi__get16le(s); // delay - 1/100th of a second, saving as 1/1000ths.

                  // unset old transparent
                  if (g->transparent >= 0) {
                     g->pal[g->transparent][3] = 255; 
                  } 
                  if (g->eflags & 0x01) {
                     g->transparent = stbi__get8(s);
                     if (g->transparent >= 0) {
                        g->pal[g->transparent][3] = 0; 
                     }
                  } else {
                     // don't need transparent
                     stbi__skip(s, 1); 
                     g->transparent = -1; 
                  }
               } else {
                  stbi__skip(s, len);
                  break;
               }
            } 
            while ((len = stbi__get8(s)) != 0) {
               stbi__skip(s, len);
            }
            break;
         }

         case 0x3B: // gif stream termination code
            return (stbi_uc *) s; // using '1' causes warning on some compilers

         default:
            return stbi__errpuc("unknown code", "Corrupt GIF");
      }
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x6df3e
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
xorl %ecx, %ecx
callq 0x6e540
cmpl $0x0, %eax
jne 0x6de19
movq $0x0, -0x8(%rbp)
jmp 0x6e524
movq -0x18(%rbp), %rax
movl (%rax), %esi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edx
movl $0x4, %edi
xorl %ecx, %ecx
callq 0x662a0
cmpl $0x0, %eax
jne 0x6de57
leaq 0x132ba(%rip), %rdi     # 0x810f8
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x6e524
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x18(%rbp), %rcx
imull 0x4(%rcx), %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
shll $0x2, %eax
movslq %eax, %rdi
callq 0x57060
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x40(%rbp), %eax
shll $0x2, %eax
movslq %eax, %rdi
callq 0x57060
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movslq -0x40(%rbp), %rdi
callq 0x57060
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x6decf
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x6decf
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x6deef
leaq 0x1322c(%rip), %rdi     # 0x81102
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x6e524
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movl -0x40(%rbp), %eax
shll $0x2, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x7290
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x40(%rbp), %eax
shll $0x2, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x7290
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movslq -0x40(%rbp), %rdx
xorl %esi, %esi
callq 0x7290
movl $0x1, -0x38(%rbp)
jmp 0x6e058
movq -0x18(%rbp), %rax
movl 0x30(%rax), %eax
andl $0x1c, %eax
sarl $0x2, %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x18(%rbp), %rcx
imull 0x4(%rcx), %eax
movl %eax, -0x40(%rbp)
cmpl $0x3, -0x34(%rbp)
jne 0x6df73
cmpq $0x0, -0x30(%rbp)
jne 0x6df73
movl $0x2, -0x34(%rbp)
cmpl $0x3, -0x34(%rbp)
jne 0x6dfcd
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x6dfcb
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x3c(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x6dfbe
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x3c(%rbp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
movq -0x30(%rbp), %rdx
movl -0x3c(%rbp), %esi
shll $0x2, %esi
movslq %esi, %rsi
movl (%rdx,%rsi), %edx
movl %edx, (%rax,%rcx)
jmp 0x6dfc0
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6df80
jmp 0x6e02f
cmpl $0x2, -0x34(%rbp)
jne 0x6e02b
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x6e029
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x3c(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x6e01c
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x3c(%rbp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
movq -0x18(%rbp), %rdx
movq 0x10(%rdx), %rdx
movl -0x3c(%rbp), %esi
shll $0x2, %esi
movslq %esi, %rsi
movl (%rdx,%rsi), %edx
movl %edx, (%rax,%rcx)
jmp 0x6e01e
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6dfda
jmp 0x6e02d
jmp 0x6e02d
jmp 0x6e02f
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movl (%rax), %eax
shll $0x2, %eax
movq -0x18(%rbp), %rcx
imull 0x4(%rcx), %eax
movslq %eax, %rdx
callq 0x7400
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x18(%rbp), %rcx
imull 0x4(%rcx), %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x7290
movq -0x10(%rbp), %rdi
callq 0x65d00
movzbl %al, %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x6c(%rbp)
subl $0x21, %eax
je 0x6e3ef
jmp 0x6e098
movl -0x6c(%rbp), %eax
subl $0x2c, %eax
je 0x6e0b3
jmp 0x6e0a2
movl -0x6c(%rbp), %eax
subl $0x3b, %eax
je 0x6e4f8
jmp 0x6e502
movq -0x10(%rbp), %rdi
callq 0x6d670
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6d670
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6d670
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6d670
movl %eax, -0x54(%rbp)
movl -0x48(%rbp), %eax
addl -0x50(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x6e100
movl -0x4c(%rbp), %eax
addl -0x54(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jle 0x6e120
leaq 0x135ad(%rip), %rdi     # 0x816b4
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x6e524
movq -0x18(%rbp), %rax
movl (%rax), %ecx
shll $0x2, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8864(%rax)
movl -0x48(%rbp), %ecx
shll $0x2, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x884c(%rax)
movl -0x4c(%rbp), %ecx
movq -0x18(%rbp), %rax
imull 0x8864(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8850(%rax)
movq -0x18(%rbp), %rax
movl 0x884c(%rax), %ecx
movl -0x50(%rbp), %eax
shll $0x2, %eax
addl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8854(%rax)
movq -0x18(%rbp), %rax
movl 0x8850(%rax), %ecx
movl -0x54(%rbp), %eax
movq -0x18(%rbp), %rdx
imull 0x8864(%rdx), %eax
addl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8858(%rax)
movq -0x18(%rbp), %rax
movl 0x884c(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x885c(%rax)
movq -0x18(%rbp), %rax
movl 0x8850(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8860(%rax)
cmpl $0x0, -0x50(%rbp)
jne 0x6e1dd
movq -0x18(%rbp), %rax
movl 0x8858(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8860(%rax)
movq -0x10(%rbp), %rdi
callq 0x65d00
movzbl %al, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8848(%rax)
movq -0x18(%rbp), %rax
movl 0x8848(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x6e22c
movq -0x18(%rbp), %rax
movl 0x8864(%rax), %ecx
shll $0x3, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8844(%rax)
movq -0x18(%rbp), %rax
movl $0x3, 0x8840(%rax)
jmp 0x6e24e
movq -0x18(%rbp), %rax
movl 0x8864(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8844(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x8840(%rax)
movq -0x18(%rbp), %rax
movl 0x8848(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x6e2ec
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rax
addq $0x434, %rax            # imm = 0x434
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movl 0x8848(%rax), %ecx
andl $0x7, %ecx
movl $0x2, %eax
shll %cl, %eax
movl %eax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movl 0x30(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x6e2b1
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x84(%rbp)
jmp 0x6e2be
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x84(%rbp)
jmp 0x6e2be
movl -0x70(%rbp), %edx
movq -0x78(%rbp), %rsi
movq -0x80(%rbp), %rdi
movl -0x84(%rbp), %ecx
callq 0x6e6f0
movq -0x18(%rbp), %rcx
addq $0x434, %rcx            # imm = 0x434
movq -0x18(%rbp), %rax
movq %rcx, 0x8838(%rax)
jmp 0x6e334
movq -0x18(%rbp), %rax
movl 0x20(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x6e312
movq -0x18(%rbp), %rcx
addq $0x34, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8838(%rax)
jmp 0x6e332
leaq 0x133b0(%rip), %rdi     # 0x816c9
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x6e524
jmp 0x6e334
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x6e790
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x6e359
movq $0x0, -0x8(%rbp)
jmp 0x6e524
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x18(%rbp), %rcx
imull 0x4(%rcx), %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x6e3e2
movq -0x18(%rbp), %rax
cmpl $0x0, 0x24(%rax)
jle 0x6e3e2
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x6e3e0
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x3c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x6e3d3
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq 0x24(%rcx), %rcx
movb $-0x1, 0x37(%rax,%rcx,4)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x3c(%rbp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
movq -0x18(%rbp), %rdx
movq -0x18(%rbp), %rsi
movslq 0x24(%rsi), %rsi
movl 0x34(%rdx,%rsi,4), %edx
movl %edx, (%rax,%rcx)
jmp 0x6e3d5
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6e381
jmp 0x6e3e2
movq -0x60(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x6e524
movq -0x10(%rbp), %rdi
callq 0x65d00
movzbl %al, %eax
movl %eax, -0x68(%rbp)
cmpl $0xf9, -0x68(%rbp)
jne 0x6e4d2
movq -0x10(%rbp), %rdi
callq 0x65d00
movzbl %al, %eax
movl %eax, -0x64(%rbp)
cmpl $0x4, -0x64(%rbp)
jne 0x6e4c2
movq -0x10(%rbp), %rdi
callq 0x65d00
movzbl %al, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x10(%rbp), %rdi
callq 0x6d670
imull $0xa, %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8868(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
jl 0x6e468
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq 0x2c(%rcx), %rcx
movb $-0x1, 0x37(%rax,%rcx,4)
movq -0x18(%rbp), %rax
movl 0x30(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x6e4a7
movq -0x10(%rbp), %rdi
callq 0x65d00
movzbl %al, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x2c(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
jl 0x6e4a5
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq 0x2c(%rcx), %rcx
movb $0x0, 0x37(%rax,%rcx,4)
jmp 0x6e4c0
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x661f0
movq -0x18(%rbp), %rax
movl $0xffffffff, 0x2c(%rax) # imm = 0xFFFFFFFF
jmp 0x6e4d0
movq -0x10(%rbp), %rdi
movl -0x64(%rbp), %esi
callq 0x661f0
jmp 0x6e51f
jmp 0x6e4d2
jmp 0x6e4d4
movq -0x10(%rbp), %rdi
callq 0x65d00
movzbl %al, %eax
movl %eax, -0x64(%rbp)
cmpl $0x0, %eax
je 0x6e4f6
movq -0x10(%rbp), %rdi
movl -0x64(%rbp), %esi
callq 0x661f0
jmp 0x6e4d4
jmp 0x6e51f
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x6e524
leaq 0x131d4(%rip), %rdi     # 0x816dd
callq 0x56040
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
cmovneq %rax, %rax
movq %rax, -0x8(%rbp)
jmp 0x6e524
jmp 0x6e078
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)