// File: /workspace/github_cpp_makefile_star2_O3/mikeakohn[P]cloudtari/build/../src/GifCompressor.cxx
// Function: GifCompressor::compress(unsigned char*, unsigned int*)
int GifCompressor::compress(uint8_t *image, uint32_t *color_table)
{
  int ptr = 6, i;

  // Remap the colors from the 128 color palette to a smaller palette
  // so the GIF compresses more.
  uint8_t color_map[256];
  uint8_t color_used[256];
  uint32_t gif_palette[256];

  memset(color_map, 0, sizeof(color_map));
  memset(color_used, 0, sizeof(color_used));

  int length = gif_header.width * gif_header.height;
  int max_colors = 0;

  // Remap colors to GIF palette.
  for (i = 0; i < length; i++)
  {
    int color = image[i];

    if (color_used[color]) { continue; }

    color_map[color] = max_colors;
    color_used[color] = 1;
    gif_palette[max_colors] = color_table[color];

    max_colors++;
  }

  // FIXME: Why is this happening?
  if (max_colors < 2) { max_colors = 4; }

  // Compute bits per pixel and max colors for that.
  int bits_per_pixel = compute_bits_per_pixel(max_colors);
  const int color_resolution = bits_per_pixel;
  max_colors = 1 << bits_per_pixel;
//printf("max_colors=%d bits_per_pixel=%d\n", max_colors, bits_per_pixel);

  // Copy GifHeader (Logical Screen Descriptor) to memory.
  set_uint16(data, ptr + 0, gif_header.width);
  set_uint16(data, ptr + 2, gif_header.height);
  data[ptr + 4] = 0x80 | ((color_resolution - 1) << 4) | (bits_per_pixel - 1);
  data[ptr + 5] = 0;
  data[ptr + 6] = 0;
  ptr += 7;

  // Global Color Map (palettes).
  for (i = 0; i < max_colors; i++)
  {
    data[ptr++] = (gif_palette[i] >> 16) & 0xff;
    data[ptr++] = (gif_palette[i] >> 8) & 0xff;
    data[ptr++] =  gif_palette[i] & 0xff;
  }

  // Image Descriptor Block.
  data[ptr + 0] = ',';
  set_uint16(data, ptr + 1, 0);
  set_uint16(data, ptr + 3, 0);
  set_uint16(data, ptr + 5, gif_header.width);
  set_uint16(data, ptr + 7, gif_header.height);
  data[ptr + 9] = bits_per_pixel - 1;
  ptr += 10;

  int code_size = bits_per_pixel;
  int clear_code = max_colors;
  int eof_code = max_colors + 1;

  data[ptr++] = code_size;

  // Setup LZW tree.
  CompressNode node[4097];

  for (i = 0; i < max_colors; i++)
  {
    node[i].color = i;
    node[i].right = i + 1;
    node[i].down = -1;
  }

  // Compressed data blocks follow.
  int table_start_size = max_colors + 2;
  int next_code = table_start_size;
  int curr_code_size = code_size + 1;
  int curr_code = -1;
  int data_size = 0;

  // LZW Compression.
  BitStream bit_stream;

  bit_stream.append(clear_code, curr_code_size);

  int image_ptr = 0;
  uint8_t color = color_map[image[image_ptr++]];
  int block_ptr;

  block_ptr = ptr;
  data[ptr++] = 0;

  curr_code = color;
  int last_code = curr_code;

  while (image_ptr < length)
  {
    color = color_map[image[image_ptr++]];

    if (node[curr_code].down == -1)
    {
      last_code = curr_code;
      node[curr_code].down = next_code;
    }
      else
    {
      last_code = curr_code;
      curr_code = node[curr_code].down;

      while (node[curr_code].color != color && node[curr_code].right != -1)
      {
        curr_code=node[curr_code].right;
      }

      if (node[curr_code].color != color && node[curr_code].right == -1)
      {
        node[curr_code].right=next_code;
      }
        else
      {
        continue;
      }
    }

    bit_stream.append(last_code, curr_code_size);

    node[next_code].right = -1;
    node[next_code].down = -1;
    node[next_code].color = color;
    curr_code = color;

    if ((next_code >> curr_code_size) != 0)
    {
      if (curr_code_size >= 12)
      {
        bit_stream.append(clear_code, curr_code_size);

        for (i = 0; i < max_colors; i++)
        {
          node[i].down = -1;
        }

        next_code = table_start_size - 1;
        curr_code_size = code_size;
      }

      curr_code_size++;
    }

    next_code++;

    while (bit_stream.size() >= 8)
    {
      data[ptr++] = bit_stream.get_byte();
      data_size++;

      if (data_size >= 255)
      {
        data[block_ptr] = data_size;
        data_size = 0;
        block_ptr = ptr;
        data[ptr++] = 0;
      }
    }
  }

  bit_stream.append(curr_code, curr_code_size);
  bit_stream.append(eof_code, curr_code_size);

  while (bit_stream.size() > 0)
  {
    data[ptr++] = bit_stream.get_byte();
    data_size++;

    if (data_size >= 255)
    {
      data[block_ptr] = data_size;
      data_size = 0;
      block_ptr = ptr;
      data[ptr++] = 0;
    }
  }

  if (data_size > 0)
  {
    data[block_ptr] = data_size;
  }

  // End Marker.
  data[ptr++] = 0;
  data[ptr++] = ';';

  gif_length = ptr;

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc648, %rsp           # imm = 0xC648
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x30(%rsp), %rdi
xorl %r14d, %r14d
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x4110
leaq 0x130(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x4110
movzwl 0x6(%rbx), %eax
movzwl 0x8(%rbx), %ecx
imull %eax, %ecx
movl $0x2, %r11d
movl %ecx, %r10d
movl %ecx, 0x4(%rsp)
testl %ecx, %ecx
je 0x4dca
xorl %ecx, %ecx
movzbl (%r12,%rcx), %edx
cmpb $0x0, 0x130(%rsp,%rdx)
jne 0x4d88
movb %r14b, 0x30(%rsp,%rdx)
movb $0x1, 0x130(%rsp,%rdx)
movl (%r15,%rdx,4), %edx
movslq %r14d, %r14
movl %edx, 0x230(%rsp,%r14,4)
incl %r14d
incq %rcx
cmpq %rcx, %r10
jne 0x4d5a
cmpl $0x2, %r14d
jl 0x4dca
leal -0x1(%r14), %ecx
leaq 0x66ef(%rip), %rdx      # 0xb490
cmpl $0x11, %r14d
jae 0x4dbb
movl %ecx, %esi
movzbl (%rsi,%rdx), %edx
testl %ecx, %ecx
movl $0x1, %r11d
cmovnel %edx, %r11d
jmp 0x4dca
sarl $0x4, %ecx
movslq %ecx, %rcx
movzbl (%rcx,%rdx), %r11d
addl $0x4, %r11d
movl $0x1, %r14d
movl %r11d, %ecx
shll %cl, %r14d
movl $0x1, %ecx
movq 0x10(%rbx), %rdx
movw %ax, 0x6(%rdx)
movq 0x10(%rbx), %rax
movzwl 0x8(%rbx), %edx
movw %dx, 0x8(%rax)
leal -0x1(%r11), %esi
movl %esi, %eax
shll $0x4, %eax
orl %esi, %eax
orb $-0x80, %al
movq 0x10(%rbx), %rdx
movb %al, 0xa(%rdx)
movq 0x10(%rbx), %rax
xorl %edx, %edx
movb %dl, 0xb(%rax)
movq 0x10(%rbx), %rax
movb %dl, 0xc(%rax)
movl $0xd, %edi
cmpl $0x1f, %r11d
je 0x4e60
cmpl $0x2, %r14d
cmovgel %r14d, %ecx
movl $0xd, %edi
xorl %edx, %edx
movl 0x230(%rsp,%rdx,4), %eax
movl %eax, %r8d
shrl $0x10, %r8d
movq 0x10(%rbx), %r9
movb %r8b, (%r9,%rdi)
movq 0x10(%rbx), %rbp
movb %ah, 0x1(%rbp,%rdi)
movq 0x10(%rbx), %r8
movb %al, 0x2(%r8,%rdi)
addq $0x3, %rdi
incq %rdx
cmpq %rdx, %rcx
jne 0x4e2d
movl %edi, %eax
leaq 0xb(%rax), %r9
xorl %r8d, %r8d
movq 0x10(%rbx), %rcx
movb $0x2c, (%rcx,%rax)
movq 0x10(%rbx), %rcx
movw %r8w, 0x1(%rcx,%rax)
movq 0x10(%rbx), %rcx
movw %r8w, 0x3(%rcx,%rax)
movq 0x10(%rbx), %rcx
movzwl 0x6(%rbx), %edx
movw %dx, 0x5(%rcx,%rax)
movq 0x10(%rbx), %rcx
movzwl 0x8(%rbx), %edx
movw %dx, 0x7(%rcx,%rax)
movq 0x10(%rbx), %rcx
movb %sil, 0x9(%rcx,%rax)
movq 0x10(%rbx), %rcx
movb %r11b, 0xa(%rcx,%rax)
cmpl $0x1f, %r11d
je 0x4ee6
cmpl $0x2, %r14d
movl $0x1, %eax
cmovgel %r14d, %eax
leaq 0x638(%rsp), %rcx
xorl %edx, %edx
movb %dl, (%rcx)
incq %rdx
movl %edx, -0x4(%rcx)
movl $0xffffffff, -0x8(%rcx) # imm = 0xFFFFFFFF
addq $0xc, %rcx
cmpq %rdx, %rax
jne 0x4ece
leal 0x1(%r14), %eax
movl %eax, (%rsp)
leal 0x1(%r11), %esi
movzbl (%r12), %eax
movzbl 0x30(%rsp,%rax), %ebp
movq 0x10(%rbx), %rax
addl $0xc, %edi
movb $0x0, (%rax,%r9)
cmpl $0x2, 0x4(%rsp)
jb 0x51d9
movq %r11, 0x8(%rsp)
movq %r10, 0x20(%rsp)
movq %r12, 0x28(%rsp)
leal 0x2(%r14), %r10d
cmpl $0x2, %r14d
movl $0x1, %eax
cmovgel %r14d, %eax
leaq -0x1(%rax), %rcx
addl $0x3, %eax
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl $0x2, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x10(%rsp)
xorl %r11d, %r11d
movl $0x1, %r13d
movdqa 0x64ec(%rip), %xmm1     # 0xb450
movdqa 0x64f4(%rip), %xmm2     # 0xb460
movdqa 0x64fc(%rip), %xmm3     # 0xb470
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x64fc(%rip), %xmm5     # 0xb480
movq %r14, 0x18(%rsp)
movl %r14d, %r12d
movl %esi, %edx
movslq %r13d, %r13
movl %ebp, %r14d
movq 0x28(%rsp), %rax
movzbl (%rax,%r13), %eax
incq %r13
movb 0x30(%rsp,%rax), %al
movslq %r14d, %rcx
leaq (%rcx,%rcx,2), %rcx
movl 0x630(%rsp,%rcx,4), %ebp
cmpl $-0x1, %ebp
je 0x51c9
movslq %ebp, %rcx
leaq (%rcx,%rcx,2), %rcx
cmpb %al, 0x638(%rsp,%rcx,4)
je 0x4fe2
leaq (%rsp,%rcx,4), %r8
addq $0x630, %r8             # imm = 0x630
movl 0x4(%r8), %ebp
cmpl $-0x1, %ebp
jne 0x4fbc
jmp 0x4ff1
movl %ebp, %r14d
cmpq 0x20(%rsp), %r13
jl 0x4f94
jmp 0x51dd
addq $0x4, %r8
movl %edx, %ecx
shll %cl, %r14d
movl %r10d, (%r8)
orl %r12d, %r14d
addl %esi, %edx
movslq %r10d, %r8
leaq (%r8,%r8,2), %rcx
movq $-0x1, 0x630(%rsp,%rcx,4)
movb %al, 0x638(%rsp,%rcx,4)
movl %esi, %ecx
sarl %cl, %r8d
testl %r8d, %r8d
je 0x514e
cmpl $0xc, %esi
jl 0x514c
movq 0x18(%rsp), %rcx
movl %ecx, %r8d
movl %edx, %ecx
shll %cl, %r8d
orl %r8d, %r14d
addl %esi, %edx
movl $0x1f, %esi
cmpl $0x1f, 0x8(%rsp)
jne 0x505a
movl (%rsp), %r10d
jmp 0x514c
xorl %ecx, %ecx
movdqa %xmm2, %xmm6
movdqa %xmm1, %xmm7
movq 0x10(%rsp), %r8
movdqa %xmm6, %xmm8
pxor %xmm3, %xmm8
movdqa %xmm8, %xmm9
pcmpgtd %xmm0, %xmm9
pcmpeqd %xmm0, %xmm8
pshufd $0xf5, %xmm8, %xmm10    # xmm10 = xmm8[1,1,3,3]
pand %xmm9, %xmm10
pshufd $0xf5, %xmm9, %xmm8     # xmm8 = xmm9[1,1,3,3]
por %xmm10, %xmm8
movd %xmm8, %esi
notl %esi
testb $0x1, %sil
je 0x50b0
movl $0xffffffff, 0x630(%rsp,%rcx) # imm = 0xFFFFFFFF
pxor %xmm4, %xmm8
pextrw $0x4, %xmm8, %esi
testb $0x1, %sil
je 0x50cc
movl $0xffffffff, 0x63c(%rsp,%rcx) # imm = 0xFFFFFFFF
movdqa %xmm7, %xmm8
pxor %xmm3, %xmm8
movdqa %xmm8, %xmm9
pcmpgtd %xmm0, %xmm9
pcmpeqd %xmm0, %xmm8
pshufd $0xf5, %xmm8, %xmm10    # xmm10 = xmm8[1,1,3,3]
pand %xmm9, %xmm10
pshufd $0xf5, %xmm9, %xmm8     # xmm8 = xmm9[1,1,3,3]
por %xmm10, %xmm8
pxor %xmm4, %xmm8
pextrw $0x0, %xmm8, %esi
testb $0x1, %sil
je 0x5117
movl $0xffffffff, 0x648(%rsp,%rcx) # imm = 0xFFFFFFFF
pextrw $0x4, %xmm8, %esi
testb $0x1, %sil
je 0x512e
movl $0xffffffff, 0x654(%rsp,%rcx) # imm = 0xFFFFFFFF
paddq %xmm5, %xmm6
paddq %xmm5, %xmm7
addq $0x30, %rcx
cmpq %rcx, %r8
jne 0x5069
movl (%rsp), %r10d
movq 0x8(%rsp), %rsi
incl %esi
cmpl $0x8, %edx
jl 0x51a7
movq 0x10(%rbx), %rcx
movslq %edi, %r15
movb %r14b, (%rcx,%r15)
incq %r15
leal 0x1(%r11), %r8d
cmpl $0xfe, %r11d
jge 0x5176
movl %r15d, %edi
movl %r9d, %r15d
jmp 0x5190
movq 0x10(%rbx), %rcx
movslq %r9d, %r9
movb %r8b, (%rcx,%r9)
movq 0x10(%rbx), %rcx
addl $0x2, %edi
movb $0x0, (%rcx,%r15)
xorl %r8d, %r8d
shrl $0x8, %r14d
addl $-0x8, %edx
leal 0x8(%rdx), %ecx
movl %r15d, %r9d
movl %r8d, %r11d
cmpl $0xf, %ecx
ja 0x5153
jmp 0x51ad
movl %r11d, %r8d
movl %r9d, %r15d
movzbl %al, %ebp
incl %r10d
movl %r15d, %r9d
movl %r8d, %r11d
movl %r14d, %r12d
cmpl %r13d, 0x4(%rsp)
jg 0x4f8e
jmp 0x51e6
leaq (%rsp,%rcx,4), %r8
addq $0x630, %r8             # imm = 0x630
jmp 0x4ff5
movl %esi, %edx
jmp 0x51e3
movl %r12d, %r14d
movl %r11d, %r8d
movl %r9d, %r15d
leal (%rsi,%rdx), %ecx
movl %ecx, %eax
addl %esi, %eax
jle 0x5252
movl (%rsp), %eax
shll %cl, %eax
movl %edx, %ecx
shll %cl, %ebp
orl %ebp, %r14d
orl %eax, %r14d
leal (%rdx,%rsi,2), %eax
addl $0x8, %eax
movq 0x10(%rbx), %rdx
movslq %edi, %rcx
movb %r14b, (%rdx,%rcx)
incq %rcx
leal 0x1(%r8), %edx
cmpl $0xfe, %r8d
jge 0x5226
movl %ecx, %edi
movl %r15d, %ecx
jmp 0x523e
movq 0x10(%rbx), %rsi
movslq %r15d, %r8
movb %dl, (%rsi,%r8)
movq 0x10(%rbx), %rdx
addl $0x2, %edi
movb $0x0, (%rdx,%rcx)
xorl %edx, %edx
shrl $0x8, %r14d
addl $-0x8, %eax
movl %ecx, %r15d
movl %edx, %r8d
cmpl $0x8, %eax
ja 0x5204
jmp 0x5258
movl %r8d, %edx
movl %r15d, %ecx
testl %edx, %edx
jle 0x5266
movq 0x10(%rbx), %rax
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq 0x10(%rbx), %rax
movslq %edi, %rcx
movb $0x0, (%rax,%rcx)
movq 0x10(%rbx), %rax
leal 0x2(%rcx), %edx
movb $0x3b, 0x1(%rax,%rcx)
movl %edx, 0x1c(%rbx)
xorl %eax, %eax
addq $0xc648, %rsp           # imm = 0xC648
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq