// File: /workspace/github_c_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc58, %rsp            # imm = 0xC58
testl %esi, %esi
je 0x5aea8
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r12
movl %esi, %edi
leaq (%r12,%rdi), %r15
movzbl (%r12), %r13d
testb $0x8, %r13b
jne 0x5a893
leaq 0x1(%r12), %r8
testb $0x10, %r13b
jne 0x5a8d8
xorl %r14d, %r14d
cmpl $0x7, %ebp
jb 0x5aa90
movl $0x1, %eax
movzbl (%r12,%rax), %edx
movl %r14d, %esi
shll $0x7, %esi
movl %edx, %r14d
andl $0x7f, %r14d
orl %esi, %r14d
leaq 0x1(%rax), %rsi
testb %dl, %dl
jns 0x5a88b
cmpl $0x6, %eax
movq %rsi, %rax
jne 0x5a866
addq %rsi, %r12
subl %r8d, %r12d
jmp 0x5a8de
leaq 0x1(%r12), %rax
cmpl $0x7, %ebp
jb 0x5a90d
movq %rdi, %r8
xorl %r13d, %r13d
movl $0x1, %edi
movzbl (%r12,%rdi), %edx
movl %r13d, %esi
shll $0x7, %esi
movl %edx, %r13d
andl $0x7f, %r13d
orl %esi, %r13d
leaq 0x1(%rdi), %r14
testb %dl, %dl
jns 0x5a8cd
cmpl $0x6, %edi
movq %r14, %rdi
jne 0x5a8a8
addq %r12, %r14
movq %r8, %rdi
subl %eax, %r14d
jmp 0x5a92e
movl (%rcx), %r14d
xorl %r12d, %r12d
cmpl $0x7ffffffe, %r14d      # imm = 0x7FFFFFFE
ja 0x5aea8
movl %r13d, %eax
andl $0x10, %eax
shrl $0x4, %eax
testq %rbx, %rbx
sete %dl
testb %al, %dl
jne 0x5aea8
movslq %r12d, %r12
addq %r12, %r8
jmp 0x5aab4
xorl %r13d, %r13d
movl $0x0, %r14d
cmpl $0x1, %ebp
je 0x5a92e
movsbl (%rax), %r13d
testl %r13d, %r13d
js 0x5abc0
movl $0x1, %r14d
leal 0x1(%r14), %eax
cmpl %ebp, %eax
jae 0x5aea8
movl %eax, %eax
movzbl (%r12,%rax), %esi
testl %esi, %esi
je 0x5aea8
testq %rbx, %rbx
je 0x5ab77
movl (%rcx), %edx
movq $0x0, 0x10(%rsp)
cmpl %edx, %r13d
jne 0x5ad43
movq %rcx, 0x30(%rsp)
movq %rbx, 0x38(%rsp)
movl %r13d, %eax
xorl %edx, %edx
divl %esi
addl $0x2, %r14d
xorl %ecx, %ecx
movq %rdi, 0x8(%rsp)
movl %esi, %r8d
leaq -0x5(%rdi), %rsi
movl %r8d, 0x1c(%rsp)
movl %r8d, %edi
movq %rdi, 0x20(%rsp)
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 0x5a9bf
movl 0x84c(%rsp,%rcx,4), %edi
addl 0x44c(%rsp,%rcx,4), %edi
movl %edi, 0x450(%rsp,%rcx,4)
movl %r14d, %edi
leaq (%r12,%rdi), %r8
cmpq %rdi, %rsi
jle 0x5aa0a
xorl %r10d, %r10d
xorl %edi, %edi
movzbl (%r8,%r10), %r11d
movl %edi, %r9d
shll $0x7, %r9d
movl %r11d, %edi
andl $0x7f, %edi
orl %r9d, %edi
leaq 0x1(%r10), %r9
testb %r11b, %r11b
jns 0x5a9fe
cmpl $0x5, %r10d
movq %r9, %r10
jne 0x5a9d7
addq %r8, %r9
movl %edi, 0x50(%rsp,%rcx,4)
subl %r8d, %r9d
jmp 0x5aa30
cmpl %ebp, %r14d
jae 0x5aa23
movsbl (%r8), %edi
testl %edi, %edi
js 0x5aa61
movl %edi, 0x50(%rsp,%rcx,4)
movl $0x1, %r9d
jmp 0x5aa30
movl $0x0, 0x50(%rsp,%rcx,4)
xorl %edi, %edi
xorl %r9d, %r9d
addl %r9d, %r14d
cmpl %ebp, %r14d
ja 0x5ad43
leal -0x1(%rdi), %r8d
cmpl %ebp, %r8d
jae 0x5ad43
movl %edi, %edi
addq %rdi, %rbx
incq %rcx
cmpq 0x20(%rsp), %rcx
jne 0x5a998
jmp 0x5ac8a
xorl %edi, %edi
movq %r8, %r9
movzbl (%r9), %r10d
incq %r9
movl %edi, %r11d
shll $0x7, %r11d
movl %r10d, %edi
andl $0x7f, %edi
orl %r11d, %edi
testb %r10b, %r10b
jns 0x5aa01
cmpq %r15, %r9
jb 0x5aa66
jmp 0x5aa01
movl $0x0, %r12d
cmpl $0x1, %ebp
je 0x5aaae
movsbl (%r8), %r14d
testl %r14d, %r14d
js 0x5ad78
movl $0x1, %r12d
movl %r12d, %eax
addq %rax, %r8
notl %r12d
addl %ebp, %r12d
testq %rbx, %rbx
je 0x5aacf
cmpl %r14d, (%rcx)
jb 0x5aea8
movl %r14d, (%rcx)
xorl %ebp, %ebp
jmp 0x5aaf5
movl %r14d, (%rcx)
movl %r14d, %edi
movq %rcx, %rbx
movq %r8, %rbp
callq 0x4680
movq %rbp, %r8
movq %rbx, %rcx
movq %rax, %rbp
movq %rax, %rbx
testq %rax, %rax
je 0x5aea8
movl %r14d, 0x50(%rsp)
testb %r13b, %r13b
movq %rcx, 0x30(%rsp)
js 0x5abf0
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movl $0x0, 0x28(%rsp)
xorl %edi, %edi
movq %rbx, %r15
movq $0x0, 0x20(%rsp)
testl %r12d, %r12d
je 0x5ac7d
testb $0x20, %r13b
jne 0x5ad4d
testb $0x4, %r13b
jne 0x5adb4
movl %r13d, %eax
andl $0x3, %eax
testb $0x40, %r13b
movq %rdi, 0x8(%rsp)
movq %r8, %rdi
movl %r12d, %esi
movq %r15, %rdx
movl %r14d, %ecx
jne 0x5ae1f
cmpl $0x1, %eax
jne 0x5ae2b
callq 0x5bb5d
jmp 0x5ae37
cmpl $0x7ffffffe, %r13d      # imm = 0x7FFFFFFE
ja 0x5aea8
movl %esi, 0x1c(%rsp)
movq %rdi, 0x8(%rsp)
movq %rcx, %rbx
movl %r13d, %edi
callq 0x4680
testq %rax, %rax
je 0x5aea8
movq %rbx, %rcx
movl %r13d, (%rbx)
movl %r13d, %edx
movq %rax, 0x10(%rsp)
movq %rax, %rbx
movq 0x8(%rsp), %rdi
movl 0x1c(%rsp), %esi
jmp 0x5a95d
xorl %r13d, %r13d
movq %rax, %r14
movzbl (%r14), %esi
incq %r14
movl %r13d, %edx
shll $0x7, %edx
movl %esi, %r13d
andl $0x7f, %r13d
orl %edx, %r13d
testb %sil, %sil
jns 0x5a8d3
cmpq %r15, %r14
jb 0x5abc6
jmp 0x5a8d3
movq %rbp, 0x10(%rsp)
movq %r8, %rbp
movl %r14d, %r14d
movq %r14, %rdi
callq 0x4680
testq %rax, %rax
je 0x5ada8
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rcx
movaps %xmm0, (%rcx)
leaq 0x28(%rsp), %r8
movl $0x0, (%r8)
movq %rbp, %rdi
movl %r12d, %esi
movq %r14, %rdx
callq 0x6189d
testb %al, %al
je 0x5ac6e
movq %r14, 0x20(%rsp)
movq %rbx, 0x38(%rsp)
movzbl %al, %ebx
addq %rbx, %rbp
leaq 0x850(%rsp), %r14
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x5aebf
movl (%r14), %r14d
cmpl 0x50(%rsp), %r14d
jbe 0x5addd
movq 0x10(%rsp), %rbp
movq 0x8(%rsp), %rdi
jmp 0x5ae9b
movl $0x0, 0x50(%rsp)
jmp 0x5ae44
movl %r14d, %eax
addq %rax, %rbx
cmpq 0x8(%rsp), %rbx
ja 0x5ad43
movl %r13d, %edi
callq 0x4680
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x5ad43
xorl %ebp, %ebp
movl 0x850(%rsp,%rbp,4), %r15d
movl %r15d, 0x2c(%rsp)
movl %ebx, %esi
subl %r14d, %esi
jb 0x5ae0b
movl %r14d, %edi
addq %r12, %rdi
movl 0x450(%rsp,%rbp,4), %edx
addq 0x8(%rsp), %rdx
leaq 0x2c(%rsp), %rcx
callq 0x5a814
testq %rax, %rax
je 0x5ae0b
cmpl %r15d, 0x2c(%rsp)
jne 0x5ae0b
addl 0x50(%rsp,%rbp,4), %r14d
incq %rbp
cmpq %rbp, 0x20(%rsp)
jne 0x5acb3
leaq 0x450(%rsp), %r8
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %r14
movq %r14, %rsi
movl %r13d, %edx
movl 0x1c(%rsp), %ecx
callq 0x5af4a
movq %r14, %rdi
callq 0x45c0
movq 0x30(%rsp), %rax
movl %r13d, (%rax)
jmp 0x5aeaa
movq 0x10(%rsp), %rdi
jmp 0x5aea3
cmpl %r12d, %r14d
ja 0x5ae9b
cmpl (%rcx), %r14d
ja 0x5ae9b
movl %r14d, %edx
movq %rdi, %r14
movq %r15, %rdi
movq %r8, %rsi
callq 0x43c0
movq %r14, %rdi
jmp 0x5ae44
xorl %r14d, %r14d
movq %r8, %r12
movzbl (%r12), %eax
incq %r12
movl %r14d, %edx
shll $0x7, %edx
movl %eax, %r14d
andl $0x7f, %r14d
orl %edx, %r14d
testb %al, %al
jns 0x5a88e
cmpq %r15, %r12
jb 0x5ad7e
jmp 0x5a88e
xorl %edi, %edi
movq 0x10(%rsp), %rbp
jmp 0x5ae9b
leaq 0x50(%rsp), %rsi
movq %rdi, %r14
movq %r15, %rdi
movq %r8, %rdx
movl %r12d, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x4560
movq %r14, %rdi
testl %eax, %eax
jne 0x5ae9b
jmp 0x5ae44
addl %eax, %ebx
subl %ebx, %r12d
cltq
addq %rax, %rbp
movl %r14d, 0x50(%rsp)
movq 0x8(%rsp), %rdi
movq %rdi, %r15
movq 0x38(%rsp), %rbx
movq 0x30(%rsp), %rcx
movq %rbp, %r8
movq 0x10(%rsp), %rbp
jmp 0x5ab26
movq 0x10(%rsp), %rdi
callq 0x45c0
movq 0x8(%rsp), %rdi
jmp 0x5aea3
cmpl $0x1, %eax
jne 0x5ae32
callq 0x5b132
jmp 0x5ae37
callq 0x5bddc
jmp 0x5ae37
callq 0x5b67f
movq %rax, %r15
testq %rax, %rax
movq 0x8(%rsp), %rdi
je 0x5ae9b
testb %r13b, %r13b
js 0x5ae50
movl 0x50(%rsp), %r14d
jmp 0x5ae87
movq %rdi, %r14
movl 0x28(%rsp), %r8d
cmpl $0x1, %r8d
movl 0x50(%rsp), %esi
movq 0x20(%rsp), %rcx
cmoveq %rsi, %rcx
leaq 0x40(%rsp), %r9
movq %r15, %rdi
movq %rbx, %rdx
movq %rcx, %r15
callq 0x61913
movq %r14, %rdi
testq %rax, %rax
je 0x5ae9b
movq %r15, %r14
testq %rdi, %rdi
je 0x5ae91
callq 0x45c0
movq 0x30(%rsp), %rax
movl %r14d, (%rax)
jmp 0x5aeaa
callq 0x45c0
movq %rbp, %rdi
callq 0x45c0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xc58, %rsp            # imm = 0xC58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq