// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/htslib/cram/rANS_static.c
// Function: rans_compress_O0
unsigned char *rans_compress_O0(unsigned char *in, unsigned int in_size,
				unsigned int *out_size) {
    unsigned char *out_buf = malloc(1.05*in_size + 257*257*3 + 9);
    unsigned char *cp, *out_end;
    RansEncSymbol syms[256];
    RansState rans0, rans1, rans2, rans3;
    uint8_t* ptr;
    int F[256] = {0}, i, j, tab_size, rle, x, fsum = 0;
    int m = 0, M = 0;
    uint64_t tr;

    if (!out_buf)
	return NULL;

    ptr = out_end = out_buf + (int)(1.05*in_size) + 257*257*3 + 9;

    // Compute statistics
    for (i = 0; i < in_size; i++) {
	F[in[i]]++;
    }
    tr = ((uint64_t)TOTFREQ<<31)/in_size + (1<<30)/in_size;

    // Normalise so T[i] == TOTFREQ
    for (m = M = j = 0; j < 256; j++) {
	if (!F[j])
	    continue;

	if (m < F[j])
	    m = F[j], M = j;

	if ((F[j] = (F[j]*tr)>>31) == 0)
	    F[j] = 1;
	fsum += F[j];
    }

    fsum++;
    if (fsum < TOTFREQ)
	F[M] += TOTFREQ-fsum;
    else
	F[M] -= fsum-TOTFREQ;

    //printf("F[%d]=%d\n", M, F[M]);
    assert(F[M]>0);

    // Encode statistics.
    cp = out_buf+9;

    for (x = rle = j = 0; j < 256; j++) {
	if (F[j]) {
	    // j
	    if (rle) {
		rle--;
	    } else {
		*cp++ = j;
		if (!rle && j && F[j-1])  {
		    for(rle=j+1; rle<256 && F[rle]; rle++)
			;
		    rle -= j+1;
		    *cp++ = rle;
		}
		//fprintf(stderr, "%d: %d %d\n", j, rle, N[j]);
	    }

	    // F[j]
	    if (F[j]<128) {
		*cp++ = F[j];
	    } else {
		*cp++ = 128 | (F[j]>>8);
		*cp++ = F[j]&0xff;
	    }
	    RansEncSymbolInit(&syms[j], x, F[j], TF_SHIFT);
	    x += F[j];
	}
    }
    *cp++ = 0;

    //write(1, out_buf+4, cp-(out_buf+4));
    tab_size = cp-out_buf;

    RansEncInit(&rans0);
    RansEncInit(&rans1);
    RansEncInit(&rans2);
    RansEncInit(&rans3);

    switch (i=(in_size&3)) {
    case 3: RansEncPutSymbol(&rans2, &ptr, &syms[in[in_size-(i-2)]]);
    case 2: RansEncPutSymbol(&rans1, &ptr, &syms[in[in_size-(i-1)]]);
    case 1: RansEncPutSymbol(&rans0, &ptr, &syms[in[in_size-(i-0)]]);
    case 0:
	break;
    }
    for (i=(in_size &~3); i>0; i-=4) {
	RansEncSymbol *s3 = &syms[in[i-1]];
	RansEncSymbol *s2 = &syms[in[i-2]];
	RansEncSymbol *s1 = &syms[in[i-3]];
	RansEncSymbol *s0 = &syms[in[i-4]];

	RansEncPutSymbol(&rans3, &ptr, s3);
	RansEncPutSymbol(&rans2, &ptr, s2);
	RansEncPutSymbol(&rans1, &ptr, s1);
	RansEncPutSymbol(&rans0, &ptr, s0);
    }

    RansEncFlush(&rans3, &ptr);
    RansEncFlush(&rans2, &ptr);
    RansEncFlush(&rans1, &ptr);
    RansEncFlush(&rans0, &ptr);

    // Finalise block size and return it
    *out_size = (out_end - ptr) + tab_size;

    cp = out_buf;

    *cp++ = 0; // order
    *cp++ = ((*out_size-9)>> 0) & 0xff;
    *cp++ = ((*out_size-9)>> 8) & 0xff;
    *cp++ = ((*out_size-9)>>16) & 0xff;
    *cp++ = ((*out_size-9)>>24) & 0xff;

    *cp++ = (in_size>> 0) & 0xff;
    *cp++ = (in_size>> 8) & 0xff;
    *cp++ = (in_size>>16) & 0xff;
    *cp++ = (in_size>>24) & 0xff;

    memmove(out_buf + tab_size, ptr, out_end-ptr);

    return out_buf;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1458, %rsp           # imm = 0x1458
movq %rdx, %r14
movq %rdi, 0x18(%rsp)
movq %rsi, 0x38(%rsp)
movl %esi, %ebx
cvtsi2sd %rbx, %xmm1
mulsd 0x15177(%rip), %xmm1    # 0x458f8
movsd 0x15b9f(%rip), %xmm0    # 0x46328
movsd %xmm1, 0x30(%rsp)
addsd %xmm1, %xmm0
addsd 0x15b95(%rip), %xmm0    # 0x46330
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x13169(%rip), %xmm0    # 0x43918
cvttsd2si %xmm0, %rdi
andq %rcx, %rdi
orq %rax, %rdi
callq 0x46f0
movq %rax, %rbp
leaq 0x50(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x42e0
testq %rbp, %rbp
je 0x30b7a
cvttsd2si 0x30(%rsp), %eax
cltq
addq %rbp, %rax
addq $0x3060c, %rax          # imm = 0x3060C
movq %rax, 0x40(%rsp)
movq %rax, 0x20(%rsp)
xorl %eax, %eax
cmpq %rax, %rbx
je 0x30810
movq 0x18(%rsp), %rcx
movzbl (%rcx,%rax), %ecx
incl 0x50(%rsp,%rcx,4)
incq %rax
jmp 0x307f9
xorl %esi, %esi
movabsq $0x80000000000, %rax    # imm = 0x80000000000
xorl %edx, %edx
divq %rbx
movq %rax, %rcx
movl $0x40000000, %eax       # imm = 0x40000000
xorl %edx, %edx
divl 0x38(%rsp)
addq %rcx, %rax
pushq $0x1
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %edi, %edi
cmpq $0x100, %rsi            # imm = 0x100
je 0x30879
movslq 0x50(%rsp,%rsi,4), %r9
testq %r9, %r9
je 0x30874
cmpl %r9d, %r8d
cmovlel %r9d, %r8d
cmovll %esi, %edi
imulq %rax, %r9
shrq $0x1f, %r9
cmpl $0x2, %r9d
cmovbl %edx, %r9d
movl %r9d, 0x50(%rsp,%rsi,4)
addl %ecx, %r9d
movl %r9d, %ecx
incq %rsi
jmp 0x3083c
movq %r14, 0x48(%rsp)
movslq %edi, %rax
movl 0x50(%rsp,%rax,4), %edx
subl %ecx, %edx
addl $0xfff, %edx            # imm = 0xFFF
movl %edx, 0x50(%rsp,%rax,4)
testl %edx, %edx
jle 0x30b8f
movq %rbp, 0x30(%rsp)
leaq 0x9(%rbp), %rbx
xorl %r14d, %r14d
pushq $0x1
popq %r15
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpq $0x100, %r14            # imm = 0x100
je 0x30973
cmpl $0x0, 0x50(%rsp,%r14,4)
je 0x30922
testl %r13d, %r13d
je 0x3092a
decl %r13d
movq %rbx, %r12
movl 0x50(%rsp,%r14,4), %eax
cmpl $0x80, %eax
jge 0x308e2
pushq $0x1
popq %rdx
movq %r12, %rcx
jmp 0x308f8
shrl $0x8, %eax
orb $-0x80, %al
leaq 0x1(%r12), %rcx
movb %al, (%r12)
movl 0x50(%rsp,%r14,4), %eax
pushq $0x2
popq %rdx
addq %rdx, %r12
movb %al, (%rcx)
movq %r14, %rax
shlq $0x4, %rax
leaq (%rsp,%rax), %rdi
addq $0x450, %rdi            # imm = 0x450
movl 0x50(%rsp,%r14,4), %ebx
movl %ebp, %esi
movl %ebx, %edx
callq 0x30bac
addl %ebx, %ebp
movq %r12, %rbx
incq %r14
incq %r15
jmp 0x308ae
leaq 0x1(%rbx), %r12
movb %r14b, (%rbx)
xorl %r13d, %r13d
testq %r14, %r14
je 0x308ce
cmpl $0x0, 0x4c(%rsp,%r14,4)
je 0x308ce
movq %r15, %rax
cmpq $0x100, %rax            # imm = 0x100
je 0x30958
cmpl $0x0, 0x50(%rsp,%rax,4)
je 0x3095d
incq %rax
jmp 0x30944
movl $0x100, %eax            # imm = 0x100
movl %r14d, %r13d
notl %r13d
addl %eax, %r13d
movb %r13b, 0x1(%rbx)
addq $0x2, %rbx
jmp 0x308cb
movb $0x0, (%rbx)
subq 0x30(%rsp), %rbx
movl $0x800000, %eax         # imm = 0x800000
movl %eax, 0x14(%rsp)
movl %eax, 0x10(%rsp)
movl %eax, 0xc(%rsp)
movl %eax, 0x2c(%rsp)
movq 0x38(%rsp), %r15
movl %r15d, %r14d
andl $0x3, %r14d
leaq 0x1599d(%rip), %rax     # 0x46340
movslq (%rax,%r14,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl %r15d, %r14d
andl $-0x4, %r14d
jmp 0x30a3e
leal -0x1(%r15), %eax
movq 0x18(%rsp), %rcx
movzbl (%rcx,%rax), %eax
shll $0x4, %eax
leaq (%rsp,%rax), %rdx
addq $0x450, %rdx            # imm = 0x450
leaq 0xc(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x30c69
movl %r15d, %eax
subl %r14d, %eax
incl %eax
movq 0x18(%rsp), %rcx
movzbl (%rcx,%rax), %eax
shll $0x4, %eax
leaq (%rsp,%rax), %rdx
addq $0x450, %rdx            # imm = 0x450
leaq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x30c69
movl %r15d, %r14d
andl $-0x4, %r14d
movq 0x18(%rsp), %rax
movzbl (%rax,%r14), %eax
shll $0x4, %eax
leaq (%rsp,%rax), %rdx
addq $0x450, %rdx            # imm = 0x450
leaq 0x14(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x30c69
incq %rbx
movslq %r14d, %r12
leaq 0x20(%rsp), %rbp
testq %r12, %r12
jle 0x30aed
movq 0x18(%rsp), %rcx
movzbl -0x1(%rcx,%r12), %eax
shll $0x4, %eax
leaq (%rsp,%rax), %rdx
addq $0x450, %rdx            # imm = 0x450
movzbl -0x2(%rcx,%r12), %eax
shll $0x4, %eax
leaq (%rsp,%rax), %r13
addq $0x450, %r13            # imm = 0x450
movzbl -0x3(%rcx,%r12), %eax
shll $0x4, %eax
leaq (%rsp,%rax), %r14
addq $0x450, %r14            # imm = 0x450
movzbl -0x4(%rcx,%r12), %eax
addq $-0x4, %r12
shll $0x4, %eax
leaq (%rsp,%rax), %r15
addq $0x450, %r15            # imm = 0x450
leaq 0x2c(%rsp), %rdi
movq %rbp, %rsi
callq 0x30c69
leaq 0xc(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x30c69
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x30c69
leaq 0x14(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x30c69
jmp 0x30a49
movl 0x2c(%rsp), %eax
movq 0x20(%rsp), %rsi
movl %eax, -0x4(%rsi)
movl 0xc(%rsp), %eax
movl %eax, -0x8(%rsi)
movl 0x10(%rsp), %eax
movl %eax, -0xc(%rsi)
movl 0x14(%rsp), %eax
movl %eax, -0x10(%rsi)
addq $-0x10, %rsi
movq 0x40(%rsp), %rdx
subq %rsi, %rdx
movslq %ebx, %rax
addq %rdx, %rbx
movq 0x48(%rsp), %rcx
movl %ebx, (%rcx)
movq 0x30(%rsp), %rbp
movb $0x0, (%rbp)
movl %ebx, %ecx
addb $-0x9, %cl
movb %cl, 0x1(%rbp)
leal 0xfff7(%rbx), %ecx
movb %ch, 0x2(%rbp)
leal 0xfffff7(%rbx), %ecx
shrl $0x10, %ecx
movb %cl, 0x3(%rbp)
leal -0x9(%rbx), %ecx
shrl $0x18, %ecx
movb %cl, 0x4(%rbp)
movq 0x38(%rsp), %rbx
movb %bl, 0x5(%rbp)
movb %bh, 0x6(%rbp)
movl %ebx, %ecx
shrl $0x10, %ecx
movb %cl, 0x7(%rbp)
shrl $0x18, %ebx
movb %bl, 0x8(%rbp)
movq %rbp, %rdi
addq %rax, %rdi
callq 0x48b0
movq %rbp, %rax
addq $0x1458, %rsp           # imm = 0x1458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x157ca(%rip), %rdi     # 0x46360
leaq 0x157ca(%rip), %rsi     # 0x46367
leaq 0x157d6(%rip), %rcx     # 0x4637a
pushq $0x6f
popq %rdx
callq 0x4360