// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movl %edx, 0x8c(%rsp)
movq %rcx, 0x80(%rsp)
movq %r8, 0x78(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x60(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x54(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0x3148
movl $0xffffffff, 0xa4(%rsp) # imm = 0xFFFFFFFF
jmp 0x344a
leaq 0xa003dd1(%rip), %rdi   # 0xa006f20
xorl %esi, %esi
movl $0x1000000, %edx        # imm = 0x1000000
callq 0x20c0
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0xa003db4(%rip), %rax   # 0xa006f20
movq %rax, 0x30(%rsp)
movq $0x0, 0x68(%rsp)
movl $0x0, 0x3c(%rsp)
movl $0x0, 0x48(%rsp)
movl $0x0, 0x4c(%rsp)
movl 0x8c(%rsp), %edi
callq 0x2a40
movl %eax, 0x2c(%rsp)
movslq 0x3c(%rsp), %rax
cmpq 0x60(%rsp), %rax
jae 0x33e1
movl $0x0, 0x48(%rsp)
movq 0x20(%rsp), %rax
movzbl (%rax), %eax
movq 0x68(%rsp), %rdx
movq %rdx, %rcx
addq $0x1, %rcx
movq %rcx, 0x68(%rsp)
movl $0x7, %ecx
subq %rdx, %rcx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x3227
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
cmpq $0x8, 0x68(%rsp)
jb 0x3225
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movq 0x68(%rsp), %rax
subq $0x8, %rax
movq %rax, 0x68(%rsp)
jmp 0x31ba
cmpq $0x8, 0x68(%rsp)
jb 0x3256
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movq 0x68(%rsp), %rax
subq $0x8, %rax
movq %rax, 0x68(%rsp)
movl 0x2c(%rsp), %ecx
movl $0x1, %eax
shll %cl, %eax
subl 0x54(%rsp), %eax
movl %eax, 0x40(%rsp)
movl $0x0, 0x4c(%rsp)
movl 0x2c(%rsp), %eax
subl $0x1, %eax
subl $0x1, %eax
movl %eax, 0xc(%rsp)
cmpl $-0x1, 0xc(%rsp)
jle 0x3314
movl 0x4c(%rsp), %eax
shll %eax
movq 0x20(%rsp), %rcx
movzbl (%rcx), %ecx
movl %ecx, 0x8(%rsp)
movl $0x7, %ecx
subq 0x68(%rsp), %rcx
movl $0x1, %edx
shll %cl, %edx
movl 0x8(%rsp), %ecx
andl %edx, %ecx
cmpl $0x0, %ecx
setg %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, 0x4c(%rsp)
movq 0x68(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x68(%rsp)
cmpq $0x8, 0x68(%rsp)
jb 0x3302
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movq 0x68(%rsp), %rax
subq $0x8, %rax
movq %rax, 0x68(%rsp)
jmp 0x3304
movl 0xc(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x327f
jmp 0x3316
movl 0x4c(%rsp), %eax
addl $0x1, %eax
cmpl 0x40(%rsp), %eax
jle 0x33af
movl 0x4c(%rsp), %eax
shll %eax
movq 0x20(%rsp), %rcx
movzbl (%rcx), %ecx
movl %ecx, 0x4(%rsp)
movl $0x7, %ecx
subq 0x68(%rsp), %rcx
movl $0x1, %edx
shll %cl, %edx
movl 0x4(%rsp), %ecx
andl %edx, %ecx
cmpl $0x0, %ecx
setg %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, 0x4c(%rsp)
movl 0x40(%rsp), %ecx
movl 0x4c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x4c(%rsp)
movq 0x68(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x68(%rsp)
cmpq $0x8, 0x68(%rsp)
jb 0x33ad
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movq 0x68(%rsp), %rax
subq $0x8, %rax
movq %rax, 0x68(%rsp)
jmp 0x33af
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %ecx
movl 0x48(%rsp), %eax
imull 0x54(%rsp), %eax
addl %eax, %ecx
movq 0x30(%rsp), %rax
movq %rax, %rdx
addq $0x4, %rdx
movq %rdx, 0x30(%rsp)
movl %ecx, (%rax)
jmp 0x31a2
movq 0x30(%rsp), %rax
leaq 0xa003b33(%rip), %rcx   # 0xa006f20
subq %rcx, %rax
sarq $0x2, %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rsi
subq $0x1, %rsi
movq 0x80(%rsp), %rdx
movq 0x78(%rsp), %rcx
leaq 0xa003b06(%rip), %rdi   # 0xa006f20
callq 0x28d0
cmpl $0x0, %eax
je 0x343f
leaq 0xe8b(%rip), %rdi       # 0x42b6
movb $0x0, %al
callq 0x2080
movl $0x1, 0xa4(%rsp)
jmp 0x344a
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
addq $0xa8, %rsp
retq
nopl (%rax)