// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/htscodecs/htscodecs/rANS_static4x16pr.c
// Function: rans_compress_O0_4x16
unsigned char *rans_compress_O0_4x16(unsigned char *in, unsigned int in_size,
                                     unsigned char *out, unsigned int *out_size) {
    unsigned char *cp, *out_end;
    RansEncSymbol syms[256];
    RansState rans0;
    RansState rans2;
    RansState rans1;
    RansState rans3;
    uint8_t* ptr;
    uint32_t F[256+MAGIC] = {0};
    int i, j, tab_size = 0, rle, x;
    // -20 for order/size/meta
    uint32_t bound = rans_compress_bound_4x16(in_size,0)-20;

    if (!out) {
        *out_size = bound;
        out = malloc(*out_size);
    }
    if (!out || bound > *out_size)
        return NULL;

    // If "out" isn't word aligned, tweak out_end/ptr to ensure it is.
    // We already added more round in bound to allow for this.
    if (((size_t)out)&1)
        bound--;
    ptr = out_end = out + bound;

    if (in_size == 0)
        goto empty;

    // Compute statistics
    if (hist8(in, in_size, F) < 0)
        return NULL;

    // Normalise so frequences sum to power of 2
    uint32_t fsum = in_size;
    uint32_t max_val = round2(fsum);
    if (max_val > TOTFREQ)
        max_val = TOTFREQ;

    if (normalise_freq(F, fsum, max_val) < 0)
        return NULL;
    fsum=max_val;

    cp = out;
    cp += encode_freq(cp, F);
    tab_size = cp-out;
    //write(2, out+4, cp-(out+4));

    if (normalise_freq(F, fsum, TOTFREQ) < 0)
        return NULL;

    // Encode statistics.
    for (x = rle = j = 0; j < 256; j++) {
        if (F[j]) {
            RansEncSymbolInit(&syms[j], x, F[j], TF_SHIFT);
            x += F[j];
        }
    }

    RansEncInit(&rans0);
    RansEncInit(&rans1);
    RansEncInit(&rans2);
    RansEncInit(&rans3);

    switch (i=(in_size&3)) {
    case 3: RansEncPutSymbol(&rans2, &ptr, &syms[in[in_size-(i-2)]]);
        // fall-through
    case 2: RansEncPutSymbol(&rans1, &ptr, &syms[in[in_size-(i-1)]]);
        // fall-through
    case 1: RansEncPutSymbol(&rans0, &ptr, &syms[in[in_size-(i-0)]]);
        // fall-through
    case 0:
        break;
    }
    for (i=(in_size &~3); i>0; i-=4) {
        RansEncSymbol *s3 = &syms[in[i-1]];
        RansEncSymbol *s2 = &syms[in[i-2]];
        RansEncSymbol *s1 = &syms[in[i-3]];
        RansEncSymbol *s0 = &syms[in[i-4]];

#if 1
        RansEncPutSymbol(&rans3, &ptr, s3);
        RansEncPutSymbol(&rans2, &ptr, s2);
        RansEncPutSymbol(&rans1, &ptr, s1);
        RansEncPutSymbol(&rans0, &ptr, s0);
#else
        // Slightly beter on gcc, much better on clang
        uint16_t *ptr16 = (uint16_t *)ptr;

        if (rans3 >= s3->x_max) *--ptr16 = (uint16_t)rans3, rans3 >>= 16;
        if (rans2 >= s2->x_max) *--ptr16 = (uint16_t)rans2, rans2 >>= 16;
        uint32_t q3 = (uint32_t) (((uint64_t)rans3 * s3->rcp_freq) >> s3->rcp_shift);
        uint32_t q2 = (uint32_t) (((uint64_t)rans2 * s2->rcp_freq) >> s2->rcp_shift);
        rans3 += s3->bias + q3 * s3->cmpl_freq;
        rans2 += s2->bias + q2 * s2->cmpl_freq;

        if (rans1 >= s1->x_max) *--ptr16 = (uint16_t)rans1, rans1 >>= 16;
        if (rans0 >= s0->x_max) *--ptr16 = (uint16_t)rans0, rans0 >>= 16;
        uint32_t q1 = (uint32_t) (((uint64_t)rans1 * s1->rcp_freq) >> s1->rcp_shift);
        uint32_t q0 = (uint32_t) (((uint64_t)rans0 * s0->rcp_freq) >> s0->rcp_shift);
        rans1 += s1->bias + q1 * s1->cmpl_freq;
        rans0 += s0->bias + q0 * s0->cmpl_freq;

        ptr = (uint8_t *)ptr16;
#endif
    }

    RansEncFlush(&rans3, &ptr);
    RansEncFlush(&rans2, &ptr);
    RansEncFlush(&rans1, &ptr);
    RansEncFlush(&rans0, &ptr);

 empty:
    // Finalise block size and return it
    *out_size = (out_end - ptr) + tab_size;

    memmove(out + tab_size, ptr, out_end-ptr);

    return out;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1ca8, %rsp           # imm = 0x1CA8
movq %rcx, %rbx
movq %rdx, %r13
movl %esi, %r14d
movq %rdi, %r15
leaq 0x40(%rsp), %rdi
movl $0x420, %edx            # imm = 0x420
xorl %esi, %esi
callq 0x4260
movq %r14, 0x30(%rsp)
movl %r14d, %eax
movq %rax, 0x18(%rsp)
cvtsi2sd %rax, %xmm0
mulsd 0x2dcff(%rip), %xmm0    # 0x7cba8
addsd 0x2e95f(%rip), %xmm0    # 0x7d810
addsd 0x2dcb7(%rip), %xmm0    # 0x7cb70
addsd 0x2ec87(%rip), %xmm0    # 0x7db48
cvttsd2si %xmm0, %r14
movl %r14d, %r12d
andl $0x1, %r12d
leal (%r14,%r12), %ebp
addl $-0x12, %ebp
movq %r13, 0x20(%rsp)
testq %r13, %r13
movq %rbx, 0x28(%rsp)
je 0x4eeec
cmpl (%rbx), %ebp
jbe 0x4ef03
jmp 0x4f1da
movl %ebp, (%rbx)
movl %ebp, %edi
callq 0x4680
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x4f1da
leal (%r14,%r12), %edx
addl $0x2, %edx
addl $-0x15, %edx
movq 0x20(%rsp), %r12
testb $0x1, %r12b
cmovel %ebp, %edx
addq %r12, %rdx
movq %rdx, 0x8(%rsp)
movq 0x30(%rsp), %rbx
testl %ebx, %ebx
je 0x4f011
movq %rdx, 0x38(%rsp)
cmpl $0x7a121, %ebx          # imm = 0x7A121
jb 0x4f020
pushq $0x4
popq %rsi
movl $0x3006f, %edi          # imm = 0x3006F
callq 0x6426c
testq %rax, %rax
movq 0x18(%rsp), %r10
je 0x4f1da
movl %ebx, %edx
andl $-0x10, %edx
xorl %ecx, %ecx
cmpq %rdx, %rcx
jae 0x4efd7
movzwl (%r15,%rcx), %esi
movzwl 0x2(%r15,%rcx), %edi
movzwl 0x4(%r15,%rcx), %r8d
movzwl 0x6(%r15,%rcx), %r9d
incl (%rax,%rsi,4)
incl 0x40094(%rax,%rdi,4)
incl 0x80128(%rax,%r8,4)
incl (%rax,%r9,4)
movzwl 0x8(%r15,%rcx), %esi
movzwl 0xa(%r15,%rcx), %edi
movzwl 0xc(%r15,%rcx), %r8d
movzwl 0xe(%r15,%rcx), %r9d
incl 0x40094(%rax,%rsi,4)
incl (%rax,%rdi,4)
incl 0x40094(%rax,%r8,4)
incl 0x80128(%rax,%r9,4)
addq $0x10, %rcx
jmp 0x4ef61
movzbl (%r15,%rcx), %edx
incq %rcx
incl 0x40(%rsp,%rdx,4)
cmpq %r10, %rcx
jb 0x4efcb
xorl %ecx, %ecx
cmpq $0x10000, %rcx          # imm = 0x10000
je 0x4f110
movl 0x40094(%rax,%rcx,4), %edx
addl (%rax,%rcx,4), %edx
addl 0x80128(%rax,%rcx,4), %edx
movzbl %cl, %esi
addl %edx, 0x40(%rsp,%rsi,4)
movl %ecx, %esi
shrl $0x8, %esi
addl %edx, 0x40(%rsp,%rsi,4)
incq %rcx
jmp 0x4efde
xorl %edi, %edi
movq %rdx, %rsi
movq 0x28(%rsp), %rcx
jmp 0x4f385
leaq 0xca0(%rsp), %rdi
xorl %r14d, %r14d
movl $0x420, %edx            # imm = 0x420
xorl %esi, %esi
callq 0x4260
leaq 0x880(%rsp), %rdi
movl $0x420, %edx            # imm = 0x420
xorl %esi, %esi
callq 0x4260
leaq 0x460(%rsp), %rdi
movl $0x420, %edx            # imm = 0x420
xorl %esi, %esi
callq 0x4260
movl %ebx, %eax
andl $0x7fff8, %eax          # imm = 0x7FFF8
movq 0x18(%rsp), %rdx
cmpq %rax, %r14
jae 0x4f0e3
movzbl (%r15,%r14), %ecx
incl 0x40(%rsp,%rcx,4)
movzbl 0x1(%r15,%r14), %ecx
incl 0xca0(%rsp,%rcx,4)
movzbl 0x2(%r15,%r14), %ecx
incl 0x880(%rsp,%rcx,4)
movzbl 0x3(%r15,%r14), %ecx
incl 0x460(%rsp,%rcx,4)
movzbl 0x4(%r15,%r14), %ecx
incl 0x40(%rsp,%rcx,4)
movzbl 0x5(%r15,%r14), %ecx
incl 0xca0(%rsp,%rcx,4)
movzbl 0x6(%r15,%r14), %ecx
incl 0x880(%rsp,%rcx,4)
movzbl 0x7(%r15,%r14), %ecx
incl 0x460(%rsp,%rcx,4)
addq $0x8, %r14
jmp 0x4f06b
movzbl (%r15,%r14), %eax
incq %r14
incl 0x40(%rsp,%rax,4)
cmpq %rdx, %r14
jb 0x4f0d7
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x4f118
movl 0x880(%rsp,%rax,4), %ecx
addl 0xca0(%rsp,%rax,4), %ecx
addl 0x460(%rsp,%rax,4), %ecx
addl %ecx, 0x40(%rsp,%rax,4)
incq %rax
jmp 0x4f0ea
movq %rax, %rdi
callq 0x64291
movl %ebx, %edi
callq 0x4f3aa
movl $0x1000, %r14d          # imm = 0x1000
cmpl %r14d, %eax
cmovbl %eax, %r14d
leaq 0x40(%rsp), %rdi
movl %ebx, %esi
movl %r14d, %edx
callq 0x4f3d2
testl %eax, %eax
js 0x4f1da
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x513f2
movslq %eax, %r13
addq %r12, %r13
xorl %ebx, %ebx
cmpq $0x100, %rbx            # imm = 0x100
je 0x4f17d
movl 0x40(%rsp,%rbx,4), %edx
testl %edx, %edx
je 0x4f178
movq %r13, %rdi
xorl %esi, %esi
callq 0x50765
cltq
addq %rax, %r13
incq %rbx
jmp 0x4f158
leaq 0x40(%rsp), %rdi
movl %r14d, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x4f3d2
xorl %ebx, %ebx
testl %eax, %eax
js 0x4f1da
subq %r12, %r13
movq %r13, 0x18(%rsp)
leaq 0xca0(%rsp), %r14
pushq $0xc
popq %r12
xorl %ebp, %ebp
cmpq $0x100, %rbp            # imm = 0x100
je 0x4f1e2
movl 0x40(%rsp,%rbp,4), %r13d
testl %r13d, %r13d
je 0x4f1d1
movq %r14, %rdi
movl %ebx, %esi
movl %r13d, %edx
movl %r12d, %ecx
callq 0x4f4f5
addl %r13d, %ebx
incq %rbp
addq $0x10, %r14
jmp 0x4f1ab
xorl %r12d, %r12d
jmp 0x4f395
movl $0x8000, %eax           # imm = 0x8000
movl %eax, 0x880(%rsp)
movl %eax, 0x4(%rsp)
movl %eax, 0x460(%rsp)
movl %eax, 0x14(%rsp)
movq 0x30(%rsp), %r14
movl %r14d, %ebx
andl $0x3, %ebx
leaq 0x2e949(%rip), %rax     # 0x7db58
movslq (%rax,%rbx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
andl $-0x4, %r14d
jmp 0x4f299
leal -0x1(%r14), %eax
movzbl (%r15,%rax), %eax
shll $0x4, %eax
leaq (%rsp,%rax), %rdx
addq $0xca0, %rdx            # imm = 0xCA0
leaq 0x460(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4f5c0
movl %r14d, %eax
subl %ebx, %eax
incl %eax
movzbl (%r15,%rax), %eax
shll $0x4, %eax
leaq (%rsp,%rax), %rdx
addq $0xca0, %rdx            # imm = 0xCA0
leaq 0x4(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4f5c0
andl $-0x4, %r14d
movzbl (%r15,%r14), %eax
shll $0x4, %eax
leaq (%rsp,%rax), %rdx
addq $0xca0, %rdx            # imm = 0xCA0
leaq 0x880(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4f5c0
movslq %r14d, %rbp
leaq 0x8(%rsp), %r13
testq %rbp, %rbp
jle 0x4f346
movzbl -0x1(%r15,%rbp), %eax
shll $0x4, %eax
leaq (%rsp,%rax), %rdx
addq $0xca0, %rdx            # imm = 0xCA0
movzbl -0x2(%r15,%rbp), %eax
shll $0x4, %eax
leaq (%rsp,%rax), %rbx
addq $0xca0, %rbx            # imm = 0xCA0
movzbl -0x3(%r15,%rbp), %eax
shll $0x4, %eax
leaq (%rsp,%rax), %r12
addq $0xca0, %r12            # imm = 0xCA0
movzbl -0x4(%r15,%rbp), %eax
addq $-0x4, %rbp
shll $0x4, %eax
leaq (%rsp,%rax), %r14
addq $0xca0, %r14            # imm = 0xCA0
leaq 0x14(%rsp), %rdi
movq %r13, %rsi
callq 0x4f5c0
leaq 0x460(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x4f5c0
leaq 0x4(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x4f5c0
leaq 0x880(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x4f5c0
jmp 0x4f2a1
movl 0x14(%rsp), %eax
movq 0x8(%rsp), %rsi
movl %eax, -0x4(%rsi)
movl 0x460(%rsp), %eax
movl %eax, -0x8(%rsi)
movl 0x4(%rsp), %eax
movl %eax, -0xc(%rsi)
movl 0x880(%rsp), %eax
movl %eax, -0x10(%rsi)
addq $-0x10, %rsi
movslq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %rcx
movq 0x38(%rsp), %rdx
subq %rsi, %rdx
leal (%rdi,%rdx), %eax
movl %eax, (%rcx)
addq %r12, %rdi
callq 0x4830
movq %r12, %rax
addq $0x1ca8, %rsp           # imm = 0x1CA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq