// File: /workspace/github_cpp_makefile_star2_O3/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 $0x448, %rsp            # imm = 0x448
cmpb $0x0, (%rdi)
jne 0x3bcf1
movq %rdi, %r13
addl $-0x9, %esi
cmpl %esi, 0x1(%rdi)
jne 0x3bcf1
movq %rdx, %r14
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, 0x18(%rsp)
callq 0x4700
xorl %ebx, %ebx
testq %rax, %rax
je 0x3bcf1
movq %r15, 0x28(%rsp)
movq %rax, 0x8(%rsp)
movq %r14, 0x20(%rsp)
movzbl 0x9(%r13), %ebp
addq $0xa, %r13
xorl %r12d, %r12d
xorl %r15d, %r15d
movl %r15d, %r14d
movzbl (%r13), %r15d
testb %r15b, %r15b
js 0x3bc58
incq %r13
jmp 0x3bc6c
andl $0x7f, %r15d
shll $0x8, %r15d
movzbl 0x1(%r13), %eax
addq $0x2, %r13
orl %eax, %r15d
cmpl $0x10001, %r14d         # imm = 0x10001
jae 0x3bfd3
movl $0x10000, %eax          # imm = 0x10000
subl %r14d, %eax
cmpl %r15d, %eax
jb 0x3bfb4
movslq %ebp, %rax
movw %r14w, 0x40(%rsp,%rax,4)
movw %r15w, 0x42(%rsp,%rax,4)
testq %r12, %r12
jne 0x3bcab
movl $0x1000, %edi           # imm = 0x1000
callq 0x4700
movq %rax, %r12
movl %r14d, %edi
addq %r12, %rdi
movl %r15d, %edx
movl %ebp, %esi
callq 0x42d0
testl %ebx, %ebx
je 0x3bcc5
decl %ebx
incl %ebp
jmp 0x3bcd9
incl %ebp
movzbl (%r13), %eax
cmpl %eax, %ebp
jne 0x3bcea
movzbl 0x1(%r13), %ebx
addq $0x2, %r13
movl %ebp, %eax
addl %r14d, %r15d
movl %eax, %ebp
testl %eax, %eax
jne 0x3bc46
jmp 0x3bd08
incq %r13
xorl %ebx, %ebx
jmp 0x3bcdb
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1000, %r15d          # imm = 0x1000
jae 0x3bff2
movl (%r13), %ecx
movl 0x4(%r13), %edx
movl 0x8(%r13), %eax
leaq 0x10(%r13), %rsi
movslq 0x18(%rsp), %rdi
andq $-0x4, %rdi
testl %edi, %edi
movq %rdi, 0x10(%rsp)
jle 0x3bea2
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, %ebp
andl %r8d, %ebp
movl %r11d, %r13d
andl %r8d, %r13d
movzbl (%r12,%rdi), %r10d
movzbl (%r12,%r14), %r8d
movzbl (%r12,%rbp), %r15d
movzwl 0x42(%rsp,%r10,4), %ebx
shrl $0xc, %ecx
imull %ebx, %ecx
movzbl (%r12,%r13), %ebx
movq 0x8(%rsp), %r12
movb %r10b, (%r12,%r9)
movzwl 0x40(%rsp,%r10,4), %r10d
subl %r10d, %edi
addl %ecx, %edi
movb %r8b, 0x1(%r12,%r9)
movb %r15b, 0x2(%r12,%r9)
movb %bl, 0x3(%r12,%r9)
cmpl $0x7fffff, %edi         # imm = 0x7FFFFF
ja 0x3bdd1
movl %edi, %r10d
shll $0x8, %r10d
movzbl (%rsi), %ecx
incq %rsi
orl %r10d, %ecx
cmpl $0x8000, %edi           # imm = 0x8000
movl %ecx, %edi
jb 0x3bdb5
jmp 0x3bdd3
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 0x3be15
movl %r14d, %edi
shll $0x8, %edi
movzbl (%rsi), %edx
incq %rsi
orl %edi, %edx
cmpl $0x8000, %r14d          # imm = 0x8000
movl %edx, %r14d
jb 0x3bdf9
jmp 0x3be18
movl %r14d, %edx
movzwl 0x42(%rsp,%r15,4), %edi
shrl $0xc, %eax
imull %edi, %eax
movzwl 0x40(%rsp,%r15,4), %edi
subl %edi, %ebp
addl %eax, %ebp
cmpl $0x7fffff, %ebp         # imm = 0x7FFFFF
ja 0x3be4f
movl %ebp, %edi
shll $0x8, %edi
movzbl (%rsi), %eax
incq %rsi
orl %edi, %eax
cmpl $0x8000, %ebp           # imm = 0x8000
movl %eax, %ebp
jb 0x3be36
jmp 0x3be51
movl %ebp, %eax
movzwl 0x42(%rsp,%rbx,4), %edi
shrl $0xc, %r11d
imull %edi, %r11d
movzwl 0x40(%rsp,%rbx,4), %edi
subl %edi, %r13d
addl %r11d, %r13d
cmpl $0x7fffff, %r13d        # imm = 0x7FFFFF
ja 0x3be90
movl %r13d, %edi
shll $0x8, %edi
movzbl (%rsi), %r11d
incq %rsi
orl %edi, %r11d
cmpl $0x8000, %r13d          # imm = 0x8000
movl %r11d, %r13d
jb 0x3be72
jmp 0x3be93
movl %r13d, %r11d
addq $0x4, %r9
cmpq 0x30(%rsp), %r9
jb 0x3bd4e
movq 0x28(%rsp), %r8
andl $0x3, %r8d
leaq 0x1772e(%rip), %rdi     # 0x535e0
movslq (%rdi,%r8,4), %r8
addq %rdi, %r8
jmpq *%r8
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rcx
movl %ecx, (%rax)
testq %r12, %r12
movq 0x8(%rsp), %rbx
jne 0x3bfa7
jmp 0x3bcf3
movl $0xfff, %eax            # imm = 0xFFF
andl %eax, %ecx
movb (%r12,%rcx), %cl
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %r8
movb %cl, (%rbx,%r8)
andl %eax, %edx
addq %r12, %rdx
orl $0x1, %r8d
movq %rdx, %rax
jmp 0x3bf93
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
movq 0x8(%rsp), %rbx
ja 0x3bf57
movl %r8d, %ecx
shll $0x8, %ecx
movzbl (%rsi), %r9d
incq %rsi
orl %ecx, %r9d
cmpl $0x8000, %r8d           # imm = 0x8000
movl %r9d, %r8d
jb 0x3bf3b
movq 0x10(%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
jmp 0x3bf93
andl $0xfff, %ecx            # imm = 0xFFF
addq %r12, %rcx
movq %rcx, %rax
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %r8
movb (%rax), %al
movslq %r8d, %rcx
movb %al, (%rbx,%rcx)
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rcx
movl %ecx, (%rax)
movq %r12, %rdi
callq 0x4610
jmp 0x3bcf3
leaq 0x178fa(%rip), %rdi     # 0x538b5
leaq 0x177b6(%rip), %rsi     # 0x53778
leaq 0x178b0(%rip), %rcx     # 0x53879
movl $0x104, %edx            # imm = 0x104
callq 0x4350
leaq 0x1788c(%rip), %rdi     # 0x53866
leaq 0x17797(%rip), %rsi     # 0x53778
leaq 0x17891(%rip), %rcx     # 0x53879
movl $0x103, %edx            # imm = 0x103
callq 0x4350
leaq 0x17660(%rip), %rdi     # 0x53659
leaq 0x175f7(%rip), %rsi     # 0x535f7
leaq 0x1765e(%rip), %rcx     # 0x53665
jmp 0x3bfc9