// File: /workspace/github_c_makefile_star2_O1/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
cmpl $0x10, %esi
setb %al
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
setae %cl
orb %al, %cl
jne 0x65657
movq %rdx, %rbx
movl %esi, %r12d
movq %rdi, %r15
testq %rdx, %rdx
je 0x653c1
xorl %eax, %eax
jmp 0x653cb
movl %ebp, %edi
callq 0x4680
movq %rax, %rbx
xorl %r14d, %r14d
testq %rbx, %rbx
je 0x65657
movq %rax, 0x20(%rsp)
movl %ebp, 0x1c(%rsp)
movl %r12d, %ebp
leaq (%r15,%rbp), %r13
addq $-0x8, %r13
leaq 0x40(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x4260
cmpq %r15, %r13
je 0x6556d
leaq 0x1(%r15), %rax
movzbl (%r15), %ecx
xorl %edx, %edx
cmpl $0xc, %r12d
jb 0x65461
xorl %r14d, %r14d
xorl %edx, %edx
movl %ecx, %esi
movl $0x1, 0x40(%rsp,%rsi,4)
testl %edx, %edx
je 0x6543a
cmpl $0xfe, %ecx
jg 0x654bb
decl %edx
incl %ecx
jmp 0x65454
movl %ecx, %edx
incl %edx
movzbl (%rax), %ecx
cmpl %ecx, %edx
jne 0x6544f
movzbl 0x1(%rax), %edx
addq $0x2, %rax
jmp 0x65454
incq %rax
xorl %edx, %edx
testl %ecx, %ecx
je 0x65461
leaq 0x2(%rax), %rsi
cmpq %r13, %rsi
jb 0x6541a
testl %ecx, %ecx
je 0x654b5
xorl %r14d, %r14d
movl %ecx, %esi
movl $0x1, 0x40(%rsp,%rsi,4)
cmpq %r13, %rax
jae 0x654bb
testl %edx, %edx
je 0x65489
cmpl $0xfe, %ecx
jg 0x654bb
decl %edx
incl %ecx
jmp 0x654ac
movl %ecx, %edi
incl %edi
movzbl (%rax), %ecx
leaq 0x1(%rax), %rsi
xorl %edx, %edx
cmpl %ecx, %edi
jne 0x654a9
cmpq %r13, %rsi
jae 0x654bb
movzbl 0x1(%rax), %edx
addq $0x2, %rax
jmp 0x654ac
movq %rsi, %rax
testl %ecx, %ecx
je 0x654b5
cmpq %r13, %rax
jb 0x65468
subl %r15d, %eax
movslq %eax, %r14
addq %r15, %r14
xorl %ecx, %ecx
xorl %eax, %eax
cmpl $0x0, 0x40(%rsp,%rcx,4)
je 0x65534
movq %r13, %rdx
subq %r14, %rdx
cmpq $0x6, %rdx
jl 0x6550b
xorl %edi, %edi
xorl %edx, %edx
movzbl (%r14,%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 0x654fc
cmpl $0x5, %edi
movq %rsi, %rdi
jne 0x654d9
addq %r14, %rsi
movl %edx, 0x40(%rsp,%rcx,4)
subl %r14d, %esi
movslq %esi, %rdx
jmp 0x6552d
cmpq %r13, %r14
jae 0x65523
movsbl (%r14), %edx
testl %edx, %edx
js 0x65542
movl %edx, 0x40(%rsp,%rcx,4)
movl $0x1, %edx
jmp 0x6552d
movl $0x0, 0x40(%rsp,%rcx,4)
xorl %edx, %edx
addq %rdx, %r14
addl 0x40(%rsp,%rcx,4), %eax
incq %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0x654c2
jmp 0x65568
xorl %edx, %edx
movq %r14, %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 0x654ff
cmpq %r13, %rsi
jb 0x65547
jmp 0x654ff
subl %r15d, %r14d
jmp 0x6556d
testl %r14d, %r14d
je 0x6564d
movslq %r14d, %rdx
testl $0xffffefff, %eax       # imm = 0xFFFFEFFF
je 0x655aa
xorl %ecx, %ecx
cmpl $0xfff, %eax            # imm = 0xFFF
ja 0x65599
xorl %ecx, %ecx
leal (%rax,%rax), %esi
incl %ecx
cmpl $0x800, %eax            # imm = 0x800
movl %esi, %eax
jb 0x6558b
xorl %eax, %eax
shll %cl, 0x40(%rsp,%rax,4)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x6559b
addq %r15, %rbp
addq %r15, %rdx
xorl %eax, %eax
xorl %ecx, %ecx
movl 0x40(%rsp,%rax,4), %esi
testq %rsi, %rsi
je 0x655fb
movl $0x1000, %edi           # imm = 0x1000
subl %ecx, %edi
cmpl %edi, %esi
ja 0x6564d
movl %ecx, %edi
xorl %r8d, %r8d
leal (%rdi,%r8), %r9d
movb %al, 0x440(%rsp,%r9)
movw %si, 0x34c0(%rsp,%r9,2)
movw %r8w, 0x1480(%rsp,%r9,2)
incq %r8
cmpq %r8, %rsi
jne 0x655d1
addl %ecx, %esi
movl %esi, %ecx
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x655b4
cmpl $0x1000, %ecx           # imm = 0x1000
jne 0x6564d
leaq 0x10(%rdx), %rcx
cmpq %rbp, %rcx
ja 0x6564d
movl (%rdx), %eax
movl %eax, (%rsp)
cmpl $0x8000, %eax           # imm = 0x8000
jb 0x6564d
movl 0x4(%rdx), %eax
movl %eax, 0x4(%rsp)
cmpl $0x8000, %eax           # imm = 0x8000
jb 0x6564d
movl 0x8(%rdx), %eax
movl %eax, 0x8(%rsp)
cmpl $0x8000, %eax           # imm = 0x8000
jb 0x6564d
movl 0xc(%rdx), %eax
movl %eax, 0xc(%rsp)
cmpl $0x8000, %eax           # imm = 0x8000
jae 0x6566e
movq 0x20(%rsp), %rdi
callq 0x45c0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x5508, %rsp           # imm = 0x5508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, 0x28(%rsp)
leaq -0x10(%rbp), %rax
movq %rax, 0x38(%rsp)
cmpq %rax, %rcx
setae %dl
movl 0x1c(%rsp), %eax
andl $0x7ffffff8, %eax       # imm = 0x7FFFFFF8
movl (%rsp), %esi
movl 0x4(%rsp), %ebp
movl 0x8(%rsp), %r8d
movl 0xc(%rsp), %edi
sete %r10b
xorl %r9d, %r9d
orb %dl, %r10b
jne 0x65842
movl %eax, %eax
movq %rax, 0x30(%rsp)
leaq 0x3(%rbx), %r15
xorl %r9d, %r9d
movl $0xfff, %r12d           # imm = 0xFFF
movq %r9, 0x20(%rsp)
xorl %r13d, %r13d
movl %esi, %eax
andl %r12d, %eax
movl %ebp, %edx
andl %r12d, %edx
movb 0x440(%rsp,%rax), %r10b
movb %r10b, -0x3(%r15,%r13)
movb 0x440(%rsp,%rdx), %r10b
movb %r10b, -0x2(%r15,%r13)
addl %eax, %eax
movzwl 0x34c0(%rsp,%rax), %r10d
shrl $0xc, %esi
imull %r10d, %esi
movzwl 0x1480(%rsp,%rax), %r11d
addl %esi, %r11d
addl %edx, %edx
movzwl 0x34c0(%rsp,%rdx), %eax
shrl $0xc, %ebp
imull %eax, %ebp
movzwl 0x1480(%rsp,%rdx), %r9d
addl %ebp, %r9d
movl %r8d, %r10d
andl %r12d, %r10d
movl %edi, %r14d
andl %r12d, %r14d
movzwl (%rcx), %eax
movl %r11d, %edx
shll $0x10, %edx
orl %eax, %edx
xorl %eax, %eax
cmpl $0x8000, %r11d          # imm = 0x8000
cmovbl %edx, %r11d
leaq 0x2(%rcx), %rax
cmovbq %rax, %rcx
movl %r11d, %esi
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
movl %r9d, %ebp
movb 0x440(%rsp,%r10), %r9b
addl %r10d, %r10d
movzwl 0x34c0(%rsp,%r10), %eax
shrl $0xc, %r8d
imull %eax, %r8d
movzwl 0x1480(%rsp,%r10), %r10d
addl %r8d, %r10d
movl %r14d, %eax
addl %eax, %eax
movzwl 0x34c0(%rsp,%rax), %edx
shrl $0xc, %edi
imull %edx, %edi
movzwl 0x1480(%rsp,%rax), %r11d
addl %edi, %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, %r8d
movzwl (%rcx), %eax
movl %r11d, %edx
shll $0x10, %edx
orl %eax, %edx
xorl %eax, %eax
cmpl $0x8000, %r11d          # imm = 0x8000
cmovbl %edx, %r11d
leaq 0x2(%rcx), %rax
cmovbq %rax, %rcx
movl %r11d, %edi
movb %r9b, -0x1(%r15,%r13)
movb 0x440(%rsp,%r14), %al
movb %al, (%r15,%r13)
addq $0x4, %r13
cmpq $0x4, %r13
je 0x656c7
movq 0x20(%rsp), %r9
addq $0x8, %r9
cmpq 0x38(%rsp), %rcx
jae 0x65842
addq $0x8, %r15
cmpq 0x30(%rsp), %r9
jb 0x656bf
movl %esi, (%rsp)
movl %ebp, 0x4(%rsp)
movl %r8d, 0x8(%rsp)
movl %edi, 0xc(%rsp)
movl 0x1c(%rsp), %edx
cmpl %edx, %r9d
jae 0x65659
movl %r9d, %eax
movl %edx, %edx
movq 0x28(%rsp), %r14
movl %eax, %esi
andl $0x3, %esi
movl %esi, %r8d
movl (%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, (%rsp,%r8,4)
movb 0x440(%rsp,%r10), %r8b
movb %r8b, (%rbx,%rax)
cmpl $0x7fff, %edi           # imm = 0x7FFF
ja 0x658dd
leaq 0x1(%rcx), %r8
cmpq %r14, %r8
jae 0x658dd
movzbl (%rcx), %r8d
movzbl 0x1(%rcx), %r9d
shll $0x8, %r9d
shll $0x10, %edi
orl %r8d, %edi
orl %r9d, %edi
addq $0x2, %rcx
movl %edi, (%rsp,%rsi,4)
incq %rax
cmpq %rax, %rdx
jne 0x65869
jmp 0x65659