// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/htslib/cram/rANS_static.c
// Function: rans_uncompress_O1
unsigned char *rans_uncompress_O1(unsigned char *in, unsigned int in_size,
				  unsigned int *out_size) {
    /* Load in the static tables */
    unsigned char *cp = in + 9;
    int i, j = -999, x, out_sz, in_sz, rle_i, rle_j;
    char *out_buf = NULL;
    ari_decoder *D = NULL;              /* D[256] */
    RansDecSymbol (*syms)[256] = NULL;  /* syms[256][256] */

    if (*in++ != 1) // Order-1 check
	return NULL;

    in_sz  = ((in[0])<<0) | ((in[1])<<8) | ((in[2])<<16) | ((in[3])<<24);
    out_sz = ((in[4])<<0) | ((in[5])<<8) | ((in[6])<<16) | ((in[7])<<24);
    if (in_sz != in_size-9)
	return NULL;

    D = calloc(256, sizeof(*D));
    if (!D) goto cleanup;
    syms = malloc(256 * sizeof(*syms));
    if (!syms) goto cleanup;

    //fprintf(stderr, "out_sz=%d\n", out_sz);

    //i = *cp++;
    rle_i = 0;
    i = *cp++;
    do {
	rle_j = x = 0;
	j = *cp++;
	do {
	    if ((D[i].fc[j].F = *cp++) >= 128) {
		D[i].fc[j].F &= ~128;
		D[i].fc[j].F = ((D[i].fc[j].F & 127) << 8) | *cp++;
	    }
	    D[i].fc[j].C = x;

	    //fprintf(stderr, "i=%d j=%d F=%d C=%d\n", i, j, D[i].fc[j].F, D[i].fc[j].C);

	    if (!D[i].fc[j].F)
		D[i].fc[j].F = TOTFREQ;

	    RansDecSymbolInit(&syms[i][j], D[i].fc[j].C, D[i].fc[j].F);

	    /* Build reverse lookup table */
	    if (!D[i].R) {
                D[i].R = (unsigned char *)malloc(TOTFREQ);
                if (!D[i].R)
                    goto cleanup;
            }
	    memset(&D[i].R[x], j, D[i].fc[j].F);

	    x += D[i].fc[j].F;
	    assert(x <= TOTFREQ);

	    if (!rle_j && j+1 == *cp) {
		j = *cp++;
		rle_j = *cp++;
	    } else if (rle_j) {
		rle_j--;
		j++;
	    } else {
		j = *cp++;
	    }
	} while(j);

	if (!rle_i && i+1 == *cp) {
	    i = *cp++;
	    rle_i = *cp++;
	} else if (rle_i) {
	    rle_i--;
	    i++;
	} else {
	    i = *cp++;
	}
    } while (i);

    // Precompute reverse lookup of frequency.

    RansState rans0, rans1, rans2, rans3;
    uint8_t *ptr = cp;
    RansDecInit(&rans0, &ptr);
    RansDecInit(&rans1, &ptr);
    RansDecInit(&rans2, &ptr);
    RansDecInit(&rans3, &ptr);

    int isz4 = out_sz>>2;
    int l0 = 0;
    int l1 = 0;
    int l2 = 0;
    int l3 = 0;
    int i4[] = {0*isz4, 1*isz4, 2*isz4, 3*isz4};

    RansState R[4];
    R[0] = rans0;
    R[1] = rans1;
    R[2] = rans2;
    R[3] = rans3;

    /* Allocate output buffer */
    out_buf = malloc(out_sz);
    if (!out_buf) goto cleanup;

    for (; i4[0] < isz4; i4[0]++, i4[1]++, i4[2]++, i4[3]++) {
	uint32_t m[4] = {R[0] & ((1u << TF_SHIFT)-1),
			 R[1] & ((1u << TF_SHIFT)-1),
			 R[2] & ((1u << TF_SHIFT)-1),
			 R[3] & ((1u << TF_SHIFT)-1)};

	uint8_t c[4] = {D[l0].R[m[0]],
			D[l1].R[m[1]],
			D[l2].R[m[2]],
			D[l3].R[m[3]]};

	out_buf[i4[0]] = c[0];
	out_buf[i4[1]] = c[1];
	out_buf[i4[2]] = c[2];
	out_buf[i4[3]] = c[3];

	//RansDecAdvanceSymbolStep(&R[0], &syms[l0][c[0]], TF_SHIFT);
	//RansDecAdvanceSymbolStep(&R[1], &syms[l1][c[1]], TF_SHIFT);
	//RansDecAdvanceSymbolStep(&R[2], &syms[l2][c[2]], TF_SHIFT);
	//RansDecAdvanceSymbolStep(&R[3], &syms[l3][c[3]], TF_SHIFT);

	R[0] = syms[l0][c[0]].freq * (R[0]>>TF_SHIFT);
	R[1] = syms[l1][c[1]].freq * (R[1]>>TF_SHIFT);
	R[2] = syms[l2][c[2]].freq * (R[2]>>TF_SHIFT);
	R[3] = syms[l3][c[3]].freq * (R[3]>>TF_SHIFT);

	R[0] += m[0] - syms[l0][c[0]].start;
	R[1] += m[1] - syms[l1][c[1]].start;
	R[2] += m[2] - syms[l2][c[2]].start;
	R[3] += m[3] - syms[l3][c[3]].start;

	RansDecRenorm(&R[0], &ptr);
	RansDecRenorm(&R[1], &ptr);
	RansDecRenorm(&R[2], &ptr);
	RansDecRenorm(&R[3], &ptr);

	l0 = c[0];
	l1 = c[1];
	l2 = c[2];
	l3 = c[3];
    }

    rans0 = R[0];
    rans1 = R[1];
    rans2 = R[2];
    rans3 = R[3];

    // Remainder
    for (; i4[3] < out_sz; i4[3]++) {
	unsigned char c3 = D[l3].R[RansDecGet(&rans3, TF_SHIFT)];
	out_buf[i4[3]] = c3;
	RansDecAdvanceSymbol(&rans3, &ptr, &syms[l3][c3], TF_SHIFT);
	l3 = c3;
    }

    *out_size = out_sz;

 cleanup:
    if (D) {
        for (i = 0; i < 256; i++)
            if (D[i].R) free(D[i].R);
        free(D);
    }
    free(syms);

    return (unsigned char *)out_buf;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
cmpb $0x1, (%rdi)
jne 0x31db2
movq %rdi, %r13
addl $-0x9, %esi
cmpl %esi, 0x1(%rdi)
jne 0x31db2
movq %rdx, %rbx
movl 0x5(%r13), %r14d
movl $0x100, %edi            # imm = 0x100
movl $0x808, %esi            # imm = 0x808
callq 0x4370
testq %rax, %rax
je 0x31db7
movq %rax, %rbp
movl %r14d, 0x44(%rsp)
movq %rbx, 0x80(%rsp)
movl $0x40000, %edi          # imm = 0x40000
callq 0x46f0
movq %rax, %r15
xorl %r14d, %r14d
testq %rax, %rax
je 0x31dcc
movzbl 0x9(%r13), %ecx
addq $0xa, %r13
movq %r15, 0x38(%rsp)
movq %rbp, 0x10(%rsp)
movq %r14, 0x48(%rsp)
movzbl (%r13), %r12d
incq %r13
movl %ecx, 0x8(%rsp)
movslq %ecx, %rax
imulq $0x808, %rax, %r14      # imm = 0x808
addq %rbp, %r14
shlq $0xa, %rax
addq %r15, %rax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
xorl %ebp, %ebp
movl %ebp, %ebx
movzbl (%r13), %ebp
movslq %r12d, %rax
movl %ebp, (%r14,%rax,8)
testb %bpl, %bpl
js 0x319fc
incq %r13
jmp 0x31a15
andl $0x7f, %ebp
movl %ebp, (%r14,%rax,8)
shll $0x8, %ebp
movzbl 0x1(%r13), %ecx
addq $0x2, %r13
orl %ecx, %ebp
movl %ebp, (%r14,%rax,8)
movl %ebx, 0x4(%r14,%rax,8)
testl %ebp, %ebp
jne 0x31a2b
movl $0x1000, (%r14,%rax,8)  # imm = 0x1000
movl $0x1000, %ebp           # imm = 0x1000
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax,4), %rdi
movl %ebx, %esi
movl %ebp, %edx
callq 0x310f3
movq 0x800(%r14), %rax
testq %rax, %rax
jne 0x31a63
movl $0x1000, %edi           # imm = 0x1000
callq 0x46f0
movq %rax, 0x800(%r14)
testq %rax, %rax
je 0x31dbf
movslq %ebx, %rdi
addq %rax, %rdi
movl %ebp, %edx
movl %r12d, %esi
callq 0x42e0
addl %ebx, %ebp
cmpl $0x1001, %ebp           # imm = 0x1001
jge 0x31eb7
testl %r15d, %r15d
je 0x31a8e
decl %r15d
incl %r12d
jmp 0x31aa4
incl %r12d
movzbl (%r13), %eax
cmpl %eax, %r12d
jne 0x31ab4
movzbl 0x1(%r13), %r15d
addq $0x2, %r13
movl %r12d, %eax
movl %eax, %r12d
testl %eax, %eax
jne 0x319e4
jmp 0x31abc
incq %r13
xorl %r15d, %r15d
jmp 0x31aa7
movq 0x48(%rsp), %r14
testl %r14d, %r14d
je 0x31adb
decl %r14d
movl 0x8(%rsp), %eax
incl %eax
movq 0x38(%rsp), %r15
movq 0x10(%rsp), %rbp
jmp 0x31b07
movl 0x8(%rsp), %ecx
incl %ecx
movzbl (%r13), %eax
cmpl %eax, %ecx
movq 0x38(%rsp), %r15
movq 0x10(%rsp), %rbp
jne 0x31b01
movzbl 0x1(%r13), %r14d
addq $0x2, %r13
movl %ecx, %eax
jmp 0x31b07
incq %r13
xorl %r14d, %r14d
movl %eax, %ecx
testl %eax, %eax
jne 0x319b5
movq %r13, 0x50(%rsp)
leaq 0x4(%r13), %rax
leaq 0x10(%r13), %rcx
movss (%r13), %xmm0
movq %rax, 0x50(%rsp)
movsd 0x8(%r13), %xmm1
movss 0x4(%r13), %xmm2
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movq %rcx, 0x50(%rsp)
movaps %xmm0, 0x20(%rsp)
movl 0x44(%rsp), %ebx
movslq %ebx, %rdi
movq %rdi, 0x60(%rsp)
callq 0x46f0
movq $0x0, 0x8(%rsp)
testq %rax, %rax
je 0x31eaf
movl %ebx, %esi
sarl $0x2, %esi
leal (%rsi,%rsi,2), %ecx
leal (%rsi,%rsi), %edx
movslq %ecx, %rcx
movq %rcx, 0x18(%rsp)
movslq %edx, %rcx
movq %rcx, 0x70(%rsp)
movslq %esi, %rdx
testl %edx, %edx
movl $0x0, %esi
movq %rdx, 0x68(%rsp)
cmovgl %edx, %esi
negq %rsi
movq %rsi, 0x78(%rsp)
movq %rax, 0x48(%rsp)
movq %rax, %r15
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
cmpq %rsi, 0x78(%rsp)
je 0x31e12
movl 0x20(%rsp), %r9d
movq %rsi, 0xb0(%rsp)
movl 0x24(%rsp), %esi
movq %r9, %rbx
movl $0xfff, %r10d           # imm = 0xFFF
andq %r10, %rbx
movq %rsi, %r11
andq %r10, %r11
movl 0x28(%rsp), %r8d
movq %r8, %rdi
andq %r10, %rdi
movq %rdi, %r10
movq %rdi, 0xa0(%rsp)
movl 0x2c(%rsp), %edi
movq %r15, 0xa8(%rsp)
imulq $0x808, %rdx, %r15      # imm = 0x808
movq 0x800(%rbp,%r15), %r15
movzbl (%r15,%rbx), %r12d
movq %r12, 0x98(%rsp)
imulq $0x808, %rcx, %r15      # imm = 0x808
movq 0x800(%rbp,%r15), %r15
movzbl (%r15,%r11), %r14d
movq %r14, 0x90(%rsp)
imulq $0x808, %rax, %r15      # imm = 0x808
movq 0x800(%rbp,%r15), %r15
movzbl (%r15,%r10), %r10d
movq %r10, 0x88(%rsp)
shlq $0xa, %rdx
movq 0x38(%rsp), %rbp
addq %rbp, %rdx
movzwl 0x2(%rdx,%r12,4), %r15d
shrl $0xc, %r9d
imull %r15d, %r9d
shlq $0xa, %rcx
addq %rbp, %rcx
movzwl 0x2(%rcx,%r14,4), %r15d
shrl $0xc, %esi
imull %r15d, %esi
shlq $0xa, %rax
addq %rbp, %rax
movzwl 0x2(%rax,%r10,4), %r15d
shrl $0xc, %r8d
imull %r15d, %r8d
movq %rdi, %r15
movl $0xfff, %r10d           # imm = 0xFFF
andq %r10, %r15
movl 0x8(%rsp), %r13d
imulq $0x808, %r13, %r14      # imm = 0x808
movq 0x10(%rsp), %r12
movq 0x800(%r12,%r14), %r14
movzbl (%r14,%r15), %r10d
shlq $0xa, %r13
addq %rbp, %r13
shrl $0xc, %edi
movzwl 0x2(%r13,%r10,4), %ebp
imull %ebp, %edi
movq 0x10(%rsp), %rbp
addl %ebx, %r9d
movq 0x98(%rsp), %r12
movzwl (%rdx,%r12,4), %edx
subl %edx, %r9d
movl %r9d, 0x20(%rsp)
addl %r11d, %esi
movq 0x90(%rsp), %rdx
movzwl (%rcx,%rdx,4), %ecx
subl %ecx, %esi
movl %esi, 0x24(%rsp)
addl 0xa0(%rsp), %r8d
movq 0x88(%rsp), %r14
movzwl (%rax,%r14,4), %eax
subl %eax, %r8d
movl %r8d, 0x28(%rsp)
addl %r15d, %edi
movq 0xa8(%rsp), %r15
movq %r10, 0x8(%rsp)
movzwl (%r13,%r10,4), %eax
subl %eax, %edi
movl %edi, 0x2c(%rsp)
movb %r12b, (%r15)
movq 0x68(%rsp), %rax
movb %dl, (%r15,%rax)
movq %rdx, %r13
movq 0x70(%rsp), %rax
movb %r14b, (%r15,%rax)
movq 0x18(%rsp), %rax
movb %r10b, (%r15,%rax)
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rbx
movq %rbx, %rsi
callq 0x3114e
leaq 0x24(%rsp), %rdi
movq %rbx, %rsi
callq 0x3114e
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x3114e
leaq 0x2c(%rsp), %rdi
movq %rbx, %rsi
callq 0x3114e
movq 0xb0(%rsp), %rsi
decq %rsi
incq %r15
movq %r12, %rdx
movq %r13, %rcx
movq %r14, %rax
jmp 0x31bad
xorl %r14d, %r14d
jmp 0x31dfd
xorl %r14d, %r14d
xorl %r15d, %r15d
jmp 0x31df5
xorl %r14d, %r14d
movq 0x38(%rsp), %r15
movq 0x10(%rsp), %rbp
movl $0x800, %ebx            # imm = 0x800
cmpq $0x81000, %rbx          # imm = 0x81000
je 0x31ded
movq (%rbp,%rbx), %rdi
callq 0x4600
addq $0x808, %rbx            # imm = 0x808
jmp 0x31dd1
movq %rbp, %rdi
callq 0x4600
movq %r15, %rdi
callq 0x4600
movq %r14, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x2c(%rsp), %eax
leaq 0x5c(%rsp), %rbp
movl %eax, (%rbp)
subq %rsi, 0x18(%rsp)
leaq 0x50(%rsp), %r14
movq 0x38(%rsp), %r15
movq 0x60(%rsp), %rbx
cmpq %rbx, 0x18(%rsp)
jge 0x31e92
movl 0x8(%rsp), %eax
imulq $0x808, %rax, %rcx      # imm = 0x808
movq 0x10(%rsp), %rdx
movq 0x800(%rdx,%rcx), %rcx
movl 0x5c(%rsp), %edx
andl $0xfff, %edx            # imm = 0xFFF
movzbl (%rcx,%rdx), %edx
movq 0x48(%rsp), %rcx
movq 0x18(%rsp), %r13
movb %dl, (%rcx,%r13)
shlq $0xa, %rax
addq %r15, %rax
movq %rdx, 0x8(%rsp)
leaq (%rax,%rdx,4), %rdx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3117a
incq %r13
movq %r13, 0x18(%rsp)
jmp 0x31e32
movq 0x80(%rsp), %rax
movl 0x44(%rsp), %ecx
movl %ecx, (%rax)
movq 0x10(%rsp), %rbp
movq 0x48(%rsp), %r14
jmp 0x31dcc
xorl %r14d, %r14d
jmp 0x31dcc
leaq 0x145cc(%rip), %rdi     # 0x4648a
leaq 0x144a2(%rip), %rsi     # 0x46367
leaq 0x145cb(%rip), %rcx     # 0x46497
movl $0x26a, %edx            # imm = 0x26A
callq 0x4360