// File: 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
movq %rsp, %rbp
subq $0x14c0, %rsp           # imm = 0x14C0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl -0x14(%rbp), %eax
cvtsi2sd %rax, %xmm0
movsd 0x24c6a(%rip), %xmm1    # 0x7cca0
mulsd %xmm1, %xmm0
movsd 0x256b6(%rip), %xmm1    # 0x7d6f8
addsd %xmm1, %xmm0
movsd 0x256b2(%rip), %xmm1    # 0x7d700
addsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rdi
movq %rdi, %rcx
sarq $0x3f, %rcx
movsd 0x22a57(%rip), %xmm1    # 0x7aac0
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
andq %rcx, %rax
orq %rax, %rdi
callq 0x47a0
movq %rax, -0x28(%rbp)
leaq -0x1460(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x42d0
movl $0x0, -0x1478(%rbp)
movl $0x0, -0x147c(%rbp)
movl $0x0, -0x1480(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x580c6
movq $0x0, -0x8(%rbp)
jmp 0x58954
movq -0x28(%rbp), %rax
movl -0x14(%rbp), %ecx
cvtsi2sd %rcx, %xmm1
movsd 0x24bc6(%rip), %xmm0    # 0x7cca0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
addq $0x30603, %rax          # imm = 0x30603
addq $0x9, %rax
movq %rax, -0x38(%rbp)
movq %rax, -0x1058(%rbp)
movl $0x0, -0x1464(%rbp)
movl -0x1464(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x58143
movq -0x10(%rbp), %rax
movslq -0x1464(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl -0x1460(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x1460(%rbp,%rax,4)
movl -0x1464(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1464(%rbp)
jmp 0x58107
movl -0x14(%rbp), %eax
movl %eax, %ecx
movabsq $0x80000000000, %rax    # imm = 0x80000000000
xorl %edx, %edx
divq %rcx
movq %rax, -0x14b0(%rbp)
movl $0x40000000, %eax       # imm = 0x40000000
xorl %edx, %edx
divl -0x14(%rbp)
movl %eax, %ecx
movq -0x14b0(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x1488(%rbp)
movl $0x0, -0x1468(%rbp)
movl $0x0, -0x1480(%rbp)
movl $0x0, -0x147c(%rbp)
cmpl $0x100, -0x1468(%rbp)   # imm = 0x100
jge 0x58265
movslq -0x1468(%rbp), %rax
cmpl $0x0, -0x1460(%rbp,%rax,4)
jne 0x581c1
jmp 0x58251
movl -0x147c(%rbp), %eax
movslq -0x1468(%rbp), %rcx
cmpl -0x1460(%rbp,%rcx,4), %eax
jge 0x581f7
movslq -0x1468(%rbp), %rax
movl -0x1460(%rbp,%rax,4), %eax
movl %eax, -0x147c(%rbp)
movl -0x1468(%rbp), %eax
movl %eax, -0x1480(%rbp)
movslq -0x1468(%rbp), %rax
movslq -0x1460(%rbp,%rax,4), %rax
imulq -0x1488(%rbp), %rax
shrq $0x1f, %rax
movslq -0x1468(%rbp), %rcx
movl %eax, -0x1460(%rbp,%rcx,4)
cmpl $0x0, %eax
jne 0x58237
movslq -0x1468(%rbp), %rax
movl $0x1, -0x1460(%rbp,%rax,4)
movslq -0x1468(%rbp), %rax
movl -0x1460(%rbp,%rax,4), %eax
addl -0x1478(%rbp), %eax
movl %eax, -0x1478(%rbp)
movl -0x1468(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1468(%rbp)
jmp 0x5819b
movl -0x1478(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1478(%rbp)
cmpl $0x1000, -0x1478(%rbp)  # imm = 0x1000
jge 0x582a2
movl $0x1000, %ecx           # imm = 0x1000
subl -0x1478(%rbp), %ecx
movslq -0x1480(%rbp), %rax
addl -0x1460(%rbp,%rax,4), %ecx
movl %ecx, -0x1460(%rbp,%rax,4)
jmp 0x582c5
movl -0x1478(%rbp), %edx
subl $0x1000, %edx           # imm = 0x1000
movslq -0x1480(%rbp), %rax
movl -0x1460(%rbp,%rax,4), %ecx
subl %edx, %ecx
movl %ecx, -0x1460(%rbp,%rax,4)
movslq -0x1480(%rbp), %rax
cmpl $0x0, -0x1460(%rbp,%rax,4)
jle 0x582d8
jmp 0x582f7
leaq 0x25451(%rip), %rdi     # 0x7d730
leaq 0x25451(%rip), %rsi     # 0x7d737
movl $0x6f, %edx
leaq 0x25458(%rip), %rcx     # 0x7d74a
callq 0x4360
movq -0x28(%rbp), %rax
addq $0x9, %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x1468(%rbp)
movl $0x0, -0x1470(%rbp)
movl $0x0, -0x1474(%rbp)
cmpl $0x100, -0x1468(%rbp)   # imm = 0x100
jge 0x58524
movslq -0x1468(%rbp), %rax
cmpl $0x0, -0x1460(%rbp,%rax,4)
je 0x5850e
cmpl $0x0, -0x1470(%rbp)
je 0x58363
movl -0x1470(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1470(%rbp)
jmp 0x5843b
movl -0x1468(%rbp), %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
cmpl $0x0, -0x1470(%rbp)
jne 0x58439
cmpl $0x0, -0x1468(%rbp)
je 0x58439
movl -0x1468(%rbp), %eax
subl $0x1, %eax
cltq
cmpl $0x0, -0x1460(%rbp,%rax,4)
je 0x58439
movl -0x1468(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1470(%rbp)
xorl %eax, %eax
cmpl $0x100, -0x1470(%rbp)   # imm = 0x100
movb %al, -0x14b1(%rbp)
jge 0x583ea
movslq -0x1470(%rbp), %rax
cmpl $0x0, -0x1460(%rbp,%rax,4)
setne %al
movb %al, -0x14b1(%rbp)
movb -0x14b1(%rbp), %al
testb $0x1, %al
jne 0x583f6
jmp 0x58409
jmp 0x583f8
movl -0x1470(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1470(%rbp)
jmp 0x583be
movl -0x1468(%rbp), %ecx
addl $0x1, %ecx
movl -0x1470(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1470(%rbp)
movl -0x1470(%rbp), %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
jmp 0x5843b
movslq -0x1468(%rbp), %rax
cmpl $0x80, -0x1460(%rbp,%rax,4)
jge 0x58472
movslq -0x1468(%rbp), %rax
movl -0x1460(%rbp,%rax,4), %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
jmp 0x584c1
movslq -0x1468(%rbp), %rax
movl -0x1460(%rbp,%rax,4), %eax
sarl $0x8, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movslq -0x1468(%rbp), %rax
movl -0x1460(%rbp,%rax,4), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movslq -0x1468(%rbp), %rax
leaq -0x1040(%rbp), %rdi
shlq $0x4, %rax
addq %rax, %rdi
movl -0x1474(%rbp), %esi
movslq -0x1468(%rbp), %rax
movl -0x1460(%rbp,%rax,4), %edx
movl $0xc, %ecx
callq 0x58970
movslq -0x1468(%rbp), %rax
movl -0x1460(%rbp,%rax,4), %eax
addl -0x1474(%rbp), %eax
movl %eax, -0x1474(%rbp)
jmp 0x58510
movl -0x1468(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1468(%rbp)
jmp 0x58321
movq -0x30(%rbp), %rax
movq %rax, %rcx
incq %rcx
movq %rcx, -0x30(%rbp)
movb $0x0, (%rax)
movl -0x30(%rbp), %eax
movl -0x28(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x146c(%rbp)
leaq -0x1044(%rbp), %rdi
callq 0x58b10
leaq -0x1048(%rbp), %rdi
callq 0x58b10
leaq -0x104c(%rbp), %rdi
callq 0x58b10
leaq -0x1050(%rbp), %rdi
callq 0x58b10
movl -0x14(%rbp), %eax
andl $0x3, %eax
movl %eax, -0x1464(%rbp)
movl %eax, %ecx
movq %rcx, -0x14c0(%rbp)
subl $0x3, %eax
ja 0x58655
movq -0x14c0(%rbp), %rax
leaq 0x25171(%rip), %rcx     # 0x7d710
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
movl -0x1464(%rbp), %edx
subl $0x2, %edx
subl %edx, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
leaq -0x1040(%rbp), %rdx
shlq $0x4, %rax
addq %rax, %rdx
leaq -0x104c(%rbp), %rdi
leaq -0x1058(%rbp), %rsi
callq 0x58b30
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
movl -0x1464(%rbp), %edx
subl $0x1, %edx
subl %edx, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
leaq -0x1040(%rbp), %rdx
shlq $0x4, %rax
addq %rax, %rdx
leaq -0x1048(%rbp), %rdi
leaq -0x1058(%rbp), %rsi
callq 0x58b30
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
movl -0x1464(%rbp), %edx
subl $0x0, %edx
subl %edx, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
leaq -0x1040(%rbp), %rdx
shlq $0x4, %rax
addq %rax, %rdx
leaq -0x1044(%rbp), %rdi
leaq -0x1058(%rbp), %rsi
callq 0x58b30
jmp 0x58655
movl -0x14(%rbp), %eax
andl $-0x4, %eax
movl %eax, -0x1464(%rbp)
cmpl $0x0, -0x1464(%rbp)
jle 0x58796
movq -0x10(%rbp), %rax
movl -0x1464(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq -0x1040(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x1490(%rbp)
movq -0x10(%rbp), %rax
movl -0x1464(%rbp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq -0x1040(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x1498(%rbp)
movq -0x10(%rbp), %rax
movl -0x1464(%rbp), %ecx
subl $0x3, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq -0x1040(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x14a0(%rbp)
movq -0x10(%rbp), %rax
movl -0x1464(%rbp), %ecx
subl $0x4, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq -0x1040(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x14a8(%rbp)
movq -0x1490(%rbp), %rdx
leaq -0x1050(%rbp), %rdi
leaq -0x1058(%rbp), %rsi
callq 0x58b30
movq -0x1498(%rbp), %rdx
leaq -0x104c(%rbp), %rdi
leaq -0x1058(%rbp), %rsi
callq 0x58b30
movq -0x14a0(%rbp), %rdx
leaq -0x1048(%rbp), %rdi
leaq -0x1058(%rbp), %rsi
callq 0x58b30
movq -0x14a8(%rbp), %rdx
leaq -0x1044(%rbp), %rdi
leaq -0x1058(%rbp), %rsi
callq 0x58b30
movl -0x1464(%rbp), %eax
subl $0x4, %eax
movl %eax, -0x1464(%rbp)
jmp 0x58661
leaq -0x1050(%rbp), %rdi
leaq -0x1058(%rbp), %rsi
callq 0x58c10
leaq -0x104c(%rbp), %rdi
leaq -0x1058(%rbp), %rsi
callq 0x58c10
leaq -0x1048(%rbp), %rdi
leaq -0x1058(%rbp), %rsi
callq 0x58c10
leaq -0x1044(%rbp), %rdi
leaq -0x1058(%rbp), %rsi
callq 0x58c10
movq -0x38(%rbp), %rax
movq -0x1058(%rbp), %rcx
subq %rcx, %rax
movslq -0x146c(%rbp), %rcx
addq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movb $0x0, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %eax
subl $0x9, %eax
shrl $0x0, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %eax
subl $0x9, %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %eax
subl $0x9, %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %eax
subl $0x9, %eax
shrl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movl -0x14(%rbp), %eax
shrl $0x0, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movl -0x14(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movl -0x14(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movl -0x14(%rbp), %eax
shrl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x28(%rbp), %rdi
movslq -0x146c(%rbp), %rax
addq %rax, %rdi
movq -0x1058(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x1058(%rbp), %rax
subq %rax, %rdx
callq 0x4970
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x14c0, %rsp           # imm = 0x14C0
popq %rbp
retq
nopw %cs:(%rax,%rax)