// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/htscodecs/htscodecs/arith_dynamic.c
// Function: arith_compress_O0_RLE
static
unsigned char *arith_compress_O0_RLE(unsigned char *in, unsigned int in_size,
                                     unsigned char *out, unsigned int *out_size) {
    int i, bound = arith_compress_bound(in_size,0)-5; // -5 for order/size
    unsigned char *out_free = NULL;

    if (!out) {
        *out_size = bound;
        out_free = out = malloc(*out_size);
    }
    if (!out || bound > *out_size)
        return NULL;

    unsigned int m = 0;
    for (i = 0; i < in_size; i++)
        if (m < in[i])
            m = in[i];
    m++;
    *out = m;

    SIMPLE_MODEL(256,_) byte_model;
    SIMPLE_MODEL(256,_init)(&byte_model, m);

    SIMPLE_MODEL(NSYM,_) *run_model =
        htscodecs_tls_alloc(NSYM * sizeof(*run_model));
    if (!run_model) {
        free(out_free);
        return NULL;
    }

    for (i = 0; i < NSYM; i++)
        SIMPLE_MODEL(NSYM,_init)(&run_model[i], MAX_RUN);

    RangeCoder rc;
    RC_SetOutput(&rc, (char *)out+1);
    RC_SetOutputEnd(&rc, (char *)out + *out_size);
    RC_StartEncode(&rc);

    unsigned char last = 0;
    for (i = 0; i < in_size;) {
        //SIMPLE_MODEL(256, _encodeSymbol)(&byte_model, &rc, in[i]);
        SIMPLE_MODEL(256, _encodeSymbol)(&byte_model, &rc, in[i]);
        //fprintf(stderr, "lit %c (ctx %c)\n", in[i], last);
        int run = 0;
        last = in[i++];
        while (i < in_size && in[i] == last/* && run < MAX_RUN-1*/)
            run++, i++;
        int rctx = last;
        do {
            int c = run < MAX_RUN ? run : MAX_RUN-1;
            SIMPLE_MODEL(NSYM, _encodeSymbol)(&run_model[rctx], &rc, c);
            run -= c;

            if (rctx == last)
                rctx = 256;
            else
                rctx += (rctx < NSYM-1);
            if (c == MAX_RUN-1 && run == 0)
                SIMPLE_MODEL(NSYM, _encodeSymbol)(&run_model[rctx], &rc, 0);
        } while (run);
    }

    if (RC_FinishEncode(&rc) < 0) {
        htscodecs_tls_free(run_model);
        free(out_free);
        return NULL;
    }

    // Finalise block size and return it
    *out_size = RC_OutSize(&rc)+1;

    //fprintf(stderr, "RLE %d to %d\n", in_size, *out_size);

    htscodecs_tls_free(run_model);
    return out;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x498, %rsp            # imm = 0x498
movq %rcx, %r14
movl %esi, %ebp
movq %rdi, %r15
movl %esi, %ebx
cvtsi2sd %rbx, %xmm0
mulsd 0x32b60(%rip), %xmm0    # 0x7cba8
addsd 0x337c0(%rip), %xmm0    # 0x7d810
addsd 0x32b18(%rip), %xmm0    # 0x7cb70
addsd 0x337b8(%rip), %xmm0    # 0x7d818
cvttsd2si %xmm0, %rcx
addl $-0x5, %ecx
testq %rdx, %rdx
je 0x4a079
xorl %eax, %eax
cmpl (%r14), %ecx
jbe 0x4a08f
jmp 0x4a268
movl %ecx, (%r14)
movl %ecx, %edi
callq 0x4680
movq %rax, %rdx
testq %rax, %rax
je 0x4a266
xorl %edi, %edi
xorl %esi, %esi
cmpq %rdi, %rbx
je 0x4a0a7
movzbl (%r15,%rdi), %ecx
cmpl %ecx, %esi
cmovbel %ecx, %esi
incq %rdi
jmp 0x4a093
movq %rax, 0x18(%rsp)
movq %r14, 0x20(%rsp)
incl %esi
movq %rdx, 0x28(%rsp)
movb %sil, (%rdx)
leaq 0x88(%rsp), %rdi
callq 0x4b089
movl $0x42030, %edi          # imm = 0x42030
callq 0x6412c
testq %rax, %rax
je 0x4a25c
movq %rax, %r13
xorl %ebx, %ebx
cmpq $0x42030, %rbx          # imm = 0x42030
je 0x4a0fb
leaq (%rbx,%r13), %rdi
callq 0x4b0da
addq $0x418, %rbx            # imm = 0x418
jmp 0x4a0e0
movq 0x28(%rsp), %rcx
leaq 0x1(%rcx), %rax
leaq 0x40(%rsp), %rdx
movq %rax, 0x20(%rdx)
movq %rax, 0x18(%rdx)
movq 0x20(%rsp), %rax
movl (%rax), %eax
addq %rcx, %rax
movq %rax, 0x30(%rdx)
andq $0x0, 0x10(%rdx)
movapd 0x2d5a4(%rip), %xmm0    # 0x776d0
movapd %xmm0, (%rdx)
andl $0x0, 0x38(%rdx)
leaq 0x1(%r15), %rax
movq %rax, 0x38(%rsp)
xorl %ebx, %ebx
movl %ebp, 0xc(%rsp)
movq %r15, 0x30(%rsp)
cmpl %ebp, %ebx
jae 0x4a226
movslq %ebx, %r14
movzbl (%r15,%r14), %edx
leaq 0x88(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x4b130
movzbl (%r15,%r14), %r12d
leal 0x1(%rbx), %eax
addq 0x38(%rsp), %r14
xorl %r15d, %r15d
leal (%rax,%r15), %ecx
cmpl %ebp, %ecx
jae 0x4a18d
cmpb %r12b, (%r14,%r15)
jne 0x4a196
incq %r15
jmp 0x4a17a
movl %ebp, %eax
movq %rax, 0x10(%rsp)
jmp 0x4a1a0
addl %r15d, %ebx
incl %ebx
movq %rbx, 0x10(%rsp)
movl %r12d, %eax
movslq %eax, %r14
imulq $0x418, %r14, %rdi      # imm = 0x418
addq %r13, %rdi
cmpl $0x3, %r15d
pushq $0x3
popq %rbx
cmovll %r15d, %ebx
movzwl %bx, %edx
setge %bpl
leaq 0x40(%rsp), %rsi
callq 0x4b1ab
xorl %eax, %eax
cmpl $0x101, %r14d           # imm = 0x101
setl %al
addl %r14d, %eax
cmpl %r12d, %r14d
movl $0x100, %ecx            # imm = 0x100
cmovel %ecx, %eax
subl %ebx, %r15d
sete %cl
andb %bpl, %cl
cmpb $0x1, %cl
je 0x4a1fb
testl %r15d, %r15d
jne 0x4a1a3
jmp 0x4a213
cltq
imulq $0x418, %rax, %rdi      # imm = 0x418
addq %r13, %rdi
leaq 0x40(%rsp), %rsi
xorl %edx, %edx
callq 0x4b1ab
movl 0xc(%rsp), %ebp
movq 0x30(%rsp), %r15
movq 0x10(%rsp), %rbx
jmp 0x4a148
leaq 0x40(%rsp), %rdi
callq 0x4b226
testl %eax, %eax
js 0x4a254
movl 0x60(%rsp), %eax
subl 0x58(%rsp), %eax
incl %eax
movq 0x20(%rsp), %rcx
movl %eax, (%rcx)
movq %r13, %rdi
callq 0x64291
movq 0x28(%rsp), %rax
jmp 0x4a268
movq %r13, %rdi
callq 0x64291
movq 0x18(%rsp), %rdi
callq 0x45c0
xorl %eax, %eax
addq $0x498, %rsp            # imm = 0x498
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq