// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/htscodecs/htscodecs/rANS_static4x16pr.c
// Function: rans_uncompress_O0_4x16
unsigned char *rans_uncompress_O0_4x16(unsigned char *in, unsigned int in_size,
                                       unsigned char *out, unsigned int out_sz) {
    if (in_size < 16) // 4-states at least
        return NULL;

    if (out_sz >= INT_MAX)
        return NULL; // protect against some overflow cases

#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
    if (out_sz > 100000)
        return NULL;
#endif

    /* Load in the static tables */
    unsigned char *cp = in, *out_free = NULL;
    unsigned char *cp_end = in + in_size - 8; // within 8 => be extra safe
    int i, j;
    unsigned int x, y;
    uint16_t sfreq[TOTFREQ+32];
    uint16_t sbase[TOTFREQ+32]; // faster to use 32-bit on clang
    uint8_t  ssym [TOTFREQ+64]; // faster to use 16-bit on clang

    if (!out)
        out_free = out = malloc(out_sz);
    if (!out)
        return NULL;

    // Precompute reverse lookup of frequency.
    uint32_t F[256] = {0}, fsum;
    int fsz = decode_freq(cp, cp_end, F, &fsum);
    if (!fsz)
        goto err;
    cp += fsz;

    normalise_freq_shift(F, fsum, TOTFREQ);

    // Build symbols; fixme, do as part of decode, see the _d variant
    for (j = x = 0; j < 256; j++) {
        if (F[j]) {
            if (F[j] > TOTFREQ - x)
                goto err;
            for (y = 0; y < F[j]; y++) {
                ssym [y + x] = j;
                sfreq[y + x] = F[j];
                sbase[y + x] = y;
            }
            x += F[j];
        }
    }

    if (x != TOTFREQ)
        goto err;

    if (cp+16 > cp_end+8)
        goto err;

    RansState R[4];
    RansDecInit(&R[0], &cp); if (R[0] < RANS_BYTE_L) goto err;
    RansDecInit(&R[1], &cp); if (R[1] < RANS_BYTE_L) goto err;
    RansDecInit(&R[2], &cp); if (R[2] < RANS_BYTE_L) goto err;
    RansDecInit(&R[3], &cp); if (R[3] < RANS_BYTE_L) goto err;

// Simple version is comparable to below, but only with -O3
//
//    for (i = 0; cp < cp_end-8 && i < (out_sz&~7); i+=8) {
//        for(j=0; j<8;j++) {
//          RansState m = RansDecGet(&R[j%4], TF_SHIFT);
//          R[j%4] = sfreq[m] * (R[j%4] >> TF_SHIFT) + sbase[m];
//          out[i+j] = ssym[m];
//          RansDecRenorm(&R[j%4], &cp);
//        }
//    }

    for (i = 0; cp < cp_end-8 && i < (out_sz&~7); i+=8) {
        for (j = 0; j < 8; j+=4) {
            RansState m0 = RansDecGet(&R[0], TF_SHIFT);
            RansState m1 = RansDecGet(&R[1], TF_SHIFT);
            out[i+j+0] = ssym[m0];
            out[i+j+1] = ssym[m1];

            R[0] = sfreq[m0] * (R[0] >> TF_SHIFT) + sbase[m0];
            R[1] = sfreq[m1] * (R[1] >> TF_SHIFT) + sbase[m1];

            RansState m2 = RansDecGet(&R[2], TF_SHIFT);
            RansState m3 = RansDecGet(&R[3], TF_SHIFT);

            RansDecRenorm(&R[0], &cp);
            RansDecRenorm(&R[1], &cp);

            R[2] = sfreq[m2] * (R[2] >> TF_SHIFT) + sbase[m2];
            R[3] = sfreq[m3] * (R[3] >> TF_SHIFT) + sbase[m3];

            RansDecRenorm(&R[2], &cp);
            RansDecRenorm(&R[3], &cp);

            out[i+j+2] = ssym[m2];
            out[i+j+3] = ssym[m3];
        }
    }

    // remainder
    for (; i < out_sz; i++) {
        RansState m = RansDecGet(&R[i%4], TF_SHIFT);
        R[i%4] = sfreq[m] * (R[i%4] >> TF_SHIFT) + sbase[m];
        out[i] = ssym[m];
        RansDecRenormSafe(&R[i%4], &cp, cp_end+8);
    }

    //fprintf(stderr, "    0 Decoded %d bytes\n", (int)(cp-in)); //c-size

    return out;

 err:
    free(out_free);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5508, %rsp           # imm = 0x5508
movl %ecx, %r15d
cmpl $0x10, %esi
setb %al
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
setae %cl
orb %al, %cl
jne 0x4f799
movl %esi, %r14d
movq %rdi, %r12
xorl %ebp, %ebp
movl %r15d, %ebx
testq %rdx, %rdx
je 0x4f638
xorl %eax, %eax
jmp 0x4f64c
movq %rbx, %rdi
callq 0x4680
movq %rax, %rdx
testq %rax, %rax
je 0x4f799
movq %rax, 0x38(%rsp)
movq %r15, (%rsp)
movq %rbx, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movl %r14d, %eax
leaq (%r12,%rax), %rcx
movq %rcx, 0x8(%rsp)
leaq -0x8(%r12,%rax), %r14
leaq 0x40(%rsp), %r13
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
xorl %esi, %esi
callq 0x4260
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x51470
movslq %eax, %r13
addq %r12, %r13
xorl %ebx, %ebx
cmpq $0x400, %rbx            # imm = 0x400
je 0x4f6ce
cmpl $0x0, 0x40(%rsp,%rbx)
je 0x4f6c8
leaq (%rsp,%rbx), %r15
addq $0x40, %r15
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x51248
cltq
addq %rax, %r13
addl (%r15), %ebp
addq $0x4, %rbx
jmp 0x4f69a
subq %r12, %r13
testl %r13d, %r13d
je 0x4f78f
movslq %r13d, %rax
addq %rax, %r12
leaq 0x40(%rsp), %rdi
movl %ebp, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x4f9ec
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x100, %rax            # imm = 0x100
je 0x4f746
movl 0x40(%rsp,%rax,4), %edx
testq %rdx, %rdx
je 0x4f741
movl $0x1000, %esi           # imm = 0x1000
subl %ecx, %esi
cmpl %esi, %edx
ja 0x4f78f
movl %ecx, %esi
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x4f73d
leal (%rsi,%rdi), %r8d
movb %al, 0x440(%rsp,%r8)
movw %dx, 0x34c0(%rsp,%r8,2)
movw %di, 0x1480(%rsp,%r8,2)
incq %rdi
jmp 0x4f715
addl %ecx, %edx
movl %edx, %ecx
incq %rax
jmp 0x4f6f5
cmpl $0x1000, %ecx           # imm = 0x1000
jne 0x4f78f
leaq 0x10(%r12), %rcx
cmpq 0x8(%rsp), %rcx
ja 0x4f78f
movl (%r12), %edx
cmpl $0x8000, %edx           # imm = 0x8000
jb 0x4f78f
movl 0x4(%r12), %r13d
cmpl $0x8000, %r13d          # imm = 0x8000
jb 0x4f78f
movl 0x8(%r12), %r9d
cmpl $0x8000, %r9d           # imm = 0x8000
jb 0x4f78f
movl 0xc(%r12), %edi
cmpl $0x8000, %edi           # imm = 0x8000
jae 0x4f7ad
movq 0x38(%rsp), %rdi
callq 0x45c0
xorl %eax, %eax
addq $0x5508, %rsp           # imm = 0x5508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
leaq -0x10(%rax), %r8
movq (%rsp), %rax
andl $0x7ffffff8, %eax       # imm = 0x7FFFFFF8
movq %rax, (%rsp)
movq 0x28(%rsp), %rax
leaq 0x3(%rax), %r10
xorl %esi, %esi
movl $0xfff, %r11d           # imm = 0xFFF
cmpq %r8, %rcx
jae 0x4f95a
cmpq (%rsp), %rsi
jae 0x4f95a
xorl %r14d, %r14d
cmpq $0x7, %r14
ja 0x4f948
movl %edx, %ebx
andl %r11d, %ebx
movl %r13d, %r15d
andl %r11d, %r15d
movb 0x440(%rsp,%rbx), %bpl
movb %bpl, -0x3(%r10,%r14)
movb 0x440(%rsp,%r15), %bpl
movb %bpl, -0x2(%r10,%r14)
addl %ebx, %ebx
movzwl 0x34c0(%rsp,%rbx), %ebp
shrl $0xc, %edx
imull %ebp, %edx
movzwl 0x1480(%rsp,%rbx), %ebp
addl %edx, %ebp
addl %r15d, %r15d
movzwl 0x34c0(%rsp,%r15), %edx
shrl $0xc, %r13d
imull %edx, %r13d
movzwl 0x1480(%rsp,%r15), %r15d
addl %r13d, %r15d
movl %r9d, %r13d
andl %r11d, %r13d
movl %edi, %r12d
andl %r11d, %r12d
movzwl (%rcx), %eax
movl %ebp, %edx
shll $0x10, %edx
orl %eax, %edx
xorl %eax, %eax
cmpl $0x8000, %ebp           # imm = 0x8000
cmovbl %edx, %ebp
leaq 0x2(%rcx), %rax
cmovbq %rax, %rcx
movzwl (%rcx), %eax
movl %r15d, %edx
shll $0x10, %edx
orl %eax, %edx
xorl %eax, %eax
cmpl $0x8000, %r15d          # imm = 0x8000
cmovbl %edx, %r15d
leaq 0x2(%rcx), %rax
cmovbq %rax, %rcx
movb 0x440(%rsp,%r13), %bl
addl %r13d, %r13d
movzwl 0x34c0(%rsp,%r13), %eax
shrl $0xc, %r9d
imull %eax, %r9d
movzwl 0x1480(%rsp,%r13), %r13d
addl %r9d, %r13d
movl %r12d, %eax
addl %eax, %eax
movzwl 0x34c0(%rsp,%rax), %edx
shrl $0xc, %edi
imull %edx, %edi
movzwl 0x1480(%rsp,%rax), %r9d
addl %edi, %r9d
movzwl (%rcx), %eax
movl %r13d, %edx
shll $0x10, %edx
orl %eax, %edx
xorl %eax, %eax
cmpl $0x8000, %r13d          # imm = 0x8000
cmovbl %edx, %r13d
leaq 0x2(%rcx), %rax
cmovbq %rax, %rcx
movzwl (%rcx), %eax
movl %r9d, %edx
shll $0x10, %edx
orl %eax, %edx
xorl %eax, %eax
cmpl $0x8000, %r9d           # imm = 0x8000
cmovbl %edx, %r9d
leaq 0x2(%rcx), %rax
cmovbq %rax, %rcx
movb %bl, -0x1(%r10,%r14)
movb 0x440(%rsp,%r12), %al
movb %al, (%r10,%r14)
addq $0x4, %r14
movl %r9d, %edi
movl %r13d, %r9d
movl %r15d, %r13d
movl %ebp, %edx
jmp 0x4f7ea
addq $0x8, %rsi
addq $0x8, %r10
movq 0x28(%rsp), %rax
jmp 0x4f7d4
movl %edx, 0x10(%rsp)
movl %r13d, 0x14(%rsp)
movl %r9d, 0x18(%rsp)
movl %edi, 0x1c(%rsp)
movq 0x30(%rsp), %rbx
cmpq %rbx, %rsi
jae 0x4f79b
movl %esi, %r11d
andl $0x3, %r11d
movl %r11d, %edi
movl 0x10(%rsp,%rdi,4), %r8d
movl %r8d, %r9d
andl $0xfff, %r9d            # imm = 0xFFF
leal (%r9,%r9), %edx
movzwl 0x34c0(%rsp,%rdx), %r10d
shrl $0xc, %r8d
imull %r10d, %r8d
movzwl 0x1480(%rsp,%rdx), %edx
addl %r8d, %edx
movl %edx, 0x10(%rsp,%rdi,4)
movb 0x440(%rsp,%r9), %dil
movb %dil, (%rax,%rsi)
cmpl $0x7fff, %edx           # imm = 0x7FFF
ja 0x4f9e7
leaq 0x1(%rcx), %rdi
cmpq 0x8(%rsp), %rdi
jae 0x4f9e7
movzwl (%rcx), %edi
shll $0x10, %edx
orl %edi, %edx
addq $0x2, %rcx
movl %edx, 0x10(%rsp,%r11,4)
incq %rsi
jmp 0x4f971