// File: /workspace/github_c_makefile_star2_O1/ijleesw[P]data-compression/src/golomb/encode.c
// Function: get_run_length_encoding
int
get_run_length_encoding (unsigned char *in, 
    unsigned long size,
    unsigned int **out,
    unsigned long *outsize)
{
  unsigned int *rle;
  int i, j;
  int rle_index = 0;
  int need_to_splice = 0;
  unsigned char allones = 255;

  rle = rle_buf;


  for (i = 0; i < size; ++i) {
    //if (i>=2904188) { printf ("got char %d, i = %d, r_l_s[in[i]] = %d\n", in[i], i, rle_lookup_sizes[in[i]]); fflush(stdout); }
    for (j = 0; j < rle_lookup_sizes[in[i]]; ++j) {
      //if (i>=2904188) { printf ("> j = %d\n", j); fflush(stdout); }
      if (!j && need_to_splice) {
        //printf ("splicing..");
        if (rle_lookup[in[i]][0] > 1) {
          /* this means that the first bit is a run of one or more
           * zeros.  so we can successfully splice with the previous set
           * */
          //assert (rle_index > 7);
          //printf ("increased %d with %d\n", 
              //rle[rle_index - 2], rle_lookup[in[i]][0]);
          rle[rle_index - 2] += rle_lookup[in[i]][0];
          rle_index--;
        } else { 
          /* darn, the first entry is a 1 (ie, the first bit is a 1), so
           * we cant splice, so move increase the value of the last
           * non-zero int in the rle, and move index back one so as to
           * overwrite the last 0 */
          rle[rle_index - 2]++;
          --rle_index; 
        }
        need_to_splice = 0;
      } else {
        //if (i>=2904188) printf ("> rie_index = %d\n", rle_index);
        rle[rle_index++] = rle_lookup[in[i]][j];
        //if (i>=2904188) printf ("> reached here too\n");
        //printf ("writing %d\n", rle_lookup[in[i]][j]);
      }
    }

    if (rle[rle_index - 1] == 0)
      need_to_splice = 1;
  }

  //printf ("got char %d\n", allones);
  for (j = 0; j < rle_lookup_sizes[allones]; ++j) {
    if (!j && need_to_splice) {
      //printf ("splicing..");
      if (rle_lookup[allones][0] > 1) {
        /* this means that the first bit is a run of one or more
         * zeros.  so we can successfully splice with the previous set
         * */
        //assert (rle_index > 7);
        //printf ("increased %d with %d\n", 
            //rle[rle_index - 2], rle_lookup[allones][0]);
        rle[rle_index - 2] += rle_lookup[allones][0];
        rle_index--;
      } else { 
        /* darn, the first entry is a 1 (ie, the first bit is a 1), so
         * we cant splice, so move index back one so as to overwrite
         * the last 0 */
        rle[rle_index - 2]++;
        --rle_index; 
      }
      need_to_splice = 0;
    } else {
      rle[rle_index++] = rle_lookup[allones][j];
      //printf ("writing %d\n", rle_lookup[allones][j]);
    }
  }

  if ( !(*out = malloc (sizeof (unsigned int) * (rle_index))) ) {
    perror ("cant malloc: ");
    return 1;
  }

  if ( !(memcpy (*out, rle, sizeof (unsigned int) * (rle_index))) ) {
    perror ("cant memcpy: ");
    free (*out);
    return 1;
  }

  *outsize = rle_index;
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rdx, %r14
testq %rsi, %rsi
je 0x25f6
xorl %edx, %edx
leaq 0x3c29(%rip), %r8       # 0x6190
leaq 0x40a2(%rip), %r9       # 0x6610
leaq 0x49ab(%rip), %r10      # 0x6f20
movl $0x1, %r11d
xorl %eax, %eax
xorl %ecx, %ecx
movzbl (%rdi,%rdx), %r12d
cmpl $0x0, (%r8,%r12,4)
jle 0x25df
xorl %r15d, %r15d
movslq %ecx, %r13
testq %r15, %r15
jne 0x25ba
testl %eax, %eax
je 0x25ba
leaq (%r12,%r12,8), %rax
movzbl (%r9,%rax), %eax
cmpb $0x1, %al
adcl $0x0, %eax
movzbl %al, %eax
addl %eax, -0x8(%r10,%r13,4)
decl %r13d
xorl %eax, %eax
movl %r13d, %ecx
jmp 0x25ce
leaq (%r12,%r12,8), %rcx
addq %r15, %rcx
movzbl (%r9,%rcx), %ebp
leal 0x1(%r13), %ecx
movl %ebp, (%r10,%r13,4)
incq %r15
movzbl (%rdi,%rdx), %r12d
movslq (%r8,%r12,4), %r13
cmpq %r13, %r15
jl 0x258e
movslq %ecx, %r15
cmpl $0x0, -0x4(%r10,%r15,4)
cmovel %r11d, %eax
incq %rdx
cmpq %rsi, %rdx
jne 0x257f
jmp 0x25fa
xorl %ecx, %ecx
xorl %eax, %eax
movslq 0x3f8b(%rip), %rdx      # 0x658c
testq %rdx, %rdx
jle 0x265c
movzbl 0x48fa(%rip), %esi      # 0x6f07
cmpb $0x1, %sil
adcl $0x0, %esi
movzbl %sil, %esi
xorl %edi, %edi
leaq 0x48ff(%rip), %r8       # 0x6f20
leaq 0x3fe8(%rip), %r9       # 0x6610
movslq %ecx, %r10
testq %rdi, %rdi
jne 0x2643
testl %eax, %eax
je 0x2643
addl %esi, -0x8(%r8,%r10,4)
decl %r10d
xorl %eax, %eax
movl %r10d, %ecx
jmp 0x2654
movzbl 0x8f7(%rdi,%r9), %r11d
leal 0x1(%r10), %ecx
movl %r11d, (%r8,%r10,4)
incq %rdi
cmpq %rdi, %rdx
jne 0x2628
movslq %ecx, %r12
leaq (,%r12,4), %r15
movq %r15, %rdi
callq 0x2170
movq %rax, (%r14)
testq %rax, %rax
je 0x269d
leaq 0x48a2(%rip), %rsi      # 0x6f20
movq %rax, %rdi
movq %r15, %rdx
callq 0x2140
movq %r12, (%rbx)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b5c(%rip), %rdi      # 0x4200
callq 0x21d0
movl $0x1, %eax
jmp 0x268e