// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, 0x50(%rsp)
movl %esi, %ebx
cvtsi2sd %rbx, %xmm0
mulsd 0x419ef(%rip), %xmm0    # 0x9dbb0
addsd 0x4264f(%rip), %xmm0    # 0x9e818
addsd 0x419a7(%rip), %xmm0    # 0x9db78
addsd 0x42647(%rip), %xmm0    # 0x9e820
cvttsd2si %xmm0, %r14
addl $-0x5, %r14d
testq %rdx, %rdx
movl %esi, 0x10(%rsp)
je 0x5c1f5
movq %rdx, %r15
movq %rcx, %r12
xorl %eax, %eax
jmp 0x5c20a
movq %rcx, %r12
movl %r14d, (%rcx)
movl %r14d, %edi
callq 0x4680
movl 0x10(%rsp), %esi
movq %rax, %r15
testq %r15, %r15
je 0x5ca8b
cmpl (%r12), %r14d
ja 0x5ca8b
movq %r12, 0x68(%rsp)
testl %esi, %esi
movq %rax, 0x40(%rsp)
je 0x5c24b
xorl %edx, %edx
xorl %ebp, %ebp
movq 0x50(%rsp), %rsi
movl %ebp, %ecx
movzbl (%rsi,%rdx), %ebp
cmpl %ebp, %ecx
cmoval %ecx, %ebp
incq %rdx
cmpq %rdx, %rbx
jne 0x5c234
incl %ebp
jmp 0x5c250
movl $0x1, %ebp
movb %bpl, (%r15)
movl $0x41000, %edi          # imm = 0x41000
callq 0x8060c
testq %rax, %rax
je 0x5ca81
movq %rax, %r14
movl %ebp, %eax
leaq 0xa(%r14), %rcx
leaq 0xe(%r14), %rdx
xorl %esi, %esi
movl $0xffef, %edi           # imm = 0xFFEF
movl $0x410, %r8d            # imm = 0x410
imulq $0x410, %rsi, %r9       # imm = 0x410
addq %r14, %r9
movq %rdx, %rbx
xorl %r10d, %r10d
movq %rbx, %r11
movw %r10w, (%rcx,%r10,4)
movw $0x1, -0x2(%rcx,%r10,4)
incq %r10
addq $0x4, %rbx
cmpq %r10, %rax
jne 0x5c290
leaq -0x1(%r10), %rbx
cmpq $0xfe, %rbx
ja 0x5c2d4
movw %r10w, (%r11)
movw $0x0, -0x2(%r11)
incq %r10
addq $0x4, %r11
cmpq $0x100, %r10            # imm = 0x100
jne 0x5c2b9
movl %eax, (%r9)
movl %edi, 0x4(%r9)
movl %edi, 0x40c(%r9)
movw $0x0, 0x408(%r9)
incq %rsi
addq %r8, %rcx
addq %r8, %rdx
cmpq $0x100, %rsi            # imm = 0x100
jne 0x5c280
movl $0x42030, %edi          # imm = 0x42030
callq 0x8060c
testq %rax, %rax
je 0x5c9aa
movq %rax, %rbx
addq $0xa, %rax
leaq 0x1a(%rbx), %rcx
xorl %edx, %edx
movabsq $0xffef00000004, %rsi   # imm = 0xFFEF00000004
movl $0x418, %edi            # imm = 0x418
imulq $0x418, %rdx, %r8       # imm = 0x418
addq %rbx, %r8
xorl %r9d, %r9d
movw %r9w, (%rax,%r9,4)
movw $0x1, -0x2(%rax,%r9,4)
incq %r9
cmpq $0x4, %r9
jne 0x5c33a
xorl %r9d, %r9d
leal 0x4(%r9), %r10d
movw %r10w, (%rcx,%r9,4)
movw $0x0, -0x2(%rcx,%r9,4)
incq %r9
cmpq $0xfe, %r9
jne 0x5c353
movq %rsi, (%r8)
movl $0xffef, 0x414(%r8) # imm = 0xFFEF
movw $0x0, 0x410(%r8)
incq %rdx
addq %rdi, %rax
addq %rdi, %rcx
cmpq $0x102, %rdx            # imm = 0x102
jne 0x5c32d
leaq 0x1(%r15), %rdx
movq 0x68(%rsp), %rax
movl (%rax), %r10d
movq %r15, 0x80(%rsp)
addq %r15, %r10
movl 0x10(%rsp), %eax
testl %eax, %eax
movq %r14, 0x48(%rsp)
movq %rbx, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rdx, 0x78(%rsp)
je 0x5c9b2
leaq 0xc(%r14), %rcx
movq %rcx, 0x88(%rsp)
negl %eax
movq %rax, 0x90(%rsp)
leaq 0xc(%rbx), %rax
movq %rax, 0x70(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq $0x0, 0x58(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %r9d, %r9d
xorl %r11d, %r11d
xorl %esi, %esi
xorl %ecx, %ecx
movq %rdx, %r8
movzbl 0x58(%rsp), %edx
imulq $0x410, %rdx, %rbp      # imm = 0x410
movl %r12d, 0x14(%rsp)
movslq %r12d, %rdi
movq 0x50(%rsp), %rdx
movq %rdi, 0x8(%rsp)
movzbl (%rdx,%rdi), %edx
movq 0x48(%rsp), %rdi
leaq (%rdi,%rbp), %r14
addq $0x8, %r14
cmpw %dx, 0xa(%rdi,%rbp)
jne 0x5c44b
xorl %edi, %edi
jmp 0x5c46e
movq %r8, %r13
movl %r9d, %r12d
xorl %edi, %edi
movq %r14, %r8
movzwl (%r14), %r9d
addq $0x4, %r14
addl %r9d, %edi
cmpw %dx, 0x6(%r8)
jne 0x5c453
movl %r12d, %r9d
movq %r13, %r8
movq 0x48(%rsp), %rdx
leaq (%rdx,%rbp), %r12
movzwl (%r14), %r13d
xorl %edx, %edx
divl (%r12)
imull %eax, %edi
imull %eax, %r13d
addl %edi, %r15d
adcl $0x0, %ecx
cmpl $0xffffff, %r13d        # imm = 0xFFFFFF
movq %r12, 0x18(%rsp)
ja 0x5c536
cmpl $0xff000000, %r15d      # imm = 0xFF000000
jb 0x5c4b2
testl %ecx, %ecx
jne 0x5c4b2
incl %r11d
jmp 0x5c512
movl %r11d, %edx
movq %r10, %rax
subq %r8, %rax
cmpq %rdx, %rax
jle 0x5c518
addl %ecx, %esi
leaq 0x1(%r8), %rdi
movb %sil, (%r8)
testl %r11d, %r11d
je 0x5c507
decb %cl
movzbl %cl, %esi
movq %r11, %r12
movl %r9d, 0x4(%rsp)
movq %r8, 0x20(%rsp)
callq 0x4260
movl 0x4(%rsp), %r9d
movq 0x38(%rsp), %r10
decl %r12d
movq 0x20(%rsp), %rax
leaq (%rax,%r12), %rdi
addq $0x2, %rdi
movq 0x18(%rsp), %r12
xorl %r11d, %r11d
movl %r15d, %esi
shrl $0x18, %esi
xorl %ecx, %ecx
movq %rdi, %r8
shll $0x8, %r15d
jmp 0x5c51e
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %r13d, %eax
shll $0x8, %eax
cmpl $0x10000, %r13d         # imm = 0x10000
movl %eax, %r13d
jb 0x5c4a0
jmp 0x5c539
movl %r13d, %eax
addw $0x10, (%r14)
movl (%r12), %edx
addl $0x10, %edx
movl %edx, (%r12)
cmpl $0xfff0, %edx           # imm = 0xFFF0
movl 0x14(%rsp), %r13d
jb 0x5c5a0
movl $0x0, (%r12)
movzwl 0x8(%r12), %edx
testw %dx, %dx
je 0x5c5a0
movq %r8, %r12
addq 0x88(%rsp), %rbp
xorl %edi, %edi
movzwl %dx, %r8d
shrl %r8d
subl %r8d, %edx
movw %dx, -0x4(%rbp)
movzwl %dx, %edx
addl %edx, %edi
movzwl (%rbp), %edx
addq $0x4, %rbp
testw %dx, %dx
jne 0x5c576
movq 0x18(%rsp), %rdx
movl %edi, (%rdx)
movq %r12, %r8
movzwl (%r14), %edx
cmpw -0x4(%r14), %dx
jbe 0x5c5b0
rolq $0x20, -0x4(%r14)
movq 0x50(%rsp), %rdi
movq 0x8(%rsp), %rbp
movb (%rdi,%rbp), %r12b
incq %rbp
movl 0x10(%rsp), %edx
cmpl %edx, %ebp
movq %r12, 0x58(%rsp)
jae 0x5c60d
movq %r8, %r14
notl %r13d
addl %edx, %r13d
movq %rbp, %rdx
addq %rdi, %rdx
movq %rbp, 0x8(%rsp)
movl %ebp, %edi
addq 0x90(%rsp), %rdi
xorl %r8d, %r8d
cmpb %r12b, (%rdx,%r8)
jne 0x5c615
incq %r8
movl %r9d, %ebp
movl %edi, %r9d
addl %r8d, %r9d
movl %ebp, %r9d
jne 0x5c5ef
movl 0x10(%rsp), %r12d
jmp 0x5c621
movl %ebp, %r12d
xorl %r13d, %r13d
jmp 0x5c624
movq 0x8(%rsp), %rdx
leal (%rdx,%r8), %r12d
movl %r8d, %r13d
movq %r14, %r8
movzbl 0x58(%rsp), %edx
movl %edx, 0x64(%rsp)
movl %edx, %ebp
movl %r12d, 0x14(%rsp)
cmpl $0x3, %r13d
movl $0x3, %r14d
movl %r13d, 0x4(%rsp)
cmovll %r13d, %r14d
movl %ebp, 0x18(%rsp)
movslq %ebp, %rdx
imulq $0x418, %rdx, %r13      # imm = 0x418
leaq (%rbx,%r13), %rbp
addq $0x8, %rbp
xorl %r12d, %r12d
cmpw %r14w, 0xa(%rbx,%r13)
je 0x5c680
xorl %r12d, %r12d
movq %rbp, %rdx
movzwl (%rbp), %edi
addq $0x4, %rbp
addl %edi, %r12d
cmpw %r14w, 0x6(%rdx)
jne 0x5c66b
leaq (%rbx,%r13), %rdi
movzwl (%rbp), %ebx
xorl %edx, %edx
movq %rdi, 0x8(%rsp)
divl (%rdi)
imull %eax, %r12d
imull %eax, %ebx
addl %r15d, %r12d
adcl $0x0, %ecx
cmpl $0xffffff, %ebx         # imm = 0xFFFFFF
movl %r14d, 0x2c(%rsp)
ja 0x5c748
movq 0x8(%rsp), %r15
cmpl $0xff000000, %r12d      # imm = 0xFF000000
jb 0x5c6c6
testl %ecx, %ecx
jne 0x5c6c6
incl %r11d
jmp 0x5c727
movl %r11d, %edx
movq %r10, %rax
subq %r8, %rax
cmpq %rdx, %rax
jle 0x5c72d
addl %ecx, %esi
leaq 0x1(%r8), %rdi
movb %sil, (%r8)
testl %r11d, %r11d
je 0x5c71c
decb %cl
movzbl %cl, %esi
movq %r11, %r15
movl %r9d, %r14d
movq %r8, 0x20(%rsp)
callq 0x4260
movl %r14d, %r9d
movl 0x2c(%rsp), %r14d
movq 0x38(%rsp), %r10
decl %r15d
movq 0x20(%rsp), %rax
leaq (%rax,%r15), %rdi
addq $0x2, %rdi
movq 0x8(%rsp), %r15
xorl %r11d, %r11d
movl %r12d, %esi
shrl $0x18, %esi
xorl %ecx, %ecx
movq %rdi, %r8
shll $0x8, %r12d
jmp 0x5c733
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %ebx, %eax
shll $0x8, %eax
cmpl $0x10000, %ebx          # imm = 0x10000
movl %eax, %ebx
jb 0x5c6b4
jmp 0x5c74f
movl %ebx, %eax
movq 0x8(%rsp), %r15
addw $0x10, (%rbp)
movl (%r15), %edx
addl $0x10, %edx
movl %edx, (%r15)
cmpl $0xfff0, %edx           # imm = 0xFFF0
movq 0x30(%rsp), %rbx
jb 0x5c7b2
movl $0x0, (%r15)
movzwl 0x8(%r15), %edx
testw %dx, %dx
je 0x5c7b2
movq %r8, %r14
addq 0x70(%rsp), %r13
xorl %edi, %edi
movzwl %dx, %r8d
shrl %r8d
subl %r8d, %edx
movw %dx, -0x4(%r13)
movzwl %dx, %edx
addl %edx, %edi
movzwl (%r13), %edx
addq $0x4, %r13
testw %dx, %dx
jne 0x5c785
movl %edi, (%r15)
movq %r14, %r8
movl 0x2c(%rsp), %r14d
movzwl (%rbp), %edx
cmpw -0x4(%rbp), %dx
jbe 0x5c7c1
rolq $0x20, -0x4(%rbp)
xorl %edx, %edx
movl 0x18(%rsp), %edi
cmpl $0x101, %edi            # imm = 0x101
setl %dl
addl %edi, %edx
cmpl 0x64(%rsp), %edi
movl %edx, %ebp
movl $0x100, %edx            # imm = 0x100
cmovel %edx, %ebp
movl 0x4(%rsp), %r13d
cmpl $0x3, %r13d
setge %dl
subl %r14d, %r13d
sete %dil
andb %dl, %dil
cmpb $0x1, %dil
jne 0x5c835
movl %r13d, 0x4(%rsp)
movslq %ebp, %rdx
imulq $0x418, %rdx, %r14      # imm = 0x418
leaq (%rbx,%r14), %r13
addq $0x8, %r13
cmpw $0x0, 0xa(%rbx,%r14)
je 0x5c842
xorl %r15d, %r15d
movzwl (%r13), %edx
addl %edx, %r15d
cmpw $0x0, 0x6(%r13)
leaq 0x4(%r13), %r13
jne 0x5c81f
jmp 0x5c845
movl %r12d, %r15d
movl 0x14(%rsp), %r12d
jmp 0x5c994
xorl %r15d, %r15d
leaq (%rbx,%r14), %rdi
movzwl (%r13), %ebx
xorl %edx, %edx
movq %rdi, 0x8(%rsp)
divl (%rdi)
imull %eax, %r15d
imull %eax, %ebx
addl %r12d, %r15d
adcl $0x0, %ecx
cmpl $0xffffff, %ebx         # imm = 0xFFFFFF
ja 0x5c90c
movq 0x8(%rsp), %r12
movl %ebp, 0x18(%rsp)
cmpl $0xff000000, %r15d      # imm = 0xFF000000
jb 0x5c88b
testl %ecx, %ecx
jne 0x5c88b
incl %r11d
jmp 0x5c8eb
movl %r11d, %edx
movq %r10, %rax
subq %r8, %rax
cmpq %rdx, %rax
jle 0x5c8f1
addl %ecx, %esi
leaq 0x1(%r8), %rdi
movb %sil, (%r8)
testl %r11d, %r11d
je 0x5c8e0
decb %cl
movzbl %cl, %esi
movq %r11, %r12
movl %r9d, %ebp
movq %r8, 0x20(%rsp)
callq 0x4260
movl %ebp, %r9d
movl 0x18(%rsp), %ebp
movq 0x38(%rsp), %r10
decl %r12d
movq 0x20(%rsp), %rax
leaq (%rax,%r12), %rdi
addq $0x2, %rdi
movq 0x8(%rsp), %r12
xorl %r11d, %r11d
movl %r15d, %esi
shrl $0x18, %esi
xorl %ecx, %ecx
movq %rdi, %r8
shll $0x8, %r15d
jmp 0x5c8f7
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %ebx, %eax
shll $0x8, %eax
cmpl $0x10000, %ebx          # imm = 0x10000
movl %eax, %ebx
jb 0x5c879
jmp 0x5c913
movl %ebx, %eax
movq 0x8(%rsp), %r12
addw $0x10, (%r13)
movl (%r12), %edx
addl $0x10, %edx
movl %edx, (%r12)
cmpl $0xfff0, %edx           # imm = 0xFFF0
movq 0x30(%rsp), %rbx
jb 0x5c979
movl $0x0, (%r12)
movzwl 0x8(%r12), %edx
testw %dx, %dx
je 0x5c979
movq %r8, %r12
addq 0x70(%rsp), %r14
xorl %edi, %edi
movzwl %dx, %r8d
shrl %r8d
subl %r8d, %edx
movw %dx, -0x4(%r14)
movzwl %dx, %edx
addl %edx, %edi
movzwl (%r14), %edx
addq $0x4, %r14
testw %dx, %dx
jne 0x5c94e
movq 0x8(%rsp), %rdx
movl %edi, (%rdx)
movq %r12, %r8
movzwl (%r13), %edx
cmpw -0x4(%r13), %dx
jbe 0x5c98a
rolq $0x20, -0x4(%r13)
movl 0x14(%rsp), %r12d
movl 0x4(%rsp), %r13d
testl %r13d, %r13d
jne 0x5c634
cmpl 0x10(%rsp), %r12d
jb 0x5c411
jmp 0x5c9c2
movq %r14, %rdi
jmp 0x5ca7c
xorl %ecx, %ecx
movq %rdx, %r8
xorl %esi, %esi
xorl %r11d, %r11d
xorl %r9d, %r9d
xorl %r15d, %r15d
movl $0x5, %ebx
movq 0x48(%rsp), %r14
cmpl $0xff000000, %r15d      # imm = 0xFF000000
jb 0x5c9de
testl %ecx, %ecx
jne 0x5c9de
incl %r11d
jmp 0x5ca2e
movl %r11d, %edx
movq %r10, %rax
subq %r8, %rax
cmpq %rdx, %rax
jle 0x5ca34
addl %ecx, %esi
leaq 0x1(%r8), %rdi
movb %sil, (%r8)
testl %r11d, %r11d
je 0x5ca23
decb %cl
movzbl %cl, %esi
movq %r11, %r12
movl %r9d, %ebp
movq %r8, %r13
callq 0x4260
movl %ebp, %r9d
movq 0x38(%rsp), %r10
decl %r12d
leaq (%r12,%r13), %rdi
addq $0x2, %rdi
xorl %r11d, %r11d
movl %r15d, %esi
shrl $0x18, %esi
xorl %ecx, %ecx
movq %rdi, %r8
shll $0x8, %r15d
jmp 0x5ca3a
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
decl %ebx
jne 0x5c9cc
testl %r9d, %r9d
js 0x5ca6f
subl 0x78(%rsp), %r8d
incl %r8d
movq 0x68(%rsp), %rax
movl %r8d, (%rax)
movq %r14, %rdi
callq 0x80732
movq 0x30(%rsp), %rdi
callq 0x80732
movq 0x80(%rsp), %rax
jmp 0x5ca8d
movq %r14, %rdi
callq 0x80732
movq 0x30(%rsp), %rdi
callq 0x80732
movq 0x40(%rsp), %rdi
callq 0x45c0
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq