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

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

  /* allocate exactly enough space to out as in */
  /* XXX this is probably too much space, since we're encoding
   * what are the performance hits of large malloc, i wonder... */
  if ( !(out_head = (unsigned char*) malloc (input_len)) ) {
    printf ("malloc failed\n");
    goto encode_error_save;
  }
  out = out_head;
  output_bytes_left = input_len;

  /* allocate deflate state */
  strm.zalloc = Z_NULL;
  strm.zfree = Z_NULL;
  strm.opaque = Z_NULL;
  ret = deflateInit(&strm, level);
  if (ret != Z_OK) {
    //return ret;
    return 1;
  }

  /* compress until 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;
      flush = Z_NO_FLUSH;
    } 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;
      flush = Z_FINISH;
    }

    //printf ("going ahead with %d bytes\n", strm.avail_in);

    //if (ferror(source)) {
      //(void)deflateEnd(&strm);
      //return Z_ERRNO;
    //}

    //flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
    //strm.next_in = in;

    /* run deflate() on input until output buffer not full, finish
       compression if all of source has been read in */
    do {

      /* to take care of the case when output is not big enough */
      if (bytes_left && output_bytes_left <= bytes_left) {
        unsigned extra = buf_realloc_penalty*CHUNK;
        unsigned offset = out - out_head;
        if (!(out_head = realloc (out_head, input_len + extra)) ) {
          printf ("allocing more bytes didnt work");
          goto encode_error_save;
        }
        out = out_head + offset;
        output_bytes_left += extra;
        buf_realloc_penalty *= BUF_REALLOC_PENALTY;
      }

      strm.avail_out = CHUNK;
      strm.next_out = out;
      ret = deflate(&strm, flush);    /* no bad return value */
      assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
      have = CHUNK - strm.avail_out;
      out += have;

      bytes_written += have;

      //if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
        //(void)deflateEnd(&strm);
        //return Z_ERRNO;
      //}
    } while (strm.avail_out == 0);
    assert(strm.avail_in == 0);     /* all input will be used */

    bytes_left = (bytes_left > CHUNK) ? (bytes_left - CHUNK) : 0;
    /* done when last data in file processed */
  } while (flush != Z_FINISH);
  assert(ret == Z_STREAM_END);        /* stream will be complete */

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

  /* general sanity checks and cleanup */
  if (bytes_written > input_len)
    printf ("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 encode_error_save;
  }
  *output = out_head;
  *output_len = bytes_written;
  return 0;

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %r8d, %ebx
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, %r13
movq %rsi, %rdi
callq 0x2170
testq %rax, %rax
je 0x2e63
movq %rax, %r15
xorps %xmm0, %xmm0
leaq 0x38(%rsp), %rdi
movups %xmm0, 0x40(%rdi)
movq $0x0, 0x50(%rdi)
leaq 0x1562(%rip), %rdx      # 0x426b
movl %ebx, %esi
movl $0x70, %ecx
callq 0x2120
movl %eax, %ecx
movl $0x1, %eax
testl %ecx, %ecx
jne 0x2e89
movq %r13, 0x10(%rsp)
movq %r12, 0x28(%rsp)
movq %r14, 0x8(%rsp)
movl $0x3, %ebx
leaq 0x38(%rsp), %r14
movq %r15, %r13
movq %rbp, %rdx
xorl %r12d, %r12d
movq %rbp, 0x18(%rsp)
movq %rbp, 0x20(%rsp)
xorl %ebp, %ebp
cmpq $0x2001, %rdx           # imm = 0x2001
jae 0x2d75
testq %rdx, %rdx
je 0x2e9b
movl %edx, 0x40(%rsp)
movl $0x4, 0x4(%rsp)
movq %rdx, %rax
jmp 0x2d8a
movl $0x2000, 0x40(%rsp)     # imm = 0x2000
movl $0x0, 0x4(%rsp)
movl $0x2000, %eax           # imm = 0x2000
movq 0x10(%rsp), %rcx
movq %rcx, 0x38(%rsp)
addq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq %rdx, 0x30(%rsp)
testq %rdx, %rdx
je 0x2dfe
cmpq %rdx, 0x20(%rsp)
ja 0x2dfe
movq %r15, %r14
movq %rbx, %r15
shll $0xd, %ebx
movq 0x18(%rsp), %rax
leaq (%rbx,%rax), %rsi
movq %r14, %rdi
callq 0x21a0
movq %rax, %rbp
testq %rax, %rax
je 0x2de0
subl %r14d, %r13d
addq %rbp, %r13
addq %rbx, 0x20(%rsp)
leal (%r15,%r15,2), %r15d
jmp 0x2dee
leaq 0x148b(%rip), %rdi      # 0x4272
xorl %eax, %eax
callq 0x2080
movq %r15, %rbx
leaq 0x38(%rsp), %r14
movq %rbp, %r15
testq %rbp, %rbp
je 0x2e79
movl $0x2000, 0x58(%rsp)     # imm = 0x2000
movq %r13, 0x50(%rsp)
movq %r14, %rdi
movl 0x4(%rsp), %esi
callq 0x20b0
cmpl $-0x2, %eax
je 0x2f07
movl %eax, %ebp
movl 0x58(%rsp), %eax
movl $0x2000, %ecx           # imm = 0x2000
subl %eax, %ecx
addq %rcx, %r13
addq %rcx, %r12
testl %eax, %eax
movq 0x30(%rsp), %rdx
je 0x2da1
cmpl $0x0, 0x40(%rsp)
jne 0x2f26
movl $0x0, %eax
subq $0x2000, %rdx           # imm = 0x2000
cmovbq %rax, %rdx
ja 0x2d52
jmp 0x2ea7
leaq 0x1619(%rip), %rdi      # 0x4483
callq 0x2040
movl $0x1, %eax
xorl %r12d, %r12d
jmp 0x2e86
movl $0x1, %eax
xorl %r12d, %r12d
movq 0x8(%rsp), %r14
movq %r12, (%r14)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x15ef(%rip), %rdi      # 0x4491
callq 0x2040
cmpl $0x1, %ebp
jne 0x2f45
leaq 0x38(%rsp), %rdi
callq 0x2190
movq 0x18(%rsp), %rsi
cmpq %rsi, %r12
movq 0x8(%rsp), %r14
jbe 0x2eda
leaq 0x14b5(%rip), %rdi      # 0x4385
movq %r12, %rdx
xorl %eax, %eax
callq 0x2080
movq %r15, %rdi
movq %r12, %rsi
callq 0x21a0
testq %rax, %rax
je 0x2ef6
movq 0x28(%rsp), %rcx
movq %rax, (%rcx)
xorl %eax, %eax
jmp 0x2e86
leaq 0x142b(%rip), %rdi      # 0x4328
callq 0x21d0
jmp 0x2e6f
leaq 0x1383(%rip), %rdi      # 0x4291
leaq 0x1392(%rip), %rsi      # 0x42a7
leaq 0x139f(%rip), %rcx      # 0x42bb
movl $0x3f9, %edx            # imm = 0x3F9
callq 0x2090
leaq 0x13d4(%rip), %rdi      # 0x4301
leaq 0x1373(%rip), %rsi      # 0x42a7
leaq 0x1380(%rip), %rcx      # 0x42bb
movl $0x404, %edx            # imm = 0x404
callq 0x2090
leaq 0x13c8(%rip), %rdi      # 0x4314
leaq 0x1354(%rip), %rsi      # 0x42a7
leaq 0x1361(%rip), %rcx      # 0x42bb
movl $0x409, %edx            # imm = 0x409
callq 0x2090