// File: /workspace/github_c_makefile_star2_O2/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:
pushq $-0x2
popq %rax
testq %rdi, %rdi
je 0x1f3b
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
movq 0x58(%rdi), %r14
testq %r14, %r14
sete %cl
cmpl $0x5, %esi
setae %dl
orb %cl, %dl
jne 0x1f31
cmpq $0x0, 0x18(%rbx)
je 0x1f22
cmpq $0x0, (%rbx)
je 0x1f18
movl 0x8(%r14), %ecx
cmpl $0x29a, %ecx            # imm = 0x29A
sete %dl
cmpl $0x4, %ebp
setne %sil
testb %dl, %sil
jne 0x1f22
cmpl $0x2, 0x48(%rbx)
jae 0x1f22
cmpl $0x0, 0x20(%rbx)
je 0x1f4c
movq %rbx, (%r14)
movl 0x34(%r14), %r15d
movl %ebp, 0x34(%r14)
cmpl $0x2a, %ecx
jne 0x1eeb
movl 0x3c(%r14), %ecx
movl 0xf4(%r14), %eax
shll $0xc, %ecx
decl %eax
sarl %eax
cmpl $0x3, %eax
pushq $0x3
popq %rdx
cmovbl %eax, %edx
shll $0x6, %edx
leal (%rcx,%rdx), %eax
addl $0xffff8500, %eax       # imm = 0xFFFF8500
orl %edx, %ecx
pushq $0x1f
popq %rsi
xorl %edx, %edx
divl %esi
leal (%rdx,%rcx), %esi
addl $0xffff8500, %esi       # imm = 0xFFFF8500
xorl $0x1f, %esi
movl $0x71, 0x8(%r14)
movq %r14, %rdi
callq 0x24a4
cmpl $0x0, 0x28(%r14)
je 0x1f3c
movq %rbx, %rdi
callq 0x24c9
cmpl $0x0, 0x20(%rbx)
je 0x2026
movl 0x8(%r14), %eax
movl 0x8(%rbx), %ecx
cmpl $0x29a, %eax            # imm = 0x29A
jne 0x1f60
testl %ecx, %ecx
jne 0x1f4c
jmp 0x1f64
cmpl $0x0, 0x8(%rbx)
je 0x1e5f
leaq 0xc217(%rip), %rcx      # 0xe140
movq 0x18(%rcx), %rcx
movq %rcx, 0x50(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x8(%rbx)
jne 0x1f04
cmpl $0x4, %ebp
je 0x1f04
cmpl %ebp, %r15d
jl 0x1f04
leaq 0xc1ed(%rip), %rax      # 0xe140
movq 0x30(%rax), %rax
movq %rax, 0x50(%rbx)
pushq $-0x5
popq %rax
jmp 0x1f31
testl %ecx, %ecx
jne 0x1f6e
cmpl $0x0, 0xbc(%r14)
je 0x1fcc
movslq 0xf4(%r14), %rax
shlq $0x4, %rax
leaq 0xbdb0(%rip), %rcx      # 0xdd30
movq %r14, %rdi
movl %ebp, %esi
callq *0x8(%rax,%rcx)
movl %eax, %ecx
andl $-0x2, %ecx
cmpl $0x2, %ecx
jne 0x1f9b
movl $0x29a, 0x8(%r14)       # imm = 0x29A
testl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
je 0x1fb6
cmpl $0x1, %eax
jne 0x2013
movq %r14, %rdi
cmpl $0x1, %ebp
jne 0x1fd9
callq 0x478b
jmp 0x2005
xorl %eax, %eax
cmpl $0x0, 0x20(%rbx)
jne 0x1f31
orl $-0x1, 0x34(%r14)
jmp 0x1f31
testl %ebp, %ebp
je 0x202b
cmpl $0x29a, %eax            # imm = 0x29A
jne 0x1f6e
jmp 0x2013
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4675
cmpl $0x3, %ebp
jne 0x2005
movq 0x78(%r14), %rdi
movl 0x90(%r14), %edx
decl %edx
andw $0x0, (%rdi,%rdx,2)
addq %rdx, %rdx
xorl %esi, %esi
callq 0x1090
movq %rbx, %rdi
callq 0x24c9
cmpl $0x0, 0x20(%rbx)
je 0x2026
cmpl $0x4, %ebp
jne 0x202b
cmpl $0x0, 0x2c(%r14)
je 0x2032
pushq $0x1
jmp 0x1f5d
orl $-0x1, 0x34(%r14)
xorl %eax, %eax
jmp 0x1f31
movl 0x82(%rbx), %esi
movq %r14, %rdi
callq 0x24a4
movzwl 0x80(%rbx), %esi
movq %r14, %rdi
callq 0x24a4
movq %rbx, %rdi
callq 0x24c9
orl $-0x1, 0x2c(%r14)
xorl %eax, %eax
cmpl $0x0, 0x28(%r14)
sete %al
jmp 0x1f31