// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xc8, %rsp
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
movq %rcx, 0xa0(%rsp)
movq %r8, 0x98(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, 0x40(%rsp)
jne 0x2b24
movl $0xffffffff, 0xc4(%rsp) # imm = 0xFFFFFFFF
jmp 0x305d
movq 0x80(%rsp), %rsi
movq %rsi, %rax
shlq $0x3, %rax
movq %rax, 0x20(%rsp)
movq 0x40(%rsp), %rdi
callq 0x3070
movq %rax, %rcx
movq 0x20(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, 0x2c(%rsp)
testq %rax, %rax
movss %xmm0, 0x30(%rsp)
js 0x2b96
movss 0x2c(%rsp), %xmm0
movss %xmm0, 0x30(%rsp)
movss 0x30(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
movq 0x80(%rsp), %rax
shlq $0x3, %rax
cvtsi2ss %rax, %xmm0
movss %xmm0, 0x18(%rsp)
movq %rax, %rcx
shrq %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
testq %rax, %rax
movss %xmm0, 0x1c(%rsp)
js 0x2bdf
movss 0x18(%rsp), %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x14(%rsp), %xmm0
movss 0x1c(%rsp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, 0x3c(%rsp)
movss 0x3c(%rsp), %xmm0
callq 0x20f0
cvtss2sd %xmm0, %xmm1
movsd 0x14cc(%rip), %xmm0     # 0x40d8
divsd %xmm1, %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
cvtsd2ss %xmm0, %xmm0
callq 0x2110
cvttss2si %xmm0, %eax
movl %eax, 0x74(%rsp)
cmpl $0x1, 0x74(%rsp)
jne 0x2c4a
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
movq 0x40(%rsp), %rdi
movq 0x80(%rsp), %rsi
leaq 0x58(%rsp), %rdx
leaq 0x78(%rsp), %rcx
callq 0x2580
cmpl $0x0, %eax
je 0x2c93
movq 0x3386(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x15ca(%rip), %rsi      # 0x4246
movb $0x0, %al
callq 0x2130
movl $0x1, 0xc4(%rsp)
jmp 0x305d
leaq 0xa004286(%rip), %rdi   # 0xa006f20
xorl %esi, %esi
movl $0x1000000, %edx        # imm = 0x1000000
callq 0x20c0
leaq 0xa004273(%rip), %rax   # 0xa006f20
movq %rax, 0x48(%rsp)
movq $0x0, 0x88(%rsp)
movl $0x0, 0x60(%rsp)
movl 0x74(%rsp), %edi
callq 0x2a40
movl %eax, 0x54(%rsp)
movl $0x0, 0x70(%rsp)
movslq 0x70(%rsp), %rax
cmpq 0x78(%rsp), %rax
jae 0x2f9f
movq 0x58(%rsp), %rax
movslq 0x70(%rsp), %rcx
movl (%rax,%rcx,4), %eax
subl $0x1, %eax
xorl %edx, %edx
divl 0x74(%rsp)
movl %eax, 0x6c(%rsp)
movq 0x58(%rsp), %rax
movslq 0x70(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl 0x6c(%rsp), %ecx
imull 0x74(%rsp), %ecx
subl %ecx, %eax
movl %eax, 0x68(%rsp)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x6c(%rsp)
cmpl $0x0, %eax
je 0x2dac
movl $0x7, %eax
subq 0x88(%rsp), %rax
movl %eax, %ecx
movl $0x1, %edx
shll %cl, %edx
movq 0x48(%rsp), %rax
movzbl (%rax), %ecx
orl %edx, %ecx
movb %cl, (%rax)
movq 0x88(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x88(%rsp)
cmpq $0x8, 0x88(%rsp)
jb 0x2daa
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movq 0x88(%rsp), %rax
subq $0x8, %rax
movq %rax, 0x88(%rsp)
jmp 0x2d2c
movq 0x88(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x88(%rsp)
cmpq $0x8, 0x88(%rsp)
jb 0x2df8
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movq 0x88(%rsp), %rax
subq $0x8, %rax
movq %rax, 0x88(%rsp)
movl 0x54(%rsp), %ecx
movl $0x1, %eax
shll %cl, %eax
subl 0x74(%rsp), %eax
movl %eax, 0x64(%rsp)
movl 0x68(%rsp), %eax
cmpl 0x64(%rsp), %eax
jle 0x2ed5
jmp 0x2e1b
movl 0x54(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x38(%rsp)
cmpl $-0x1, 0x38(%rsp)
jle 0x2ece
movl 0x68(%rsp), %eax
subl $0x1, %eax
addl 0x64(%rsp), %eax
movl 0x38(%rsp), %ecx
sarl %cl, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2e6e
movl $0x7, %eax
subq 0x88(%rsp), %rax
movl %eax, %ecx
movl $0x1, %edx
shll %cl, %edx
movq 0x48(%rsp), %rax
movzbl (%rax), %ecx
orl %edx, %ecx
movb %cl, (%rax)
jmp 0x2e70
jmp 0x2e70
movq 0x88(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x88(%rsp)
cmpq $0x8, 0x88(%rsp)
jb 0x2ebc
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movq 0x88(%rsp), %rax
subq $0x8, %rax
movq %rax, 0x88(%rsp)
jmp 0x2ebe
movl 0x38(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x2e26
jmp 0x2ed0
jmp 0x2f8d
jmp 0x2ed7
movl 0x54(%rsp), %eax
subl $0x1, %eax
subl $0x1, %eax
movl %eax, 0x34(%rsp)
cmpl $-0x1, 0x34(%rsp)
jle 0x2f89
movl 0x68(%rsp), %eax
subl $0x1, %eax
movl 0x34(%rsp), %ecx
sarl %cl, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2f29
movl $0x7, %eax
subq 0x88(%rsp), %rax
movl %eax, %ecx
movl $0x1, %edx
shll %cl, %edx
movq 0x48(%rsp), %rax
movzbl (%rax), %ecx
orl %edx, %ecx
movb %cl, (%rax)
jmp 0x2f2b
jmp 0x2f2b
movq 0x88(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x88(%rsp)
cmpq $0x8, 0x88(%rsp)
jb 0x2f77
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movq 0x88(%rsp), %rax
subq $0x8, %rax
movq %rax, 0x88(%rsp)
jmp 0x2f79
movl 0x34(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x2ee5
jmp 0x2f8b
jmp 0x2f8d
jmp 0x2f8f
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x2cdb
movslq 0x60(%rsp), %rcx
movq 0xa0(%rsp), %rax
movq %rcx, (%rax)
movq 0xa0(%rsp), %rax
movq (%rax), %rdi
callq 0x2180
movq 0xa8(%rsp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x2fe9
leaq 0x128e(%rip), %rdi      # 0x4265
callq 0x21e0
movl $0x1, 0xc4(%rsp)
jmp 0x305d
movq 0xa8(%rsp), %rax
movq (%rax), %rdi
movq %rdi, 0x8(%rsp)
movq 0xa0(%rsp), %rax
movq (%rax), %rdx
leaq 0xa003f15(%rip), %rsi   # 0xa006f20
callq 0x2150
movq 0x8(%rsp), %rax
cmpq $0x0, %rax
jne 0x3044
leaq 0x126c(%rip), %rdi      # 0x428e
callq 0x21e0
movq 0xa8(%rsp), %rax
movq (%rax), %rdi
callq 0x2030
movl $0x1, 0xc4(%rsp)
jmp 0x305d
movl 0x74(%rsp), %ecx
movq 0x98(%rsp), %rax
movl %ecx, (%rax)
movl $0x0, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
addq $0xc8, %rsp
retq
nopl (%rax)