// File: /workspace/github_cpp_makefile_star2_O1/sisong[P]ApkDiffPatch/zlib1.3.1/deflate.c
// Function: deflateInit2_
int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
                          int windowBits, int memLevel, int strategy,
                          const char *version, int stream_size) {
    deflate_state *s;
    int wrap = 1;
    static const char my_version[] = ZLIB_VERSION;

    if (version == Z_NULL || version[0] != my_version[0] ||
        stream_size != sizeof(z_stream)) {
        return Z_VERSION_ERROR;
    }
    if (strm == Z_NULL) return Z_STREAM_ERROR;

    strm->msg = Z_NULL;
    if (strm->zalloc == (alloc_func)0) {
#ifdef Z_SOLO
        return Z_STREAM_ERROR;
#else
        strm->zalloc = zcalloc;
        strm->opaque = (voidpf)0;
#endif
    }
    if (strm->zfree == (free_func)0)
#ifdef Z_SOLO
        return Z_STREAM_ERROR;
#else
        strm->zfree = zcfree;
#endif

#ifdef FASTEST
    if (level != 0) level = 1;
#else
    if (level == Z_DEFAULT_COMPRESSION) level = 6;
#endif

    if (windowBits < 0) { /* suppress zlib wrapper */
        wrap = 0;
        if (windowBits < -15)
            return Z_STREAM_ERROR;
        windowBits = -windowBits;
    }
#ifdef GZIP
    else if (windowBits > 15) {
        wrap = 2;       /* write gzip wrapper instead */
        windowBits -= 16;
    }
#endif
    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
        windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
        strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
        return Z_STREAM_ERROR;
    }
    if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
    s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
    if (s == Z_NULL) return Z_MEM_ERROR;
    strm->state = (struct internal_state FAR *)s;
    s->strm = strm;
    s->status = INIT_STATE;     /* to pass state test in deflateReset() */

    s->wrap = wrap;
    s->gzhead = Z_NULL;
    s->w_bits = (uInt)windowBits;
    s->w_size = 1 << s->w_bits;
    s->w_mask = s->w_size - 1;

    s->hash_bits = (uInt)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);

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

    s->high_water = 0;      /* nothing written to s->window yet */

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

    /* We overlay pending_buf and sym_buf. This works since the average size
     * for length/distance pairs over any compressed block is assured to be 31
     * bits or less.
     *
     * Analysis: The longest fixed codes are a length code of 8 bits plus 5
     * extra bits, for lengths 131 to 257. The longest fixed distance codes are
     * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
     * possible fixed-codes length/distance pair is then 31 bits total.
     *
     * sym_buf starts one-fourth of the way into pending_buf. So there are
     * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
     * in sym_buf is three bytes -- two for the distance and one for the
     * literal/length. As each symbol is consumed, the pointer to the next
     * sym_buf value to read moves forward three bytes. From that symbol, up to
     * 31 bits are written to pending_buf. The closest the written pending_buf
     * bits gets to the next sym_buf symbol to read is just before the last
     * code is written. At that time, 31*(n - 2) bits have been written, just
     * after 24*(n - 2) bits have been consumed from sym_buf. sym_buf starts at
     * 8*n bits into pending_buf. (Note that the symbol buffer fills when n - 1
     * symbols are written.) The closest the writing gets to what is unread is
     * then n + 14 bits. Here n is lit_bufsize, which is 16384 by default, and
     * can range from 128 to 32768.
     *
     * Therefore, at a minimum, there are 142 bits of space between what is
     * written and what is read in the overlain buffers, so the symbols cannot
     * be overwritten by the compressed data. That space is actually 139 bits,
     * due to the three-bit fixed-code block header.
     *
     * That covers the case where either Z_FIXED is specified, forcing fixed
     * codes, or when the use of fixed codes is chosen, because that choice
     * results in a smaller compressed block than dynamic codes. That latter
     * condition then assures that the above analysis also covers all dynamic
     * blocks. A dynamic-code block will only be chosen to be emitted if it has
     * fewer bits than a fixed-code block would for the same set of symbols.
     * Therefore its average symbol length is assured to be less than 31. So
     * the compressed data for a dynamic block also cannot overwrite the
     * symbols from which it is being constructed.
     */

    s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, LIT_BUFS);
    s->pending_buf_size = (ulg)s->lit_bufsize * 4;

    if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
        s->pending_buf == Z_NULL) {
        s->status = FINISH_STATE;
        strm->msg = ERR_MSG(Z_MEM_ERROR);
        deflateEnd (strm);
        return Z_MEM_ERROR;
    }
#ifdef LIT_MEM
    s->d_buf = (ushf *)(s->pending_buf + (s->lit_bufsize << 1));
    s->l_buf = s->pending_buf + (s->lit_bufsize << 2);
    s->sym_end = s->lit_bufsize - 1;
#else
    s->sym_buf = s->pending_buf + s->lit_bufsize;
    s->sym_end = (s->lit_bufsize - 1) * 3;
#endif
    /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
     * on 16 bit machines and because stored blocks are restricted to
     * 64K-1 bytes.
     */

    s->level = level;
    s->strategy = strategy;
    s->method = (Byte)method;

    return deflateReset(strm);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq 0x40(%rsp), %rax
movl $0xfffffffa, %ebx       # imm = 0xFFFFFFFA
testq %rax, %rax
je 0x4ad9c
cmpl $0x70, 0x48(%rsp)
jne 0x4ad9c
cmpb $0x31, (%rax)
jne 0x4ad9c
movq %rdi, %r14
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
testq %rdi, %rdi
je 0x4ad9c
movl %r9d, %ebp
movl %r8d, %r15d
movl %ecx, %r13d
movq $0x0, 0x30(%r14)
cmpq $0x0, 0x40(%r14)
jne 0x4ab8d
leaq 0x87b9(%rip), %rax      # 0x5333a
movq %rax, 0x40(%r14)
movq $0x0, 0x50(%r14)
cmpq $0x0, 0x48(%r14)
jne 0x4ab9f
leaq 0x87a9(%rip), %rax      # 0x53344
movq %rax, 0x48(%r14)
cmpl $-0x1, %esi
movl $0x6, %edi
cmovnel %esi, %edi
testl %r13d, %r13d
js 0x4abc0
cmpl $0x10, %r13d
jl 0x4abd3
addl $-0x10, %r13d
movl $0x2, %esi
jmp 0x4abcf
cmpl $-0xf, %r13d
jl 0x4ad9c
negl %r13d
xorl %esi, %esi
movb $0x1, %al
jmp 0x4abda
movl $0x1, %esi
xorl %eax, %eax
cmpl $0x4, %ebp
ja 0x4ad9c
cmpl $0x9, %edi
ja 0x4ad9c
cmpl $0x8, %edx
jne 0x4ad9c
leal -0xa(%r15), %ecx
cmpl $-0x9, %ecx
jb 0x4ad9c
leal -0x10(%r13), %ecx
cmpl $-0x8, %ecx
jb 0x4ad9c
cmpl $0x8, %r13d
sete %cl
testb %al, %cl
jne 0x4ad9c
movl %esi, 0x4(%rsp)
movl %edi, (%rsp)
movq 0x50(%r14), %rdi
movl $0x1, %esi
movl $0x1740, %edx           # imm = 0x1740
callq *0x40(%r14)
movl $0xfffffffc, %ebx       # imm = 0xFFFFFFFC
testq %rax, %rax
je 0x4ad9c
movq %rax, %r12
cmpl $0x8, %r13d
movl $0x9, %ecx
cmovnel %r13d, %ecx
movq %rax, 0x38(%r14)
movq %r14, (%rax)
movl $0x2a, 0x8(%rax)
movl 0x4(%rsp), %eax
movl %eax, 0x30(%r12)
xorl %r13d, %r13d
movq %r13, 0x38(%r12)
movl %ecx, 0x54(%r12)
movl $0x1, %esi
shll %cl, %esi
movl %esi, 0x50(%r12)
leal -0x1(%rsi), %eax
movl %eax, 0x58(%r12)
movl $0x80, %eax
movl %r15d, %ecx
shll %cl, %eax
leal 0x7(%r15), %ecx
movl %ecx, 0x88(%r12)
movl %eax, 0x84(%r12)
decl %eax
movl %eax, 0x8c(%r12)
movl %r15d, %eax
addl $0x9, %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rax, %rcx
shrq $0x21, %rcx
movl %ecx, 0x90(%r12)
movq 0x50(%r14), %rdi
movl $0x2, %edx
callq *0x40(%r14)
movq %rax, 0x60(%r12)
movq 0x50(%r14), %rdi
movl 0x50(%r12), %esi
movl $0x2, %edx
callq *0x40(%r14)
movq %rax, 0x70(%r12)
movq 0x50(%r14), %rdi
movl 0x84(%r12), %esi
movl $0x2, %edx
callq *0x40(%r14)
movl $0x40, %esi
movl %r15d, %ecx
shll %cl, %esi
movq %rax, 0x78(%r12)
movq %r13, 0x1738(%r12)
movl %esi, 0x1708(%r12)
movq 0x50(%r14), %rdi
movl $0x4, %edx
callq *0x40(%r14)
movq %rax, 0x10(%r12)
movl 0x1708(%r12), %ecx
leaq (,%rcx,4), %rdx
movq %rdx, 0x18(%r12)
cmpq %r13, 0x60(%r12)
je 0x4ad7c
cmpq $0x0, 0x70(%r12)
je 0x4ad7c
cmpq $0x0, 0x78(%r12)
sete %dl
testq %rax, %rax
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x4adad
movl $0x29a, 0x8(%r12)       # imm = 0x29A
leaq 0x30fb4(%rip), %rax     # 0x7bd40
movq 0x30(%rax), %rax
movq %rax, 0x30(%r14)
movq %r14, %rdi
callq 0x4adf5
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq %rcx, %rax
movq %rax, 0x1700(%r12)
leal (%rcx,%rcx,2), %eax
addl $-0x3, %eax
movl %eax, 0x1710(%r12)
movl (%rsp), %eax
movl %eax, 0xc4(%r12)
movl %ebp, 0xc8(%r12)
movb $0x8, 0x48(%r12)
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4ae86