// File: /workspace/github_c_makefile_star2_O1/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
movl %r8d, -0x4(%rsp)
leaq 0xcb5f(%rip), %rax      # 0x12e30
cmpq %rax, %rdx
je 0x65ca
movq %rcx, %r9
movzwl 0x2(%rdx), %r11d
movzbl (%rsi), %r8d
leal -0x8(,%r8,2), %ebp
movl $0x5, %eax
testl %r8d, %r8d
je 0x6347
xorl %r10d, %r10d
leaq 0xcb0e(%rip), %rbx      # 0x12e10
movl %ebp, %r15d
movzbl (%r10,%rbx), %ecx
movzwl 0x2(%r9,%rcx,4), %r14d
movl %r14d, %ebp
movl %eax, %ecx
shll %cl, %ebp
orl %r15d, %ebp
movl $0x3, %ecx
cmpl $0xe, %eax
jb 0x633a
movb $0x10, %cl
subb %al, %cl
shrl %cl, %r14d
movw %bp, (%rsi)
addq $0x2, %rsi
movl $0xfffffff3, %ecx       # imm = 0xFFFFFFF3
movl %r14d, %ebp
addl %ecx, %eax
incq %r10
movl %ebp, %r15d
cmpq %r10, %r8
jne 0x6305
xorl %ebx, %ebx
testl %r11d, %r11d
movl $0x8a, %r8d
movl $0x7, %ecx
cmovel %r8d, %ecx
setne %bl
addl $0x3, %ebx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %r14d, %r14d
xorl %r12d, %r12d
movzwl 0x6(%rdx,%r14,4), %r8d
leal 0x1(%r12), %r10d
cmpl %ecx, %r10d
jge 0x6386
cmpl %r8d, %r11d
je 0x65b2
cmpl %ebx, %r10d
jge 0x63d6
movl %r11d, %ebx
movzwl (%r9,%rbx,4), %r13d
movzwl 0x2(%r9,%rbx,4), %r12d
movl %r13d, %r15d
movl %eax, %ecx
shll %cl, %r15d
movl $0x10, %ecx
subl %r12d, %ecx
orl %ebp, %r15d
cmpl %ecx, %eax
jbe 0x63c6
movb $0x10, %cl
subb %al, %cl
shrl %cl, %r13d
movw %r15w, (%rsi)
addq $0x2, %rsi
addl $-0x10, %r12d
movl %r13d, %r15d
addl %r12d, %eax
movl %r15d, %ebp
decl %r10d
jne 0x638e
jmp 0x6578
testl %r11d, %r11d
je 0x63e8
cmpl %r13d, %r11d
jne 0x645c
movl %r10d, %r12d
jmp 0x6499
cmpl $0x9, %r12d
jg 0x6502
movzwl 0x44(%r9), %r15d
movzwl 0x46(%r9), %r10d
movl %r15d, %ebx
movl %eax, %ecx
shll %cl, %ebx
movl $0x10, %ecx
subl %r10d, %ecx
orl %ebp, %ebx
cmpl %ecx, %eax
jbe 0x6426
movb $0x10, %cl
subb %al, %cl
shrl %cl, %r15d
movw %bx, (%rsi)
addq $0x2, %rsi
addl $-0x10, %r10d
movl %r15d, %ebx
addl %eax, %r10d
addl $-0x2, %r12d
movl %r12d, %r15d
movl %r10d, %ecx
shll %cl, %r15d
orl %ebx, %r15d
cmpl $0xe, %r10d
jb 0x656b
movw %r15w, (%rsi)
movb $0x10, %cl
subb %r10b, %cl
shrl %cl, %r12d
addq $0x2, %rsi
addl $-0xd, %r10d
jmp 0x6563
movl %r11d, %ecx
movzwl (%r9,%rcx,4), %r15d
movzwl 0x2(%r9,%rcx,4), %r10d
movl %r15d, %ebx
movl %eax, %ecx
shll %cl, %ebx
movl $0x10, %ecx
subl %r10d, %ecx
orl %ebp, %ebx
cmpl %ecx, %eax
jbe 0x6494
movb $0x10, %cl
subb %al, %cl
shrl %cl, %r15d
movw %bx, (%rsi)
addq $0x2, %rsi
addl $-0x10, %r10d
movl %r15d, %ebx
addl %r10d, %eax
movl %ebx, %ebp
movzwl 0x40(%r9), %r15d
movzwl 0x42(%r9), %r10d
movl %r15d, %ebx
movl %eax, %ecx
shll %cl, %ebx
movl $0x10, %ecx
subl %r10d, %ecx
orl %ebp, %ebx
cmpl %ecx, %eax
jbe 0x64cd
movb $0x10, %cl
subb %al, %cl
shrl %cl, %r15d
movw %bx, (%rsi)
addq $0x2, %rsi
addl $-0x10, %r10d
movl %r15d, %ebx
addl %eax, %r10d
addl $-0x3, %r12d
movl %r12d, %r15d
movl %r10d, %ecx
shll %cl, %r15d
orl %ebx, %r15d
cmpl $0xf, %r10d
jb 0x64fc
movw %r15w, (%rsi)
movb $0x10, %cl
subb %r10b, %cl
shrl %cl, %r12d
addq $0x2, %rsi
addl $-0xe, %r10d
jmp 0x6563
addl $0x2, %r10d
jmp 0x6575
movzwl 0x48(%r9), %r15d
movzwl 0x4a(%r9), %r10d
movl %r15d, %ebx
movl %eax, %ecx
shll %cl, %ebx
movl $0x10, %ecx
subl %r10d, %ecx
orl %ebp, %ebx
cmpl %ecx, %eax
jbe 0x6536
movb $0x10, %cl
subb %al, %cl
shrl %cl, %r15d
movw %bx, (%rsi)
addq $0x2, %rsi
addl $-0x10, %r10d
movl %r15d, %ebx
addl %eax, %r10d
addl $-0xa, %r12d
movl %r12d, %r15d
movl %r10d, %ecx
shll %cl, %r15d
orl %ebx, %r15d
cmpl $0xa, %r10d
jb 0x6571
movw %r15w, (%rsi)
movb $0x10, %cl
subb %r10b, %cl
shrl %cl, %r12d
addq $0x2, %rsi
addl $-0x9, %r10d
movl %r10d, %eax
movl %r12d, %r15d
jmp 0x6578
addl $0x3, %r10d
jmp 0x6575
addl $0x7, %r10d
movl %r10d, %eax
xorl %r10d, %r10d
cmpl %r8d, %r11d
sete %r10b
movl %r10d, %ecx
xorl $0x7, %ecx
testl %r8d, %r8d
sete %bl
movl $0x8a, %ebp
cmovel %ebp, %ecx
orb %bl, %r10b
movzbl %r10b, %r10d
andl $0x1, %r10d
movl $0x4, %ebx
subl %r10d, %ebx
xorl %r10d, %r10d
movl %r15d, %ebp
movl %r11d, %r13d
incq %r14
movl %r10d, %r12d
movl %r8d, %r11d
cmpq $0x101, %r14            # imm = 0x101
jne 0x636d
jmp 0x65d4
movl $0x1, %eax
movl $0x1, %ebp
movl -0x4(%rsp), %r8d
cmpq $0x1, %r8
adcq $0x0, %r8
xorl %r9d, %r9d
movl %ebp, %ebx
movzbl (%rdi,%r9), %ecx
movzwl (%rdx,%rcx,4), %r11d
movzwl 0x2(%rdx,%rcx,4), %r10d
movl %r11d, %ebp
movl %eax, %ecx
shll %cl, %ebp
movl $0x10, %ecx
subl %r10d, %ecx
orl %ebx, %ebp
cmpl %ecx, %eax
jbe 0x6620
movb $0x10, %cl
subb %al, %cl
shrl %cl, %r11d
movw %bp, (%rsi)
addq $0x2, %rsi
addl $-0x10, %r10d
movl %r11d, %ebp
addl %r10d, %eax
incq %r9
cmpl %r9d, %r8d
jne 0x65e4
movzwl 0x400(%rdx), %r8d
movzwl 0x402(%rdx), %edi
movl %r8d, %edx
movl %eax, %ecx
shll %cl, %edx
movl $0x10, %ecx
subl %edi, %ecx
orl %ebp, %edx
cmpl %ecx, %eax
jbe 0x6662
movb $0x10, %cl
subb %al, %cl
shrl %cl, %r8d
movw %dx, (%rsi)
addq $0x2, %rsi
addl $-0x10, %edi
movl %r8d, %edx
addl %eax, %edi
cmpl $0x9, %edi
jb 0x6673
movb %dl, (%rsi)
incq %rsi
shrl $0x8, %edx
jmp 0x6677
testl %edi, %edi
je 0x6679
movb %dl, (%rsi)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq