// File: /workspace/github_c_makefile_star2_O1/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
movl $0xfffffffa, %r14d      # imm = 0xFFFFFFFA
testq %rax, %rax
je 0x24f8
cmpl $0x90, 0x58(%rsp)
jne 0x24f8
cmpb $0x32, (%rax)
jne 0x24f8
movq %rdi, %rbx
movl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
testq %rdi, %rdi
je 0x24f8
movl %ecx, %ebp
movl 0x48(%rbx), %r10d
movq $0x0, 0x50(%rbx)
cmpq $0x0, 0x60(%rbx)
jne 0x22c8
leaq 0x4131(%rip), %rax      # 0x63ed
movq %rax, 0x60(%rbx)
movq $0x0, 0x70(%rbx)
cmpq $0x0, 0x68(%rbx)
jne 0x22da
leaq 0x4120(%rip), %rax      # 0x63f6
movq %rax, 0x68(%rbx)
cmpl $-0x1, %esi
movl $0x7, %edi
cmovnel %esi, %edi
movl %ebp, %r13d
negl %r13d
cmovsl %ebp, %r13d
leal -0xa(%r8), %eax
cmpl $-0x9, %eax
setb %al
cmpl $0x5, %edx
setne %cl
leal -0x10(%r13), %edx
cmpl $-0x8, %edx
setb %dl
orb %al, %cl
cmpl $0xa, %edi
setae %al
orb %cl, %al
orb %dl, %al
cmpl $0x9, %r9d
setae %cl
orb %al, %cl
cmpl $0x2, %r10d
setae %al
orb %cl, %al
jne 0x24f8
movq %r8, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movl %edi, (%rsp)
movl %r9d, 0x4(%rsp)
movq 0x70(%rbx), %rdi
movl $0x1, %r12d
movl $0x1, %esi
movl $0x1ab0, %edx           # imm = 0x1AB0
callq *0x60(%rbx)
movl $0xfffffffc, %r14d      # imm = 0xFFFFFFFC
testq %rax, %rax
je 0x24f8
movq %rax, %r15
shrl $0x1f, %ebp
movq %rax, 0x58(%rbx)
movq %rbx, (%rax)
movl %ebp, 0x2c(%rax)
movl %r13d, 0x3c(%rax)
movl %r13d, %ecx
shll %cl, %r12d
movl %r12d, 0x38(%rax)
decl %r12d
movl %r12d, 0x40(%rax)
movq 0x8(%rsp), %r12
leal 0x7(%r12), %eax
movl $0x80, %edx
movl %r12d, %ecx
shll %cl, %edx
movl %eax, 0x94(%r15)
movl %edx, 0x90(%r15)
decl %edx
movl %edx, 0x98(%r15)
movl %r12d, %eax
addl $0x9, %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rax, %rcx
shrq $0x21, %rcx
movl %ecx, 0x9c(%r15)
movl $0x2, %eax
movl %r13d, %ecx
shll %cl, %eax
decl %eax
movl %eax, 0x44(%r15)
movq 0x70(%rbx), %rdi
movq 0x10(%rsp), %rax
leal 0x1(%rax), %esi
incb %r13b
movl %r13d, %ecx
movq %rax, %r13
shll %cl, %esi
movl $0x1, %edx
callq *0x60(%rbx)
movq %rax, 0x50(%r15)
leal (,%r13,2), %ebp
movl 0x38(%r15), %esi
movl %esi, %ecx
imull %ebp, %ecx
addq %rax, %rcx
movq %rcx, 0x48(%r15)
movq 0x70(%rbx), %rdi
movl $0x2, %edx
callq *0x60(%rbx)
movq %rax, 0x68(%r15)
movq 0x70(%rbx), %rdi
movl 0x90(%r15), %esi
movl $0x2, %edx
callq *0x60(%rbx)
movq %rax, 0x78(%r15)
movq 0x70(%rbx), %rdi
imull 0x38(%r15), %ebp
movl %ebp, %esi
movl $0x2, %edx
callq *0x60(%rbx)
movq %rax, 0x70(%r15)
movq 0x70(%rbx), %rdi
movl 0x90(%r15), %esi
imull %r13d, %esi
movl $0x2, %edx
callq *0x60(%rbx)
movl $0x40, %esi
movl %r12d, %ecx
shll %cl, %esi
movq %rax, 0x80(%r15)
movl %esi, 0x1a40(%r15)
movq 0x70(%rbx), %rdi
movl $0x6, %edx
callq *0x60(%rbx)
movq %rax, 0x10(%r15)
movl 0x1a40(%r15), %ecx
leaq (,%rcx,4), %rdx
movq %rdx, 0x18(%r15)
cmpq $0x0, 0x50(%r15)
je 0x24e1
cmpq $0x0, 0x68(%r15)
je 0x24e1
cmpq $0x0, 0x78(%r15)
je 0x24e1
cmpq $0x0, 0x70(%r15)
je 0x24e1
cmpq $0x0, 0x80(%r15)
sete %dl
testq %rax, %rax
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x250a
leaq 0xbc58(%rip), %rax      # 0xe140
movq 0x28(%rax), %rax
movq %rax, 0x50(%rbx)
movq %rbx, %rdi
callq 0x25a2
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ecx, %edx
andl $-0x2, %edx
addq %rax, %rdx
movq %rdx, 0x1a48(%r15)
leaq (%rdx,%rcx,2), %rdx
movq %rdx, 0x1a38(%r15)
leaq (%rcx,%rcx,4), %rcx
addq %rax, %rcx
movq %rcx, 0x1a50(%r15)
cmpl $0x2, %r13d
jl 0x256c
movl 0x38(%r15), %eax
movl 0x90(%r15), %ecx
addl %eax, %eax
movq 0x50(%r15), %rdx
addq %rax, %rdx
movq %rdx, 0x58(%r15)
addq %rax, %rax
addq 0x70(%r15), %rax
movq %rax, 0x78(%r15)
addq %rcx, %rcx
addq 0x80(%r15), %rcx
movq %rcx, 0x88(%r15)
movl (%rsp), %eax
movl %eax, 0xf4(%r15)
movl 0x4(%rsp), %eax
movl %eax, 0xf8(%r15)
movb $0x5, 0x31(%r15)
movq %rbx, %rdi
callq 0x1b34
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal -0x2(,%rcx,2), %r14d
jmp 0x24f8