// File: /workspace/github_c_makefile_star2_O2/Cyan4973[P]FiniteStateEntropy/programs/zlibh.c
// Function: ZLIBH_compress_block
static void ZLIBH_compress_block(const unsigned char* ip, unsigned char* op, const ct_data * ltree, const ct_data * bltree, unsigned int ip_len)
{
    unsigned int bi_buf;    /* bit buffer */
    unsigned int bi_valid;  /* bits used in bit_buf */
    unsigned int lx = 0;    /* running index in l_buf */
    unsigned int value;     /* value to send */
    unsigned short length;  /* number of bits to send */

    if  (ltree != static_ltree) {  /* write dynamic Huffman header first */
        /* send_tree is merged here */
        int prevlen = -1;            /* last emitted length */
        int nextlen = ltree[0].Len;  /* length of next code */
        int count = 0;               /* repeat count of the current code */
        int max_count = 7;           /* max repeat count */
        int min_count = 4;           /* min repeat count */
        unsigned int max_code = 256;
        unsigned int blcodes;
        unsigned int n;

        bi_valid = 5;
        blcodes = op[0];
        bi_buf = (unsigned int)((blcodes-4)<<1);
        length = 3;
        for (n = 0; n < blcodes; n++) {
            value = bltree[bl_order[n]].Len;
            SENDBITS();
        }

        if (nextlen == 0) max_count = 138, min_count = 3;

        for (n = 0; n <= max_code; n++) {
            int curlen;                  /* length of current code */
            curlen = nextlen; nextlen = ltree[n+1].Len;
            if (++count < max_count && curlen == nextlen) {
                continue;
            } else if (count < min_count) {
                do {
                    value = bltree[curlen].Code;
                    length = bltree[curlen].Len;
                    SENDBITS();
                } while (--count != 0);

            } else if (curlen != 0) {
                if (curlen != prevlen) {
                    value = bltree[curlen].Code;
                    length = bltree[curlen].Len;
                    SENDBITS();
                    count--;
                }
                value = bltree[REP_3_6].Code;
                length = bltree[REP_3_6].Len;
                SENDBITS();

                length = 2;
                value = count-3;
                SENDBITS();

            } else if (count < 11) {
                value = bltree[REPZ_3_10].Code;
                length = bltree[REPZ_3_10].Len;
                SENDBITS();

                length = 3;
                value = count-3;
                SENDBITS();

            } else {
                value = bltree[REPZ_11_138].Code;
                length = bltree[REPZ_11_138].Len;
                SENDBITS();

                length = 7;
                value = count-11;
                SENDBITS();

            }
            count = 0; prevlen = curlen;
            if (nextlen == 0) {
                max_count = 138, min_count = 3;
            } else if (curlen == nextlen) {
                max_count = 6, min_count = 3;
            } else {
                max_count = 7, min_count = 4;
            }
        }
    }
    else {            /* static case only identified by a single one bit */
        bi_valid = 1;
        bi_buf = 1;
    }
    lx = 1;
    do {
        unsigned int t_index;
        t_index = *ip++;
        value = (unsigned int)ltree[t_index].Code;
        length = ltree[t_index].Len;
        if (bi_valid > (16U - length)) {
            bi_buf |= value << bi_valid;
            *op++=(unsigned char)(bi_buf & 0xff);
            *op++=(unsigned char)(bi_buf >> 8);
            bi_buf = value >> (16 - bi_valid);
            bi_valid += length - 16;
        } else {
            bi_buf |= value << bi_valid;
            bi_valid += length;
        }
    } while (lx++ < ip_len);

    value = (unsigned int)ltree[END_BLOCK].Code;   /* send End of Block */
    length = ltree[END_BLOCK].Len;
    if (bi_valid > (16U - length)) {
        bi_buf |= value << bi_valid;
        *op++=(unsigned char)(bi_buf & 0xff);
        *op++=(unsigned char)(bi_buf >> 8);
        bi_buf = value >> (16 - bi_valid);
        bi_valid += length - 16;
    } else {
        bi_buf |= value << bi_valid;
        bi_valid += length;
    }

    if (bi_valid > 8) {                             /* flush bit_buf */
        *op++=(unsigned char)(bi_buf & 0xff);
        *op++=(unsigned char)(bi_buf >> 8);
    }
    else if (bi_valid > 0) {
        *op++=(unsigned char)bi_buf;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq 0xa3fb(%rip), %rax      # 0xfeb0
movl $0x10, %ebp
cmpq %rax, %rdx
je 0x5e0e
movq %rcx, %r9
movl %r8d, -0x1c(%rsp)
movzwl 0x2(%rdx), %ebx
movzbl (%rsi), %r8d
leal -0x8(,%r8,2), %eax
movl $0x5, %r10d
xorl %r14d, %r14d
leaq 0xa3a5(%rip), %r15      # 0xfe90
movl $0x3, %ebp
movl $0xfffffff3, %r12d      # imm = 0xFFFFFFF3
cmpq %r14, %r8
je 0x5b3d
movzbl (%r14,%r15), %ecx
movzwl 0x2(%r9,%rcx,4), %r11d
movl %r11d, %r13d
movl %r10d, %ecx
shll %cl, %r13d
orl %eax, %r13d
cmpl $0xe, %r10d
jb 0x5b30
movb $0x10, %cl
subb %r10b, %cl
shrl %cl, %r11d
movw %r13w, (%rsi)
addq $0x2, %rsi
movl %r12d, %eax
movl %r11d, %r13d
jmp 0x5b32
movl %ebp, %eax
addl %eax, %r10d
incq %r14
movl %r13d, %eax
jmp 0x5af6
xorl %r8d, %r8d
testl %ebx, %ebx
movl $0x8a, %r11d
movl $0x7, %ecx
cmovel %r11d, %ecx
setne %r8b
addl $0x3, %r8d
leaq 0x6(%rdx), %r15
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %r12d, %r12d
movl %r13d, %r14d
movq %r12, -0x10(%rsp)
leaq (%r15,%r12,4), %r11
xorl %r12d, %r12d
movl %ebx, %r13d
movq -0x10(%rsp), %rbx
addq %r12, %rbx
cmpq $0x101, %rbx            # imm = 0x101
je 0x5e02
movq %r12, %rbp
movzwl (%r11,%r12,4), %ebx
incq %r12
cmpl %ecx, %r12d
jge 0x5ba2
cmpl %ebx, %r13d
je 0x5b75
movq %r15, -0x8(%rsp)
movl %r13d, %r15d
cmpl %r8d, %r12d
jae 0x5bfb
notl %ebp
movzwl (%r9,%r15,4), %r14d
movzwl 0x2(%r9,%r15,4), %r8d
movl %r14d, %r11d
movl %r10d, %ecx
shll %cl, %r11d
movl $0x10, %ecx
subl %r8d, %ecx
orl %eax, %r11d
cmpl %ecx, %r10d
jbe 0x5bec
movb $0x10, %cl
subb %r10b, %cl
shrl %cl, %r14d
movw %r11w, (%rsi)
addq $0x2, %rsi
addl $-0x10, %r8d
movl %r14d, %r11d
addl %r8d, %r10d
movl %r11d, %eax
incl %ebp
jne 0x5bb1
jmp 0x5dbd
leaq -0x1(%r12), %rcx
testl %r13d, %r13d
je 0x5c21
movq %rcx, -0x18(%rsp)
cmpl %r14d, %r13d
jne 0x5c95
movl %r12d, %r15d
movl $0x10, %r14d
jmp 0x5cdb
cmpl $0x9, %ecx
ja 0x5d48
movzwl 0x44(%r9), %r11d
movzwl 0x46(%r9), %ebp
movl %r11d, %r8d
movl %r10d, %ecx
shll %cl, %r8d
movl $0x10, %ecx
subl %ebp, %ecx
orl %eax, %r8d
cmpl %ecx, %r10d
jbe 0x5c62
movb $0x10, %cl
subb %r10b, %cl
shrl %cl, %r11d
movw %r8w, (%rsi)
addq $0x2, %rsi
addl $-0x10, %ebp
movl %r11d, %r8d
addl %r10d, %ebp
leaq -0x3(%r12), %rax
movl %eax, %r11d
movl %ebp, %ecx
shll %cl, %r11d
orl %r8d, %r11d
cmpl $0xe, %ebp
jb 0x5db2
movw %r11w, (%rsi)
movb $0x10, %cl
subb %bpl, %cl
shrl %cl, %eax
addq $0x2, %rsi
addl $-0xd, %ebp
jmp 0x5daa
movzwl (%r9,%r15,4), %r11d
movzwl 0x2(%r9,%r15,4), %r8d
movl %r11d, %ebp
movl %r10d, %ecx
shll %cl, %ebp
movl $0x10, %r14d
movl %r14d, %ecx
subl %r8d, %ecx
orl %eax, %ebp
cmpl %ecx, %r10d
jbe 0x5cd1
movb $0x10, %cl
subb %r10b, %cl
shrl %cl, %r11d
movw %bp, (%rsi)
addq $0x2, %rsi
addl $-0x10, %r8d
movl %r11d, %ebp
movq -0x18(%rsp), %r15
addl %r8d, %r10d
movl %ebp, %eax
movzwl 0x40(%r9), %r11d
movzwl 0x42(%r9), %ebp
movl %r11d, %r8d
movl %r10d, %ecx
shll %cl, %r8d
movl %r14d, %ecx
subl %ebp, %ecx
orl %eax, %r8d
cmpl %ecx, %r10d
jbe 0x5d11
movb $0x10, %cl
subb %r10b, %cl
shrl %cl, %r11d
movw %r8w, (%rsi)
addq $0x2, %rsi
addl $-0x10, %ebp
movl %r11d, %r8d
addl %r10d, %ebp
addl $-0x3, %r15d
movl %r15d, %r11d
movl %ebp, %ecx
shll %cl, %r11d
orl %r8d, %r11d
cmpl $0xf, %ebp
jb 0x5d43
movw %r11w, (%rsi)
movb $0x10, %cl
subb %bpl, %cl
shrl %cl, %r15d
addq $0x2, %rsi
addl $-0xe, %ebp
movl %ebp, %r10d
movl %r15d, %r11d
jmp 0x5dbd
addl $0x2, %ebp
jmp 0x5dba
movzwl 0x48(%r9), %r11d
movzwl 0x4a(%r9), %ebp
movl %r11d, %r8d
movl %r10d, %ecx
shll %cl, %r8d
movl $0x10, %ecx
subl %ebp, %ecx
orl %eax, %r8d
cmpl %ecx, %r10d
jbe 0x5d80
movb $0x10, %cl
subb %r10b, %cl
shrl %cl, %r11d
movw %r8w, (%rsi)
addq $0x2, %rsi
addl $-0x10, %ebp
movl %r11d, %r8d
addl %r10d, %ebp
leaq -0xb(%r12), %rax
movl %eax, %r11d
movl %ebp, %ecx
shll %cl, %r11d
orl %r8d, %r11d
cmpl $0xa, %ebp
jb 0x5db7
movw %r11w, (%rsi)
movb $0x10, %cl
subb %bpl, %cl
shrl %cl, %eax
addq $0x2, %rsi
addl $-0x9, %ebp
movl %ebp, %r10d
movl %eax, %r11d
jmp 0x5dbd
addl $0x3, %ebp
jmp 0x5dba
addl $0x7, %ebp
movl %ebp, %r10d
movq -0x10(%rsp), %rax
addq %r12, %rax
movq %rax, %r12
xorl %eax, %eax
cmpl %ebx, %r13d
sete %al
movl %eax, %ecx
xorl $0x7, %ecx
testl %ebx, %ebx
sete %r8b
movl $0x8a, %ebp
cmovel %ebp, %ecx
orb %r8b, %al
movzbl %al, %eax
andl $0x1, %eax
movl $0x4, %r8d
subl %eax, %r8d
movl %r11d, %eax
movq -0x8(%rsp), %r15
jmp 0x5b66
movl $0x10, %ebp
movl -0x1c(%rsp), %r8d
jmp 0x5e17
movl $0x1, %r10d
movl %r10d, %eax
xorl %r14d, %r14d
movl %eax, %ebx
movzbl (%rdi,%r14), %eax
movzwl (%rdx,%rax,4), %r11d
movzwl 0x2(%rdx,%rax,4), %r9d
movl %r11d, %eax
movl %r10d, %ecx
shll %cl, %eax
movl %ebp, %ecx
subl %r9d, %ecx
orl %ebx, %eax
cmpl %ecx, %r10d
jbe 0x5e56
movb $0x10, %cl
subb %r10b, %cl
shrl %cl, %r11d
movw %ax, (%rsi)
addq $0x2, %rsi
addl $-0x10, %r9d
movl %r11d, %eax
addl %r9d, %r10d
incq %r14
cmpl %r8d, %r14d
jb 0x5e1a
movzwl 0x400(%rdx), %r8d
movl %r8d, %edi
movl %r10d, %ecx
shll %cl, %edi
movzwl 0x402(%rdx), %edx
subl %edx, %ebp
orl %eax, %edi
cmpl %ebp, %r10d
jbe 0x5e96
movb $0x10, %cl
subb %r10b, %cl
shrl %cl, %r8d
movw %di, (%rsi)
addq $0x2, %rsi
addl $-0x10, %edx
movl %r8d, %edi
addl %r10d, %edx
cmpl $0x9, %edx
jb 0x5ea9
movb %dil, (%rsi)
incq %rsi
shrl $0x8, %edi
jmp 0x5ead
testl %edx, %edx
je 0x5eb0
movb %dil, (%rsi)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq