// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/htscodecs/htscodecs/arith_dynamic.c
// Function: arith_uncompress_to
unsigned char *arith_uncompress_to(unsigned char *in,  unsigned int in_size,
                                   unsigned char *out, unsigned int *out_size) {
    unsigned char *in_end = in + in_size;
    unsigned char *out_free = NULL;
    unsigned char *tmp_free = NULL;

    if (in_size == 0)
        return NULL;

    if (*in & X_STRIPE) {
        unsigned int ulen, olen, c_meta_len = 1;
        int i;
        uint64_t clen_tot = 0;

        // Decode lengths
        c_meta_len += var_get_u32(in+c_meta_len, in_end, &ulen);
        if (c_meta_len >= in_size)
            return NULL;
        unsigned int N = in[c_meta_len++];
        if (N < 1)  // Must be at least one stripe
            return NULL;
        unsigned int clenN[256], ulenN[256], idxN[256];
        if (!out) {
            if (ulen >= INT_MAX)
                return NULL;
            if (!(out_free = out = malloc(ulen))) {
                return NULL;
            }
            *out_size = ulen;
        }
        if (ulen != *out_size) {
            free(out_free);
            return NULL;
        }

        for (i = 0; i < N; i++) {
            ulenN[i] = ulen / N + ((ulen % N) > i);
            idxN[i] = i ? idxN[i-1] + ulenN[i-1] : 0;
            c_meta_len += var_get_u32(in+c_meta_len, in_end, &clenN[i]);
            clen_tot += clenN[i];
            if (c_meta_len > in_size || clenN[i] > in_size || clenN[i] < 1) {
                free(out_free);
                return NULL;
            }
        }

        // We can call this with a larger buffer, but once we've determined
        // how much we really use we limit it so the recursion becomes easier
        // to limit.
        if (c_meta_len + clen_tot > in_size) {
            free(out_free);
            return NULL;
        }
        in_size = c_meta_len + clen_tot;

        //fprintf(stderr, "    stripe meta %d\n", c_meta_len); //c-size

        // Uncompress the N streams
        unsigned char *outN = malloc(ulen);
        if (!outN) {
            free(out_free);
            return NULL;
        }
        for (i = 0; i < N; i++) {
            olen = ulenN[i];
            if (in_size < c_meta_len) {
                free(out_free);
                free(outN);
                return NULL;
            }
            if (!arith_uncompress_to(in+c_meta_len, in_size-c_meta_len, outN + idxN[i], &olen)
                || olen != ulenN[i]) {
                free(out_free);
                free(outN);
                return NULL;
            }
            c_meta_len += clenN[i];
        }

        unstripe(out, outN, ulen, N, idxN);

        free(outN);
        *out_size = ulen;
        return out;
    }

    int order = *in++;  in_size--;
    int do_pack = order & X_PACK;
    int do_rle  = order & X_RLE;
    int do_cat  = order & X_CAT;
    int no_size = order & X_NOSZ;
    int do_ext  = order & X_EXT;
    order &= 3;

    int sz = 0;
    unsigned int osz;
    if (!no_size)
        sz = var_get_u32(in, in_end, &osz);
    else
        sz = 0, osz = *out_size;
    in += sz;
    in_size -= sz;

    if (osz >= INT_MAX)
        return NULL;

#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
        // Limit maximum size to get fast turnaround on fuzzing test cases
        if (osz > 100000)
            goto err;
#endif

    if (no_size && !out)
        return NULL; // Need one or the other

    if (!out) {
        *out_size = osz;
        if (!(out_free = out = malloc(*out_size)))
            return NULL;
    } else {
        if (*out_size < osz)
            return NULL;
        *out_size = osz;
    }

    uint32_t c_meta_size = 0;
    unsigned int tmp1_size = *out_size;
    unsigned int tmp2_size = *out_size;
    unsigned char *tmp1 = NULL, *tmp2 = NULL, *tmp = NULL;

    // Need In, Out and Tmp buffers with temporary buffer of the same size
    // as output.  Our entropy decode is either arithmetic (with/without RLE)
    // or external (bz2, gzip, lzma) but with an optional unPACK transform
    // at the end.
    //
    // To avoid pointless memcpy when unpacking we switch around which
    // buffers we're writing to accordingly.

    // Format is pack meta data if present, followed by compressed data.
    if (do_pack) {
        if (!(tmp_free = tmp = malloc(*out_size)))
            goto err;
        tmp1 = tmp;  // uncompress
        tmp2 = out;  // unpack
    } else {
        // no pack
        tmp  = NULL;
        tmp1 = out;  // uncompress
        tmp2 = out;  // NOP
    }

    
    // Decode the bit-packing map.
    uint8_t map[16] = {0};
    int npacked_sym = 0;
    uint64_t unpacked_sz = 0; // FIXME: rename to packed_per_byte
    if (do_pack) {
        c_meta_size = hts_unpack_meta(in, in_size, *out_size, map, &npacked_sym);
        if (c_meta_size == 0)
            goto err;

        unpacked_sz = osz;
        in      += c_meta_size;
        in_size -= c_meta_size;

        // New unpacked size.  We could derive this bit from *out_size
        // and npacked_sym.
        unsigned int osz;
        sz = var_get_u32(in, in_end, &osz);
        in += sz;
        in_size -= sz;
        if (osz > tmp1_size)
            goto err;
        tmp1_size = osz;
    }

    //fprintf(stderr, "    meta_size %d bytes\n", (int)(in - orig_in)); //c-size

    // uncompress RLE data.  in -> tmp1
    if (in_size) {
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
        // Limit maximum size to get fast turnaround on fuzzing test cases
        if (tmp1_size > 100000)
            goto err;
#endif
        if (do_cat) {
            //fprintf(stderr, "    CAT %d\n", tmp1_size); //c-size
            if (tmp1_size > in_size)
                goto err;
            if (tmp1_size > *out_size)
                goto err;
            memcpy(tmp1, in, tmp1_size);
        } else if (do_ext) {
#ifdef HAVE_LIBBZ2
            if (BZ_OK != BZ2_bzBuffToBuffDecompress((char *)tmp1, &tmp1_size,
                                                    (char *)in, in_size, 0, 0))
                goto err;
#else
            fprintf(stderr, "Htscodecs has been compiled without libbz2 support\n");
            goto err;
#endif
          } else {
            // in -> tmp1
            if (do_rle) {
                tmp1 = order == 1
                    ? arith_uncompress_O1_RLE(in, in_size, tmp1, tmp1_size)
                    : arith_uncompress_O0_RLE(in, in_size, tmp1, tmp1_size);
            } else {
                //if (order == 2)
                //    tmp1 = arith_uncompress_O2(in, in_size, tmp1, tmp1_size)
                //else
                tmp1 = order == 1
                    ? arith_uncompress_O1(in, in_size, tmp1, tmp1_size)
                    : arith_uncompress_O0(in, in_size, tmp1, tmp1_size);
            }
            if (!tmp1)
                goto err;
        }
    } else {
        tmp1_size = 0;
    }

    if (do_pack) {
        // Unpack bits via pack-map.  tmp1 -> tmp2
        if (npacked_sym == 1)
            unpacked_sz = tmp1_size;
        //uint8_t *porig = unpack(tmp2, tmp2_size, unpacked_sz, npacked_sym, map);
        //memcpy(tmp3, porig, unpacked_sz);
        if (!hts_unpack(tmp1, tmp1_size, tmp2, unpacked_sz, npacked_sym, map))
            goto err;
        tmp2_size = unpacked_sz;
    } else {
        tmp2_size = tmp1_size;
    }

    if (tmp)
        free(tmp);

    *out_size = tmp2_size;
    return tmp2;

 err:
    free(tmp_free);
    free(out_free);
    return NULL;
}

// Assembly:
testl %esi, %esi
je 0x5d26f
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc58, %rsp            # imm = 0xC58
movq %rcx, %rbx
movq %rdx, %r8
movl %esi, %ebp
movq %rdi, %r15
movl %esi, %edi
leaq (%r15,%rdi), %r12
movzbl (%r15), %r14d
testb $0x8, %r14b
jne 0x5d272
leaq 0x1(%r15), %rdi
testb $0x10, %r14b
jne 0x5d2b4
movq %rbx, %r13
xorl %ebx, %ebx
cmpl $0x7, %ebp
jb 0x5d48d
movl $0x1, %eax
movzbl (%r15,%rax), %edx
movl %ebx, %ecx
shll $0x7, %ecx
movl %edx, %ebx
andl $0x7f, %ebx
orl %ecx, %ebx
leaq 0x1(%rax), %rcx
testb %dl, %dl
jns 0x5d267
cmpl $0x6, %eax
movq %rcx, %rax
jne 0x5d246
addq %rcx, %r15
subl %edi, %r15d
jmp 0x5d2bc
xorl %eax, %eax
retq
leaq 0x1(%r15), %rax
xorl %r13d, %r13d
cmpl $0x7, %ebp
jb 0x5d309
movl $0x1, %ecx
movzbl (%r15,%rcx), %edx
movl %r13d, %esi
shll $0x7, %esi
movl %edx, %r13d
andl $0x7f, %r13d
orl %esi, %r13d
leaq 0x1(%rcx), %r14
testb %dl, %dl
jns 0x5d2ac
cmpl $0x6, %ecx
movq %r14, %rcx
jne 0x5d287
addq %r15, %r14
subl %eax, %r14d
jmp 0x5d327
movq %rbx, %r13
movl (%rbx), %ebx
xorl %r15d, %r15d
cmpl $0x7ffffffe, %ebx       # imm = 0x7FFFFFFE
ja 0x5d802
movl %r14d, %eax
andl $0x10, %eax
shrl $0x4, %eax
testq %r8, %r8
sete %cl
testb %al, %cl
jne 0x5d802
movq %rdi, 0x18(%rsp)
testq %r8, %r8
je 0x5d4f2
cmpl %ebx, (%r13)
jb 0x5d802
movl %ebx, (%r13)
movq $0x0, 0x8(%rsp)
jmp 0x5d50e
movl $0x0, %r14d
cmpl $0x1, %ebp
je 0x5d327
movsbl (%rax), %r13d
testl %r13d, %r13d
js 0x5d538
movl $0x1, %r14d
leal 0x1(%r14), %eax
cmpl %ebp, %eax
jae 0x5d802
movl %eax, %eax
movzbl (%r15,%rax), %esi
testl %esi, %esi
je 0x5d802
testq %r8, %r8
je 0x5d4b2
movq $0x0, 0x8(%rsp)
cmpl (%rbx), %r13d
jne 0x5d623
movq %rbx, 0x38(%rsp)
movq %r8, 0x28(%rsp)
movl %r13d, %eax
xorl %edx, %edx
divl %esi
addl $0x2, %r14d
xorl %ecx, %ecx
cmpl $0x1, %esi
movl %esi, 0x20(%rsp)
adcl $0x0, %esi
movq %rsi, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
leaq -0x5(%rdi), %rsi
xorl %ebx, %ebx
cmpq %rdx, %rcx
movl %eax, %edi
adcl $0x0, %edi
movl %edi, 0x850(%rsp,%rcx,4)
movl $0x0, %edi
testq %rcx, %rcx
je 0x5d3b5
movl 0x84c(%rsp,%rcx,4), %edi
addl 0x44c(%rsp,%rcx,4), %edi
movl %edi, 0x450(%rsp,%rcx,4)
movl %r14d, %r8d
leaq (%r15,%r8), %rdi
cmpq %r8, %rsi
jle 0x5d403
xorl %r10d, %r10d
xorl %r9d, %r9d
movzbl (%rdi,%r10), %r11d
movl %r9d, %r8d
shll $0x7, %r8d
movl %r11d, %r9d
andl $0x7f, %r9d
orl %r8d, %r9d
leaq 0x1(%r10), %r8
testb %r11b, %r11b
jns 0x5d3f6
cmpl $0x5, %r10d
movq %r8, %r10
jne 0x5d3ce
addq %rdi, %r8
movl %r9d, 0x50(%rsp,%rcx,4)
subl %edi, %r8d
jmp 0x5d429
cmpl %ebp, %r14d
jae 0x5d41e
movsbl (%rdi), %r8d
testl %r8d, %r8d
js 0x5d45c
movl %r8d, 0x50(%rsp,%rcx,4)
movl $0x1, %r8d
jmp 0x5d429
movl $0x0, 0x50(%rsp,%rcx,4)
xorl %r8d, %r8d
addl %r8d, %r14d
cmpl %ebp, %r14d
ja 0x5d623
movl 0x50(%rsp,%rcx,4), %edi
leal -0x1(%rdi), %r8d
cmpl %ebp, %r8d
jae 0x5d623
addq %rdi, %rbx
incq %rcx
cmpq 0x18(%rsp), %rcx
jne 0x5d38e
jmp 0x5d567
xorl %r9d, %r9d
movq %rdi, %r8
movzbl (%r8), %r10d
incq %r8
movl %r9d, %r11d
shll $0x7, %r11d
movl %r10d, %r9d
andl $0x7f, %r9d
orl %r11d, %r9d
testb %r10b, %r10b
jns 0x5d3f9
cmpq %r12, %r8
jb 0x5d462
jmp 0x5d3f9
movl $0x0, %r15d
cmpl $0x1, %ebp
je 0x5d2bc
movsbl (%rdi), %ebx
testl %ebx, %ebx
js 0x5d6d0
movl $0x1, %r15d
jmp 0x5d2bc
cmpl $0x7ffffffe, %r13d      # imm = 0x7FFFFFFE
ja 0x5d802
movl %esi, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
movl %r13d, %edi
callq 0x4680
testq %rax, %rax
je 0x5d802
movl %r13d, (%rbx)
movq %rax, 0x8(%rsp)
movq %rax, %r8
movq 0x10(%rsp), %rdi
movl 0x20(%rsp), %esi
jmp 0x5d354
movl %ebx, (%r13)
movl %ebx, %edi
callq 0x4680
movq %rax, %r8
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x5d802
movq %r13, 0x38(%rsp)
movl (%r13), %edi
movl %edi, 0x50(%rsp)
testb %r14b, %r14b
movq %rdi, 0x10(%rsp)
js 0x5d62d
xorl %edi, %edi
movq %r8, %r9
movq 0x18(%rsp), %r13
jmp 0x5d650
xorl %r13d, %r13d
movq %rax, %r14
movzbl (%r14), %ecx
incq %r14
movl %r13d, %edx
shll $0x7, %edx
movl %ecx, %r13d
andl $0x7f, %r13d
orl %edx, %r13d
testb %cl, %cl
jns 0x5d2af
cmpq %r12, %r14
jb 0x5d53e
jmp 0x5d2af
movl %r14d, %eax
addq %rax, %rbx
cmpq 0x10(%rsp), %rbx
ja 0x5d623
movl %r13d, %edi
callq 0x4680
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x5d623
xorl %ebp, %ebp
movl 0x850(%rsp,%rbp,4), %r12d
movl %r12d, 0x34(%rsp)
movl %ebx, %esi
subl %r14d, %esi
jb 0x5d816
movl %r14d, %edi
addq %r15, %rdi
movl 0x450(%rsp,%rbp,4), %edx
addq 0x10(%rsp), %rdx
leaq 0x34(%rsp), %rcx
callq 0x5d1f5
testq %rax, %rax
je 0x5d816
cmpl %r12d, 0x34(%rsp)
jne 0x5d816
addl 0x50(%rsp,%rbp,4), %r14d
incq %rbp
cmpq %rbp, 0x18(%rsp)
jne 0x5d590
leaq 0x450(%rsp), %r8
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
movl %r13d, %edx
movl 0x20(%rsp), %ecx
callq 0x5d93d
movq %r14, %rdi
callq 0x45c0
movq %rbx, %rax
movq 0x38(%rsp), %rcx
movl %r13d, (%rcx)
jmp 0x5d804
movq 0x8(%rsp), %rdi
jmp 0x5d7fd
movq %r8, 0x28(%rsp)
callq 0x4680
movq 0x28(%rsp), %r8
movq %rax, %r9
movq %rax, %rdi
testq %rax, %rax
movq 0x18(%rsp), %r13
je 0x5d798
movslq %r15d, %r15
addq %r15, %r13
notl %r15d
addl %ebp, %r15d
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movl $0x0, 0x30(%rsp)
testb %r14b, %r14b
js 0x5d6fa
xorl %r12d, %r12d
testl %r15d, %r15d
movq 0x38(%rsp), %rax
je 0x5d75c
testb $0x20, %r14b
jne 0x5d769
testb $0x4, %r14b
jne 0x5d7c1
movl %r14d, %eax
andl $0x3, %eax
movl 0x50(%rsp), %ecx
movq %rdi, %rbp
movq %r8, %rbx
movq %r13, %rdi
movl %r15d, %esi
movq %r9, %rdx
testb $0x40, %r14b
jne 0x5d827
cmpl $0x1, %eax
jne 0x5d833
callq 0x5e56f
jmp 0x5d83f
xorl %ebx, %ebx
movq %rdi, %r15
movzbl (%r15), %eax
incq %r15
movl %ebx, %ecx
shll $0x7, %ecx
movl %eax, %ebx
andl $0x7f, %ebx
orl %ecx, %ebx
testb %al, %al
jns 0x5d26a
cmpq %r12, %r15
jb 0x5d6d5
jmp 0x5d26a
movq %r9, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movq %r8, 0x28(%rsp)
leaq 0x40(%rsp), %rcx
leaq 0x30(%rsp), %r8
movq %r13, %rdi
movl %r15d, %esi
movq 0x10(%rsp), %rdx
callq 0x64238
testb %al, %al
je 0x5d74d
movzbl %al, %ebp
addq %rbp, %r13
leaq 0x850(%rsp), %rdx
movq %r13, %rdi
movq %r12, %rsi
movq %rdx, %r12
callq 0x5d8b2
movl (%r12), %ecx
cmpl 0x50(%rsp), %ecx
jbe 0x5d79c
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %rdi
jmp 0x5d7f5
movl $0x0, 0x50(%rsp)
jmp 0x5d84d
movl 0x50(%rsp), %edx
cmpl %r15d, %edx
ja 0x5d7f0
cmpl (%rax), %edx
ja 0x5d7f0
movq %rdi, %r15
movq %r9, %rdi
movq %r13, %rsi
movq %r8, %rbx
movq %r9, %r13
callq 0x43c0
movq %r13, %r9
movq %r15, %rdi
movq %rbx, %r8
jmp 0x5d84d
xorl %edi, %edi
jmp 0x5d7f0
addl %eax, %ebp
subl %ebp, %r15d
cltq
addq %rax, %r13
movl %ebx, %r12d
movl %ecx, 0x50(%rsp)
movq 0x28(%rsp), %r8
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r9
jmp 0x5d678
leaq 0x50(%rsp), %rsi
movq %rdi, %rbp
movq %r9, %rdi
movq %r13, %rdx
movl %r15d, %ecx
movq %r8, %rbx
xorl %r8d, %r8d
movq %r9, %r15
xorl %r9d, %r9d
callq 0x4560
movq %r15, %r9
movq %rbp, %rdi
movq %rbx, %r8
testl %eax, %eax
je 0x5d84d
movq 0x8(%rsp), %rbx
callq 0x45c0
movq %rbx, %rdi
callq 0x45c0
xorl %eax, %eax
addq $0xc58, %rsp            # imm = 0xC58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rdi
callq 0x45c0
movq 0x10(%rsp), %rdi
jmp 0x5d7fd
cmpl $0x1, %eax
jne 0x5d83a
callq 0x5db09
jmp 0x5d83f
callq 0x5e7f5
jmp 0x5d83f
callq 0x5e065
movq %rax, %r9
testq %rax, %rax
movq %rbx, %r8
movq %rbp, %rdi
je 0x5d7f0
movq %r8, %rbx
testb %r14b, %r14b
js 0x5d85c
movl 0x50(%rsp), %r12d
jmp 0x5d88b
movq %rdi, %r14
movl 0x30(%rsp), %r8d
cmpl $0x1, %r8d
movl 0x50(%rsp), %esi
cmoveq %rsi, %r12
movq %r9, %rdi
leaq 0x40(%rsp), %r9
movq %rbx, %rdx
movq %r12, %rcx
callq 0x642ae
testq %rax, %rax
je 0x5d8a5
movq %r14, %rdi
testq %rdi, %rdi
je 0x5d895
callq 0x45c0
movq 0x38(%rsp), %rax
movl %r12d, (%rax)
movq %rbx, %rax
jmp 0x5d804
movq 0x8(%rsp), %rbx
movq %r14, %rdi
jmp 0x5d7f5