// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc48, %rsp            # imm = 0xC48
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r13
leaq 0x440(%rsp), %rdi
xorl %ebx, %ebx
movl $0x808, %edx            # imm = 0x808
xorl %esi, %esi
callq 0x42d0
cmpb $0x0, (%r13)
jne 0x3ccd5
addl $-0x9, %ebp
cmpl %ebp, 0x1(%r13)
jne 0x3cc1e
movzbl 0x5(%r13), %r15d
movzbl 0x6(%r13), %eax
shll $0x8, %eax
movzbl 0x7(%r13), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movzbl 0x8(%r13), %eax
shll $0x18, %eax
orl %ecx, %eax
movslq %eax, %rdi
orq %r15, %rdi
movq %rdi, 0x10(%rsp)
callq 0x4700
xorl %ebx, %ebx
testq %rax, %rax
je 0x3cc1e
movq %r15, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movq %r14, 0x28(%rsp)
movzbl 0x9(%r13), %ebp
addq $0xa, %r13
movq 0xc40(%rsp), %r12
xorl %r15d, %r15d
movl %r15d, %r14d
movzbl (%r13), %ecx
movslq %ebp, %rax
movl %ecx, 0x440(%rsp,%rax,8)
testb %cl, %cl
js 0x3c98f
incq %r13
jmp 0x3c9ae
andl $0x7f, %ecx
movl %ecx, 0x440(%rsp,%rax,8)
shll $0x8, %ecx
movzbl 0x1(%r13), %edx
addq $0x2, %r13
orl %ecx, %edx
movl %edx, 0x440(%rsp,%rax,8)
movl %r14d, 0x444(%rsp,%rax,8)
cmpl $0x10001, %r14d         # imm = 0x10001
jae 0x3cd09
movslq 0x440(%rsp,%rax,8), %r15
movl $0x10000, %ecx          # imm = 0x10000
subl %r14d, %ecx
cmpl %r15d, %ecx
jb 0x3ccea
movw %r14w, 0x40(%rsp,%rax,4)
movw %r15w, 0x42(%rsp,%rax,4)
testq %r12, %r12
jne 0x3ca02
movl $0x1000, %edi           # imm = 0x1000
callq 0x4700
movq %rax, %r12
movq %rax, 0xc40(%rsp)
movl %r14d, %edi
addq %r12, %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x42d0
testl %ebx, %ebx
je 0x3ca1c
decl %ebx
incl %ebp
jmp 0x3ca39
movl %ebp, %eax
incl %eax
movzbl (%r13), %ebp
cmpl %ebp, %eax
jne 0x3ca34
movzbl 0x1(%r13), %ebx
addq $0x2, %r13
jmp 0x3ca39
incq %r13
xorl %ebx, %ebx
addl %r14d, %r15d
testl %ebp, %ebp
jne 0x3c974
cmpl $0x1000, %r15d          # imm = 0x1000
jge 0x3cd28
movl (%r13), %ecx
movl 0x4(%r13), %edx
movl 0x8(%r13), %eax
leaq 0x10(%r13), %rsi
movslq 0x10(%rsp), %rdi
andq $-0x4, %rdi
testl %edi, %edi
movq %rdi, 0x8(%rsp)
jle 0x3cbe4
movl 0xc(%r13), %r11d
movl %edi, %edi
movq %rdi, 0x30(%rsp)
xorl %r9d, %r9d
movq %r12, 0x38(%rsp)
movl %ecx, %edi
movl $0xfff, %r8d            # imm = 0xFFF
andl %r8d, %edi
movl %edx, %r14d
andl %r8d, %r14d
movl %eax, %r13d
andl %r8d, %r13d
movl %r11d, %r15d
andl %r8d, %r15d
movzbl (%r12,%rdi), %r10d
movzbl (%r12,%r14), %r8d
movzbl (%r12,%r13), %ebp
movzwl 0x42(%rsp,%r10,4), %ebx
shrl $0xc, %ecx
imull %ebx, %ecx
movzbl (%r12,%r15), %ebx
movq 0x18(%rsp), %r12
movb %r10b, (%r12,%r9)
movzwl 0x40(%rsp,%r10,4), %r10d
subl %r10d, %edi
addl %ecx, %edi
movb %r8b, 0x1(%r12,%r9)
movb %bpl, 0x2(%r12,%r9)
movb %bl, 0x3(%r12,%r9)
cmpl $0x7fffff, %edi         # imm = 0x7FFFFF
ja 0x3cb0e
movl %edi, %r10d
shll $0x8, %r10d
movzbl (%rsi), %ecx
incq %rsi
orl %r10d, %ecx
cmpl $0x8000, %edi           # imm = 0x8000
movl %ecx, %edi
jb 0x3caf2
jmp 0x3cb10
movl %edi, %ecx
movzwl 0x42(%rsp,%r8,4), %edi
shrl $0xc, %edx
imull %edi, %edx
movzwl 0x40(%rsp,%r8,4), %edi
subl %edi, %r14d
addl %edx, %r14d
cmpl $0x7fffff, %r14d        # imm = 0x7FFFFF
movq 0x38(%rsp), %r12
ja 0x3cb52
movl %r14d, %edi
shll $0x8, %edi
movzbl (%rsi), %edx
incq %rsi
orl %edi, %edx
cmpl $0x8000, %r14d          # imm = 0x8000
movl %edx, %r14d
jb 0x3cb36
jmp 0x3cb55
movl %r14d, %edx
movzwl 0x42(%rsp,%rbp,4), %edi
shrl $0xc, %eax
imull %edi, %eax
movzwl 0x40(%rsp,%rbp,4), %edi
subl %edi, %r13d
addl %eax, %r13d
cmpl $0x7fffff, %r13d        # imm = 0x7FFFFF
ja 0x3cb90
movl %r13d, %edi
shll $0x8, %edi
movzbl (%rsi), %eax
incq %rsi
orl %edi, %eax
cmpl $0x8000, %r13d          # imm = 0x8000
movl %eax, %r13d
jb 0x3cb74
jmp 0x3cb93
movl %r13d, %eax
movzwl 0x42(%rsp,%rbx,4), %edi
shrl $0xc, %r11d
imull %edi, %r11d
movzwl 0x40(%rsp,%rbx,4), %edi
subl %edi, %r15d
addl %r11d, %r15d
cmpl $0x7fffff, %r15d        # imm = 0x7FFFFF
ja 0x3cbd2
movl %r15d, %edi
shll $0x8, %edi
movzbl (%rsi), %r11d
incq %rsi
orl %edi, %r11d
cmpl $0x8000, %r15d          # imm = 0x8000
movl %r11d, %r15d
jb 0x3cbb4
jmp 0x3cbd5
movl %r15d, %r11d
addq $0x4, %r9
cmpq 0x30(%rsp), %r9
jb 0x3ca8a
movq 0x20(%rsp), %r8
andl $0x3, %r8d
leaq 0x1873c(%rip), %rdi     # 0x55330
movslq (%rdi,%r8,4), %r8
addq %rdi, %r8
movq 0x28(%rsp), %r11
movq 0x18(%rsp), %rbx
jmpq *%r8
andl $0xfff, %ecx            # imm = 0xFFF
addq %r12, %rcx
movq %rcx, %rax
movq 0x8(%rsp), %r8
jmp 0x3ccb8
xorl %ebx, %ebx
jmp 0x3ccd5
movl $0xfff, %eax            # imm = 0xFFF
andl %eax, %ecx
movb (%r12,%rcx), %cl
movq 0x8(%rsp), %r8
movb %cl, (%rbx,%r8)
andl %eax, %edx
addq %r12, %rdx
orl $0x1, %r8d
movq %rdx, %rax
jmp 0x3ccb8
movl %ecx, %r8d
andl $0xfff, %r8d            # imm = 0xFFF
movzbl (%r12,%r8), %edi
movzwl 0x40(%rsp,%rdi,4), %r9d
movzwl 0x42(%rsp,%rdi,4), %r10d
shrl $0xc, %ecx
imull %r10d, %ecx
subl %r9d, %r8d
addl %ecx, %r8d
cmpl $0x7fffff, %r8d         # imm = 0x7FFFFF
ja 0x3cc94
movl %r8d, %ecx
shll $0x8, %ecx
movzbl (%rsi), %r9d
incq %rsi
orl %ecx, %r9d
cmpl $0x8000, %r8d           # imm = 0x8000
movl %r9d, %r8d
jb 0x3cc78
movq 0x8(%rsp), %r8
movslq %r8d, %rcx
movb %dil, (%rbx,%rcx)
movl $0xfff, %esi            # imm = 0xFFF
andl %esi, %edx
movb (%r12,%rdx), %dl
movb %dl, 0x1(%rbx,%rcx)
andl %esi, %eax
addq %r12, %rax
orl $0x2, %r8d
movb (%rax), %al
movslq %r8d, %rcx
movb %al, (%rbx,%rcx)
movq 0x10(%rsp), %rax
movl %eax, (%r11)
testq %r12, %r12
je 0x3ccd5
movq %r12, %rdi
callq 0x4610
movq %rbx, %rax
addq $0xc48, %rsp            # imm = 0xC48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18914(%rip), %rdi     # 0x55605
leaq 0x187d0(%rip), %rsi     # 0x554c8
leaq 0x188ca(%rip), %rcx     # 0x555c9
movl $0x104, %edx            # imm = 0x104
callq 0x4350
leaq 0x188a6(%rip), %rdi     # 0x555b6
leaq 0x187b1(%rip), %rsi     # 0x554c8
leaq 0x188ab(%rip), %rcx     # 0x555c9
movl $0x103, %edx            # imm = 0x103
callq 0x4350
leaq 0x1867a(%rip), %rdi     # 0x553a9
leaq 0x18611(%rip), %rsi     # 0x55347
leaq 0x18678(%rip), %rcx     # 0x553b5
jmp 0x3ccff