// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/htscodecs/htscodecs/arith_dynamic.c
// Function: arith_compress_O1_RLE
static
unsigned char *arith_compress_O1_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 =
        htscodecs_tls_alloc(256 * sizeof(*byte_model));
    if (!byte_model) {
        free(out_free);
        return NULL;
    }
    for (i = 0; i < 256; i++)
        SIMPLE_MODEL(256,_init)(&byte_model[i], m);

    SIMPLE_MODEL(NSYM,_) *run_model =
        htscodecs_tls_alloc(NSYM * sizeof(*run_model));
    if (!run_model) {
        htscodecs_tls_free(byte_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[last], &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(byte_model);
        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(byte_model);
    htscodecs_tls_free(run_model);
    return out;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl -0x14(%rbp), %edi
xorl %esi, %esi
callq 0x826c0
subl $0x5, %eax
movl %eax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x83a47
movl -0x30(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rax
movl (%rax), %eax
movl %eax, %edi
callq 0x4650
movq %rax, -0x20(%rbp)
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x83a59
movl -0x30(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
jbe 0x83a66
movq $0x0, -0x8(%rbp)
jmp 0x83ded
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x83aab
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jae 0x83a9e
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x3c(%rbp)
jmp 0x83aa0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x83a74
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movb %cl, (%rax)
movl $0x41000, %edi          # imm = 0x41000
callq 0xce6a0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x83aea
movq -0x38(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
jmp 0x83ded
movl $0x0, -0x2c(%rbp)
cmpl $0x100, -0x2c(%rbp)     # imm = 0x100
jge 0x83b1f
movq -0x48(%rbp), %rdi
movslq -0x2c(%rbp), %rax
imulq $0x410, %rax, %rax      # imm = 0x410
addq %rax, %rdi
movl -0x3c(%rbp), %esi
callq 0x859d0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x83af1
movl $0x42030, %edi          # imm = 0x42030
callq 0xce6a0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x83b53
movq -0x48(%rbp), %rdi
callq 0xce8f0
movq -0x38(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
jmp 0x83ded
movl $0x0, -0x2c(%rbp)
cmpl $0x102, -0x2c(%rbp)     # imm = 0x102
jge 0x83b8a
movq -0x50(%rbp), %rdi
movslq -0x2c(%rbp), %rax
imulq $0x418, %rax, %rax      # imm = 0x418
addq %rax, %rdi
movl $0x4, %esi
callq 0x85aa0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x83b5a
movq -0x20(%rbp), %rsi
addq $0x1, %rsi
leaq -0x90(%rbp), %rdi
callq 0x85b70
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rax
movl (%rax), %eax
addq %rax, %rsi
leaq -0x90(%rbp), %rdi
callq 0x85ba0
leaq -0x90(%rbp), %rdi
callq 0x85bc0
movb $0x0, -0x91(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x83d85
movq -0x48(%rbp), %rdi
movzbl -0x91(%rbp), %eax
imulq $0x410, %rax, %rax      # imm = 0x410
addq %rax, %rdi
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
leaq -0x90(%rbp), %rsi
movzwl %ax, %edx
callq 0x85c20
movl $0x0, -0x98(%rbp)
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x91(%rbp)
movl -0x2c(%rbp), %ecx
xorl %eax, %eax
cmpl -0x14(%rbp), %ecx
movb %al, -0xa1(%rbp)
jae 0x83c60
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movzbl -0x91(%rbp), %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0xa1(%rbp)
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0x83c6c
jmp 0x83c86
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x83c32
movzbl -0x91(%rbp), %eax
movl %eax, -0x9c(%rbp)
cmpl $0x4, -0x98(%rbp)
jge 0x83caa
movl -0x98(%rbp), %eax
movl %eax, -0xa8(%rbp)
jmp 0x83cb7
movl $0x3, %eax
movl %eax, -0xa8(%rbp)
jmp 0x83cb7
movl -0xa8(%rbp), %eax
movl %eax, -0xa0(%rbp)
movq -0x50(%rbp), %rdi
movslq -0x9c(%rbp), %rax
imulq $0x418, %rax, %rax      # imm = 0x418
addq %rax, %rdi
movl -0xa0(%rbp), %eax
leaq -0x90(%rbp), %rsi
movzwl %ax, %edx
callq 0x85d00
movl -0xa0(%rbp), %ecx
movl -0x98(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x98(%rbp)
movl -0x9c(%rbp), %eax
movzbl -0x91(%rbp), %ecx
cmpl %ecx, %eax
jne 0x83d1e
movl $0x100, -0x9c(%rbp)     # imm = 0x100
jmp 0x83d3c
cmpl $0x101, -0x9c(%rbp)     # imm = 0x101
setl %al
andb $0x1, %al
movzbl %al, %eax
addl -0x9c(%rbp), %eax
movl %eax, -0x9c(%rbp)
cmpl $0x3, -0xa0(%rbp)
jne 0x83d71
cmpl $0x0, -0x98(%rbp)
jne 0x83d71
movq -0x50(%rbp), %rdi
movslq -0x9c(%rbp), %rax
imulq $0x418, %rax, %rax      # imm = 0x418
addq %rax, %rdi
leaq -0x90(%rbp), %rsi
xorl %edx, %edx
callq 0x85d00
jmp 0x83d73
cmpl $0x0, -0x98(%rbp)
jne 0x83c93
jmp 0x83bd1
leaq -0x90(%rbp), %rdi
callq 0x85de0
cmpl $0x0, %eax
jge 0x83dbb
movq -0x48(%rbp), %rdi
callq 0xce8f0
movq -0x50(%rbp), %rdi
callq 0xce8f0
movq -0x38(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
jmp 0x83ded
leaq -0x90(%rbp), %rdi
callq 0x85e20
addq $0x1, %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x48(%rbp), %rdi
callq 0xce8f0
movq -0x50(%rbp), %rdi
callq 0xce8f0
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)