// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/cram/cram_codecs.c
// Function: cram_xrle_encode_flush
int cram_xrle_encode_flush(cram_codec *c) {
    uint8_t *out_lit, *out_len;
    uint64_t out_lit_size, out_len_size;
    uint8_t rle_syms[256];
    int rle_nsyms = 0, i;

    for (i = 0; i < 256; i++)
        if (c->u.e_xrle.rep_score[i] > 0)
            rle_syms[rle_nsyms++] = i;

    if (!c->u.e_xrle.to_flush) {
        c->u.e_xrle.to_flush = (char *)BLOCK_DATA(c->out);
        c->u.e_xrle.to_flush_size = BLOCK_SIZE(c->out);
    }

    out_len = malloc(c->u.e_xrle.to_flush_size+8);
    if (!out_len)
        return -1;

    int nb = var_put_u64(out_len, NULL, c->u.e_xrle.to_flush_size);

    out_lit = hts_rle_encode((uint8_t *)c->u.e_xrle.to_flush, c->u.e_xrle.to_flush_size,
                             out_len+nb, &out_len_size,
                             rle_syms, &rle_nsyms,
                             NULL, &out_lit_size);
    out_len_size += nb;


    // TODO: can maybe "gift" the sub codec the data block, to remove
    // one level of memcpy.
    if (c->u.e_xrle.len_codec->encode(NULL,
                                      c->u.e_xrle.len_codec,
                                      (char *)out_len, out_len_size))
        return -1;

    if (c->u.e_xrle.lit_codec->encode(NULL,
                                      c->u.e_xrle.lit_codec,
                                      (char *)out_lit, out_lit_size))
        return -1;

    free(out_len);
    free(out_lit);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, %rbx
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x100, %rax            # imm = 0x100
je 0x71ca2
cmpl $0x0, 0x90(%rbx,%rax,4)
jle 0x71c9d
movslq %ecx, %rdx
incl %ecx
movb %al, 0x20(%rsp,%rdx)
incq %rax
jmp 0x71c82
movl %ecx, 0xc(%rsp)
movq 0x490(%rbx), %r15
testq %r15, %r15
je 0x71cbb
movq 0x498(%rbx), %r12
jmp 0x71cd5
movq 0x8(%rbx), %rax
movq 0x20(%rax), %r15
movq %r15, 0x490(%rbx)
movq 0x30(%rax), %r12
movq %r12, 0x498(%rbx)
leaq 0x8(%r12), %rdi
callq 0x4680
movq %rax, %r14
pushq $-0x1
popq %rax
testq %r14, %r14
je 0x7200d
cmpq $0x7f, %r12
ja 0x71cfe
movb %r12b, (%r14)
pushq $0x1
jmp 0x71f8f
cmpq $0x3fff, %r12           # imm = 0x3FFF
ja 0x71d22
movl %r12d, %eax
shrl $0x7, %eax
orb $-0x80, %al
movb %al, (%r14)
movl %r12d, %eax
andb $0x7f, %al
movb %al, 0x1(%r14)
pushq $0x2
jmp 0x71f8f
cmpq $0x1fffff, %r12         # imm = 0x1FFFFF
ja 0x71d52
movl %r12d, %eax
shrl $0xe, %eax
orb $-0x80, %al
movb %al, (%r14)
movl %r12d, %eax
shrl $0x7, %eax
orb $-0x80, %al
movb %al, 0x1(%r14)
movl %r12d, %eax
andb $0x7f, %al
movb %al, 0x2(%r14)
pushq $0x3
jmp 0x71f8f
cmpq $0xfffffff, %r12        # imm = 0xFFFFFFF
ja 0x71d8e
movl %r12d, %eax
shrl $0x15, %eax
orb $-0x80, %al
movb %al, (%r14)
movl %r12d, %eax
shrl $0xe, %eax
orb $-0x80, %al
movb %al, 0x1(%r14)
movl %r12d, %eax
shrl $0x7, %eax
orb $-0x80, %al
movb %al, 0x2(%r14)
movl %r12d, %eax
andb $0x7f, %al
movb %al, 0x3(%r14)
pushq $0x4
jmp 0x71f8f
movq %r12, %rax
shrq $0x23, %rax
jne 0x71dd7
movq %r12, %rax
shrq $0x1c, %rax
orb $-0x80, %al
movb %al, (%r14)
movl %r12d, %eax
shrl $0x15, %eax
orb $-0x80, %al
movb %al, 0x1(%r14)
movl %r12d, %eax
shrl $0xe, %eax
orb $-0x80, %al
movb %al, 0x2(%r14)
movl %r12d, %eax
shrl $0x7, %eax
orb $-0x80, %al
movb %al, 0x3(%r14)
movl %r12d, %eax
andb $0x7f, %al
movb %al, 0x4(%r14)
pushq $0x5
jmp 0x71f8f
movq %r12, %rcx
shrq $0x2a, %rcx
jne 0x71e26
orb $-0x80, %al
movb %al, (%r14)
movq %r12, %rax
shrq $0x1c, %rax
orb $-0x80, %al
movb %al, 0x1(%r14)
movl %r12d, %eax
shrl $0x15, %eax
orb $-0x80, %al
movb %al, 0x2(%r14)
movl %r12d, %eax
shrl $0xe, %eax
orb $-0x80, %al
movb %al, 0x3(%r14)
movl %r12d, %eax
shrl $0x7, %eax
orb $-0x80, %al
movb %al, 0x4(%r14)
movl %r12d, %eax
andb $0x7f, %al
movb %al, 0x5(%r14)
pushq $0x6
jmp 0x71f8f
movq %r12, %rdx
shrq $0x31, %rdx
jne 0x71e7c
orb $-0x80, %cl
movb %cl, (%r14)
orb $-0x80, %al
movb %al, 0x1(%r14)
movq %r12, %rax
shrq $0x1c, %rax
orb $-0x80, %al
movb %al, 0x2(%r14)
movl %r12d, %eax
shrl $0x15, %eax
orb $-0x80, %al
movb %al, 0x3(%r14)
movl %r12d, %eax
shrl $0xe, %eax
orb $-0x80, %al
movb %al, 0x4(%r14)
movl %r12d, %eax
shrl $0x7, %eax
orb $-0x80, %al
movb %al, 0x5(%r14)
movl %r12d, %eax
andb $0x7f, %al
movb %al, 0x6(%r14)
pushq $0x7
jmp 0x71f8f
movq %r12, %rsi
shrq $0x38, %rsi
jne 0x71ed9
orb $-0x80, %dl
movb %dl, (%r14)
orb $-0x80, %cl
movb %cl, 0x1(%r14)
orb $-0x80, %al
movb %al, 0x2(%r14)
movq %r12, %rax
shrq $0x1c, %rax
orb $-0x80, %al
movb %al, 0x3(%r14)
movl %r12d, %eax
shrl $0x15, %eax
orb $-0x80, %al
movb %al, 0x4(%r14)
movl %r12d, %eax
shrl $0xe, %eax
orb $-0x80, %al
movb %al, 0x5(%r14)
movl %r12d, %eax
shrl $0x7, %eax
orb $-0x80, %al
movb %al, 0x6(%r14)
movl %r12d, %eax
andb $0x7f, %al
movb %al, 0x7(%r14)
pushq $0x8
jmp 0x71f8f
testq %r12, %r12
js 0x71f37
orb $-0x80, %sil
movb %sil, (%r14)
orb $-0x80, %dl
movb %dl, 0x1(%r14)
orb $-0x80, %cl
movb %cl, 0x2(%r14)
orb $-0x80, %al
movb %al, 0x3(%r14)
movq %r12, %rax
shrq $0x1c, %rax
orb $-0x80, %al
movb %al, 0x4(%r14)
movl %r12d, %eax
shrl $0x15, %eax
orb $-0x80, %al
movb %al, 0x5(%r14)
movl %r12d, %eax
shrl $0xe, %eax
orb $-0x80, %al
movb %al, 0x6(%r14)
movl %r12d, %eax
shrl $0x7, %eax
orb $-0x80, %al
movb %al, 0x7(%r14)
movl %r12d, %eax
andb $0x7f, %al
movb %al, 0x8(%r14)
pushq $0x9
jmp 0x71f8f
movb $-0x7f, (%r14)
movb %sil, 0x1(%r14)
orb $-0x80, %dl
movb %dl, 0x2(%r14)
orb $-0x80, %cl
movb %cl, 0x3(%r14)
orb $-0x80, %al
movb %al, 0x4(%r14)
movq %r12, %rax
shrq $0x1c, %rax
orb $-0x80, %al
movb %al, 0x5(%r14)
movl %r12d, %eax
shrl $0x15, %eax
orb $-0x80, %al
movb %al, 0x6(%r14)
movl %r12d, %eax
shrl $0xe, %eax
orb $-0x80, %al
movb %al, 0x7(%r14)
movl %r12d, %eax
shrl $0x7, %eax
orb $-0x80, %al
movb %al, 0x8(%r14)
movl %r12d, %eax
andb $0x7f, %al
movb %al, 0x9(%r14)
pushq $0xa
popq %r13
movq %r14, %rdx
addq %r13, %rdx
leaq 0x10(%rsp), %rax
leaq 0x18(%rsp), %rbp
leaq 0x20(%rsp), %r8
leaq 0xc(%rsp), %r9
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rcx
pushq %rax
pushq $0x0
callq 0x61058
popq %rcx
popq %rdx
movq %rax, %r15
addq (%rbp), %r13
movq %r13, (%rbp)
movq 0x78(%rbx), %rsi
xorl %edi, %edi
movq %r14, %rdx
movl %r13d, %ecx
callq *0x30(%rsi)
testl %eax, %eax
je 0x71fe1
pushq $-0x1
popq %rax
jmp 0x7200d
movq 0x80(%rbx), %rsi
movl 0x10(%rsp), %ecx
xorl %edi, %edi
movq %r15, %rdx
callq *0x30(%rsi)
testl %eax, %eax
pushq $-0x1
popq %rax
jne 0x7200d
movq %r14, %rdi
callq 0x45c0
movq %r15, %rdi
callq 0x45c0
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq