// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
movb $-0x1, 0x17(%rsp)
leaq 0x496c(%rip), %rax      # 0x6f20
movq %rax, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movslq 0x24(%rsp), %rax
cmpq 0x40(%rsp), %rax
jae 0x2729
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
movq 0x48(%rsp), %rcx
movslq 0x24(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
movl %ecx, %edx
leaq 0x3b9c(%rip), %rcx      # 0x6190
cmpl (%rcx,%rdx,4), %eax
jge 0x26fa
cmpl $0x0, 0x20(%rsp)
jne 0x26a9
cmpl $0x0, 0x18(%rsp)
je 0x26a9
movq 0x48(%rsp), %rax
movslq 0x24(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x3fe6(%rip), %rax      # 0x6610
imulq $0x9, %rcx, %rcx
addq %rcx, %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
jle 0x267c
movq 0x48(%rsp), %rax
movslq 0x24(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x3fc0(%rip), %rax      # 0x6610
imulq $0x9, %rcx, %rcx
addq %rcx, %rax
movzbl (%rax), %edx
movq 0x28(%rsp), %rax
movl 0x1c(%rsp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x269f
movq 0x28(%rsp), %rax
movl 0x1c(%rsp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
jmp 0x26e8
movq 0x48(%rsp), %rax
movslq 0x24(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x3f50(%rip), %rax      # 0x6610
imulq $0x9, %rcx, %rcx
addq %rcx, %rax
movslq 0x20(%rsp), %rcx
movzbl (%rax,%rcx), %edx
movq 0x28(%rsp), %rax
movl 0x1c(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x1c(%rsp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x26ea
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x25d9
movq 0x28(%rsp), %rax
movl 0x1c(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x2717
movl $0x1, 0x18(%rsp)
jmp 0x2719
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x25c1
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
movzbl 0x17(%rsp), %ecx
movl %ecx, %edx
leaq 0x3a4d(%rip), %rcx      # 0x6190
cmpl (%rcx,%rdx,4), %eax
jge 0x282e
cmpl $0x0, 0x20(%rsp)
jne 0x27e6
cmpl $0x0, 0x18(%rsp)
je 0x27e6
movzbl 0x17(%rsp), %eax
movl %eax, %ecx
leaq 0x3ea0(%rip), %rax      # 0x6610
imulq $0x9, %rcx, %rcx
addq %rcx, %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
jle 0x27b9
movzbl 0x17(%rsp), %eax
movl %eax, %ecx
leaq 0x3e83(%rip), %rax      # 0x6610
imulq $0x9, %rcx, %rcx
addq %rcx, %rax
movzbl (%rax), %edx
movq 0x28(%rsp), %rax
movl 0x1c(%rsp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x27dc
movq 0x28(%rsp), %rax
movl 0x1c(%rsp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
jmp 0x281c
movzbl 0x17(%rsp), %eax
movl %eax, %ecx
leaq 0x3e1c(%rip), %rax      # 0x6610
imulq $0x9, %rcx, %rcx
addq %rcx, %rax
movslq 0x20(%rsp), %rcx
movzbl (%rax,%rcx), %edx
movq 0x28(%rsp), %rax
movl 0x1c(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x1c(%rsp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x281e
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x2731
movslq 0x1c(%rsp), %rdi
shlq $0x2, %rdi
callq 0x2180
movq 0x38(%rsp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x2860
leaq 0x19af(%rip), %rdi      # 0x4200
callq 0x21e0
movl $0x1, 0x54(%rsp)
jmp 0x28c3
movq 0x38(%rsp), %rax
movq (%rax), %rdi
movq %rdi, 0x8(%rsp)
movq 0x28(%rsp), %rsi
movslq 0x1c(%rsp), %rdx
shlq $0x2, %rdx
callq 0x2150
movq 0x8(%rsp), %rax
cmpq $0x0, %rax
jne 0x28ae
leaq 0x197c(%rip), %rdi      # 0x420e
callq 0x21e0
movq 0x38(%rsp), %rax
movq (%rax), %rdi
callq 0x2030
movl $0x1, 0x54(%rsp)
jmp 0x28c3
movslq 0x1c(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopl (%rax)