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

    memset(&D, 0, sizeof(D));

    if (*in++ != 0) // Order-0 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;

    out_buf = malloc(out_sz);
    if (!out_buf)
	return NULL;

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

    // Precompute reverse lookup of frequency.
    rle = x = 0;
    j = *cp++;
    do {
	if ((D.fc[j].F = *cp++) >= 128) {
	    D.fc[j].F &= ~128;
	    D.fc[j].F = ((D.fc[j].F & 127) << 8) | *cp++;
	}
	D.fc[j].C = x;

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

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

	x += D.fc[j].F;

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

    assert(x < TOTFREQ);

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

    int out_end = (out_sz&~3);

    RansState R[4];
    R[0] = rans0;
    R[1] = rans1;
    R[2] = rans2;
    R[3] = rans3;
    uint32_t mask = (1u << TF_SHIFT)-1;

    for (i=0; i < out_end; i+=4) {
	uint32_t m[4] = {R[0] & mask,
			 R[1] & mask,
			 R[2] & mask,
			 R[3] & mask};
	uint8_t c[4] = {D.R[m[0]],
			D.R[m[1]],
			D.R[m[2]],
			D.R[m[3]]};
	out_buf[i+0] = c[0];
	out_buf[i+1] = c[1];
	out_buf[i+2] = c[2];
	out_buf[i+3] = c[3];

	// RansDecAdvanceSymbolStep(&R[0], &syms[c[0]], TF_SHIFT);
	// RansDecAdvanceSymbolStep(&R[1], &syms[c[1]], TF_SHIFT);
	// RansDecAdvanceSymbolStep(&R[2], &syms[c[2]], TF_SHIFT);
	// RansDecAdvanceSymbolStep(&R[3], &syms[c[3]], TF_SHIFT);
	R[0] = syms[c[0]].freq * (R[0]>>TF_SHIFT);
	R[1] = syms[c[1]].freq * (R[1]>>TF_SHIFT);
	R[2] = syms[c[2]].freq * (R[2]>>TF_SHIFT);
	R[3] = syms[c[3]].freq * (R[3]>>TF_SHIFT);

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

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

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

    switch(out_sz&3) {
	unsigned char c;
    case 0:
	break;
    case 1:
	c = D.R[RansDecGet(&rans0, TF_SHIFT)];
	RansDecAdvanceSymbol(&rans0, &ptr, &syms[c], TF_SHIFT);
	out_buf[out_end] = c;
	break;

    case 2:
	c = D.R[RansDecGet(&rans0, TF_SHIFT)];
	RansDecAdvanceSymbol(&rans0, &ptr, &syms[c], TF_SHIFT);
	out_buf[out_end] = c;

	c = D.R[RansDecGet(&rans1, TF_SHIFT)];
	RansDecAdvanceSymbol(&rans1, &ptr, &syms[c], TF_SHIFT);
	out_buf[out_end+1] = c;
	break;

    case 3:
	c = D.R[RansDecGet(&rans0, TF_SHIFT)];
	RansDecAdvanceSymbol(&rans0, &ptr, &syms[c], TF_SHIFT);
	out_buf[out_end] = c;

	c = D.R[RansDecGet(&rans1, TF_SHIFT)];
	RansDecAdvanceSymbol(&rans1, &ptr, &syms[c], TF_SHIFT);
	out_buf[out_end+1] = c;

	c = D.R[RansDecGet(&rans2, TF_SHIFT)];
	RansDecAdvanceSymbol(&rans2, &ptr, &syms[c], TF_SHIFT);
	out_buf[out_end+2] = c;
	break;
    }

    *out_size = out_sz;

    if (D.R) free(D.R);

    return (unsigned char *)out_buf;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x468, %rsp            # imm = 0x468
cmpb $0x0, (%rdi)
jne 0x30dd9
movq %rdi, %r13
addl $-0x9, %esi
cmpl %esi, 0x1(%rdi)
jne 0x30dd9
movq %rdx, %rbx
movzbl 0x5(%r13), %r15d
movzbl 0x6(%r13), %eax
shll $0x8, %eax
movzwl 0x7(%r13), %ecx
shll $0x10, %ecx
movslq %ecx, %rdi
orq %rax, %rdi
orq %r15, %rdi
movq %rdi, 0x48(%rsp)
callq 0x46f0
xorl %r14d, %r14d
testq %rax, %rax
je 0x30dd9
movq %r15, 0x50(%rsp)
movq %rax, 0x30(%rsp)
movq %rbx, 0x58(%rsp)
movzbl 0x9(%r13), %r15d
addq $0xa, %r13
xorl %r12d, %r12d
xorl %ebx, %ebx
movl %ebx, %ebp
movzbl (%r13), %ebx
testb %bl, %bl
js 0x30d55
incq %r13
jmp 0x30d66
andl $0x7f, %ebx
shll $0x8, %ebx
movzbl 0x1(%r13), %eax
addq $0x2, %r13
orl %eax, %ebx
movslq %r15d, %rax
leaq (%rsp,%rax,4), %rdi
addq $0x60, %rdi
movl %ebp, %esi
movl %ebx, %edx
callq 0x310f3
testq %r12, %r12
jne 0x30d8c
movl $0x1000, %edi           # imm = 0x1000
callq 0x46f0
movq %rax, %r12
movslq %ebp, %rdi
addq %r12, %rdi
movl %ebx, %edx
movl %r15d, %esi
callq 0x42e0
testl %r14d, %r14d
je 0x30da9
decl %r14d
incl %r15d
jmp 0x30dbf
incl %r15d
movzbl (%r13), %eax
cmpl %eax, %r15d
jne 0x30dd1
movzbl 0x1(%r13), %r14d
addq $0x2, %r13
movl %r15d, %eax
addl %ebp, %ebx
movl %eax, %r15d
testl %eax, %eax
jne 0x30d45
jmp 0x30ded
incq %r13
xorl %r14d, %r14d
jmp 0x30dc2
xorl %eax, %eax
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1000, %ebx           # imm = 0x1000
jge 0x310d4
leaq 0x28(%rsp), %rdi
movq %r13, (%rdi)
leaq 0x4(%r13), %rax
leaq 0x10(%r13), %rcx
movslq 0x48(%rsp), %rbp
andq $-0x4, %rbp
leaq 0x1c(%rsp), %r8
movl (%r13), %edx
movq %rax, (%rdi)
movl %edx, 0x8(%rsp)
movl 0x4(%r13), %eax
movl %eax, 0xc(%rsp)
movq 0x8(%r13), %rsi
movq %rsi, %xmm0
movl %esi, 0x3c(%rsp)
movq %rcx, (%rdi)
movd %edx, %xmm1
movd %eax, %xmm2
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqa %xmm1, -0xc(%r8)
xorl %ebx, %ebx
movq %rbp, 0x40(%rsp)
movl 0x10(%rsp), %eax
cmpq %rbp, %rbx
jge 0x30f72
movq %rax, %r10
movl $0xfff, %r11d           # imm = 0xFFF
andq %r11, %r10
movl 0x14(%rsp), %esi
movl 0x18(%rsp), %ecx
movq %rsi, %r9
andq %r11, %r9
movq %rcx, %r8
andq %r11, %r8
movl 0x1c(%rsp), %edx
movq %rdx, %rdi
andq %r11, %rdi
movzbl (%r12,%r10), %ebp
movzbl (%r12,%r9), %r15d
movzbl (%r12,%r8), %r14d
movzbl (%r12,%rdi), %r11d
movzwl 0x62(%rsp,%rbp,4), %r13d
shrl $0xc, %eax
imull %r13d, %eax
movzwl 0x62(%rsp,%r15,4), %r13d
shrl $0xc, %esi
imull %r13d, %esi
movzwl 0x62(%rsp,%r14,4), %r13d
shrl $0xc, %ecx
imull %r13d, %ecx
movzwl 0x62(%rsp,%r11,4), %r13d
shrl $0xc, %edx
imull %r13d, %edx
addl %r10d, %eax
movq 0x30(%rsp), %r13
movb %bpl, (%r13,%rbx)
movzwl 0x60(%rsp,%rbp,4), %r10d
movq 0x40(%rsp), %rbp
subl %r10d, %eax
movl %eax, 0x10(%rsp)
addl %r9d, %esi
movb %r15b, 0x1(%r13,%rbx)
movzwl 0x60(%rsp,%r15,4), %eax
subl %eax, %esi
movl %esi, 0x14(%rsp)
addl %r8d, %ecx
movb %r14b, 0x2(%r13,%rbx)
movzwl 0x60(%rsp,%r14,4), %eax
subl %eax, %ecx
movl %ecx, 0x18(%rsp)
addl %edi, %edx
movb %r11b, 0x3(%r13,%rbx)
movzwl 0x60(%rsp,%r11,4), %eax
subl %eax, %edx
movl %edx, 0x1c(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x3114e
leaq 0x14(%rsp), %rdi
movq %r14, %rsi
callq 0x3114e
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x3114e
leaq 0x1c(%rsp), %rdi
movq %r14, %rsi
callq 0x3114e
addq $0x4, %rbx
jmp 0x30e57
movl %eax, 0x8(%rsp)
movl 0x14(%rsp), %r14d
movl 0x18(%rsp), %r13d
movl %r14d, 0xc(%rsp)
movl %r13d, 0x3c(%rsp)
movq 0x50(%rsp), %rdx
andl $0x3, %edx
leaq 0x153b7(%rip), %rcx     # 0x46350
movslq (%rcx,%rdx,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
andl $0xfff, %eax            # imm = 0xFFF
movzbl (%r12,%rax), %r14d
leaq (%rsp,%r14,4), %rdx
addq $0x60, %rdx
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x3117a
movq 0x30(%rsp), %r15
jmp 0x310b2
movl $0xfff, %ebp            # imm = 0xFFF
andq %rbp, %rax
movzbl (%r12,%rax), %r13d
leaq (%rsp,%r13,4), %rdx
addq $0x60, %rdx
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
callq 0x3117a
movq 0x30(%rsp), %r15
movq 0x40(%rsp), %rax
movb %r13b, (%r15,%rax)
andq %rbp, %r14
movq 0x40(%rsp), %rbp
movzbl (%r12,%r14), %r14d
leaq (%rsp,%r14,4), %rdx
addq $0x60, %rdx
leaq 0xc(%rsp), %rdi
movq %rbx, %rsi
callq 0x3117a
orl $0x1, %ebp
jmp 0x310af
movl $0xfff, %ecx            # imm = 0xFFF
andq %rcx, %rax
movq %rbp, %rbx
movzbl (%r12,%rax), %ebp
leaq (%rsp,%rbp,4), %rdx
addq $0x60, %rdx
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x3117a
movq 0x30(%rsp), %r15
movb %bpl, (%r15,%rbx)
movl $0xfff, %eax            # imm = 0xFFF
andq %rax, %r14
movzbl (%r12,%r14), %r14d
leaq (%rsp,%r14,4), %rdx
addq $0x60, %rdx
leaq 0xc(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x3117a
movslq %ebx, %rbp
movb %r14b, 0x1(%r15,%rbp)
movl $0xfff, %eax            # imm = 0xFFF
andq %rax, %r13
movzbl (%r12,%r13), %r14d
leaq (%rsp,%r14,4), %rdx
addq $0x60, %rdx
leaq 0x3c(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x3117a
orl $0x2, %ebp
movslq %ebp, %rbp
movb %r14b, (%r15,%rbp)
movq 0x58(%rsp), %rax
movq 0x48(%rsp), %rcx
movl %ecx, (%rax)
movq %r12, %rdi
callq 0x4600
movq 0x30(%rsp), %rax
jmp 0x30ddb
leaq 0x152ee(%rip), %rdi     # 0x463c9
leaq 0x15285(%rip), %rsi     # 0x46367
leaq 0x152ec(%rip), %rcx     # 0x463d5
movl $0x104, %edx            # imm = 0x104
callq 0x4360