// File: tomfunkhouser[P]gaps/pkgs/png/deflate.c
// Function: deflateBound
uLong ZEXPORT deflateBound(strm, sourceLen)
    z_streamp strm;
    uLong sourceLen;
{
    deflate_state *s;
    uLong complen, wraplen;
    Bytef *str;

    /* conservative upper bound for compressed data */
    complen = sourceLen +
              ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;

    /* if can't get parameters, return conservative bound plus zlib wrapper */
    if (strm == Z_NULL || strm->state == Z_NULL)
        return complen + 6;

    /* compute wrapper length */
    s = strm->state;
    switch (s->wrap) {
    case 0:                                 /* raw deflate */
        wraplen = 0;
        break;
    case 1:                                 /* zlib wrapper */
        wraplen = 6 + (s->strstart ? 4 : 0);
        break;
    case 2:                                 /* gzip wrapper */
        wraplen = 18;
        if (s->gzhead != Z_NULL) {          /* user-supplied gzip header */
            if (s->gzhead->extra != Z_NULL)
                wraplen += 2 + s->gzhead->extra_len;
            str = s->gzhead->name;
            if (str != Z_NULL)
                do {
                    wraplen++;
                } while (*str++);
            str = s->gzhead->comment;
            if (str != Z_NULL)
                do {
                    wraplen++;
                } while (*str++);
            if (s->gzhead->hcrc)
                wraplen += 2;
        }
        break;
    default:                                /* for compiler happiness */
        wraplen = 6;
    }

    /* if not default parameters, return conservative bound */
    if (s->w_bits != 15 || s->hash_bits != 8 + 7)
        return complen + wraplen;

    /* default settings: return tight bound for that case */
    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
           (sourceLen >> 25) + 13 - 6 + wraplen;
}

// Assembly:
movq %rdi, -0x10(%rsp)
movq %rsi, -0x18(%rsp)
movq -0x18(%rsp), %rax
movq -0x18(%rsp), %rcx
addq $0x7, %rcx
shrq $0x3, %rcx
addq %rcx, %rax
movq -0x18(%rsp), %rcx
addq $0x3f, %rcx
shrq $0x6, %rcx
addq %rcx, %rax
addq $0x5, %rax
movq %rax, -0x28(%rsp)
cmpq $0x0, -0x10(%rsp)
je 0x116f4c
movq -0x10(%rsp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x116f5f
movq -0x28(%rsp), %rax
addq $0x6, %rax
movq %rax, -0x8(%rsp)
jmp 0x117121
movq -0x10(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x20(%rsp)
movq -0x20(%rsp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x3c(%rsp)
testl %eax, %eax
je 0x116f98
jmp 0x116f7f
movl -0x3c(%rsp), %eax
subl $0x1, %eax
je 0x116fa6
jmp 0x116f8a
movl -0x3c(%rsp), %eax
subl $0x2, %eax
je 0x116fcd
jmp 0x1170b6
movq $0x0, -0x30(%rsp)
jmp 0x1170bf
movq -0x20(%rsp), %rax
movl 0x9c(%rax), %edx
xorl %eax, %eax
movl $0x4, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
addl $0x6, %eax
cltq
movq %rax, -0x30(%rsp)
jmp 0x1170bf
movq $0x12, -0x30(%rsp)
movq -0x20(%rsp), %rax
cmpq $0x0, 0x30(%rax)
je 0x1170b4
movq -0x20(%rsp), %rax
movq 0x30(%rax), %rax
cmpq $0x0, 0x18(%rax)
je 0x117011
movq -0x20(%rsp), %rax
movq 0x30(%rax), %rax
movl 0x20(%rax), %eax
addl $0x2, %eax
movl %eax, %eax
addq -0x30(%rsp), %rax
movq %rax, -0x30(%rsp)
movq -0x20(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rsp)
cmpq $0x0, -0x38(%rsp)
je 0x117053
jmp 0x11702d
movq -0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rsp)
movq -0x38(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rsp)
cmpb $0x0, (%rax)
jne 0x11702d
jmp 0x117053
movq -0x20(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x38(%rax), %rax
movq %rax, -0x38(%rsp)
cmpq $0x0, -0x38(%rsp)
je 0x117095
jmp 0x11706f
movq -0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rsp)
movq -0x38(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rsp)
cmpb $0x0, (%rax)
jne 0x11706f
jmp 0x117095
movq -0x20(%rsp), %rax
movq 0x30(%rax), %rax
cmpl $0x0, 0x44(%rax)
je 0x1170b2
movq -0x30(%rsp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rsp)
jmp 0x1170b4
jmp 0x1170bf
movq $0x6, -0x30(%rsp)
movq -0x20(%rsp), %rax
cmpl $0xf, 0x48(%rax)
jne 0x1170d5
movq -0x20(%rsp), %rax
cmpl $0xf, 0x78(%rax)
je 0x1170e6
movq -0x28(%rsp), %rax
addq -0x30(%rsp), %rax
movq %rax, -0x8(%rsp)
jmp 0x117121
movq -0x18(%rsp), %rax
movq -0x18(%rsp), %rcx
shrq $0xc, %rcx
addq %rcx, %rax
movq -0x18(%rsp), %rcx
shrq $0xe, %rcx
addq %rcx, %rax
movq -0x18(%rsp), %rcx
shrq $0x19, %rcx
addq %rcx, %rax
addq $0xd, %rax
subq $0x6, %rax
addq -0x30(%rsp), %rax
movq %rax, -0x8(%rsp)
movq -0x8(%rsp), %rax
retq
nopw (%rax,%rax)