// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdi, %rbx
leaq 0x30(%rsp), %rdi
xorl %r12d, %r12d
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), %r13d
imull %eax, %r13d
movl %r13d, %r9d
testl %r13d, %r13d
je 0x4d90
xorl %ecx, %ecx
xorl %r12d, %r12d
movzbl (%r14,%rcx), %edx
cmpb $0x0, 0x130(%rsp,%rdx)
jne 0x4d88
movb %r12b, 0x30(%rsp,%rdx)
movb $0x1, 0x130(%rsp,%rdx)
movl (%r15,%rdx,4), %edx
movslq %r12d, %r12
movl %edx, 0x230(%rsp,%r12,4)
incl %r12d
incq %rcx
cmpq %rcx, %r9
jne 0x4d5a
leal -0x1(%r12), %edx
cmpl $0x2, %r12d
movl $0x3, %ecx
cmovgel %edx, %ecx
leaq 0x56a8(%rip), %rdx      # 0xa450
cmpl $0x10, %ecx
jae 0x4dc1
movl %ecx, %esi
movzbl (%rsi,%rdx), %edx
testl %ecx, %ecx
movl $0x1, %r15d
cmovnel %edx, %r15d
jmp 0x4dd0
sarl $0x4, %ecx
movslq %ecx, %rcx
movzbl (%rcx,%rdx), %r15d
addl $0x4, %r15d
movl $0x1, %r10d
movl %r15d, %ecx
shll %cl, %r10d
movl $0x1, %edx
movq 0x10(%rbx), %rcx
movw %ax, 0x6(%rcx)
movq 0x10(%rbx), %rax
movzwl 0x8(%rbx), %ecx
movw %cx, 0x8(%rax)
leal -0x1(%r15), %esi
movl %esi, %eax
shll $0x4, %eax
orl %esi, %eax
orb $-0x80, %al
movq 0x10(%rbx), %rcx
movb %al, 0xa(%rcx)
movq 0x10(%rbx), %rax
xorl %ecx, %ecx
movb %cl, 0xb(%rax)
movq 0x10(%rbx), %rax
movb %cl, 0xc(%rax)
movl $0xd, %edi
cmpl $0x1f, %r15d
je 0x4e6c
cmpl $0x2, %r10d
movq %r10, %r11
cmovgel %r10d, %edx
movl $0xd, %edi
xorl %eax, %eax
movl 0x230(%rsp,%rax,4), %ecx
movl %ecx, %r8d
shrl $0x10, %r8d
movq 0x10(%rbx), %r10
movb %r8b, (%r10,%rdi)
movq 0x10(%rbx), %rbp
movb %ch, 0x1(%rbp,%rdi)
movq 0x10(%rbx), %r8
movb %cl, 0x2(%r8,%rdi)
addq $0x3, %rdi
incq %rax
cmpq %rax, %rdx
jne 0x4e36
movq %r11, %r10
movq 0x10(%rbx), %rcx
movl %edi, %eax
movb $0x2c, (%rcx,%rax)
movq 0x10(%rbx), %rcx
xorl %r12d, %r12d
movw %r12w, 0x1(%rcx,%rax)
movq 0x10(%rbx), %rcx
movw %r12w, 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 %r15b, 0xa(%rcx,%rax)
addq $0xb, %rax
cmpl $0x1f, %r15d
je 0x4ef3
cmpl $0x2, %r10d
movl $0x1, %ecx
cmovgel %r10d, %ecx
leaq 0x638(%rsp), %rdx
xorl %esi, %esi
movb %sil, (%rdx)
incq %rsi
movl %esi, -0x4(%rdx)
movl $0xffffffff, -0x8(%rdx) # imm = 0xFFFFFFFF
addq $0xc, %rdx
cmpq %rsi, %rcx
jne 0x4eda
leal 0x1(%r10), %ecx
movl %ecx, 0x4(%rsp)
leal 0x1(%r15), %esi
movzbl (%r14), %ecx
movzbl 0x30(%rsp,%rcx), %r11d
movq 0x10(%rbx), %rcx
addl $0xc, %edi
movb $0x0, (%rcx,%rax)
cmpl $0x2, %r13d
movq %rsi, 0x8(%rsp)
jb 0x50e6
movq %r15, 0x28(%rsp)
movl %r13d, 0x14(%rsp)
leal 0x2(%r10), %ecx
movl %ecx, (%rsp)
cmpl $0x2, %r10d
movl $0x1, %ecx
cmovgel %r10d, %ecx
shlq $0x2, %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rcx, 0x18(%rsp)
xorl %r8d, %r8d
movl $0x1, %r13d
movq %r10, 0x20(%rsp)
movl %r10d, %edx
movslq %r13d, %r13
movl %r11d, %ebp
movzbl (%r14,%r13), %ecx
incq %r13
movb 0x30(%rsp,%rcx), %r10b
movslq %r11d, %rcx
leaq (%rcx,%rcx,2), %rcx
movl 0x630(%rsp,%rcx,4), %r12d
cmpl $-0x1, %r12d
je 0x4fcd
movl %r12d, %r11d
movslq %r12d, %rcx
leaq (%rcx,%rcx,2), %r15
movb 0x638(%rsp,%r15,4), %cl
cmpb %r10b, %cl
je 0x4fc3
leaq (%rsp,%r15,4), %r15
addq $0x630, %r15            # imm = 0x630
movl 0x4(%r15), %r12d
cmpl $-0x1, %r12d
jne 0x4f87
cmpb %r10b, %cl
je 0x4fc3
cmpl $-0x1, 0x4(%r15)
je 0x50dd
cmpq %r9, %r13
jl 0x4f62
jmp 0x50eb
leaq (%rsp,%rcx,4), %r15
addq $0x630, %r15            # imm = 0x630
movl %esi, %ecx
shll %cl, %ebp
movl (%rsp), %r11d
movl %r11d, (%r15)
orl %edx, %ebp
movq 0x8(%rsp), %rcx
addl %ecx, %esi
movslq %r11d, %rdx
leaq (%rdx,%rdx,2), %r11
movq $-0x1, 0x630(%rsp,%r11,4)
movb %r10b, 0x638(%rsp,%r11,4)
sarl %cl, %edx
testl %edx, %edx
je 0x505c
cmpl $0xc, %ecx
movq 0x28(%rsp), %r11
movq %rcx, %rdx
jl 0x5058
movq 0x20(%rsp), %rcx
movq %rdx, %r15
movl %ecx, %edx
movl %esi, %ecx
shll %cl, %edx
orl %edx, %ebp
addl %r15d, %esi
cmpl $0x1f, %r11d
je 0x504e
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
movl $0xffffffff, 0x630(%rsp,%rcx) # imm = 0xFFFFFFFF
addq $0xc, %rcx
cmpq %rcx, %rdx
jne 0x503a
movl 0x4(%rsp), %ecx
movl %ecx, (%rsp)
movl %r11d, %edx
incl %edx
jmp 0x505f
movq %rcx, %rdx
cmpl $0x8, %esi
jl 0x50b6
movq 0x10(%rbx), %rcx
movslq %edi, %r15
movb %bpl, (%rcx,%r15)
incq %r15
leal 0x1(%r8), %r12d
cmpl $0xfe, %r8d
jge 0x5087
movl %r15d, %edi
movl %eax, %r15d
jmp 0x50a0
movq 0x10(%rbx), %rcx
cltq
movb %r12b, (%rcx,%rax)
movq 0x10(%rbx), %rax
addl $0x2, %edi
movb $0x0, (%rax,%r15)
xorl %r12d, %r12d
shrl $0x8, %ebp
addl $-0x8, %esi
leal 0x8(%rsi), %ecx
movl %r15d, %eax
movl %r12d, %r8d
cmpl $0xf, %ecx
jg 0x5064
jmp 0x50bc
movl %r8d, %r12d
movl %eax, %r15d
movq %rdx, 0x8(%rsp)
movzbl %r10b, %r11d
incl (%rsp)
movl %r15d, %eax
movl %r12d, %r8d
movl %ebp, %edx
cmpl %r13d, 0x14(%rsp)
jg 0x4f5f
jmp 0x50f3
addq $0x4, %r15
jmp 0x4fd8
movl %r10d, %ebp
jmp 0x50f0
movl %edx, %ebp
movl %r8d, %r12d
movl %eax, %r15d
movq 0x8(%rsp), %rdx
leal (%rdx,%rsi), %ecx
movl %ecx, %eax
addl %edx, %eax
jle 0x5165
movl 0x4(%rsp), %eax
shll %cl, %eax
movl %esi, %ecx
shll %cl, %r11d
orl %r11d, %ebp
orl %eax, %ebp
leal (%rsi,%rdx,2), %eax
addl $0x8, %eax
movq 0x10(%rbx), %rdx
movslq %edi, %rcx
movb %bpl, (%rdx,%rcx)
incq %rcx
leal 0x1(%r12), %edx
cmpl $0xfe, %r12d
jge 0x513a
movl %ecx, %edi
movl %r15d, %ecx
jmp 0x5152
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, %ebp
addl $-0x8, %eax
movl %ecx, %r15d
movl %edx, %r12d
cmpl $0x8, %eax
jg 0x5117
jmp 0x516b
movl %r12d, %edx
movl %r15d, %ecx
testl %edx, %edx
jle 0x5179
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
nop