// File: /workspace/github_c_makefile_star2_O3/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, %ebp
movq %rdx, 0x8(%rsp)
cmpl $0x10, %esi
setb %al
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
setae %cl
orb %al, %cl
jne 0x62cd5
movl %esi, %r12d
movq %rdi, %r14
cmpq $0x0, 0x8(%rsp)
je 0x62a3a
xorl %ecx, %ecx
jmp 0x62a52
movl %ebp, %edi
callq 0x4680
movq %rax, %rcx
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x62cd5
movq %rcx, 0x18(%rsp)
movl %ebp, 0x14(%rsp)
movl %r12d, %r12d
leaq (%r14,%r12), %r13
addq $-0x8, %r13
leaq 0x40(%rsp), %rdi
xorl %r15d, %r15d
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x4260
cmpq %r14, %r13
je 0x62ccb
addq %r14, %r12
leaq 0x1(%r14), %rax
movzbl (%r14), %ecx
xorl %edx, %edx
movl %ecx, %esi
movl $0x1, 0x40(%rsp,%rsi,4)
testl %edx, %edx
je 0x62ab0
cmpl $0xfe, %ecx
jg 0x62b31
decl %edx
incl %ecx
jmp 0x62aca
incl %ecx
movzbl (%rax), %esi
cmpl %esi, %ecx
jne 0x62ac3
movzbl 0x1(%rax), %edx
addq $0x2, %rax
jmp 0x62aca
incq %rax
xorl %edx, %edx
movl %esi, %ecx
testl %ecx, %ecx
je 0x62ad7
leaq 0x2(%rax), %rsi
cmpq %r13, %rsi
jb 0x62a90
testl %ecx, %ecx
je 0x62b2b
xorl %r15d, %r15d
movl %ecx, %esi
movl $0x1, 0x40(%rsp,%rsi,4)
cmpq %r13, %rax
jae 0x62b31
testl %edx, %edx
je 0x62aff
cmpl $0xfe, %ecx
jg 0x62b31
decl %edx
incl %ecx
jmp 0x62b22
incl %ecx
movzbl (%rax), %esi
leaq 0x1(%rax), %rdi
xorl %edx, %edx
cmpl %esi, %ecx
jne 0x62b1d
cmpq %r13, %rdi
jae 0x62b31
movzbl 0x1(%rax), %edx
addq $0x2, %rax
jmp 0x62b22
movq %rdi, %rax
movl %esi, %ecx
testl %ecx, %ecx
je 0x62b2b
cmpq %r13, %rax
jb 0x62ade
subl %r14d, %eax
movslq %eax, %r15
addq %r14, %r15
xorl %ecx, %ecx
xorl %eax, %eax
cmpl $0x0, 0x40(%rsp,%rcx,4)
je 0x62baa
movq %r13, %rdx
subq %r15, %rdx
cmpq $0x6, %rdx
jl 0x62b81
xorl %edi, %edi
xorl %edx, %edx
movzbl (%r15,%rdi), %r8d
movl %edx, %esi
shll $0x7, %esi
movl %r8d, %edx
andl $0x7f, %edx
orl %esi, %edx
leaq 0x1(%rdi), %rsi
testb %r8b, %r8b
jns 0x62b72
cmpl $0x5, %edi
movq %rsi, %rdi
jne 0x62b4f
addq %r15, %rsi
movl %edx, 0x40(%rsp,%rcx,4)
subl %r15d, %esi
movslq %esi, %rsi
jmp 0x62ba5
cmpq %r13, %r15
jae 0x62b99
movsbl (%r15), %edx
testl %edx, %edx
js 0x62bb8
movl %edx, 0x40(%rsp,%rcx,4)
movl $0x1, %esi
jmp 0x62ba5
movl $0x0, 0x40(%rsp,%rcx,4)
xorl %edx, %edx
xorl %esi, %esi
addq %rsi, %r15
addl %edx, %eax
incq %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0x62b38
jmp 0x62bde
xorl %edx, %edx
movq %r15, %rsi
movzbl (%rsi), %edi
incq %rsi
movl %edx, %r8d
shll $0x7, %r8d
movl %edi, %edx
andl $0x7f, %edx
orl %r8d, %edx
testb %dil, %dil
jns 0x62b75
cmpq %r13, %rsi
jb 0x62bbd
jmp 0x62b75
subq %r14, %r15
testl %r15d, %r15d
je 0x62ccb
movslq %r15d, %rcx
testl $0xffffefff, %eax       # imm = 0xFFFFEFFF
je 0x62c36
xorl %edx, %edx
cmpl $0xfff, %eax            # imm = 0xFFF
ja 0x62c0d
xorl %edx, %edx
leal (%rax,%rax), %esi
incl %edx
cmpl $0x800, %eax            # imm = 0x800
movl %esi, %eax
jb 0x62bff
movd %edx, %xmm1
xorl %eax, %eax
xorps %xmm0, %xmm0
movss %xmm1, %xmm0            # xmm0 = xmm1[0],xmm0[1,2,3]
movdqa 0x40(%rsp,%rax,4), %xmm1
pslld %xmm0, %xmm1
movdqa %xmm1, 0x40(%rsp,%rax,4)
addq $0x4, %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x62c1a
addq %rcx, %r14
xorl %eax, %eax
xorl %ecx, %ecx
movl 0x40(%rsp,%rax,4), %edx
testq %rdx, %rdx
je 0x62c7f
movl $0x1000, %esi           # imm = 0x1000
subl %ecx, %esi
cmpl %esi, %edx
ja 0x62ccb
movl %ecx, %esi
xorl %edi, %edi
leal (%rsi,%rdi), %r8d
movb %al, 0x440(%rsp,%r8)
movw %dx, 0x34c0(%rsp,%r8,2)
movw %di, 0x1480(%rsp,%r8,2)
incq %rdi
cmpq %rdi, %rdx
jne 0x62c55
addl %ecx, %edx
movl %edx, %ecx
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x62c3d
cmpl $0x1000, %ecx           # imm = 0x1000
jne 0x62ccb
leaq 0x10(%r14), %rcx
cmpq %r12, %rcx
ja 0x62ccb
movl (%r14), %ebp
cmpl $0x8000, %ebp           # imm = 0x8000
jb 0x62ccb
movl 0x4(%r14), %r9d
cmpl $0x8000, %r9d           # imm = 0x8000
jb 0x62ccb
movl 0x8(%r14), %edi
cmpl $0x8000, %edi           # imm = 0x8000
jb 0x62ccb
movl 0xc(%r14), %esi
cmpl $0x8000, %esi           # imm = 0x8000
jae 0x62cf5
movq 0x18(%rsp), %rdi
callq 0x45c0
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rax
addq $0x5508, %rsp           # imm = 0x5508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x10(%r12), %rax
movq %rax, 0x18(%rsp)
cmpq %rax, %rcx
setae %dl
movl 0x14(%rsp), %r8d
movl %r8d, %eax
andl $0x7ffffff8, %eax       # imm = 0x7FFFFFF8
sete %r11b
xorl %r15d, %r15d
orb %dl, %r11b
jne 0x62ed6
movq %r12, 0x30(%rsp)
movl %eax, %eax
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
movl $0xfff, %edx            # imm = 0xFFF
movb $0x1, %r13b
xorl %r14d, %r14d
movl %ebp, %eax
andl %edx, %eax
movl $0xfff, %ebx            # imm = 0xFFF
movl %r9d, %edx
andl %ebx, %edx
movb 0x440(%rsp,%rax), %r11b
orq %r15, %r14
movq 0x8(%rsp), %r8
movb %r11b, (%r8,%r14)
movb 0x440(%rsp,%rdx), %r11b
movb %r11b, 0x1(%r8,%r14)
addl %eax, %eax
movzwl 0x34c0(%rsp,%rax), %r11d
shrl $0xc, %ebp
imull %r11d, %ebp
movzwl 0x1480(%rsp,%rax), %r10d
addl %ebp, %r10d
addl %edx, %edx
movzwl 0x34c0(%rsp,%rdx), %eax
shrl $0xc, %r9d
imull %eax, %r9d
movzwl 0x1480(%rsp,%rdx), %r8d
addl %r9d, %r8d
movl %edi, %r12d
andl %ebx, %r12d
movl %esi, %r11d
andl %ebx, %r11d
movzwl (%rcx), %eax
movl %r10d, %edx
shll $0x10, %edx
orl %eax, %edx
xorl %eax, %eax
cmpl $0x8000, %r10d          # imm = 0x8000
cmovbl %edx, %r10d
leaq 0x2(%rcx), %rax
cmovbq %rax, %rcx
movl %r10d, %ebp
movzwl (%rcx), %eax
movl %r8d, %edx
shll $0x10, %edx
orl %eax, %edx
xorl %eax, %eax
cmpl $0x8000, %r8d           # imm = 0x8000
cmovbl %edx, %r8d
leaq 0x2(%rcx), %rax
cmovbq %rax, %rcx
movl %r8d, %r9d
movb 0x440(%rsp,%r12), %r8b
addl %r12d, %r12d
movzwl 0x34c0(%rsp,%r12), %eax
shrl $0xc, %edi
imull %eax, %edi
movzwl 0x1480(%rsp,%r12), %r10d
addl %edi, %r10d
movl %r11d, %eax
addl %eax, %eax
movzwl 0x34c0(%rsp,%rax), %edx
shrl $0xc, %esi
imull %edx, %esi
movzwl 0x1480(%rsp,%rax), %r12d
addl %esi, %r12d
movzwl (%rcx), %eax
movl %r10d, %edx
shll $0x10, %edx
orl %eax, %edx
xorl %eax, %eax
cmpl $0x8000, %r10d          # imm = 0x8000
cmovbl %edx, %r10d
leaq 0x2(%rcx), %rax
cmovbq %rax, %rcx
movl %r10d, %edi
movq 0x8(%rsp), %rbx
movzwl (%rcx), %eax
movl %r12d, %edx
shll $0x10, %edx
orl %eax, %edx
xorl %eax, %eax
cmpl $0x8000, %r12d          # imm = 0x8000
cmovbl %edx, %r12d
leaq 0x2(%rcx), %rax
cmovbq %rax, %rcx
movl $0xfff, %edx            # imm = 0xFFF
movl %r12d, %esi
movb %r8b, 0x2(%rbx,%r14)
movb 0x440(%rsp,%r11), %al
movb %al, 0x3(%rbx,%r14)
movl $0x4, %r14d
testb $0x1, %r13b
movl $0x0, %r13d
jne 0x62d3c
addq $0x8, %r15
cmpq 0x18(%rsp), %rcx
jae 0x62ecc
cmpq 0x38(%rsp), %r15
jb 0x62d36
movq 0x30(%rsp), %r12
movl 0x14(%rsp), %r8d
movl %ebp, 0x20(%rsp)
movl %r9d, 0x24(%rsp)
movl %edi, 0x28(%rsp)
movl %esi, 0x2c(%rsp)
cmpl %r8d, %r15d
jae 0x62cde
movl %r15d, %eax
movl %r8d, %edx
movl %eax, %esi
andl $0x3, %esi
movl %esi, %r8d
movl 0x20(%rsp,%r8,4), %r9d
movl %r9d, %r10d
andl $0xfff, %r10d           # imm = 0xFFF
leal (%r10,%r10), %edi
movzwl 0x34c0(%rsp,%rdi), %r11d
shrl $0xc, %r9d
imull %r11d, %r9d
movzwl 0x1480(%rsp,%rdi), %edi
addl %r9d, %edi
movl %edi, 0x20(%rsp,%r8,4)
movb 0x440(%rsp,%r10), %r8b
movq 0x8(%rsp), %r9
movb %r8b, (%r9,%rax)
cmpl $0x7fff, %edi           # imm = 0x7FFF
ja 0x62f66
leaq 0x1(%rcx), %r8
cmpq %r12, %r8
jae 0x62f66
movzwl (%rcx), %r8d
shll $0x10, %edi
orl %r8d, %edi
addq $0x2, %rcx
movl %edi, 0x20(%rsp,%rsi,4)
incq %rax
cmpq %rax, %rdx
jne 0x62ef6
jmp 0x62cde