// File: /workspace/github_c_makefile_star2_O3/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:
testq %rdi, %rdi
je 0x281c
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r14
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r13
leaq (,%rsi,8), %rax
testq %rsi, %rsi
je 0x2822
xorl %edx, %edx
leaq 0x18fe(%rip), %rsi      # 0x4100
xorl %r8d, %r8d
movzbl (%r13,%rdx), %edi
incq %rdx
movzbl (%rdi,%rsi), %edi
addq %rdi, %r8
cmpq %rdx, %r12
jne 0x2805
jmp 0x2825
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
retq
xorl %r8d, %r8d
movq %rax, %rdx
subq %r8, %rdx
js 0x2834
cvtsi2ss %rdx, %xmm0
jmp 0x2849
movq %rdx, %rsi
shrq %rsi
andl $0x1, %edx
orq %rsi, %rdx
cvtsi2ss %rdx, %xmm0
addss %xmm0, %xmm0
testq %rax, %rax
movq %rcx, 0x18(%rsp)
js 0x285a
cvtsi2ss %rax, %xmm1
jmp 0x286f
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
divss %xmm1, %xmm0
callq 0x20f0
cvtss2sd %xmm0, %xmm0
movsd 0x1854(%rip), %xmm1     # 0x40d8
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
callq 0x2110
cvttss2si %xmm0, %eax
cmpl $0x1, %eax
movl $0x2, %ebp
cmovnel %eax, %ebp
leaq 0x20(%rsp), %rdx
movq %rsp, %rcx
movq %r13, %rdi
movq %r12, %rsi
callq 0x2544
testl %eax, %eax
jne 0x2aa6
movq %rbx, 0x10(%rsp)
leaq 0xa004656(%rip), %r12   # 0xa006f20
xorl %ebx, %ebx
movl $0x1000000, %edx        # imm = 0x1000000
movq %r12, %rdi
xorl %esi, %esi
callq 0x20c0
testl %ebp, %ebp
je 0x2911
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %ebp, %ecx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %ebp, %edx
movl %esi, %edi
testb $0x1, %cl
movl %ebx, %esi
cmovel %edi, %esi
cmovnel %edi, %eax
sarl %edx
incl %ebx
cmpl $0x1, %ecx
movl %edx, %ecx
ja 0x28ed
xorl %ecx, %ecx
cmpl $-0x1, %eax
setne %cl
addl %ecx, %esi
jmp 0x2916
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
cmpq $0x0, (%rsp)
je 0x2a63
movq %r14, 0x8(%rsp)
movl $0x1, %edi
movl %esi, %ecx
shll %cl, %edi
subl %ebp, %edi
leal -0x2(%rsi), %r8d
leal -0x1(%rsi), %r9d
leal -0x1(%rdi), %r10d
xorl %r11d, %r11d
xorl %ebx, %ebx
movq 0x20(%rsp), %rax
movl (%rax,%r11,4), %r14d
leal -0x1(%r14), %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
cmpl %ecx, %ebp
ja 0x2990
movl %eax, %edx
movb $0x7, %cl
subb %bl, %cl
movl $0x1, %r15d
shll %cl, %r15d
orb %r15b, (%r12)
leaq 0x1(%rbx), %rcx
addq $-0x7, %rbx
xorl %r15d, %r15d
cmpq $0x8, %rcx
cmovbq %rcx, %rbx
setae %r15b
addl %r15d, %r13d
addq %r15, %r12
decl %edx
jne 0x295e
movq %r12, %rcx
imull %ebp, %eax
subl %eax, %r14d
leaq 0x1(%rbx), %rax
addq $-0x7, %rbx
xorl %r12d, %r12d
cmpq $0x8, %rax
cmovbq %rax, %rbx
setae %r12b
addl %r12d, %r13d
addq %rcx, %r12
cmpl %edi, %r14d
jle 0x2a07
testl %r9d, %r9d
js 0x2a4e
addl %r10d, %r14d
movl %esi, %eax
leal -0x1(%rax), %ecx
movzbl %cl, %ecx
btl %ecx, %r14d
jae 0x29e4
movb $0x7, %cl
subb %bl, %cl
movl $0x1, %edx
shll %cl, %edx
orb %dl, (%r12)
leaq 0x1(%rbx), %rcx
addq $-0x7, %rbx
xorl %edx, %edx
cmpq $0x8, %rcx
setae %dl
cmovbq %rcx, %rbx
addl %edx, %r13d
addq %rdx, %r12
decl %eax
testl %eax, %eax
jg 0x29c9
jmp 0x2a4e
testl %r8d, %r8d
js 0x2a4e
decl %r14d
movl %r9d, %eax
leal -0x1(%rax), %ecx
movzbl %cl, %ecx
btl %ecx, %r14d
jae 0x2a2d
movb $0x7, %cl
subb %bl, %cl
movl $0x1, %edx
shll %cl, %edx
orb %dl, (%r12)
leaq 0x1(%rbx), %rcx
addq $-0x7, %rbx
xorl %edx, %edx
cmpq $0x8, %rcx
setae %dl
cmovbq %rcx, %rbx
addl %edx, %r13d
addq %rdx, %r12
decl %eax
testl %eax, %eax
jg 0x2a12
incq %r11
cmpq %r11, (%rsp)
ja 0x2945
movslq %r13d, %r13
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %rax
movq %r13, (%rax)
movq %r13, %rdi
callq 0x2170
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x2aad
leaq 0xa004499(%rip), %rsi   # 0xa006f20
movq %rax, %rdi
movq %r13, %rdx
callq 0x2140
movl %ebp, (%r14)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x22ad
jmp 0x2ab9
leaq 0x178e(%rip), %rdi      # 0x4242
callq 0x21d0
movl $0x1, %eax
jmp 0x2a97