// File: /workspace/github_c_makefile_star2_O2/ijleesw[P]data-compression/src/golomb/encode.c
// Function: golomb_encode
int
golomb_encode (void *input,
    unsigned long input_len,
    void **out,
    unsigned long *outsize,
    unsigned int *golomb_param)
{
  unsigned long setbits, currindex, size, rle_size;
  int b, i;
  int q, r;
  int d, bytecounter;
  unsigned int *rle;
  unsigned int log2_b;
  unsigned char *currbyte, *in;
  float prob;

  in = (unsigned char*) input;
  size = input_len;

  if (!in) return -1;

  setbits = size * 8 - num_set_bits (in, size); /* from bf.h */
  prob = (float)setbits / (float)(size * 8);

  //printf ("set bits in input: %lu\n", setbits);
  //b = (int) ceilf (LN2 * (float) (size * 8.0) / (float) setbits);
  b = (int) ceilf (-(LN2 / (float) (logf (prob))));
  if (b == 1) b++;
  //b = 3;

  /* b+=2; */

  //printf ("p = %f, b = %d\n",(float) setbits / (float) (size * 8.0), b);

  if (get_run_length_encoding (in, size, &rle, &rle_size) ) {
    fprintf (stderr, "golomb encode: error with RLE\n");
    return 1;
  }
  memset (golomb_buf, 0, sizeof(unsigned int) * MAX_INPUT_SIZE);
  currbyte = (unsigned char*) golomb_buf;
  currindex = 0; bytecounter = 0;

  /* this is for the ceil (log_{2} (b) ) 
   * needed in minimal binary decode
   * need to compute only once 
   */
  log2_b = ceil_log2 (b);

  //printf ("log2_b = %d\n", log2_b);

  /* main loop reading RLE input */
  for (i = 0; i < rle_size; ++i) {
    q = (rle[i] - 1) / b;
    r = rle[i] - q * b;

    //printf ("q: %d, r: %d\n", q, r);

    /* unary encode (q + 1) */
    ++q;
    while (--q) {
      *currbyte |= 1 << (7 - currindex);
      ++currindex;
      if (currindex >= 8) {
        currbyte++;
        bytecounter++;
        currindex -= 8;
      }
    }

    //printf ("after encoding quotient byte %d = %s %s\n",
        //bytecounter, h2b[*currbyte >> 4], h2b[*currbyte & 0x0f]);
    
    /* equivalent to putting the last 0 */
    currindex++;

    if (currindex >= 8) {
      currbyte++;
      bytecounter++;
      currindex -= 8;
    }

    /* minimal binary encode (r, b) */
    d = (1 << log2_b) - b;
    //printf ("d = %d\n", d);

    if (r > d) {
      /* put_one_integer (r - 1 + d, log2_b) */
      PUT_ONE_INTEGER (r - 1 + d, log2_b);
    } else {
      PUT_ONE_INTEGER (r - 1, log2_b - 1);
    }

    //printf ("after encoding remainder byte %d (%d)= %s %s\n",
        //bytecounter, currindex, h2b[*currbyte >> 4], h2b[*currbyte & 0x0f]);

  }

  *outsize = bytecounter;
  if ( !(*out = malloc (*outsize) ) ) {
    perror ("golombencode: cannot malloc output buf: ");
    return 1;
  }

  if ( !(memcpy (*out, golomb_buf, *outsize) ) ) {
    perror ("golombencode: cannot memcpy to output: ");
    free (*out);
    return 1;
  }

  //*golomb_param = log2_b;
  *golomb_param = b;

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
testq %rdi, %rdi
je 0x26fe
movq %r8, %rbx
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r13
xorl %ecx, %ecx
leaq 0x1a32(%rip), %rax      # 0x4100
xorl %edx, %edx
cmpq %rdx, %r12
je 0x26e7
movzbl (%r13,%rdx), %esi
incq %rdx
movzbl (%rsi,%rax), %esi
addq %rsi, %rcx
jmp 0x26d0
leaq (,%r12,8), %rax
movq %rax, %rdx
subq %rcx, %rdx
js 0x2707
cvtsi2ss %rdx, %xmm0
jmp 0x271c
pushq $-0x1
popq %r15
jmp 0x2900
movq %rdx, %rcx
shrq %rcx
andl $0x1, %edx
orq %rcx, %rdx
cvtsi2ss %rdx, %xmm0
addss %xmm0, %xmm0
testq %rax, %rax
js 0x2728
cvtsi2ss %rax, %xmm1
jmp 0x2734
shrq %rax
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
divss %xmm1, %xmm0
callq 0x20f0
cvtss2sd %xmm0, %xmm0
movsd 0x198f(%rip), %xmm1     # 0x40d8
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
callq 0x2110
cvttss2si %xmm0, %eax
cmpl $0x1, %eax
pushq $0x2
popq %rbp
cmovnel %eax, %ebp
leaq 0x18(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r13, %rdi
movq %r12, %rsi
callq 0x2470
testl %eax, %eax
jne 0x2912
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, (%rsp)
leaq 0xa00478b(%rip), %r13   # 0xa006f20
xorl %ebx, %ebx
movl $0x1000000, %edx        # imm = 0x1000000
movq %r13, %rdi
xorl %esi, %esi
callq 0x20c0
movl %ebp, %edi
callq 0x2679
movl %eax, %esi
pushq $0x1
popq %r15
movl %r15d, %edi
movl %esi, %ecx
shll %cl, %edi
subl %ebp, %edi
leal -0x2(%rsi), %r8d
leal -0x1(%rdi), %r9d
decl %esi
xorl %r10d, %r10d
xorl %r11d, %r11d
cmpq %rbx, 0x20(%rsp)
jbe 0x28c5
movq 0x18(%rsp), %rax
movl (%rax,%rbx,4), %r14d
leal -0x1(%r14), %eax
xorl %edx, %edx
divl %ebp
movl %eax, %edx
imull %ebp, %edx
incl %eax
decl %eax
je 0x2821
movb $0x7, %cl
subb %r11b, %cl
movl %r15d, %r12d
shll %cl, %r12d
orb %r12b, (%r13)
leaq 0x1(%r11), %rcx
addq $-0x7, %r11
xorl %r12d, %r12d
cmpq $0x8, %rcx
setae %r12b
cmovbq %rcx, %r11
addl %r12d, %r10d
addq %r12, %r13
jmp 0x27ef
subl %edx, %r14d
leaq 0x1(%r11), %rax
addq $-0x7, %r11
xorl %ecx, %ecx
cmpq $0x8, %rax
cmovbq %rax, %r11
setae %cl
addl %ecx, %r10d
addq %rcx, %r13
cmpl %edi, %r14d
jle 0x2880
addl %r9d, %r14d
movl %esi, %eax
testl %eax, %eax
js 0x28bd
btl %eax, %r14d
jae 0x2861
movb $0x7, %cl
subb %r11b, %cl
movl %r15d, %edx
shll %cl, %edx
orb %dl, (%r13)
leaq 0x1(%r11), %rcx
addq $-0x7, %r11
xorl %edx, %edx
cmpq $0x8, %rcx
cmovbq %rcx, %r11
setae %dl
addl %edx, %r10d
addq %rdx, %r13
decl %eax
jmp 0x2849
decl %r14d
movl %r8d, %eax
testl %eax, %eax
js 0x28bd
btl %eax, %r14d
jae 0x289e
movb $0x7, %cl
subb %r11b, %cl
movl %r15d, %edx
shll %cl, %edx
orb %dl, (%r13)
leaq 0x1(%r11), %rcx
addq $-0x7, %r11
xorl %edx, %edx
cmpq $0x8, %rcx
cmovbq %rcx, %r11
setae %dl
addl %edx, %r10d
addq %rdx, %r13
decl %eax
jmp 0x2886
incq %rbx
jmp 0x27cc
movslq %r10d, %r13
movq 0x8(%rsp), %rax
movq %r13, (%rax)
movq %r13, %rdi
callq 0x2170
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x2935
leaq 0xa004634(%rip), %rsi   # 0xa006f20
movq %rax, %rdi
movq %r13, %rdx
callq 0x2140
movq (%rsp), %rax
movl %ebp, (%rax)
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x36df(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x1900(%rip), %rdi      # 0x4223
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x21e0
pushq $0x1
jmp 0x2700
leaq 0x1906(%rip), %rdi      # 0x4242
callq 0x21d0
jmp 0x2900