// File: /workspace/github_c_makefile_star2_O2/snej[P]zdelta/deflate.c
// Function: zd_deflateInit2_
int ZEXPORT zd_deflateInit2_(strm, level, method, windowBits, memLevel, 
			     strategy, version, stream_size)
    zd_streamp strm;
    int  level;
    int  method;
    int  windowBits;
    int  memLevel;
    int  strategy;
    const char *version;
    int stream_size;
{
  deflate_state *s;
  int noheader = 0;
  int refnum = strm->refnum;
  static const char* zd_version = ZDLIB_VERSION;
  int i;
  
  ushf *overlay;
  /* We overlay pending_buf and d_buf+l_buf. This works since the average
   * output size for (length,distance) codes is <= 24 bits.
   */
  
  if (version == ZD_NULL || version[0] != zd_version[0] ||
      stream_size != sizeof(zd_stream)) {
    return ZD_VERSION_ERROR;
  }
  if (strm == ZD_NULL) return ZD_STREAM_ERROR;

  strm->msg = ZD_NULL;
  if (strm->zalloc == ZD_NULL) {
    strm->zalloc = zd_zcalloc;
    strm->opaque = (voidpf)0;
  }
  if (strm->zfree == ZD_NULL) strm->zfree = zd_zcfree;

  if (level == ZD_DEFAULT_COMPRESSION) level = 7;

#ifdef FASTEST
  level = 1;
#endif

  if (windowBits < 0) { /* undocumented feature: suppress zdlib header */
    noheader = 1;
    windowBits = -windowBits;
  }

#ifdef NO_ERROR_CHECK /* suppress zdlib header at compile time */
  noheader = 1;
#endif

  if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != ZD_DEFLATED ||
      windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
      strategy < ZD_HUFFMAN_ONLY || strategy > 8 || 
      refnum < 0 || refnum > REFNUM)
  {
    return ZD_STREAM_ERROR;
  }

  s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
  if (s == ZD_NULL) return ZD_MEM_ERROR;

  strm->state = (struct zd_internal_state FAR *)s;
  s->strm = strm;

  s->noheader = noheader;
  s->w_bits = windowBits;
  s->w_size = 1 << s->w_bits;
  s->w_mask = s->w_size - 1;

  s->hash_bits  = memLevel + 7;
  s->hash_size  = 1 << s->hash_bits;
  s->hash_mask  = s->hash_size - 1;
  s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);

  /* zdelta: reference window mask */
  s->rw_mask = (s->w_size << 1) - 1;

  /* zdelta: needed for the correctness of reference_longest_match() */
  s->ref_window[0]=(Bytef *) ZALLOC(strm, s->w_size*2*(refnum+1),sizeof(Byte));
  s->window       =(Bytef *) (s->ref_window[0]+(2*refnum*s->w_size)); 

  s->prev       = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));  
  s->head       = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));

  /* zdelta: reference data hash table */ 
  s->ref_prev[0] = (Posf *)  ZALLOC(strm, s->w_size*(2*refnum), sizeof(Pos));  
  s->ref_head[0] = (Posf *)  ZALLOC(strm, s->hash_size*(refnum), sizeof(Pos));

  s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */

  overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, 2*sizeof(ush)+2);
  s->pending_buf = (uchf *) overlay;
  s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);

  if (s->ref_window[0] == ZD_NULL || s->prev        == ZD_NULL ||
      s->head          == ZD_NULL || s->ref_prev[0] == ZD_NULL ||
      s->ref_head[0]   == ZD_NULL || s->pending_buf == ZD_NULL)
  {
    strm->msg = (char*)ERR_MSG(ZD_MEM_ERROR);
    zd_deflateEnd (strm);
    return ZD_MEM_ERROR;
  }

  s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  s->l_buf = s->d_buf + s->lit_bufsize;
  s->z_buf = s->pending_buf + (1+2*sizeof(ush))*s->lit_bufsize;

  /* zdelta: initialize reference window buffers */
  for(i=1;i<refnum;++i){
    s->ref_window[i] = (Bytef *) (s->ref_window[0] +(s->w_size)*2*i);
    s->ref_prev[i]   = (Posf *)  (s->ref_prev[0] + (s->w_size)*2*i);
    s->ref_head[i]   = (Posf *)  (s->ref_head[0] + i*(s->hash_size));
  }

  /* zdelta: these variables are not used; kept for future use */
  s->level = level;
  s->strategy = strategy;
  s->method = (Byte)method;

  if(zd_deflateReset(strm)!=ZD_OK){
    return ZD_STREAM_ERROR;
  };

  return ZD_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq 0x50(%rsp), %rax
pushq $-0x6
popq %r12
testq %rax, %rax
je 0x22fe
cmpl $0x90, 0x58(%rsp)
jne 0x22fe
cmpb $0x32, (%rax)
jne 0x22fe
movl %r8d, %r15d
movl %ecx, %ebp
movq %rdi, %rbx
movl 0x48(%rdi), %edi
andq $0x0, 0x50(%rbx)
movq 0x60(%rbx), %rax
testq %rax, %rax
jne 0x20eb
leaq 0x3f69(%rip), %rax      # 0x604b
movq %rax, 0x60(%rbx)
andq $0x0, 0x70(%rbx)
cmpq $0x0, 0x68(%rbx)
jne 0x20fd
leaq 0x3f5b(%rip), %rcx      # 0x6054
movq %rcx, 0x68(%rbx)
cmpl $-0x1, %esi
pushq $0x7
popq %r8
cmovnel %esi, %r8d
movl %ebp, %r13d
negl %r13d
cmovsl %ebp, %r13d
leal -0xa(%r15), %ecx
cmpl $-0x9, %ecx
setb %cl
cmpl $0x5, %edx
setne %dl
leal -0x10(%r13), %esi
cmpl $-0x8, %esi
setb %sil
orb %cl, %dl
cmpl $0xa, %r8d
setae %cl
orb %dl, %cl
orb %sil, %cl
cmpl $0x9, %r9d
setae %dl
orb %cl, %dl
cmpl $0x2, %edi
setae %cl
orb %dl, %cl
je 0x2157
pushq $-0x2
popq %r12
jmp 0x22fe
movl %r8d, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movl %r9d, 0x14(%rsp)
movq 0x70(%rbx), %rdi
pushq $0x1
popq %rsi
movl $0x1ab0, %edx           # imm = 0x1AB0
callq *%rax
pushq $-0x4
popq %r12
testq %rax, %rax
je 0x22fe
movq %rax, %r14
shrl $0x1f, %ebp
movq %rax, 0x58(%rbx)
movq %rbx, (%rax)
movl %ebp, 0x2c(%rax)
movl %r13d, 0x3c(%rax)
pushq $0x1
popq %rdx
movl %edx, %eax
movl %r13d, %ecx
shll %cl, %eax
movl %eax, 0x38(%r14)
decl %eax
movl %eax, 0x40(%r14)
movl $0x80, %eax
movl %r15d, %ecx
shll %cl, %eax
leal 0x7(%r15), %ecx
movl %ecx, 0x94(%r14)
movl %eax, 0x90(%r14)
decl %eax
movl %eax, 0x98(%r14)
leal 0x9(%r15), %eax
movzbl %al, %eax
movb $0x3, %cl
divb %cl
movzbl %al, %eax
movl %eax, 0x9c(%r14)
pushq $0x2
popq %rax
movl %r13d, %ecx
shll %cl, %eax
decl %eax
movl %eax, 0x44(%r14)
movq 0x70(%rbx), %rdi
movq 0x8(%rsp), %rax
leal 0x1(%rax), %esi
movq %rax, %rbp
incb %r13b
movl %r13d, %ecx
shll %cl, %esi
callq *0x60(%rbx)
movq %rax, 0x50(%r14)
leal (,%rbp,2), %r13d
movl 0x38(%r14), %esi
movl %esi, %ecx
imull %r13d, %ecx
addq %rax, %rcx
movq %rcx, 0x48(%r14)
movq 0x70(%rbx), %rdi
pushq $0x2
popq %rbp
movl %ebp, %edx
callq *0x60(%rbx)
movq %rax, 0x68(%r14)
movq 0x70(%rbx), %rdi
movl 0x90(%r14), %esi
movl %ebp, %edx
callq *0x60(%rbx)
movq %rax, 0x78(%r14)
movq 0x70(%rbx), %rdi
imull 0x38(%r14), %r13d
movl %r13d, %esi
movl %ebp, %edx
callq *0x60(%rbx)
movq %rax, 0x70(%r14)
movq 0x70(%rbx), %rdi
movl 0x90(%r14), %esi
imull 0x8(%rsp), %esi
movl %ebp, %edx
callq *0x60(%rbx)
pushq $0x40
popq %rsi
movl %r15d, %ecx
shll %cl, %esi
movq %rax, 0x80(%r14)
movl %esi, 0x1a40(%r14)
movq 0x70(%rbx), %rdi
pushq $0x6
popq %rdx
callq *0x60(%rbx)
movq %rax, 0x10(%r14)
movl 0x1a40(%r14), %edx
leaq (,%rdx,4), %rcx
movq %rcx, 0x18(%r14)
movq 0x50(%r14), %rcx
testq %rcx, %rcx
je 0x22e7
cmpq $0x0, 0x68(%r14)
je 0x22e7
cmpq $0x0, 0x78(%r14)
je 0x22e7
cmpq $0x0, 0x70(%r14)
je 0x22e7
cmpq $0x0, 0x80(%r14)
sete %sil
testq %rax, %rax
sete %dil
orb %sil, %dil
cmpb $0x1, %dil
jne 0x2310
leaq 0xbe52(%rip), %rax      # 0xe140
movq 0x28(%rax), %rax
movq %rax, 0x50(%rbx)
movq %rbx, %rdi
callq 0x23be
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edx, %esi
andl $-0x2, %esi
addq %rax, %rsi
movq %rsi, 0x1a48(%r14)
leaq (%rsi,%rdx,2), %rsi
movq %rsi, 0x1a38(%r14)
leaq (%rdx,%rdx,4), %rdx
addq %rax, %rdx
movq %rdx, 0x1a50(%r14)
pushq $0x1
popq %rax
pushq $0x22
popq %rdx
movl 0x14(%rsp), %r8d
movq 0x8(%rsp), %r9
cmpq %r9, %rax
jae 0x238b
leal -0x20(%rdx), %esi
imull 0x38(%r14), %esi
leaq (%rcx,%rsi), %rdi
movq %rdi, -0x30(%r14,%rdx,4)
addq %rsi, %rsi
addq 0x70(%r14), %rsi
movq %rsi, -0x10(%r14,%rdx,4)
movl 0x90(%r14), %esi
imull %eax, %esi
addq %rsi, %rsi
addq 0x80(%r14), %rsi
movq %rsi, (%r14,%rdx,4)
incq %rax
addq $0x2, %rdx
jmp 0x2348
movl 0x10(%rsp), %eax
movl %eax, 0xf4(%r14)
movl %r8d, 0xf8(%r14)
movb $0x5, 0x31(%r14)
movq %rbx, %rdi
callq 0x1a6c
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal -0x2(,%rcx,2), %r12d
jmp 0x22fe