// File: /workspace/github_c_makefile_star2_O2/Cyan4973[P]FiniteStateEntropy/programs/zlibh.c
// Function: ZLIBH_inflate
int ZLIBH_inflate(unsigned char* dest, const unsigned char* compressed)
{
    const unsigned char *next = (const unsigned char*)compressed;   /* next input */
    unsigned char *put  = (unsigned char*)dest;         /* next output */
    unsigned hold;              /* bit buffer */
    unsigned bits;              /* bits in bit buffer */
    unsigned copy;              /* number of stored or match bytes to copy */
    code here;                  /* current decoding table entry */
    unsigned len;               /* length to copy for repeats, bits to drop */
    int ret;                    /* return code */
    struct inflate_state state;
    static const unsigned short order[19] = /* permutation of code lengths */
    {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
    code const *lcode;
    unsigned lmask;             /* mask for first level of length codes */
    unsigned codebits;          /* code bits, operation, was op */

    state.mode = TYPEDO;      /* skip check */

    /* Clear the input bit accumulator */
    hold = 0;
    bits = 0;
    for (;;)
        switch (state.mode) {
        case TYPEDO:
            NEEDBITS(1);
            switch (BITS(1)) {
            case 0:                             /* dynamic block */
                state.mode = TABLE;
                break;

            case 1:                             /* fixed block */
                fixedtables(&state);
                state.mode = LEN;             /* decode codes */
                break;
            }
            DROPBITS(1);
            break;

        case TABLE:
            NEEDBITS(4);
            state.nlen = 257;
            state.ndist = 0;
            state.ncode = BITS(4) + 4;
            DROPBITS(4);

            state.have = 0;

            while (state.have < state.ncode) {
                NEEDBITS(3);
                state.lens[order[state.have++]] = (unsigned short)BITS(3);
                DROPBITS(3);
            }
            while (state.have < 19)
                state.lens[order[state.have++]] = 0;
            state.next = state.codes;
            state.lencode = (const code *)(state.next);
            state.lenbits = 7;
            ret = inflate_table(CODES, state.lens, 19, &(state.next),
                &(state.lenbits), state.work);
            if (ret) {
                state.mode = BAD;
                break;
            }
            state.have = 0;

            while (state.have < state.nlen) {
                for (;;) {
                    here = state.lencode[BITS(state.lenbits)];
                    if ((unsigned)(here.bits) <= bits) break;
                    PULLBYTE();
                }
                if (here.val < 16) {
                    DROPBITS(here.bits);
                    state.lens[state.have++] = here.val;
                }
                else {
                    if (here.val == 16) {
                        NEEDBITS(here.bits + 2);
                        DROPBITS(here.bits);
                        if (state.have == 0) {
                            state.mode = BAD;
                            break;
                        }
                        len = state.lens[state.have - 1];
                        copy = 3 + BITS(2);
                        DROPBITS(2);
                    }
                    else if (here.val == 17) {
                        NEEDBITS(here.bits + 3);
                        DROPBITS(here.bits);
                        len = 0;
                        copy = 3 + BITS(3);
                        DROPBITS(3);
                    }
                    else {
                        NEEDBITS(here.bits + 7);
                        DROPBITS(here.bits);
                        len = 0;
                        copy = 11 + BITS(7);
                        DROPBITS(7);
                    }
                    if (state.have + copy > state.nlen) {
                        state.mode = BAD;
                        break;
                    }
                    while (copy--)
                        state.lens[state.have++] = (unsigned short)len;
                }
            }

            /* handle error breaks in while */
            if (state.mode == BAD) break;

            /* check for end-of-block code (better have one) */
            if (state.lens[256] == 0) {
                state.mode = BAD;
                break;
            }

            /* build code tables -- note: do not change the lenbits or distbits
            values here (9 and 6) without reading the comments in inftrees.h
            concerning the ENOUGH constants, which depend on those values */
            state.next = state.codes;
            state.lencode = (const code *)(state.next);
            state.lenbits = 9;
            ret = inflate_table(LENS, state.lens, state.nlen, &(state.next),
                &(state.lenbits), state.work);
            if (ret) {
                state.mode = BAD;
                break;
            }
            state.mode = LEN;
            /* fallthrough */

        case LEN:
            lcode = state.lencode;
            lmask = (1U << state.lenbits) - 1;
            do {
                if (bits < 15) {
                    hold += (unsigned long)(*next++) << bits;
                    bits += 8;
                    hold += (unsigned long)(*next++) << bits;
                    bits += 8;
                }
                here = lcode[hold & lmask];
dolen:
                codebits = (unsigned)(here.bits);
                hold >>= codebits;
                bits -= codebits;
                codebits = (unsigned)(here.op);
                if (codebits == 0) {                          /* literal */
                    *put++ = (unsigned char)(here.val);
                }
                else if ((codebits & 64) == 0) {              /* 2nd level length code */
                    here = lcode[here.val + (hold & ((1U << codebits) - 1))];
                    goto dolen;
                }
                else if (codebits & 32) {                     /* end-of-block */
                    //len = bits >> 3;                        /* restitute unused bytes */
                    //next -= len;
                    break;
                }
            } while (1);
            state.mode = DONE;

        case DONE:
            goto inf_leave;
        case BAD:
            return 0;
    }
inf_leave:
    //return (int)(next-compressed);   // compressed size
    return (int)(put-dest);          // original size
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1bf8, %rsp           # imm = 0x1BF8
leaq 0x98(%rsp), %rax
andl $0x0, -0x88(%rax)
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
xorl %eax, %eax
leaq 0x9b65(%rip), %rbp      # 0xfe70
pushq $-0x1
popq %r13
leaq 0xa11a(%rip), %r15      # 0x10430
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpl $0x4, %eax
ja 0x631b
movl %eax, %eax
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
pushq $0x8
popq %rcx
testl %r12d, %r12d
jne 0x6472
movzbl (%r14), %eax
incq %r14
addl %eax, %ebx
movl %ecx, %r12d
jmp 0x632f
movl %r12d, %eax
orl $0x8, %eax
cmpl $0x3, %r12d
ja 0x6366
movzbl (%r14), %edx
movl %r12d, %ecx
shlq %cl, %rdx
incq %r14
addl %edx, %ebx
movl %eax, %r12d
jmp 0x634c
movq $0x101, 0x84(%rsp) # imm = 0x101
movl %ebx, %eax
andl $0xf, %eax
leal 0x4(%rax), %edx
movl %edx, 0x80(%rsp)
movl %ebx, %ebp
shrl $0x4, %ebp
addl $-0x4, %r12d
andl $0x0, 0x8c(%rsp)
xorl %esi, %esi
cmpq %rdx, %rsi
je 0x63df
movl %r12d, %edi
orl $0x8, %edi
cmpl $0x2, %r12d
ja 0x63ba
movzbl (%r14), %r8d
movl %r12d, %ecx
shlq %cl, %r8
incq %r14
addl %r8d, %ebp
movl %edi, %r12d
jmp 0x639f
movl %ebp, %ecx
andl $0x7, %ecx
movzwl (%r15,%rsi,2), %edi
incq %rsi
movl %esi, 0x8c(%rsp)
movw %cx, 0x98(%rsp,%rdi,2)
shrl $0x3, %ebp
addl $-0x3, %r12d
jmp 0x6394
notb %bl
movzbl %bl, %ecx
andl $0xf, %ecx
addq %rsi, %rcx
addq $-0x4, %rcx
cmpq %rax, %rcx
je 0x6411
leal 0x5(%rax), %edx
movl %edx, 0x8c(%rsp)
movzwl 0x8(%r15,%rax,2), %edx
andw $0x0, 0x98(%rsp,%rdx,2)
incq %rax
jmp 0x63ee
leaq 0x558(%rsp), %rax
movq %rax, 0x90(%rsp)
movq %rax, 0x68(%rsp)
movl $0x7, 0x78(%rsp)
xorl %edi, %edi
leaq 0x98(%rsp), %rsi
pushq $0x13
popq %rdx
leaq 0x90(%rsp), %rcx
leaq 0x78(%rsp), %r8
leaq 0x318(%rsp), %r9
callq 0x5ec1
testl %eax, %eax
je 0x64b2
movl $0x4, 0x10(%rsp)
pushq $0x4
popq %rax
movl %ebp, %ebx
leaq 0x9a03(%rip), %rbp      # 0xfe70
jmp 0x631b
pushq $0x1
popq %rax
testb $0x1, %bl
je 0x64a4
leaq 0xa0af(%rip), %rax      # 0x10530
movq %rax, 0x68(%rsp)
movabsq $0x500000009, %rax      # imm = 0x500000009
movq %rax, 0x78(%rsp)
leaq 0xa894(%rip), %rax      # 0x10d30
movq %rax, 0x70(%rsp)
pushq $0x2
popq %rax
movl %eax, 0x10(%rsp)
shrl %ebx
decl %r12d
jmp 0x631b
andl $0x0, 0x8c(%rsp)
movl 0x84(%rsp), %edx
movb 0x78(%rsp), %cl
movl %r13d, %edi
shll %cl, %edi
movq 0x68(%rsp), %r8
notl %edi
xorl %r9d, %r9d
movl %ebp, %ebx
leaq 0x9993(%rip), %rbp      # 0xfe70
cmpl %edx, %r9d
jae 0x6640
movl %r12d, %eax
movl %ebx, %ecx
andl %edi, %ecx
movzbl 0x1(%r8,%rcx,4), %esi
cmpq %rsi, %rax
jae 0x6510
movzbl (%r14), %esi
movl %eax, %ecx
shlq %cl, %rsi
incq %r14
addl %esi, %ebx
addq $0x8, %rax
addl $0x8, %r12d
jmp 0x64e9
movzwl 0x2(%r8,%rcx,4), %r10d
cmpl $0xf, %r10d
ja 0x6541
movl %esi, %ecx
shrl %cl, %ebx
subl %esi, %eax
movl %r9d, %ecx
leal 0x1(%r9), %esi
movl %esi, 0x8c(%rsp)
movw %r10w, 0x98(%rsp,%rcx,2)
movl %esi, %r9d
movl %eax, %r12d
jmp 0x64dd
cmpl $0x10, %r10d
je 0x6588
cmpl $0x11, %r10d
jne 0x65d9
leaq 0x3(%rsi), %r10
cmpq %r10, %rax
jae 0x656f
movzbl (%r14), %r11d
movl %eax, %ecx
shlq %cl, %r11
incq %r14
addl %r11d, %ebx
addq $0x8, %rax
jmp 0x6555
movl %esi, %ecx
shrl %cl, %ebx
movl %ebx, %ecx
andl $0x7, %ecx
addl $0x3, %ecx
shrl $0x3, %ebx
subl %esi, %eax
addl $-0x3, %eax
jmp 0x660b
leaq 0x2(%rsi), %r10
subl %esi, %r12d
cmpq %r10, %rax
jae 0x65ad
movzbl (%r14), %r11d
movl %eax, %ecx
shlq %cl, %r11
incq %r14
addl %r11d, %ebx
addq $0x8, %rax
addl $0x8, %r12d
jmp 0x658f
movl %esi, %ecx
shrl %cl, %ebx
testl %r9d, %r9d
je 0x669f
leal -0x1(%r9), %ecx
movzwl 0x98(%rsp,%rcx,2), %r10d
movl %ebx, %ecx
andl $0x3, %ecx
addl $0x3, %ecx
shrl $0x2, %ebx
subl %esi, %eax
addl $-0x2, %eax
jmp 0x660e
leaq 0x7(%rsi), %r10
cmpq %r10, %rax
jae 0x65f7
movzbl (%r14), %r11d
movl %eax, %ecx
shlq %cl, %r11
incq %r14
addl %r11d, %ebx
addq $0x8, %rax
jmp 0x65dd
movl %esi, %ecx
shrl %cl, %ebx
movl %ebx, %ecx
andl $0x7f, %ecx
addl $0xb, %ecx
shrl $0x7, %ebx
subl %esi, %eax
addl $-0x7, %eax
xorl %r10d, %r10d
movl %eax, %r12d
leal (%rcx,%r9), %eax
cmpl %edx, %eax
ja 0x669f
subl $0x1, %ecx
jb 0x6633
movl %r9d, %eax
incl %r9d
movw %r10w, 0x98(%rsp,%rax,2)
jmp 0x661d
movl %r9d, 0x8c(%rsp)
jmp 0x64dd
cmpl $0x4, 0x10(%rsp)
pushq $0x4
popq %rax
je 0x631b
cmpw $0x0, 0x298(%rsp)
je 0x669f
leaq 0x558(%rsp), %rax
movq %rax, 0x90(%rsp)
movq %rax, 0x68(%rsp)
movl $0x9, 0x78(%rsp)
pushq $0x1
popq %rdi
leaq 0x98(%rsp), %rsi
leaq 0x90(%rsp), %rcx
leaq 0x78(%rsp), %r8
leaq 0x318(%rsp), %r9
callq 0x5ec1
testl %eax, %eax
je 0x66c3
movl $0x4, 0x10(%rsp)
pushq $0x4
popq %rax
jmp 0x631b
xorl %eax, %eax
jmp 0x6758
movq 0x8(%rsp), %r9
movq %r9, %rax
jmp 0x6755
movl $0x2, 0x10(%rsp)
movb 0x78(%rsp), %cl
movl %r13d, %esi
shll %cl, %esi
movq 0x68(%rsp), %rdi
notl %esi
movq 0x8(%rsp), %r9
movq %r9, %rax
cmpl $0xe, %r12d
ja 0x670e
movzbl (%r14), %edx
movl %r12d, %ecx
shlq %cl, %rdx
addl %edx, %ebx
movzbl 0x1(%r14), %edx
leal 0x8(%r12), %ecx
shlq %cl, %rdx
addq $0x2, %r14
addl %ebx, %edx
orl $0x10, %r12d
movl %edx, %ebx
movl %ebx, %r8d
andl %esi, %r8d
movl %r8d, %r8d
movzbl 0x1(%rdi,%r8,4), %edx
movl %edx, %ecx
shrl %cl, %ebx
movzbl (%rdi,%r8,4), %ecx
subl %edx, %r12d
movzwl 0x2(%rdi,%r8,4), %edx
testl %ecx, %ecx
je 0x6749
testb $0x40, %cl
jne 0x6750
movl %r13d, %r8d
shll %cl, %r8d
notl %r8d
andl %ebx, %r8d
addl %edx, %r8d
jmp 0x6714
movb %dl, (%rax)
incq %rax
jmp 0x66e3
testb $0x20, %cl
je 0x66e3
subl %r9d, %eax
addq $0x1bf8, %rsp           # imm = 0x1BF8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq