// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rsi, %r12
movq %rdi, %r14
movq 0x8(%rsi), %rax
movq %rax, -0x80(%rbp)
testq %rax, %rax
movq %rsi, -0x30(%rbp)
je 0x108a1
movl 0x4(%r12), %eax
movl 0x30(%r12), %esi
shrl $0x2, %esi
andl $0x7, %esi
imull (%r12), %eax
cmpl $0x3, %esi
movl $0x2, %edx
cmovnel %esi, %edx
testq %rcx, %rcx
cmovnel %esi, %edx
cmpl $0x3, %edx
je 0x10938
cmpl $0x2, %edx
jne 0x1095e
testl %eax, %eax
jle 0x1095e
movl %eax, %eax
xorl %ecx, %ecx
movq 0x18(%r12), %rdx
cmpb $0x0, (%rdx,%rcx)
je 0x10894
movq 0x8(%r12), %rdx
movq 0x10(%r12), %rsi
movl (%rsi,%rcx,4), %esi
movl %esi, (%rdx,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x10879
jmp 0x1095e
xorl %r15d, %r15d
movq %r14, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x10fcd
testl %eax, %eax
je 0x10fbb
movl 0x4(%r12), %ebx
imull (%r12), %ebx
leal (,%rbx,4), %eax
movslq %eax, %r15
movq %r15, %rdi
callq 0x11c0
movq %rax, %r13
movq %rax, 0x8(%r12)
movq %r15, %rdi
callq 0x11c0
movq %rax, -0x40(%rbp)
movq %rax, 0x10(%r12)
movslq %ebx, %rbx
movq %rbx, %rdi
callq 0x11c0
movq %rax, 0x18(%r12)
testq %r13, %r13
je 0x10faa
movq %rax, %r12
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1110
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1110
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x1110
movq -0x30(%rbp), %r12
jmp 0x1097e
testl %eax, %eax
jle 0x1095e
movl %eax, %eax
xorl %edx, %edx
movq 0x18(%r12), %rsi
cmpb $0x0, (%rsi,%rdx)
je 0x10956
movq 0x8(%r12), %rsi
movl (%rcx,%rdx,4), %edi
movl %edi, (%rsi,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x10940
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdi
movslq (%r12), %rax
movslq 0x4(%r12), %rdx
imulq %rax, %rdx
shlq $0x2, %rdx
callq 0x11a0
movq 0x18(%r12), %rdi
movslq (%r12), %rax
movslq 0x4(%r12), %rdx
imulq %rax, %rdx
xorl %esi, %esi
callq 0x1110
leaq 0x434(%r12), %rax
movq %rax, -0x78(%rbp)
leaq 0x34(%r12), %rax
movq %rax, -0x70(%rbp)
movq %r14, %rdi
callq 0xd24e
cmpb $0x21, %al
je 0x10a7e
movzbl %al, %eax
cmpl $0x3b, %eax
je 0x10a74
cmpl $0x2c, %eax
jne 0x10b1e
movq %r14, %rdi
callq 0xd24e
movzbl %al, %r15d
movq %r14, %rdi
callq 0xd24e
movzbl %al, %ebx
shll $0x8, %ebx
orl %r15d, %ebx
movq %r14, %rdi
callq 0xd24e
movb %al, -0x40(%rbp)
movq %r14, %rdi
callq 0xd24e
movb %al, -0x38(%rbp)
movq %r14, %rdi
callq 0xd24e
movzbl %al, %r13d
movq %r14, %rdi
callq 0xd24e
movzbl %al, %r15d
shll $0x8, %r15d
addl %r13d, %r15d
movq %r14, %rdi
callq 0xd24e
movl %eax, %r13d
movq %r14, %rdi
callq 0xd24e
addl %ebx, %r15d
movl (%r12), %ecx
cmpl %ecx, %r15d
jg 0x10a68
movzbl -0x40(%rbp), %esi
movzbl -0x38(%rbp), %edx
shll $0x8, %edx
orl %esi, %edx
movzbl %r13b, %esi
movzbl %al, %eax
shll $0x8, %eax
orl %esi, %eax
addl %edx, %eax
cmpl 0x4(%r12), %eax
jle 0x10b4d
leaq 0x53de(%rip), %rax      # 0x15e4d
jmp 0x10b25
xorl %ebx, %ebx
movq %r14, %r15
jmp 0x10b31
movq %r14, %rdi
callq 0xd24e
cmpb $-0x7, %al
jne 0x10f83
movq %r14, %rdi
callq 0xd24e
cmpb $0x4, %al
jne 0x10b3e
movq %r14, %rdi
callq 0xd24e
movzbl %al, %eax
movl %eax, 0x30(%r12)
movq %r14, %rdi
callq 0xd24e
movzbl %al, %ebx
movq %r14, %rdi
callq 0xd24e
movzbl %al, %eax
shll $0x9, %eax
leal (%rax,%rbx,2), %eax
leal (%rax,%rax,4), %eax
movl %eax, 0x8868(%r12)
movslq 0x2c(%r12), %rax
testq %rax, %rax
js 0x10ae5
movb $-0x1, 0x37(%r12,%rax,4)
testb $0x1, 0x30(%r12)
jne 0x10bcb
cmpq $0x0, 0x10(%r14)
je 0x10b12
movq 0xc0(%r14), %rax
movl 0xb8(%r14), %edx
movl %eax, %ecx
subl %edx, %ecx
testl %ecx, %ecx
jle 0x10f64
incq 0xb8(%r14)
jmp 0x10f7a
leaq 0x5351(%rip), %rax      # 0x15e76
movq %rax, 0x96b4(%rip)      # 0x1a1e0
xorl %ebx, %ebx
xorl %r15d, %r15d
testb %bl, %bl
jne 0x109ac
jmp 0x10fbb
movzbl %al, %esi
movq %r14, %rdi
callq 0xd436
movb $0x1, %bl
jmp 0x10b31
shll $0x2, %ecx
movl %ecx, 0x8864(%r12)
shll $0x2, %ebx
movl %ebx, 0x884c(%r12)
imull %ecx, %edx
movl %edx, 0x8850(%r12)
shll $0x2, %r15d
movl %r15d, 0x8854(%r12)
imull %ecx, %eax
movl %eax, 0x8858(%r12)
movl %ebx, 0x885c(%r12)
movl %edx, 0x8860(%r12)
movq %r14, %rdi
callq 0xd24e
movzbl %al, %edx
movl %edx, 0x8848(%r12)
movl 0x8864(%r12), %ecx
testb $0x40, %dl
jne 0x10be6
movl %ecx, 0x8844(%r12)
movl $0x0, 0x8840(%r12)
jmp 0x10bfd
movq %r14, %rdi
callq 0xd24e
movzbl %al, %eax
movl %eax, 0x2c(%r12)
movb $0x0, 0x37(%r12,%rax,4)
jmp 0x10f83
shll $0x3, %ecx
movl %ecx, 0x8844(%r12)
movl $0x3, 0x8840(%r12)
testb %al, %al
js 0x10c15
testb $-0x80, 0x20(%r12)
jne 0x10c4b
leaq 0x5252(%rip), %rax      # 0x15e62
jmp 0x10b25
andb $0x7, %al
movl $0x2, %edx
movl %eax, %ecx
shll %cl, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testb $0x1, 0x30(%r12)
je 0x10c32
movl 0x2c(%r12), %ecx
movq %r14, %rdi
movq -0x78(%rbp), %rbx
movq %rbx, %rsi
callq 0x1112c
movq %rbx, 0x8838(%r12)
jmp 0x10c57
movq -0x70(%rbp), %rax
movq %rax, 0x8838(%r12)
movq %r14, %rdi
callq 0xd24e
cmpb $0xc, %al
jbe 0x10c70
movq $0x0, -0x38(%rbp)
jmp 0x10eef
movzbl %al, %edx
movl $0x1, %esi
movl %eax, %ecx
shll %cl, %esi
xorl %ecx, %ecx
movw $0xffff, 0x834(%r12,%rcx,4) # imm = 0xFFFF
movb %cl, 0x836(%r12,%rcx,4)
movb %cl, 0x837(%r12,%rcx,4)
incq %rcx
cmpq %rcx, %rsi
jne 0x10c7e
movl $0x2, %edi
movl %eax, %ecx
shll %cl, %edi
incl %edx
decl %edi
leal 0x2(%rsi), %eax
movq %rsi, -0x88(%rbp)
movl %esi, %ecx
incl %ecx
movl %ecx, -0x5c(%rbp)
xorl %ebx, %ebx
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
movl $0x1, %ecx
movq %rcx, -0x58(%rbp)
movl %edx, -0x68(%rbp)
movl %edx, %ecx
movq %rcx, -0x40(%rbp)
movl %edi, -0x64(%rbp)
movl %edi, -0x48(%rbp)
movl %eax, -0x60(%rbp)
movq %rax, -0x50(%rbp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movl %r13d, %r12d
subl -0x40(%rbp), %r13d
jge 0x10d2d
testl %r15d, %r15d
jne 0x10d0f
movq %r14, %rdi
callq 0xd24e
testb %al, %al
je 0x10ee2
movzbl %al, %r15d
decl %r15d
movq %r14, %rdi
callq 0xd24e
movzbl %al, %eax
movl %r12d, %ecx
shll %cl, %eax
orl %eax, %ebx
addl $0x8, %r12d
movl %r12d, %r13d
jmp 0x10cf0
movl %ebx, %r12d
movq -0x40(%rbp), %rcx
sarl %cl, %ebx
andl -0x48(%rbp), %r12d
movb $0x1, %cl
cmpl -0x88(%rbp), %r12d
jne 0x10d6a
movq $0x0, -0x58(%rbp)
movl -0x68(%rbp), %eax
movq %rax, -0x40(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x60(%rbp), %eax
movq %rax, -0x50(%rbp)
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
jmp 0x10de3
cmpl -0x5c(%rbp), %r12d
jne 0x10d8f
movq %r14, %rdi
movl %r15d, %esi
callq 0xd436
movq %r14, %rdi
callq 0xd24e
testb %al, %al
je 0x10da8
movzbl %al, %esi
movq %r14, %rdi
jmp 0x10d76
movq -0x50(%rbp), %rax
cmpl %eax, %r12d
jle 0x10dbc
leaq 0x5109(%rip), %rax      # 0x15ea8
movq %rax, 0x943a(%rip)      # 0x1a1e0
jmp 0x10dd9
movq -0x30(%rbp), %r12
movq 0x8(%r12), %rax
movq %rax, -0x38(%rbp)
xorl %r15d, %r15d
xorl %ecx, %ecx
jmp 0x10de7
cmpl $0x0, -0x58(%rbp)
je 0x10df4
leaq 0x50c2(%rip), %rax      # 0x15e8b
movq %rax, 0x9410(%rip)      # 0x1a1e0
movl $0x1, %eax
movq %rax, -0x58(%rbp)
xorl %ecx, %ecx
movq $0x0, -0x38(%rbp)
movq -0x30(%rbp), %r12
testb %cl, %cl
jne 0x10ced
jmp 0x10eef
cmpl $0x0, -0x44(%rbp)
js 0x10e22
leal 0x1(%rax), %edx
cmpl $0x2000, %eax           # imm = 0x2000
jl 0x10e3f
leaq 0x508e(%rip), %rax      # 0x15e99
movq %rax, 0x93ce(%rip)      # 0x1a1e0
movq $0x0, -0x58(%rbp)
movl %edx, %eax
movq %rax, -0x50(%rbp)
jmp 0x10dd9
cmpl %eax, %r12d
jne 0x10e7b
leaq 0x507a(%rip), %rax      # 0x15ea8
movq %rax, 0x93ab(%rip)      # 0x1a1e0
movq $0x0, -0x58(%rbp)
jmp 0x10dd9
cltq
movq -0x30(%rbp), %rdi
movl -0x44(%rbp), %ecx
movw %cx, 0x834(%rdi,%rax,4)
movl %ecx, %ecx
movb 0x836(%rdi,%rcx,4), %cl
movb %cl, 0x836(%rdi,%rax,4)
cmpl %edx, %r12d
je 0x10e6f
movslq %r12d, %rcx
movb 0x836(%rdi,%rcx,4), %cl
movb %cl, 0x837(%rdi,%rax,4)
movl %edx, -0x50(%rbp)
jmp 0x10e82
movl %eax, -0x50(%rbp)
movq -0x30(%rbp), %rdi
movzwl %r12w, %esi
callq 0x11197
movl -0x48(%rbp), %r8d
movl -0x50(%rbp), %r9d
testl %r8d, %r9d
sete %al
cmpl $0x1000, %r9d           # imm = 0x1000
setl %dl
movq -0x40(%rbp), %rdi
leal 0x1(%rdi), %ecx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
shll %cl, %esi
notl %esi
testb %al, %dl
cmovnel %ecx, %edi
movq %rdi, -0x40(%rbp)
cmovnel %esi, %r8d
movl %r8d, -0x48(%rbp)
movq $0x0, -0x58(%rbp)
movl %r9d, %eax
movq %rax, -0x50(%rbp)
movl %r12d, -0x44(%rbp)
movq -0x30(%rbp), %r12
movb $0x1, %cl
jmp 0x10de7
movq -0x30(%rbp), %r12
movq 0x8(%r12), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %r15
testq %r15, %r15
je 0x10b2c
cmpq $0x0, -0x80(%rbp)
jne 0x10f5d
movl 0x4(%r12), %eax
imull (%r12), %eax
cmpl $0x0, 0x24(%r12)
setg %cl
testl %eax, %eax
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x10f5d
movl %eax, %eax
xorl %ecx, %ecx
movq 0x18(%r12), %rdx
cmpb $0x0, (%rdx,%rcx)
jne 0x10f4e
movslq 0x24(%r12), %rdx
movb $-0x1, 0x37(%r12,%rdx,4)
movq 0x8(%r12), %rdx
movslq 0x24(%r12), %rsi
movl 0x34(%r12,%rsi,4), %esi
movl %esi, (%rdx,%rcx,4)
incq %rcx
xorl %ebx, %ebx
cmpq %rcx, %rax
jne 0x10f26
jmp 0x10b31
xorl %ebx, %ebx
jmp 0x10b31
movq %rax, 0xb8(%r14)
movq 0x28(%r14), %rdi
movl $0x1, %esi
subl %ecx, %esi
callq *0x18(%r14)
movl $0xffffffff, 0x2c(%r12) # imm = 0xFFFFFFFF
movq %r14, %rdi
callq 0xd24e
movb $0x1, %bl
testb %al, %al
je 0x10b31
movzbl %al, %esi
movq %r14, %rdi
callq 0xd436
movq %r14, %rdi
callq 0xd24e
jmp 0x10f8d
leaq 0x48f3(%rip), %rax      # 0x158a4
movq %rax, 0x9228(%rip)      # 0x1a1e0
xorl %r15d, %r15d
movq %r15, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq