// File: /workspace/github_c_makefile_star2_O2/DavidSM64[P]DKR-Decompressor/DKRGzip.c
// Function: compress_block(ct_data*, ct_data*)
void compress_block(ct_data* ltree, ct_data* dtree) {
    unsigned dist;      /* distance of matched string */
    int lc;             /* match length or unmatched char (if dist == 0) */
    unsigned lx = 0;    /* running index in l_buf */
    unsigned dx = 0;    /* running index in d_buf */
    unsigned fx = 0;    /* running index in flag_buf */
    u8 flag = 0;       /* current flags */
    unsigned code;      /* the code to send */
    int extra;          /* number of extra bits to send */

    if (last_lit != 0) do {
        if ((lx & 7) == 0) flag = flag_buf[fx++];
        lc = l_buf[lx++];
        if ((flag & 1) == 0) {
            send_code(lc, ltree); /* send a literal byte */
            //Tracecv(isgraph(lc), (stderr," '%c' ", lc));
        } else {
            /* Here, lc is the match length - MIN_MATCH */
            code = length_code[lc];
            send_code(code+LITERALS+1, ltree); /* send the length code */
            extra = extra_lbits[code];
            if (extra != 0) {
                lc -= base_length[code];
                send_bits(lc, extra);        /* send the extra length bits */
            }
            dist = d_buf[dx++];
            /* Here, dist is the match distance - 1 */
            code = d_code(dist);
            //Assert (code < D_CODES, "bad d_code");

            send_code(code, dtree);       /* send the distance code */
            extra = extra_dbits[code];
            if (extra != 0) {
                dist -= base_dist[code];
                send_bits(dist, extra);   /* send the extra distance bits */
            }
        } /* literal or match pair ? */
        flag >>= 1;
    } while (lx < last_lit);

    send_code(END_BLOCK, ltree);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, -0x40(%rbp)
movq %rdi, -0x38(%rbp)
cmpl $0x0, 0x8704(%rip)      # 0xdad0
je 0x5501
xorl %r12d, %r12d
movl $0x0, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
xorl %r14d, %r14d
testb $0x7, %r12b
jne 0x5401
movl -0x2c(%rbp), %ecx
movl %ecx, %eax
incl %ecx
movl %ecx, -0x2c(%rbp)
leaq 0x76d3(%rip), %rcx      # 0xcad0
movb (%rax,%rcx), %r14b
movq 0x1008700(%rip), %rax   # 0x100db08
movzbl (%rax,%r12), %r15d
testb $0x1, %r14b
jne 0x5427
movq -0x38(%rbp), %rax
movzwl (%rax,%r15,4), %edi
movzwl 0x2(%rax,%r15,4), %esi
jmp 0x54e7
leaq 0x72a2(%rip), %rax      # 0xc6d0
movzbl (%r15,%rax), %r13d
leal (,%r13,4), %ebx
movq -0x38(%rbp), %rax
movzwl 0x404(%rax,%rbx), %edi
movzwl 0x406(%rax,%rbx), %esi
callq 0x497b
leaq 0x4fd5(%rip), %rax      # 0xa430
movl (%rbx,%rax), %esi
testl %esi, %esi
je 0x5475
leaq 0x7567(%rip), %rax      # 0xc9d0
subl (%rax,%r13,4), %r15d
movl %r15d, %edi
callq 0x497b
movl -0x30(%rbp), %ecx
movl %ecx, %eax
incl %ecx
movl %ecx, -0x30(%rbp)
leaq 0x10386aa(%rip), %rcx   # 0x103db30
movzwl (%rcx,%rax,2), %r15d
movl %r15d, %eax
shrl $0x7, %eax
addq $0x100, %rax            # imm = 0x100
cmpl $0x100, %r15d           # imm = 0x100
movq %r15, %rcx
cmovaeq %rax, %rcx
leaq 0x7324(%rip), %rax      # 0xc7d0
movzbl (%rcx,%rax), %r13d
leal (,%r13,4), %ebx
movq -0x40(%rbp), %rax
movzwl (%rax,%rbx), %edi
movzwl 0x2(%rax,%rbx), %esi
callq 0x497b
leaq 0x4fde(%rip), %rax      # 0xa4b0
movl (%rbx,%rax), %esi
testl %esi, %esi
je 0x54ec
leaq 0x7570(%rip), %rax      # 0xca50
subl (%rax,%r13,4), %r15d
movl %r15d, %edi
callq 0x497b
incq %r12
shrb %r14b
movl 0x85d8(%rip), %eax      # 0xdad0
cmpq %rax, %r12
jb 0x53e6
movq -0x38(%rbp), %rax
movzwl 0x400(%rax), %edi
movzwl 0x402(%rax), %esi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x497b