// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
cmpb $0x1, (%rdi)
jne 0x3cf64
movq %rdi, %r13
addl $-0x9, %esi
cmpl %esi, 0x1(%rdi)
jne 0x3cf64
movq %rdx, %rbx
movl 0x5(%r13), %ebp
movl $0x100, %edi            # imm = 0x100
movl $0x808, %esi            # imm = 0x808
callq 0x4360
testq %rax, %rax
je 0x3cf69
movq %rax, %r12
movl $0x40000, %edi          # imm = 0x40000
callq 0x4700
movq %rax, %r15
testq %rax, %rax
je 0x3cf80
movl %ebp, 0x2c(%rsp)
movq %rbx, 0x70(%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movzbl 0x9(%r13), %edx
addq $0xa, %r13
xorl %ecx, %ecx
movl %ecx, 0x20(%rsp)
movzbl (%r13), %ebp
incq %r13
movl %edx, (%rsp)
movslq %edx, %rcx
movq %rcx, %rax
shlq $0xb, %rax
leaq (%rax,%rcx,8), %rbx
addq 0x8(%rsp), %rbx
xorl %r15d, %r15d
shlq $0xa, %rcx
addq 0x10(%rsp), %rcx
movq %rcx, 0x18(%rsp)
xorl %r14d, %r14d
movl %r14d, %r12d
movzbl (%r13), %r14d
movslq %ebp, %rax
movl %r14d, (%rbx,%rax,8)
testb %r14b, %r14b
js 0x3cb6f
incq %r13
jmp 0x3cb8b
andl $0x7f, %r14d
movl %r14d, (%rbx,%rax,8)
shll $0x8, %r14d
movzbl 0x1(%r13), %ecx
addq $0x2, %r13
orl %ecx, %r14d
movl %r14d, (%rbx,%rax,8)
movl %r12d, 0x4(%rbx,%rax,8)
testl %r14d, %r14d
jne 0x3cba2
movl $0x1000, (%rbx,%rax,8)  # imm = 0x1000
movl $0x1000, %r14d          # imm = 0x1000
movl $0x10000, %ecx          # imm = 0x10000
subl %r12d, %ecx
cmpl %r14d, %ecx
jb 0x3d073
movq 0x18(%rsp), %rcx
movw %r12w, (%rcx,%rax,4)
movw %r14w, 0x2(%rcx,%rax,4)
movq 0x800(%rbx), %rax
testq %rax, %rax
jne 0x3cbe9
movl $0x1000, %edi           # imm = 0x1000
callq 0x4700
movq %rax, 0x800(%rbx)
testq %rax, %rax
je 0x3cf71
movl %r12d, %edi
addq %rax, %rdi
movl %r14d, %edx
movl %ebp, %esi
callq 0x42d0
addl %r12d, %r14d
cmpl $0x1001, %r14d          # imm = 0x1001
jae 0x3d092
testl %r15d, %r15d
je 0x3cc15
decl %r15d
incl %ebp
jmp 0x3cc29
incl %ebp
movzbl (%r13), %eax
cmpl %eax, %ebp
jne 0x3cc37
movzbl 0x1(%r13), %r15d
addq $0x2, %r13
movl %ebp, %eax
movl %eax, %ebp
testl %eax, %eax
jne 0x3cb56
jmp 0x3cc3f
incq %r13
xorl %r15d, %r15d
jmp 0x3cc2b
movl 0x20(%rsp), %ecx
testl %ecx, %ecx
je 0x3cc50
decl %ecx
movl (%rsp), %eax
incl %eax
jmp 0x3cc70
movl (%rsp), %edx
incl %edx
movzbl (%r13), %eax
cmpl %eax, %edx
jne 0x3cc6b
movzbl 0x1(%r13), %ecx
addq $0x2, %r13
movl %edx, %eax
jmp 0x3cc70
incq %r13
xorl %ecx, %ecx
movl %eax, %edx
testl %eax, %eax
jne 0x3cb20
movl (%r13), %ebx
movl 0x4(%r13), %r12d
movl 0x8(%r13), %r14d
movl 0xc(%r13), %ebp
movl 0x2c(%rsp), %r15d
movslq %r15d, %rdi
movq %rdi, 0x60(%rsp)
callq 0x4700
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x3cf71
movl %r15d, %edx
sarl $0x2, %edx
leal (%rdx,%rdx,2), %ecx
addq $0x10, %r13
testl %edx, %edx
movq 0x10(%rsp), %r15
jle 0x3cfcb
movl %edx, %eax
addl %edx, %edx
movl %ecx, %ecx
xorl %esi, %esi
movq %rax, 0x68(%rsp)
movq $0x0, 0x18(%rsp)
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r10d, %r10d
movq %rdi, 0x38(%rsp)
movq %rdx, (%rsp)
movq %rcx, 0x20(%rsp)
movq %rax, 0x58(%rsp)
movq %rsi, 0x40(%rsp)
movq %r10, %r9
movl %ebx, %r15d
movl $0xfff, %r10d           # imm = 0xFFF
andl %r10d, %r15d
movl %r12d, %eax
andl %r10d, %eax
movl %r14d, %ecx
andl %r10d, %ecx
movl %ebp, %r11d
andl %r10d, %r11d
movq %r9, %rdx
shlq $0xb, %rdx
leaq (%rdx,%r9,8), %rdx
movq 0x8(%rsp), %r10
movq 0x800(%r10,%rdx), %rdx
movzbl (%rdx,%r15), %esi
shlq $0xa, %r9
addq 0x10(%rsp), %r9
movzwl 0x2(%r9,%rsi,4), %edx
shrl $0xc, %ebx
imull %edx, %ebx
movq %r8, %rdx
shlq $0xb, %rdx
movq %r8, 0x78(%rsp)
leaq (%rdx,%r8,8), %rdx
movq 0x800(%r10,%rdx), %rdx
movzbl (%rdx,%rax), %edi
movzwl (%r9,%rsi,4), %r9d
movq %rsi, %r8
subl %r9d, %r15d
movq 0x38(%rsp), %rdx
movq %rdx, %r9
shlq $0xb, %r9
leaq (%r9,%rdx,8), %r9
movq 0x800(%r10,%r9), %r9
movzbl (%r9,%rcx), %esi
movl 0x18(%rsp), %r9d
addl %ebx, %r15d
movq %r9, %rbx
shlq $0xb, %rbx
leaq (%rbx,%r9,8), %rbx
movq 0x800(%r10,%rbx), %rbx
movzbl (%rbx,%r11), %ebx
movq 0x30(%rsp), %rdx
movq %r8, 0x80(%rsp)
movq 0x40(%rsp), %r10
movb %r8b, (%rdx,%r10)
movq %rdi, 0x50(%rsp)
movq 0x58(%rsp), %r10
movb %dil, (%rdx,%r10)
movq %rsi, 0x48(%rsp)
movq (%rsp), %rdi
movb %sil, (%rdx,%rdi)
movq %rbx, 0x18(%rsp)
movq 0x20(%rsp), %rsi
movb %bl, (%rdx,%rsi)
cmpl $0x7fffff, %r15d        # imm = 0x7FFFFF
ja 0x3ce16
movl %r15d, %edx
shll $0x8, %edx
movzbl (%r13), %ebx
incq %r13
orl %edx, %ebx
cmpl $0x8000, %r15d          # imm = 0x8000
movl %ebx, %r15d
jb 0x3cdf8
jmp 0x3ce19
movl %r15d, %ebx
movq 0x78(%rsp), %rsi
shlq $0xa, %rsi
movq 0x10(%rsp), %r15
addq %r15, %rsi
movq 0x50(%rsp), %r10
movzwl 0x2(%rsi,%r10,4), %edx
shrl $0xc, %r12d
imull %edx, %r12d
movzwl (%rsi,%r10,4), %edx
subl %edx, %eax
addl %r12d, %eax
cmpl $0x7fffff, %eax         # imm = 0x7FFFFF
movq 0x80(%rsp), %r10
ja 0x3ce7c
movq 0x38(%rsp), %rdi
movq 0x48(%rsp), %r8
movl %eax, %edx
shll $0x8, %edx
movzbl (%r13), %r12d
incq %r13
orl %edx, %r12d
cmpl $0x8000, %eax           # imm = 0x8000
movl %r12d, %eax
jb 0x3ce60
jmp 0x3ce89
movl %eax, %r12d
movq 0x38(%rsp), %rdi
movq 0x48(%rsp), %r8
shlq $0xa, %rdi
addq %r15, %rdi
movzwl 0x2(%rdi,%r8,4), %eax
shrl $0xc, %r14d
imull %eax, %r14d
movzwl (%rdi,%r8,4), %eax
subl %eax, %ecx
addl %r14d, %ecx
cmpl $0x7fffff, %ecx         # imm = 0x7FFFFF
ja 0x3ced1
movq (%rsp), %rdx
movl %ecx, %eax
shll $0x8, %eax
movzbl (%r13), %r14d
incq %r13
orl %eax, %r14d
cmpl $0x8000, %ecx           # imm = 0x8000
movl %r14d, %ecx
jb 0x3ceb4
jmp 0x3ced8
movl %ecx, %r14d
movq (%rsp), %rdx
shlq $0xa, %r9
addq %r15, %r9
movq 0x18(%rsp), %rcx
movzwl 0x2(%r9,%rcx,4), %eax
shrl $0xc, %ebp
imull %eax, %ebp
movzwl (%r9,%rcx,4), %eax
subl %eax, %r11d
addl %ebp, %r11d
cmpl $0x7fffff, %r11d        # imm = 0x7FFFFF
ja 0x3cf2c
movq 0x20(%rsp), %rcx
movq 0x40(%rsp), %rsi
movl %r11d, %eax
shll $0x8, %eax
movzbl (%r13), %ebp
incq %r13
orl %eax, %ebp
cmpl $0x8000, %r11d          # imm = 0x8000
movl %ebp, %r11d
jb 0x3cf0e
jmp 0x3cf39
movl %r11d, %ebp
movq 0x20(%rsp), %rcx
movq 0x40(%rsp), %rsi
incq %rsi
movq 0x58(%rsp), %rax
incq %rax
incq %rdx
incq %rcx
movq %r8, %rdi
movq 0x50(%rsp), %r8
cmpq 0x68(%rsp), %rsi
jne 0x3cce2
movq 0x18(%rsp), %r10
jmp 0x3cfce
xorl %r14d, %r14d
jmp 0x3cfb6
xorl %r14d, %r14d
xorl %r15d, %r15d
jmp 0x3cfae
xorl %r14d, %r14d
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %r12
jmp 0x3cf83
xorl %r14d, %r14d
movl $0x800, %ebx            # imm = 0x800
movq (%r12,%rbx), %rdi
testq %rdi, %rdi
je 0x3cf96
callq 0x4610
addq $0x808, %rbx            # imm = 0x808
cmpq $0x81000, %rbx          # imm = 0x81000
jne 0x3cf88
movq %r12, %rdi
callq 0x4610
movq %r15, %rdi
callq 0x4610
movq %r14, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r10d, %r10d
movl 0x2c(%rsp), %r8d
cmpl %r8d, %ecx
movq 0x70(%rsp), %rdi
movq 0x60(%rsp), %r9
jge 0x3d066
movslq %ecx, %rax
movl %r10d, %edx
movq %rdx, %rcx
shlq $0xb, %rcx
leaq (%rcx,%rdx,8), %rcx
movq 0x8(%rsp), %rsi
movq 0x800(%rsi,%rcx), %rsi
movl %ebp, %ecx
andl $0xfff, %ecx            # imm = 0xFFF
movzbl (%rsi,%rcx), %r11d
movq 0x30(%rsp), %rsi
movb %r11b, (%rsi,%rax)
shlq $0xa, %rdx
addq %r15, %rdx
movzwl (%rdx,%r11,4), %esi
movq %r11, %r10
movzwl 0x2(%rdx,%r11,4), %edx
shrl $0xc, %ebp
imull %edx, %ebp
subl %esi, %ecx
addl %ebp, %ecx
cmpl $0x7fffff, %ecx         # imm = 0x7FFFFF
ja 0x3d05c
movl %ecx, %edx
shll $0x8, %edx
movzbl (%r13), %ebp
incq %r13
orl %edx, %ebp
cmpl $0x8000, %ecx           # imm = 0x8000
movl %ebp, %ecx
jb 0x3d041
jmp 0x3d05e
movl %ecx, %ebp
incq %rax
cmpq %r9, %rax
jl 0x3cfe9
movl %r8d, (%rdi)
movq 0x30(%rsp), %r14
jmp 0x3cf79
leaq 0x1683b(%rip), %rdi     # 0x538b5
leaq 0x166f7(%rip), %rsi     # 0x53778
leaq 0x167f1(%rip), %rcx     # 0x53879
movl $0x104, %edx            # imm = 0x104
callq 0x4350
leaq 0x16681(%rip), %rdi     # 0x5371a
leaq 0x16557(%rip), %rsi     # 0x535f7
leaq 0x16680(%rip), %rcx     # 0x53727
movl $0x26a, %edx            # imm = 0x26A
callq 0x4350