// File: /workspace/github_c_makefile_star2_O3/DavidSM64[P]n64graphics_ci/./stb/stb_image.h
// Function: stbi__gif_load_next
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; 

   // 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
      g->out = (stbi_uc *) stbi__malloc(4 * g->w * g->h);
      g->background = (stbi_uc *) stbi__malloc(4 * g->w * g->h); 
      g->history = (stbi_uc *) stbi__malloc(g->w * g->h); 
      if (g->out == 0)                      return stbi__errpuc("outofmem", "Out of memory");

      // image is treated as "tranparent" 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 teh color that was there the previous frame. 
      memset( g->out, 0x00, 4 * g->w * g->h ); 
      memset( g->background, 0x00, 4 * g->w * g->h ); // state of the background (starts transparent)
      memset( g->history, 0x00, g->w * g->h );        // 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;

            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 == NULL) 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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x8(%rsi), %r13
testq %r13, %r13
je 0xfeb4
movl 0x4(%r14), %eax
movl 0x30(%r14), %esi
shrl $0x2, %esi
andl $0x7, %esi
imull (%r14), %eax
cmpl $0x3, %esi
movl $0x2, %edx
cmovnel %esi, %edx
testq %rcx, %rcx
cmovnel %esi, %edx
cmpl $0x3, %edx
je 0xff45
cmpl $0x2, %edx
jne 0xff69
testl %eax, %eax
jle 0xff69
movl %eax, %eax
xorl %ecx, %ecx
movq 0x18(%r14), %rdx
cmpb $0x0, (%rdx,%rcx)
je 0xfea7
movq 0x8(%r14), %rdx
movq 0x10(%r14), %rsi
movl (%rsi,%rcx,4), %esi
movl %esi, (%rdx,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0xfe8f
jmp 0xff69
movq %rbx, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x104ec
testl %eax, %eax
je 0x10223
movq %r13, -0x50(%rbp)
movl 0x4(%r14), %r15d
imull (%r14), %r15d
leal (,%r15,4), %eax
movslq %eax, %r12
movl $0x1, %edi
movq %r12, %rsi
callq 0x1160
movq %rax, %r13
movq %rax, 0x8(%r14)
movq %r12, -0x30(%rbp)
movq %r12, %rdi
callq 0x11d0
movq %rax, -0x40(%rbp)
movq %rax, 0x10(%r14)
movslq %r15d, %r15
movq %r15, %rdi
callq 0x11d0
movq %rax, 0x18(%r14)
testq %r13, %r13
je 0x10215
movq %rax, %r12
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movq -0x30(%rbp), %rdx
callq 0x1110
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1110
movq -0x50(%rbp), %r13
jmp 0xff94
testl %eax, %eax
jle 0xff69
movl %eax, %eax
xorl %edx, %edx
movq 0x18(%r14), %rsi
cmpb $0x0, (%rsi,%rdx)
je 0xff61
movq 0x8(%r14), %rsi
movl (%rcx,%rdx,4), %edi
movl %edi, (%rsi,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0xff4d
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdi
movslq (%r14), %rax
movslq 0x4(%r14), %rdx
imulq %rax, %rdx
shlq $0x2, %rdx
callq 0x11b0
movq 0x18(%r14), %r12
movslq (%r14), %rax
movslq 0x4(%r14), %r15
imulq %rax, %r15
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1110
leaq 0x34(%r14), %r12
movq %rbx, %rdi
callq 0xcba6
cmpb $0x21, %al
jne 0x100b4
movq %rbx, %rdi
callq 0xcba6
cmpb $-0x7, %al
jne 0x100a3
movq %rbx, %rdi
callq 0xcba6
cmpb $0x4, %al
jne 0x10056
movq %rbx, %rdi
callq 0xcba6
movzbl %al, %eax
movl %eax, 0x30(%r14)
movq %rbx, %rdi
callq 0xcba6
movzbl %al, %r15d
movq %rbx, %rdi
callq 0xcba6
movzbl %al, %eax
shll $0x9, %eax
leal (%rax,%r15,2), %eax
leal (%rax,%rax,4), %eax
movl %eax, 0x8868(%r14)
movslq 0x2c(%r14), %rax
testq %rax, %rax
js 0x1001b
movb $-0x1, 0x3(%r12,%rax,4)
testb $0x1, 0x30(%r14)
jne 0x10066
cmpq $0x0, 0x10(%rbx)
je 0x1007d
movq 0xb8(%rbx), %rax
movq 0xc0(%rbx), %rcx
movl %ecx, %edx
subl %eax, %edx
testl %edx, %edx
jg 0x10084
movq %rcx, 0xb8(%rbx)
movq 0x28(%rbx), %rdi
movl $0x1, %esi
subl %edx, %esi
callq *0x18(%rbx)
jmp 0x1008e
movzbl %al, %esi
movq %rbx, %rdi
callq 0xcd90
jmp 0xffa5
movq %rbx, %rdi
callq 0xcba6
movzbl %al, %eax
movl %eax, 0x2c(%r14)
movb $0x0, 0x37(%r14,%rax,4)
jmp 0x100a3
movq 0xb8(%rbx), %rax
incq %rax
movq %rax, 0xb8(%rbx)
movl $0xffffffff, 0x2c(%r14) # imm = 0xFFFFFFFF
jmp 0x100a3
movzbl %al, %esi
movq %rbx, %rdi
callq 0xcd90
movq %rbx, %rdi
callq 0xcba6
testb %al, %al
jne 0x10098
jmp 0xffa5
movzbl %al, %eax
cmpl $0x3b, %eax
je 0x10225
cmpl $0x2c, %eax
jne 0x1020c
movq %r12, -0x48(%rbp)
movq %r13, -0x50(%rbp)
movq %rbx, %rdi
callq 0xcba6
movzbl %al, %r12d
movq %rbx, %rdi
callq 0xcba6
movzbl %al, %r15d
shll $0x8, %r15d
orl %r12d, %r15d
movq %rbx, %rdi
callq 0xcba6
movb %al, -0x30(%rbp)
movq %rbx, %rdi
callq 0xcba6
movb %al, -0x40(%rbp)
movq %rbx, %rdi
callq 0xcba6
movzbl %al, %r12d
movq %rbx, %rdi
callq 0xcba6
movzbl %al, %r13d
shll $0x8, %r13d
addl %r12d, %r13d
movq %rbx, %rdi
callq 0xcba6
movl %eax, %r12d
movq %rbx, %rdi
callq 0xcba6
addl %r15d, %r13d
movl (%r14), %edx
leaq 0x4d04(%rip), %rcx      # 0x14e49
cmpl %edx, %r13d
jg 0x1021c
movzbl -0x30(%rbp), %edi
movzbl -0x40(%rbp), %esi
shll $0x8, %esi
orl %edi, %esi
movzbl %al, %edi
shll $0x8, %edi
movzbl %r12b, %eax
orl %edi, %eax
addl %esi, %eax
cmpl 0x4(%r14), %eax
jg 0x1021c
shll $0x2, %edx
movl %edx, 0x8864(%r14)
shll $0x2, %r15d
movl %r15d, 0x884c(%r14)
imull %edx, %esi
movl %esi, 0x8850(%r14)
shll $0x2, %r13d
movl %r13d, 0x8854(%r14)
imull %edx, %eax
movl %eax, 0x8858(%r14)
movl %r15d, 0x885c(%r14)
movl %esi, 0x8860(%r14)
movq %rbx, %rdi
callq 0xcba6
movzbl %al, %ecx
movl %ecx, 0x8848(%r14)
movl %ecx, %edx
shll $0x19, %edx
sarl $0x1f, %edx
movl 0x8864(%r14), %esi
leal (,%rsi,8), %edi
testb $0x40, %cl
cmovel %esi, %edi
andl $0x3, %edx
movl %edi, 0x8844(%r14)
movl %edx, 0x8840(%r14)
testb %cl, %cl
js 0x10237
testb $-0x80, 0x20(%r14)
movq -0x48(%rbp), %r15
jne 0x10264
leaq 0x4c54(%rip), %rcx      # 0x14e5e
jmp 0x1021c
leaq 0x4c5f(%rip), %rcx      # 0x14e72
jmp 0x1021c
leaq 0x4684(%rip), %rcx      # 0x148a0
movq %rcx, 0x8fad(%rip)      # 0x191d0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andb $0x7, %al
movl $0x2, %edx
movl %eax, %ecx
shll %cl, %edx
leaq 0x434(%r14), %r15
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testb $0x1, 0x30(%r14)
je 0x10259
movl 0x2c(%r14), %ecx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1064b
movq %r15, 0x8838(%r14)
movq %rbx, %rdi
callq 0xcba6
cmpb $0xc, %al
ja 0x10223
movzbl %al, %edx
movl $0x1, %esi
movl %eax, %ecx
shll %cl, %esi
xorl %ecx, %ecx
movw $0xffff, 0x834(%r14,%rcx,4) # imm = 0xFFFF
movb %cl, 0x836(%r14,%rcx,4)
movb %cl, 0x837(%r14,%rcx,4)
incq %rcx
cmpq %rcx, %rsi
jne 0x10285
movl $0x2, %edi
movl %eax, %ecx
shll %cl, %edi
movl %edx, %ecx
incl %ecx
decl %edi
leal 0x2(%rsi), %r8d
movq %rsi, -0x70(%rbp)
movl %esi, %eax
incl %eax
movl %eax, -0x54(%rbp)
xorl %r9d, %r9d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %ecx, -0x60(%rbp)
movl %edi, -0x5c(%rbp)
movl %edi, %eax
movl %r8d, -0x58(%rbp)
movl %r8d, %esi
xorl %edi, %edi
xorl %r12d, %r12d
xorl %r15d, %r15d
movl %edx, -0x64(%rbp)
movl %esi, -0x48(%rbp)
movl %eax, -0x40(%rbp)
movl %ecx, %eax
movq %rax, -0x30(%rbp)
movb %r9b, -0x34(%rbp)
movl %r12d, %r13d
subl -0x30(%rbp), %r12d
jge 0x1033e
movl %edi, %r12d
testl %r15d, %r15d
jne 0x1031d
movq %rbx, %rdi
callq 0xcba6
testb %al, %al
je 0x10467
movzbl %al, %r15d
decl %r15d
movq %rbx, %rdi
callq 0xcba6
movzbl %al, %eax
movl %r13d, %ecx
shll %cl, %eax
movl %r12d, %edi
orl %eax, %edi
addl $0x8, %r13d
movl %r13d, %r12d
jmp 0x102fb
movl %edi, %r13d
movq -0x30(%rbp), %rcx
sarl %cl, %edi
andl -0x40(%rbp), %r13d
movb $0x1, %r9b
movl -0x60(%rbp), %ecx
movl -0x5c(%rbp), %eax
movl -0x58(%rbp), %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl -0x70(%rbp), %r13d
je 0x102e5
cmpl -0x54(%rbp), %r13d
je 0x10448
leaq 0x4b14(%rip), %rcx      # 0x14e87
movl -0x48(%rbp), %edx
cmpl %edx, %r13d
leaq 0x4b24(%rip), %rax      # 0x14ea4
cmovgq %rax, %rcx
jg 0x1021c
movb -0x34(%rbp), %al
xorb $0x1, %al
testb $0x1, %al
jne 0x1021c
movl -0x64(%rbp), %ecx
testl %ecx, %ecx
js 0x103e4
cmpl $0x1fff, %edx           # imm = 0x1FFF
jg 0x104d4
movslq %edx, %rax
incl %edx
movw %cx, 0x834(%r14,%rax,4)
movl %ecx, %ecx
movb 0x836(%r14,%rcx,4), %cl
movb %cl, 0x836(%r14,%rax,4)
cmpl %edx, %r13d
je 0x103da
movslq %r13d, %rcx
movb 0x836(%r14,%rcx,4), %cl
movb %cl, 0x837(%r14,%rax,4)
jmp 0x103ed
cmpl %edx, %r13d
je 0x104e0
movl %edx, -0x48(%rbp)
movzwl %r13w, %esi
movl %edi, -0x34(%rbp)
movq %r14, %rdi
callq 0x106b6
movl -0x34(%rbp), %edi
movl -0x40(%rbp), %r9d
movl -0x48(%rbp), %r10d
testl %r9d, %r10d
sete %dl
cmpl $0x1000, %r10d          # imm = 0x1000
setl %sil
movq -0x30(%rbp), %r8
leal 0x1(%r8), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
shll %cl, %eax
notl %eax
testb %dl, %sil
cmovnel %ecx, %r8d
cmovel %r9d, %eax
movb $0x1, %r9b
movl %r8d, %ecx
movl %r10d, %esi
movl %r13d, %edx
jmp 0x102e5
movq %rbx, %rdi
movl %r15d, %esi
callq 0xcd90
movq %rbx, %rdi
callq 0xcba6
testb %al, %al
je 0x10467
movzbl %al, %esi
movq %rbx, %rdi
jmp 0x1044e
movq 0x8(%r14), %rbx
testq %rbx, %rbx
je 0x10223
cmpq $0x0, -0x50(%rbp)
jne 0x10225
movl 0x4(%r14), %eax
imull (%r14), %eax
cmpl $0x0, 0x24(%r14)
setg %cl
testl %eax, %eax
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x10225
movl %eax, %eax
xorl %ecx, %ecx
movq 0x18(%r14), %rdx
cmpb $0x0, (%rdx,%rcx)
jne 0x104c7
movslq 0x24(%r14), %rdx
movb $-0x1, 0x37(%r14,%rdx,4)
movq 0x8(%r14), %rdx
movslq 0x24(%r14), %rsi
movl 0x34(%r14,%rsi,4), %esi
movl %esi, (%rdx,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x104a3
jmp 0x10225
leaq 0x49ba(%rip), %rcx      # 0x14e95
jmp 0x1021c
leaq 0x49bd(%rip), %rcx      # 0x14ea4
jmp 0x1021c