// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xf8, %rsp
movq %rdi, 0xe8(%rsp)
movq %rsi, 0xe0(%rsp)
movq %rdx, 0xd8(%rsp)
movq %rcx, 0xd0(%rsp)
movl %r8d, 0xcc(%rsp)
movl $0x3, 0x1c(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x2180
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
jne 0x34e5
leaq 0xeff(%rip), %rdi       # 0x43d8
movb $0x0, %al
callq 0x2080
jmp 0x382a
movq 0x40(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x90(%rsp)
movq $0x0, 0x98(%rsp)
movq $0x0, 0xa0(%rsp)
movl 0xcc(%rsp), %esi
leaq 0x50(%rsp), %rdi
leaq 0xdb0(%rip), %rdx       # 0x42e3
movl $0x70, %ecx
callq 0x2120
movl %eax, 0xc8(%rsp)
cmpl $0x0, 0xc8(%rsp)
je 0x355e
movl $0x1, 0xf4(%rsp)
jmp 0x3856
jmp 0x3560
cmpq $0x2000, 0x30(%rsp)     # imm = 0x2000
jbe 0x359a
movl $0x2000, 0x58(%rsp)     # imm = 0x2000
movq 0x48(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x48(%rsp), %rax
addq $0x2000, %rax           # imm = 0x2000
movq %rax, 0x48(%rsp)
movl $0x0, 0xc4(%rsp)
jmp 0x35e4
cmpq $0x0, 0x30(%rsp)
jne 0x35b5
leaq 0xd41(%rip), %rdi       # 0x42ea
movb $0x0, %al
callq 0x2080
jmp 0x3776
movq 0x30(%rsp), %rax
movl %eax, 0x58(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x30(%rsp), %rax
addq 0x48(%rsp), %rax
movq %rax, 0x48(%rsp)
movl $0x4, 0xc4(%rsp)
jmp 0x35e4
jmp 0x35e6
cmpq $0x0, 0x30(%rsp)
je 0x3679
movq 0x20(%rsp), %rax
cmpq 0x30(%rsp), %rax
ja 0x3679
movl 0x1c(%rsp), %eax
shll $0xd, %eax
movl %eax, 0x18(%rsp)
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
subq %rcx, %rax
movl %eax, 0x14(%rsp)
movq 0x40(%rsp), %rdi
movq 0xe0(%rsp), %rsi
movl 0x18(%rsp), %eax
addq %rax, %rsi
callq 0x21b0
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
jne 0x3651
leaq 0xcc4(%rip), %rdi       # 0x4309
movb $0x0, %al
callq 0x2080
jmp 0x382a
movq 0x40(%rsp), %rax
movl 0x14(%rsp), %ecx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movl 0x18(%rsp), %eax
addq 0x20(%rsp), %rax
movq %rax, 0x20(%rsp)
imull $0x3, 0x1c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl $0x2000, 0x70(%rsp)     # imm = 0x2000
movq 0x38(%rsp), %rax
movq %rax, 0x68(%rsp)
movl 0xc4(%rsp), %esi
leaq 0x50(%rsp), %rdi
callq 0x20b0
movl %eax, 0xc8(%rsp)
cmpl $-0x2, 0xc8(%rsp)
je 0x36af
jmp 0x36ce
leaq 0xc72(%rip), %rdi       # 0x4328
leaq 0xc81(%rip), %rsi       # 0x433e
movl $0x3f9, %edx            # imm = 0x3F9
leaq 0xc89(%rip), %rcx       # 0x4352
callq 0x2090
movl $0x2000, %eax           # imm = 0x2000
subl 0x70(%rsp), %eax
movl %eax, 0xc0(%rsp)
movl 0xc0(%rsp), %ecx
movq 0x38(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movl 0xc0(%rsp), %eax
addq 0x28(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpl $0x0, 0x70(%rsp)
je 0x35e6
cmpl $0x0, 0x58(%rsp)
jne 0x3719
jmp 0x3738
leaq 0xc78(%rip), %rdi       # 0x4398
leaq 0xc17(%rip), %rsi       # 0x433e
movl $0x404, %edx            # imm = 0x404
leaq 0xc1f(%rip), %rcx       # 0x4352
callq 0x2090
cmpq $0x2000, 0x30(%rsp)     # imm = 0x2000
jbe 0x3755
movq 0x30(%rsp), %rax
subq $0x2000, %rax           # imm = 0x2000
movq %rax, 0x8(%rsp)
jmp 0x375e
xorl %eax, %eax
movq %rax, 0x8(%rsp)
jmp 0x375e
movq 0x8(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpl $0x4, 0xc4(%rsp)
jne 0x3560
cmpl $0x1, 0xc8(%rsp)
jne 0x3782
jmp 0x37a1
leaq 0xc22(%rip), %rdi       # 0x43ab
leaq 0xbae(%rip), %rsi       # 0x433e
movl $0x409, %edx            # imm = 0x409
leaq 0xbb6(%rip), %rcx       # 0x4352
callq 0x2090
leaq 0x50(%rsp), %rdi
callq 0x21a0
movq 0x28(%rsp), %rax
cmpq 0xe0(%rsp), %rax
jbe 0x37d5
movq 0xe0(%rsp), %rsi
movq 0x28(%rsp), %rdx
leaq 0xc65(%rip), %rdi       # 0x4433
movb $0x0, %al
callq 0x2080
movq 0x40(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x21b0
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
jne 0x37fd
leaq 0xbc9(%rip), %rdi       # 0x43bf
callq 0x21e0
jmp 0x382a
movq 0x40(%rsp), %rcx
movq 0xd8(%rsp), %rax
movq %rcx, (%rax)
movq 0x28(%rsp), %rcx
movq 0xd0(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0xf4(%rsp)
jmp 0x3856
cmpq $0x0, 0x40(%rsp)
je 0x383c
movq 0x40(%rsp), %rdi
callq 0x2030
movq 0xd0(%rsp), %rax
movq $0x0, (%rax)
movl $0x1, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
addq $0xf8, %rsp
retq
nopw %cs:(%rax,%rax)