// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x26ed
movq -0x10(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x26ed
cmpl $0x4, -0x14(%rbp)
jg 0x26ed
cmpl $0x0, -0x14(%rbp)
jge 0x26f9
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x2a97
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x274b
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x2724
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x274b
movq -0x20(%rbp), %rax
cmpl $0x29a, 0x8(%rax)       # imm = 0x29A
jne 0x2737
cmpl $0x4, -0x14(%rbp)
jne 0x274b
movq -0x10(%rbp), %rax
cmpl $0x0, 0x48(%rax)
jl 0x274b
movq -0x10(%rbp), %rax
cmpl $0x1, 0x48(%rax)
jle 0x276a
leaq 0x119ee(%rip), %rax     # 0x14140
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x2a97
movq -0x10(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x2793
leaq 0x119c5(%rip), %rax     # 0x14140
movq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0x2a97
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x18(%rbp)
movl -0x14(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x34(%rax)
movq -0x20(%rbp), %rax
cmpl $0x2a, 0x8(%rax)
jne 0x282d
movq -0x20(%rbp), %rax
movl 0x3c(%rax), %eax
subl $0x8, %eax
shll $0x4, %eax
addl $0x5, %eax
shll $0x8, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movl 0xf4(%rax), %eax
subl $0x1, %eax
sarl %eax
movl %eax, -0x28(%rbp)
cmpl $0x3, -0x28(%rbp)
jbe 0x27f1
movl $0x3, -0x28(%rbp)
movl -0x28(%rbp), %eax
shll $0x6, %eax
orl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl $0x1f, %ecx
xorl %edx, %edx
divl %ecx
movl $0x1f, %eax
subl %edx, %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movl $0x71, 0x8(%rax)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x3220
movq -0x20(%rbp), %rax
cmpl $0x0, 0x28(%rax)
je 0x2863
movq -0x10(%rbp), %rdi
callq 0x3280
movq -0x10(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x2861
movq -0x20(%rbp), %rax
movl $0xffffffff, 0x34(%rax) # imm = 0xFFFFFFFF
movl $0x0, -0x4(%rbp)
jmp 0x2a97
jmp 0x289c
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x289a
movl -0x14(%rbp), %eax
cmpl -0x18(%rbp), %eax
jg 0x289a
cmpl $0x4, -0x14(%rbp)
je 0x289a
leaq 0x118be(%rip), %rax     # 0x14140
movq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0x2a97
jmp 0x289c
movq -0x20(%rbp), %rax
cmpl $0x29a, 0x8(%rax)       # imm = 0x29A
jne 0x28d2
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x28d2
leaq 0x11886(%rip), %rax     # 0x14140
movq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0x2a97
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x2904
movq -0x20(%rbp), %rax
cmpl $0x0, 0xbc(%rax)
jne 0x2904
cmpl $0x0, -0x14(%rbp)
je 0x2a14
movq -0x20(%rbp), %rax
cmpl $0x29a, 0x8(%rax)       # imm = 0x29A
je 0x2a14
movq -0x20(%rbp), %rax
movslq 0xf4(%rax), %rcx
leaq 0x1142a(%rip), %rax     # 0x13d40
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq -0x20(%rbp), %rdi
movl -0x14(%rbp), %esi
callq *%rax
movl %eax, -0x2c(%rbp)
cmpl $0x2, -0x2c(%rbp)
je 0x2939
cmpl $0x3, -0x2c(%rbp)
jne 0x2944
movq -0x20(%rbp), %rax
movl $0x29a, 0x8(%rax)       # imm = 0x29A
cmpl $0x0, -0x2c(%rbp)
je 0x2950
cmpl $0x2, -0x2c(%rbp)
jne 0x2971
movq -0x10(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x2965
movq -0x20(%rbp), %rax
movl $0xffffffff, 0x34(%rax) # imm = 0xFFFFFFFF
movl $0x0, -0x4(%rbp)
jmp 0x2a97
cmpl $0x1, -0x2c(%rbp)
jne 0x2a12
cmpl $0x1, -0x14(%rbp)
jne 0x298c
movq -0x20(%rbp), %rdi
callq 0x76e0
jmp 0x29e6
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
movq %rdx, %rsi
callq 0x73f0
cmpl $0x3, -0x14(%rbp)
jne 0x29e4
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
movq -0x20(%rbp), %rcx
movl 0x90(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movw $0x0, (%rax,%rcx,2)
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x20(%rbp), %rax
movl 0x90(%rax), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
shlq %rdx
xorl %esi, %esi
callq 0x1080
jmp 0x29e6
movq -0x10(%rbp), %rdi
callq 0x3280
movq -0x10(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x2a10
movq -0x20(%rbp), %rax
movl $0xffffffff, 0x34(%rax) # imm = 0xFFFFFFFF
movl $0x0, -0x4(%rbp)
jmp 0x2a97
jmp 0x2a12
jmp 0x2a14
cmpl $0x4, -0x14(%rbp)
je 0x2a23
movl $0x0, -0x4(%rbp)
jmp 0x2a97
movq -0x20(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
je 0x2a36
movl $0x1, -0x4(%rbp)
jmp 0x2a97
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
shrq $0x10, %rax
movl %eax, %esi
callq 0x3220
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
andq $0xffff, %rax           # imm = 0xFFFF
movl %eax, %esi
callq 0x3220
movq -0x10(%rbp), %rdi
callq 0x3280
movq -0x20(%rbp), %rax
movl $0xffffffff, 0x2c(%rax) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rax
movl 0x28(%rax), %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq