// File: /workspace/github_cpp_makefile_star2_O2/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 $0xc658, %rsp           # imm = 0xC658
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x40(%rsp), %rdi
xorl %r14d, %r14d
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x4130
leaq 0x140(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x4130
movzwl 0x6(%rbx), %eax
movzwl 0x8(%rbx), %edx
imull %eax, %edx
xorl %eax, %eax
cmpq %r14, %rdx
je 0x4d75
movzbl (%r12,%r14), %ecx
cmpb $0x0, 0x140(%rsp,%rcx)
jne 0x4d70
movb %al, 0x40(%rsp,%rcx)
movb $0x1, 0x140(%rsp,%rcx)
movl (%r15,%rcx,4), %ecx
cltq
movl %ecx, 0x240(%rsp,%rax,4)
incl %eax
incq %r14
jmp 0x4d41
movq %rdx, 0x20(%rsp)
cmpl $0x2, %eax
pushq $0x4
popq %rsi
cmovgel %eax, %esi
movq %rbx, %rdi
callq 0x5128
pushq $0x1
popq %rsi
movl %eax, %ecx
shll %cl, %esi
movq 0x10(%rbx), %rcx
movzwl 0x6(%rbx), %edx
movw %dx, 0x6(%rcx)
movq 0x10(%rbx), %rcx
movzwl 0x8(%rbx), %edx
movw %dx, 0x8(%rcx)
movq %rax, %r14
leal -0x1(%rax), %r9d
movl %r9d, %ecx
shll $0x4, %ecx
orl %r9d, %ecx
orb $-0x80, %cl
movq 0x10(%rbx), %rdx
movb %cl, 0xa(%rdx)
movq 0x10(%rbx), %rcx
xorl %edi, %edi
movb %dil, 0xb(%rcx)
movq 0x10(%rbx), %rcx
movb %dil, 0xc(%rcx)
testl %esi, %esi
movl $0x0, %ecx
movq %rsi, 0x28(%rsp)
cmovgl %esi, %ecx
leaq (%rcx,%rcx,2), %rsi
leaq 0x240(%rsp), %r8
cmpq %rdi, %rsi
je 0x4e24
movl (%r8), %edx
movl %edx, %r10d
shrl $0x10, %r10d
movq 0x10(%rbx), %r11
movb %r10b, 0xd(%r11,%rdi)
movq 0x10(%rbx), %rbp
movb %dh, 0xe(%rbp,%rdi)
movq 0x10(%rbx), %r10
movb %dl, 0xf(%r10,%rdi)
addq $0x3, %rdi
addq $0x4, %r8
jmp 0x4df1
movq 0x10(%rbx), %rdx
leal 0xd(%rdi), %esi
movb $0x2c, (%rdx,%rsi)
movq 0x10(%rbx), %rdx
andw $0x0, 0x1(%rdx,%rsi)
movq 0x10(%rbx), %rdx
andw $0x0, 0x3(%rdx,%rsi)
movq 0x10(%rbx), %rdx
movzwl 0x6(%rbx), %r8d
movw %r8w, 0x5(%rdx,%rsi)
movq 0x10(%rbx), %rdx
movzwl 0x8(%rbx), %r8d
movw %r8w, 0x7(%rdx,%rsi)
movq 0x10(%rbx), %rdx
movb %r9b, 0x9(%rdx,%rsi)
movq 0x10(%rbx), %rdx
movb %r14b, 0xa(%rdx,%rsi)
leaq 0x648(%rsp), %rdx
xorl %esi, %esi
movq 0x20(%rsp), %rax
cmpq %rsi, %rcx
je 0x4e9a
movb %sil, (%rdx)
incq %rsi
movl %esi, -0x4(%rdx)
orl $-0x1, -0x8(%rdx)
addq $0xc, %rdx
jmp 0x4e82
movq 0x28(%rsp), %rsi
leal 0x1(%rsi), %edx
movl %edx, 0x14(%rsp)
leal 0x18(%rdi), %r8d
leal 0x2(%rsi), %edx
movl %edx, 0x10(%rsp)
incl %r14d
movzbl (%r12), %edx
movb 0x40(%rsp,%rdx), %bpl
movq 0x10(%rbx), %rdx
addl $0x19, %edi
movq %r8, 0x18(%rsp)
movb $0x0, (%rdx,%r8)
imulq $0xc, %rcx, %r15
pushq $0x1
popq %r13
xorl %r10d, %r10d
movl %r14d, %edx
movl %esi, %r8d
movq %r14, 0x30(%rsp)
movl %r14d, 0xc(%rsp)
movq %r12, 0x38(%rsp)
movzbl %bpl, %r14d
movslq %r13d, %r13
cmpq %rax, %r13
jge 0x5063
movzbl (%r12,%r13), %ecx
incq %r13
movb 0x40(%rsp,%rcx), %bpl
movslq %r14d, %rcx
imulq $0xc, %rcx, %r9
movl 0x640(%rsp,%r9), %ecx
cmpl $-0x1, %ecx
je 0x5054
movslq %ecx, %r9
imulq $0xc, %r9, %r9
addq %rsp, %r9
addq $0x640, %r9             # imm = 0x640
cmpb %bpl, 0x8(%r9)
je 0x4f46
movl 0x4(%r9), %ecx
cmpl $-0x1, %ecx
jne 0x4f24
jmp 0x4f50
movl %ecx, %r14d
movq 0x20(%rsp), %rax
jmp 0x4ef6
addq $0x4, %r9
movl 0x10(%rsp), %eax
movl %eax, (%r9)
movl %edx, %ecx
shll %cl, %r14d
movslq %eax, %r9
imulq $0xc, %r9, %rcx
orq $-0x1, 0x640(%rsp,%rcx)
orl %r8d, %r14d
movl 0xc(%rsp), %esi
addl %esi, %edx
movb %bpl, 0x648(%rsp,%rcx)
movl %esi, %ecx
sarl %cl, %r9d
testl %r9d, %r9d
je 0x4fd0
cmpl $0xb, %esi
jle 0x4fb5
movq 0x28(%rsp), %rax
movl %eax, %r8d
movl %edx, %ecx
shll %cl, %r8d
orl %r8d, %r14d
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x4fbd
orl $-0x1, 0x640(%rsp,%rcx)
addq $0xc, %rcx
jmp 0x4fa2
incl %esi
movl %esi, 0xc(%rsp)
jmp 0x4fd0
addl %esi, %edx
movl 0x14(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x30(%rsp), %rax
movl %eax, 0xc(%rsp)
movl %r14d, %r8d
movslq %edi, %rcx
leal -0x1(%r10), %r12d
xorl %r14d, %r14d
cmpl $0x8, %edx
jl 0x503b
addl $-0x8, %edx
leaq 0x1(%r14), %r9
leaq (%r12,%r14), %rsi
incq %rsi
movl %r8d, %eax
shrl $0x8, %r8d
movq 0x10(%rbx), %r11
addq %rcx, %r11
movb %al, (%r14,%r11)
movq %r9, %r14
cmpl $0xfe, %esi
jl 0x4fdd
addl %r9d, %r10d
movq 0x10(%rbx), %rax
movslq 0x18(%rsp), %rcx
movb %r10b, (%rax,%rcx)
movq 0x10(%rbx), %rax
leal (%rdi,%r9), %ecx
leal 0x1(%rdi,%r9), %edi
movq %rcx, 0x18(%rsp)
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
xorl %r10d, %r10d
jmp 0x4fd3
incl 0x10(%rsp)
addl %r14d, %edi
addl %r14d, %r10d
movq 0x38(%rsp), %r12
movq 0x20(%rsp), %rax
jmp 0x4eef
addq %rsp, %r9
addq $0x640, %r9             # imm = 0x640
jmp 0x4f54
movl %edx, %ecx
shll %cl, %r14d
movl 0xc(%rsp), %esi
addl %esi, %edx
movl %edx, %ecx
movl 0x14(%rsp), %eax
shll %cl, %eax
orl %r8d, %r14d
orl %eax, %r14d
addl %esi, %edx
movq 0x18(%rsp), %r12
movslq %edi, %rax
leal -0x1(%r10), %r8d
xorl %ecx, %ecx
movslq %r12d, %rsi
testl %edx, %edx
jle 0x50e3
addl $-0x8, %edx
leaq 0x1(%rcx), %r9
leaq (%r8,%rcx), %r11
incq %r11
movl %r14d, %ebp
shrl $0x8, %r14d
movq 0x10(%rbx), %r15
addq %rax, %r15
movb %bpl, (%rcx,%r15)
movq %r9, %rcx
cmpl $0xfe, %r11d
jl 0x508c
addl %r9d, %r10d
movq 0x10(%rbx), %rax
movb %r10b, (%rax,%rsi)
movq 0x10(%rbx), %rax
leal (%rdi,%r9), %r12d
addl %r9d, %edi
incl %edi
movslq %r12d, %rcx
movb $0x0, (%rax,%rcx)
xorl %r10d, %r10d
jmp 0x5083
leal (%r10,%rcx), %edx
testl %edx, %edx
jle 0x50f6
addl %ecx, %r10d
movq 0x10(%rbx), %rdx
movb %r10b, (%rdx,%rsi)
movq 0x10(%rbx), %rdx
addq %rax, %rdx
movb $0x0, (%rcx,%rdx)
leal (%rax,%rcx), %edx
addl $0x2, %edx
addq 0x10(%rbx), %rax
movb $0x3b, 0x1(%rcx,%rax)
movl %edx, 0x1c(%rbx)
xorl %eax, %eax
addq $0xc658, %rsp           # imm = 0xC658
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop