// File: /workspace/github_c_makefile_star2_O3/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 0x2b33
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 0xa004434(%rip), %rdi   # 0xa006f20
xorl %r15d, %r15d
movl $0x1000000, %edx        # imm = 0x1000000
xorl %esi, %esi
callq 0x20c0
testl %ebp, %ebp
je 0x2b39
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %ebp, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ebp, %esi
movl %ecx, %edi
testb $0x1, %dl
movl %r15d, %ecx
cmovel %edi, %ecx
cmovnel %edi, %eax
sarl %esi
incl %r15d
cmpl $0x1, %edx
movl %esi, %edx
ja 0x2b0d
xorl %edx, %edx
cmpl $-0x1, %eax
setne %dl
addl %edx, %ecx
jmp 0x2b3e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
retq
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
leaq 0xa0043db(%rip), %rsi   # 0xa006f20
testq %r12, %r12
je 0x2c7d
movl $0x1, %eax
shll %cl, %eax
subl %ebp, %eax
leal -0x2(%rcx), %edx
decl %ecx
xorl %edi, %edi
leaq 0xa0043bb(%rip), %rsi   # 0xa006f20
xorl %r8d, %r8d
movzbl (%r13), %r9d
leaq 0x1(%rdi), %r10
movb $0x7, %r11b
subb %dil, %r11b
movzbl %r11b, %r11d
btl %r11d, %r9d
jae 0x2bc0
xorl %r9d, %r9d
incl %r9d
addq $-0x7, %rdi
xorl %r15d, %r15d
cmpq $0x8, %r10
setae %r15b
cmovbq %r10, %rdi
addl %r15d, %r8d
leaq (%r15,%r13), %r11
movzbl (%r13,%r15), %r15d
leaq 0x1(%rdi), %r10
movb $0x7, %r13b
subb %dil, %r13b
movzbl %r13b, %r13d
btl %r13d, %r15d
movq %r11, %r13
jb 0x2b84
jmp 0x2bc6
xorl %r9d, %r9d
movq %r13, %r11
addq $-0x7, %rdi
xorl %r15d, %r15d
cmpq $0x8, %r10
cmovbq %r10, %rdi
setae %r15b
addl %r15d, %r8d
leaq (%r15,%r11), %r13
xorl %r10d, %r10d
testl %edx, %edx
js 0x2c28
movl %ecx, %r11d
movzbl (%r13), %r15d
movb $0x7, %bl
subb %dil, %bl
movzbl %bl, %ebx
xorl %r14d, %r14d
btl %ebx, %r15d
setb %r14b
leal (%r14,%r10,2), %r10d
leaq 0x1(%rdi), %rbx
addq $-0x7, %rdi
xorl %r14d, %r14d
cmpq $0x8, %rbx
cmovbq %rbx, %rdi
setae %r14b
addl %r14d, %r8d
addq %r14, %r13
decl %r11d
jne 0x2bea
cmpl %eax, %r10d
jl 0x2c60
addl %r10d, %r10d
movzbl (%r13), %r11d
movl $0x7, %ebx
subl %edi, %ebx
subl %eax, %r10d
btl %ebx, %r11d
adcl $0x0, %r10d
leaq 0x1(%rdi), %r11
cmpq $0x8, %r11
jb 0x2c5d
incq %r13
incl %r8d
addq $-0x7, %rdi
jmp 0x2c60
movq %r11, %rdi
imull %ebp, %r9d
addl %r10d, %r9d
incl %r9d
movl %r9d, (%rsi)
addq $0x4, %rsi
movslq %r8d, %r9
cmpq %r12, %r9
jb 0x2b68
leaq 0xa00429c(%rip), %rdi   # 0xa006f20
subq %rdi, %rsi
sarq $0x2, %rsi
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
callq 0x26b0
testl %eax, %eax
je 0x2cb1
leaq 0x17aa(%rip), %rdi      # 0x444f
callq 0x2040
movl $0x1, %eax
jmp 0x2cb3
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq