// File: /workspace/github_c_makefile_star2_O0/ijleesw[P]data-compression/src/golomb/encode.c
// Function: zlib_decode
int
zlib_decode (void *input, unsigned long input_len, 
    void **output, unsigned long *output_len) 
{
  int ret;
  unsigned have;
  z_stream strm;
  //unsigned char in[CHUNK];
  //unsigned char out[CHUNK];
  unsigned char *in;
  unsigned char *out_head, *out;
  unsigned long bytes_left, bytes_written;
  unsigned long output_bytes, output_bytes_left;
  int buf_realloc_penalty = BUF_REALLOC_PENALTY;

  bytes_left = input_len;
  bytes_written = 0;
  in = (unsigned char*) input;

  /* allocate a reasonable amount of space */

  output_bytes = input_len > CHUNK_2 ? input_len : CHUNK_2;
  if ( !(out_head = (unsigned char*) malloc (output_bytes)) ) {
    printf ("decode: malloc failed\n");
    goto decode_error_save;
  }
  out = out_head;
  output_bytes_left = output_bytes;

  /* allocate inflate state */
  strm.zalloc = Z_NULL;
  strm.zfree = Z_NULL;
  strm.opaque = Z_NULL;
  strm.avail_in = 0;
  strm.next_in = Z_NULL;
  ret = inflateInit(&strm);
  if (ret != Z_OK) {
    //return ret;
    return 1;
  }

  /* decompress until deflate stream ends or end of file */
  do {
    //strm.avail_in = fread(in, 1, CHUNK, source);
    if (bytes_left > CHUNK) {
      strm.avail_in = CHUNK;
      strm.next_in = in;
      in += CHUNK;
      //bytes_left -= CHUNK;
    } else if (bytes_left == 0) {
      printf ("hmm, shouldnt have come here?\n");
      break;
    } else {
      strm.avail_in = bytes_left;
      strm.next_in = in;
      in += bytes_left;
      //bytes_left = 0;
    }

    //printf ("decode: going ahead with %d bytes\n", strm.avail_in);
    //if (ferror(source)) {
      //(void)inflateEnd(&strm);
      //return Z_ERRNO;
    //}
    //if (strm.avail_in == 0)
      //break;
    //strm.next_in = in;

    /* run inflate() on input until output buffer not full */
    do {
      /* to make sure we have at least CHUNK bytes left in out buffer */
      //assert (bytes_written + CHUNK <= input_len);
      //printf ("curr size: %lu, in bytes left: %lu, out bytes left: %lu\n",
          //output_bytes, bytes_left, output_bytes_left);
      if (bytes_left && output_bytes_left < CHUNK_2) {
        unsigned extra = buf_realloc_penalty*CHUNK;
        unsigned offset = out - out_head;
        if (!(out_head = realloc (out_head, output_bytes + extra)) ) {
          printf ("allocing more bytes didnt work");
          goto decode_error_save;
        }
        output_bytes += extra;
        output_bytes_left += extra;
        out = out_head + offset;
        buf_realloc_penalty *= BUF_REALLOC_PENALTY;
      }
         
      strm.avail_out = CHUNK;
      strm.next_out = out;
      ret = inflate(&strm, Z_NO_FLUSH);
      assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
      switch (ret) {
        case Z_NEED_DICT:
          ret = Z_DATA_ERROR;     /* and fall through */
        case Z_DATA_ERROR:
        case Z_MEM_ERROR:
          (void)inflateEnd(&strm);
          //return ret;
          return 1;
      }
      have = CHUNK - strm.avail_out;

      out += have;
      bytes_written += have;
      output_bytes_left -= have;

      //if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
        //(void)inflateEnd(&strm);
        //return Z_ERRNO;
      //}
    } while (strm.avail_out == 0);

    bytes_left = (bytes_left > CHUNK) ? (bytes_left - CHUNK) : 0;
      
    /* done when inflate() says it's done */
  } while (ret != Z_STREAM_END);

  /* clean up and return */
  (void)inflateEnd(&strm);

  /* prepare stuff to be returned */
  if (bytes_written < input_len)
    printf ("in decode: encoding sucks: input was %lu but "
        "output is %lu\n", input_len, bytes_written);

  if ( !(out_head = realloc (out_head, bytes_written)) )  {
    perror ("realloc failed: ");
    goto decode_error_save;
  }
  *output = out_head;
  *output_len = bytes_written;

  //return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  return ret == Z_STREAM_END ? 0 : -1;

decode_error_save:
  if (out_head) free (out_head);
  *output_len = 0;
  return 1;
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0xf8(%rsp)
movq %rsi, 0xf0(%rsp)
movq %rdx, 0xe8(%rsp)
movq %rcx, 0xe0(%rsp)
movl $0x3, 0x2c(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpq $0x4000, 0xf0(%rsp) # imm = 0x4000
jbe 0x38df
movq 0xf0(%rsp), %rax
movq %rax, 0x18(%rsp)
jmp 0x38eb
movl $0x4000, %eax           # imm = 0x4000
movq %rax, 0x18(%rsp)
jmp 0x38eb
movq 0x18(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
callq 0x2180
movq %rax, 0x58(%rsp)
cmpq $0x0, %rax
jne 0x391d
leaq 0xabf(%rip), %rdi       # 0x43d0
movb $0x0, %al
callq 0x2080
jmp 0x3c80
movq 0x58(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0xa8(%rsp)
movq $0x0, 0xb0(%rsp)
movq $0x0, 0xb8(%rsp)
movl $0x0, 0x70(%rsp)
movq $0x0, 0x68(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x971(%rip), %rsi       # 0x42e3
movl $0x70, %edx
callq 0x21c0
movl %eax, 0xdc(%rsp)
cmpl $0x0, 0xdc(%rsp)
je 0x399d
movl $0x1, 0x104(%rsp)
jmp 0x3cac
jmp 0x399f
cmpq $0x2000, 0x48(%rsp)     # imm = 0x2000
jbe 0x39ce
movl $0x2000, 0x70(%rsp)     # imm = 0x2000
movq 0x60(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x60(%rsp), %rax
addq $0x2000, %rax           # imm = 0x2000
movq %rax, 0x60(%rsp)
jmp 0x3a0d
cmpq $0x0, 0x48(%rsp)
jne 0x39e9
leaq 0x90d(%rip), %rdi       # 0x42ea
movb $0x0, %al
callq 0x2080
jmp 0x3be7
movq 0x48(%rsp), %rax
movl %eax, 0x70(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x48(%rsp), %rax
addq 0x60(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0x3a0d
jmp 0x3a0f
cmpq $0x0, 0x48(%rsp)
je 0x3ab0
cmpq $0x4000, 0x30(%rsp)     # imm = 0x4000
jae 0x3ab0
movl 0x2c(%rsp), %eax
shll $0xd, %eax
movl %eax, 0x28(%rsp)
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rcx
subq %rcx, %rax
movl %eax, 0x24(%rsp)
movq 0x58(%rsp), %rdi
movq 0x38(%rsp), %rsi
movl 0x28(%rsp), %eax
addq %rax, %rsi
callq 0x21b0
movq %rax, 0x58(%rsp)
cmpq $0x0, %rax
jne 0x3a7a
leaq 0x89b(%rip), %rdi       # 0x4309
movb $0x0, %al
callq 0x2080
jmp 0x3c80
movl 0x28(%rsp), %eax
addq 0x38(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0x28(%rsp), %eax
addq 0x30(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x58(%rsp), %rax
movl 0x24(%rsp), %ecx
addq %rcx, %rax
movq %rax, 0x50(%rsp)
imull $0x3, 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl $0x2000, 0x88(%rsp) # imm = 0x2000
movq 0x50(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x2040
movl %eax, 0xdc(%rsp)
cmpl $-0x2, 0xdc(%rsp)
je 0x3ae7
jmp 0x3b06
leaq 0x83a(%rip), %rdi       # 0x4328
leaq 0x849(%rip), %rsi       # 0x433e
movl $0x481, %edx            # imm = 0x481
leaq 0x8e6(%rip), %rcx       # 0x43e7
callq 0x2090
movl 0xdc(%rsp), %eax
movl %eax, 0x14(%rsp)
addl $0x4, %eax
subl $0x2, %eax
jb 0x3b31
jmp 0x3b1b
movl 0x14(%rsp), %eax
subl $0x2, %eax
jne 0x3b4b
jmp 0x3b26
movl $0xfffffffd, 0xdc(%rsp) # imm = 0xFFFFFFFD
leaq 0x68(%rsp), %rdi
callq 0x2160
movl $0x1, 0x104(%rsp)
jmp 0x3cac
movl $0x2000, %eax           # imm = 0x2000
subl 0x88(%rsp), %eax
movl %eax, 0xd8(%rsp)
movl 0xd8(%rsp), %ecx
movq 0x50(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x50(%rsp)
movl 0xd8(%rsp), %eax
addq 0x40(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0xd8(%rsp), %eax
movl %eax, %ecx
movq 0x30(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x30(%rsp)
cmpl $0x0, 0x88(%rsp)
je 0x3a0f
cmpq $0x2000, 0x48(%rsp)     # imm = 0x2000
jbe 0x3bc6
movq 0x48(%rsp), %rax
subq $0x2000, %rax           # imm = 0x2000
movq %rax, 0x8(%rsp)
jmp 0x3bcf
xorl %eax, %eax
movq %rax, 0x8(%rsp)
jmp 0x3bcf
movq 0x8(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpl $0x1, 0xdc(%rsp)
jne 0x399f
leaq 0x68(%rsp), %rdi
callq 0x2160
movq 0x40(%rsp), %rax
cmpq 0xf0(%rsp), %rax
jae 0x3c1b
movq 0xf0(%rsp), %rsi
movq 0x40(%rsp), %rdx
leaq 0x814(%rip), %rdi       # 0x4428
movb $0x0, %al
callq 0x2080
movq 0x58(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x21b0
movq %rax, 0x58(%rsp)
cmpq $0x0, %rax
jne 0x3c43
leaq 0x783(%rip), %rdi       # 0x43bf
callq 0x21e0
jmp 0x3c80
movq 0x58(%rsp), %rcx
movq 0xe8(%rsp), %rax
movq %rcx, (%rax)
movq 0x40(%rsp), %rcx
movq 0xe0(%rsp), %rax
movq %rcx, (%rax)
movl 0xdc(%rsp), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, 0x104(%rsp)
jmp 0x3cac
cmpq $0x0, 0x58(%rsp)
je 0x3c92
movq 0x58(%rsp), %rdi
callq 0x2030
movq 0xe0(%rsp), %rax
movq $0x0, (%rax)
movl $0x1, 0x104(%rsp)
movl 0x104(%rsp), %eax
addq $0x108, %rsp            # imm = 0x108
retq
nopl (%rax,%rax)