// File: /workspace/github_c_makefile_star2_O1/snej[P]zdelta/deflate.c
// Function: zd_deflate
int ZEXPORT zd_deflate (strm, flush)
    zd_streamp strm;
    int flush;
{
  int old_flush; /* value of flush param for previous deflate call */
  deflate_state *s;

  if (strm == ZD_NULL || strm->state == ZD_NULL ||
      flush > ZD_FINISH || flush < 0) {
    return ZD_STREAM_ERROR;
  }
  s = strm->state;

  if (strm->next_out == ZD_NULL ||
      (strm->next_in == ZD_NULL && strm->avail_in != 0) ||
      (s->status == FINISH_STATE && flush != ZD_FINISH) ||
      strm->refnum<0 || strm->refnum>REFNUM) {
    ERR_RETURN(strm, ZD_STREAM_ERROR);
  }
  if (strm->avail_out == 0) ERR_RETURN(strm, ZD_BUF_ERROR);

  s->strm = strm; /* just in case */
  old_flush = s->last_flush;
  s->last_flush = flush;

  /* Write the libzd header */
  if (s->status == INIT_STATE) {

    uInt header = (ZD_DEFLATED + ((s->w_bits-8)<<4)) << 8;
    uInt level_flags = (s->level-1) >> 1;

    if (level_flags > 3) level_flags = 3;
    header |= (level_flags << 6);
    header += 31 - (header % 31);
    
    s->status = BUSY_STATE;
    putShortMSB(s, header);
  }

  /* Flush as much pending output as possible */
  if (s->pending != 0) {
    flush_pending(strm);
    if (strm->avail_out == 0) {
      /* Since avail_out is 0, deflate will be called again with
       * more output space, but possibly with both pending and
       * avail_in equal to zero. There won't be anything to do,
       * but this is not an error situation so make sure we
       * return OK instead of BUF_ERROR at next call of deflate:
       */
      s->last_flush = -1;
      return ZD_OK;
    }

    /* Make sure there is something to do and avoid duplicate consecutive
     * flushes. For repeated and useless calls with ZD_FINISH, we keep
     * returning ZD_STREAM_END instead of ZD_BUFF_ERROR.
     */
  } else if (strm->avail_in == 0 && flush <= old_flush &&
	     flush != ZD_FINISH) {
    ERR_RETURN(strm, ZD_BUF_ERROR);
  }

  /* User must not provide more input after the first FINISH: */
  if (s->status == FINISH_STATE && strm->avail_in != 0) {
    ERR_RETURN(strm, ZD_BUF_ERROR);
  }

  /* Start a new block or continue the current one.
   */
  if (strm->avail_in != 0 || s->lookahead != 0 ||
      (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
    block_state bstate;
    
    /* zdelta: currently only one level of compression */
    bstate = (*(configuration_table[s->level].func))(s, flush);
   
    if (bstate == finish_started || bstate == finish_done) {
      s->status = FINISH_STATE;
    }
    if (bstate == need_more || bstate == finish_started) {
      if (strm->avail_out == 0) {
	s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
      }
      return ZD_OK;
      /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
       * of deflate should use the same flush parameter to make sure
       * that the flush is complete. So we don't have to output an
       * empty block here, this will be done at next call. This also
       * ensures that for a very small output buffer, we emit at most
       * one empty block.
       */
    }
    if (bstate == block_done) {
      if (flush == Z_PARTIAL_FLUSH) {
	zd_tr_align(s);
      } else { /* FULL_FLUSH or SYNC_FLUSH */
	zd_tr_stored_block(s, (char*)0, 0L, 0);
	/* For a full flush, this empty block will be recognized
	 * as a special marker by inflate_sync().
	 */
	if (flush == ZD_FULL_FLUSH) {
	  CLEAR_HASH(s);             /* forget history */
	}
      }
      flush_pending(strm);
      if (strm->avail_out == 0) {
	s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
	return ZD_OK;
      }
    }
  }

  Assert(strm->avail_out > 0, "bug2");

  if (flush != ZD_FINISH) return ZD_OK;
  if (s->noheader) return ZD_STREAM_END;

  /* Write the libzd trailer (adler32) */
  putShortMSB(s, (uInt)(strm->adler >> 16));
  putShortMSB(s, (uInt)(strm->adler & 0xffff));
  flush_pending(strm);
  /* If avail_out is zero, the application will call deflate again
   * to flush the rest.
   */
  s->noheader = -1; /* write the trailer only once! */
  return s->pending != 0 ? ZD_OK : ZD_STREAM_END;
}

// Assembly:
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
testq %rdi, %rdi
je 0x20e0
pushq %rbp
pushq %r14
pushq %rbx
movl %esi, %ebp
movq %rdi, %rbx
movq 0x58(%rdi), %r14
testq %r14, %r14
sete %cl
cmpl $0x5, %esi
setae %dl
orb %cl, %dl
jne 0x20dc
cmpq $0x0, 0x18(%rbx)
je 0x20cd
cmpq $0x0, (%rbx)
je 0x20c3
movl 0x8(%r14), %ecx
cmpl $0x29a, %ecx            # imm = 0x29A
sete %dl
cmpl $0x4, %ebp
setne %sil
testb %dl, %sil
jne 0x20cd
cmpl $0x2, 0x48(%rbx)
jae 0x20cd
cmpl $0x0, 0x20(%rbx)
je 0x20fc
movq %rbx, (%r14)
movl 0x34(%r14), %eax
movl %ebp, 0x34(%r14)
cmpl $0x2a, %ecx
jne 0x2030
movl 0x3c(%r14), %edx
shll $0xc, %edx
leal -0x7b00(%rdx), %ecx
movl 0xf4(%r14), %esi
decl %esi
sarl %esi
cmpl $0x3, %esi
movl $0x3, %edi
cmovbl %esi, %edi
shll $0x6, %edi
addl %edi, %edx
addl $0xffff8500, %edx       # imm = 0xFFFF8500
imulq $0x8421085, %rdx, %rsi  # imm = 0x8421085
shrq $0x20, %rsi
subl %esi, %edx
shrl %edx
addl %esi, %edx
shrl $0x4, %edx
movl %edx, %esi
shll $0x5, %esi
subl %esi, %edx
addl %edi, %edx
orl %edi, %edx
movl $0x71, 0x8(%r14)
movq 0x10(%r14), %rsi
movslq 0x28(%r14), %rdi
leal 0x1(%rdi), %r8d
movl %r8d, 0x28(%r14)
movb %ch, (%rsi,%rdi)
xorb $0x1f, %dl
movq 0x10(%r14), %rcx
movslq 0x28(%r14), %rsi
leal 0x1(%rsi), %edi
movl %edi, 0x28(%r14)
movb %dl, (%rcx,%rsi)
cmpl $0x0, 0x28(%r14)
je 0x20e1
movq %rbx, %rdi
callq 0x267b
cmpl $0x0, 0x20(%rbx)
je 0x221c
movl 0x8(%r14), %eax
cmpl $0x29a, %eax            # imm = 0x29A
jne 0x2062
cmpl $0x0, 0x8(%rbx)
jne 0x20fc
cmpl $0x0, 0x8(%rbx)
je 0x2112
movslq 0xf4(%r14), %rax
shlq $0x4, %rax
leaq 0xbcb2(%rip), %rcx      # 0xdd30
movq %r14, %rdi
movl %ebp, %esi
callq *0x8(%rax,%rcx)
movl %eax, %ecx
andl $-0x2, %ecx
cmpl $0x2, %ecx
jne 0x2099
movl $0x29a, 0x8(%r14)       # imm = 0x29A
testl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
je 0x2136
cmpl $0x1, %eax
jne 0x2187
movq %r14, %rdi
cmpl $0x1, %ebp
jne 0x2148
callq 0x49aa
jmp 0x2175
cmpl $0x0, 0x8(%rbx)
je 0x1f72
leaq 0xc06c(%rip), %rcx      # 0xe140
movq 0x18(%rcx), %rcx
movq %rcx, 0x50(%rbx)
popq %rbx
popq %r14
popq %rbp
retq
cmpl $0x0, 0x8(%rbx)
jne 0x204d
cmpl $0x4, %ebp
je 0x204d
cmpl %ebp, %eax
jl 0x204d
leaq 0xc03d(%rip), %rax      # 0xe140
movq 0x30(%rax), %rax
movq %rax, 0x50(%rbx)
movl $0xfffffffb, %eax       # imm = 0xFFFFFFFB
jmp 0x20dc
cmpl $0x29a, %eax            # imm = 0x29A
setne %al
testl %ebp, %ebp
setne %cl
testb %al, %cl
jne 0x206c
cmpl $0x0, 0xbc(%r14)
je 0x2187
jmp 0x206c
xorl %eax, %eax
cmpl $0x0, 0x20(%rbx)
jne 0x20dc
movl $0xffffffff, 0x34(%r14) # imm = 0xFFFFFFFF
jmp 0x20dc
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x487b
cmpl $0x3, %ebp
jne 0x2175
movq 0x78(%r14), %rdi
movl 0x90(%r14), %edx
decl %edx
movw $0x0, (%rdi,%rdx,2)
addq %rdx, %rdx
xorl %esi, %esi
callq 0x1090
movq %rbx, %rdi
callq 0x267b
cmpl $0x0, 0x20(%rbx)
je 0x221c
xorl %eax, %eax
cmpl $0x4, %ebp
jne 0x20dc
movl $0x1, %eax
cmpl $0x0, 0x2c(%r14)
jne 0x20dc
movb 0x82(%rbx), %al
movb 0x83(%rbx), %cl
movq 0x10(%r14), %rdx
movslq 0x28(%r14), %rsi
leal 0x1(%rsi), %edi
movl %edi, 0x28(%r14)
movb %cl, (%rdx,%rsi)
movq 0x10(%r14), %rcx
movslq 0x28(%r14), %rdx
leal 0x1(%rdx), %esi
movl %esi, 0x28(%r14)
movb %al, (%rcx,%rdx)
movq 0x80(%rbx), %rax
movq 0x10(%r14), %rcx
movslq 0x28(%r14), %rdx
leal 0x1(%rdx), %esi
movl %esi, 0x28(%r14)
movb %ah, (%rcx,%rdx)
movq 0x10(%r14), %rcx
movslq 0x28(%r14), %rdx
leal 0x1(%rdx), %esi
movl %esi, 0x28(%r14)
movb %al, (%rcx,%rdx)
movq %rbx, %rdi
callq 0x267b
movl $0xffffffff, 0x2c(%r14) # imm = 0xFFFFFFFF
xorl %eax, %eax
cmpl $0x0, 0x28(%r14)
sete %al
jmp 0x20dc
movl $0xffffffff, 0x34(%r14) # imm = 0xFFFFFFFF
xorl %eax, %eax
jmp 0x20dc