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

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

  if (!in) return -1;

  memset (golomb_buf, 0, sizeof (unsigned int) * MAX_INPUT_SIZE);

  currbyte = (unsigned char*) in;
  gd = (unsigned int*) golomb_buf;

  currindex = 0; bytecounter = 0;
  q = 0;
  x = 0;

  /* log2 hack */
  log2_b = ceil_log2 (golomb_param);

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

  while (bytecounter < size) {
    //printf ("byte: %d: %s %s\n", bytecounter,
        //h2b[*currbyte >> 4], h2b[*currbyte & 0x0f]);

    /* q := unary_decode() - 1 */
    q = 0;
    while (*currbyte & (1 << (7 - currindex++))) {
      ++q;
      if (currindex >= 8) {
        currbyte++;
        bytecounter++;
        currindex -= 8;
      }
    }
    //--q;


    /* sanity check */
    if (currindex >= 8) {
      currbyte++;
      bytecounter++;
      currindex -= 8;
    }

    //printf ("got q: %d, currbyte: %d, currindex: %d\n", 
        //q, bytecounter, currindex);

    /* r = minimal_binary_decode (b) */
    d = (1 << log2_b) - b;

    //printf ("d: %d, log2_b-1: %d\n", d, log2_b-1);

    GET_ONE_INTEGER (log2_b - 1);
    //printf ("got integer: %d\n", x);

    //printf ("x+1 (%d) ~ d (%d)\n", x+1, d);
    if (x + 1 > d) {
      x = (x << 1) | ((*currbyte & (1 << (7 - currindex))) > 0);
      x -= d;
      currindex++;
      if (currindex >= 8) {
        currbyte++;
        bytecounter++;
        currindex -= 8;
      }
    }

    r = x + 1;

    //printf ("got remainder: %d, currbyte: %d, currindex: %d\n", 
        //r, bytecounter, currindex);

    /* decode integer */
    *gd++ = r + q * b;
    //printf ("got answer: %d\n", r + q * b);
  }

  decoded_rle_size =  gd - golomb_buf + 1;
  
  if (get_run_length_decoding (golomb_buf, decoded_rle_size - 1, 
        (unsigned char**)out, 
        outsize) ) {
    printf ("Golomb decoding worked; RLE decoding failed\n");
    return 1;
  }


  /*
  if ( !(*out = malloc (*outsize * sizeof (unsigned int)) ) ) {
    perror ("golombdecode: cannot malloc output buf: ");
    return 1;
  }

  if ( !(memcpy (*out, golomb_buf, *outsize * sizeof (unsigned int)) ) ) {
    perror ("golombdecode: cannot memcpy to output: ");
    free (*out);
    return 1;
  }
  */

  return 0;
}

// Assembly:
testq %rdi, %rdi
je 0x2aba
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r13
movq %rcx, 0x8(%rsp)
movq %r8, 0x10(%rsp)
leaq 0xa0045ad(%rip), %r15   # 0xa006f20
xorl %ebx, %ebx
movl $0x1000000, %edx        # imm = 0x1000000
movq %r15, %rdi
xorl %esi, %esi
callq 0x20c0
movl %ebp, %edi
callq 0x2679
pushq $0x1
popq %rdx
movl %edx, %esi
movl %eax, %ecx
shll %cl, %esi
subl %ebp, %esi
addl $-0x2, %eax
xorl %edi, %edi
movslq %ebx, %r8
cmpq %r12, %r8
jae 0x2a85
movl %edx, %r8d
movzbl (%r13), %r10d
leaq 0x1(%rdi), %r9
movb $0x7, %r11b
subb %dil, %r11b
movzbl %r11b, %r11d
addq $-0x7, %rdi
btl %r11d, %r10d
jae 0x29e1
xorl %r10d, %r10d
cmpq $0x8, %r9
cmovbq %r9, %rdi
setae %r10b
addl %r10d, %ebx
addq %r10, %r13
addl %ebp, %r8d
jmp 0x29aa
xorl %r10d, %r10d
cmpq $0x8, %r9
setae %r10b
cmovbq %r9, %rdi
addl %r10d, %ebx
addq %r10, %r13
xorl %r9d, %r9d
movl %eax, %r10d
testl %r10d, %r10d
js 0x2a3f
movzbl (%r13), %r11d
movb $0x7, %r14b
subb %dil, %r14b
movzbl %r14b, %r14d
xorl %ecx, %ecx
btl %r14d, %r11d
setb %cl
leal (%rcx,%r9,2), %r9d
leaq 0x1(%rdi), %rcx
addq $-0x7, %rdi
xorl %r11d, %r11d
cmpq $0x8, %rcx
cmovbq %rcx, %rdi
setae %r11b
addl %r11d, %ebx
addq %r11, %r13
decl %r10d
jmp 0x29fc
cmpl %esi, %r9d
jl 0x2a76
addl %r9d, %r9d
movzbl (%r13), %ecx
pushq $0x7
popq %r10
subl %edi, %r10d
subl %esi, %r9d
btl %r10d, %ecx
adcl $0x0, %r9d
leaq 0x1(%rdi), %r10
cmpq $0x8, %r10
jb 0x2a73
incq %r13
incl %ebx
addq $-0x7, %rdi
jmp 0x2a76
movq %r10, %rdi
addl %r8d, %r9d
movl %r9d, (%r15)
addq $0x4, %r15
jmp 0x299b
leaq 0xa004494(%rip), %rdi   # 0xa006f20
subq %rdi, %r15
sarq $0x2, %r15
movq %r15, %rsi
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
callq 0x25b0
testl %eax, %eax
je 0x2abe
leaq 0x199f(%rip), %rdi      # 0x444f
callq 0x2040
pushq $0x1
popq %rax
jmp 0x2ac0
pushq $-0x1
popq %rax
retq
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq