// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/cram/cram_encode.c
// Function: cram_encode_slice
static int cram_encode_slice(cram_fd *fd, cram_container *c,
                             cram_block_compression_hdr *h, cram_slice *s,
                             int embed_ref) {
    int rec, r = 0;
    int64_t last_pos;
    enum cram_DS_ID id;

    /*
     * Slice external blocks:
     * ID 0 => base calls (insertions, soft-clip)
     * ID 1 => qualities
     * ID 2 => names
     * ID 3 => TS (insert size), NP (next frag)
     * ID 4 => tag values
     * ID 6 => tag IDs (TN), if CRAM_V1.0
     * ID 7 => TD tag dictionary, if !CRAM_V1.0
     */

    /* Create cram slice header */
    s->hdr->ref_base_id = embed_ref>0 && s->hdr->ref_seq_span > 0
        ? DS_ref
        : (CRAM_MAJOR_VERS(fd->version) >= 4 ? 0 : -1);
    s->hdr->record_counter = c->num_records + c->record_counter;
    c->num_records += s->hdr->num_records;

    int ntags = c->tags_used ? c->tags_used->n_occupied : 0;
    s->block = calloc(DS_END + ntags*2, sizeof(s->block[0]));
    s->hdr->block_content_ids = malloc(DS_END * sizeof(int32_t));
    if (!s->block || !s->hdr->block_content_ids)
        return -1;

    // Create first fixed blocks, always external.
    // CORE
    if (!(s->block[0] = cram_new_block(CORE, 0)))
        return -1;

    // TN block for CRAM v1
    if (CRAM_MAJOR_VERS(fd->version) == 1) {
        if (h->codecs[DS_TN]->codec == E_EXTERNAL) {
            if (!(s->block[DS_TN] = cram_new_block(EXTERNAL,DS_TN))) return -1;
            h->codecs[DS_TN]->u.external.content_id = DS_TN;
        } else {
            s->block[DS_TN] = s->block[0];
        }
    }

    // Embedded reference
    if (embed_ref>0) {
        if (!(s->block[DS_ref] = cram_new_block(EXTERNAL, DS_ref)))
            return -1;
        s->ref_id = DS_ref; // needed?
        BLOCK_APPEND(s->block[DS_ref],
                     c->ref + s->hdr->ref_seq_start - c->ref_start,
                     s->hdr->ref_seq_span);
    }

    /*
     * All the data-series blocks if appropriate.
     */
    for (id = DS_QS; id < DS_TN; id++) {
        if (cram_allocate_block(h->codecs[id], s, id) < 0)
            return -1;
    }

    /*
     * Add in the external tag blocks too.
     */
    if (c->tags_used) {
        int n;
        s->hdr->num_blocks = DS_END;
        for (n = 0; n < s->naux_block; n++) {
            s->block[s->hdr->num_blocks++] = s->aux_block[n];
            s->aux_block[n] = NULL;
        }
    }

    /* Encode reads */
    last_pos = s->hdr->ref_seq_start;
    for (rec = 0; rec < s->hdr->num_records; rec++) {
        cram_record *cr = &s->crecs[rec];
        if (cram_encode_slice_read(fd, c, h, s, cr, &last_pos) == -1)
            return -1;
    }

    s->block[0]->uncomp_size = s->block[0]->byte + (s->block[0]->bit < 7);
    s->block[0]->comp_size = s->block[0]->uncomp_size;

    // Make sure the fixed blocks point to the correct sources
    if (s->block[DS_IN]) cram_free_block(s->block[DS_IN]);
    s->block[DS_IN] = s->base_blk; s->base_blk = NULL;
    if (s->block[DS_QS]) cram_free_block(s->block[DS_QS]);
    s->block[DS_QS] = s->qual_blk; s->qual_blk = NULL;
    if (s->block[DS_RN]) cram_free_block(s->block[DS_RN]);
    s->block[DS_RN] = s->name_blk; s->name_blk = NULL;
    if (s->block[DS_SC]) cram_free_block(s->block[DS_SC]);
    s->block[DS_SC] = s->soft_blk; s->soft_blk = NULL;

    // Finalise any data transforms.
    for (id = DS_QS; id < DS_TN; id++) {
       if (h->codecs[id] && h->codecs[id]->flush)
           h->codecs[id]->flush(h->codecs[id]);
    }

    // Ensure block sizes are up to date.
    for (id = 1; id < s->hdr->num_blocks; id++) {
        if (!s->block[id] || s->block[id] == s->block[0])
            continue;

        if (s->block[id]->uncomp_size == 0)
            BLOCK_UPLEN(s->block[id]);
    }

    // Compress it all
    if (cram_compress_slice(fd, c, s) == -1)
        return -1;

    // Collapse empty blocks and create hdr_block
    {
        int i, j;

        s->hdr->block_content_ids = realloc(s->hdr->block_content_ids,
                                            s->hdr->num_blocks * sizeof(int32_t));
        if (!s->hdr->block_content_ids)
            return -1;

        for (i = j = 1; i < s->hdr->num_blocks; i++) {
            if (!s->block[i] || s->block[i] == s->block[0])
                continue;
            if (s->block[i]->uncomp_size == 0) {
                cram_free_block(s->block[i]);
                s->block[i] = NULL;
                continue;
            }
            s->block[j] = s->block[i];
            s->hdr->block_content_ids[j-1] = s->block[i]->content_id;
            j++;
        }
        s->hdr->num_content_ids = j-1;
        s->hdr->num_blocks = j;

        if (!(s->hdr_block = cram_encode_slice_header(fd, s)))
            return -1;
    }

    return r ? -1 : 0;

 block_err:
    return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x619b1
movq -0x28(%rbp), %rax
movq (%rax), %rax
cmpq $0x0, 0x10(%rax)
jle 0x619b1
movl $0xa, %eax
movl %eax, -0x64(%rbp)
jmp 0x619cb
movq -0x10(%rbp), %rax
movl 0xc(%rax), %edx
sarl $0x8, %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
cmpl $0x4, %edx
cmovgel %ecx, %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl %ecx, 0x38(%rax)
movq -0x18(%rbp), %rax
movslq 0x28(%rax), %rcx
movq -0x18(%rbp), %rax
addq 0x18(%rax), %rcx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl 0x18(%rax), %ecx
movq -0x18(%rbp), %rax
addl 0x28(%rax), %ecx
movl %ecx, 0x28(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x270(%rax)
je 0x61a28
movq -0x18(%rbp), %rax
movq 0x270(%rax), %rax
movl 0x8(%rax), %eax
movl %eax, -0x68(%rbp)
jmp 0x61a2f
xorl %eax, %eax
movl %eax, -0x68(%rbp)
jmp 0x61a2f
movl -0x68(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
shll %eax
addl $0x2f, %eax
movslq %eax, %rdi
movl $0x8, %esi
callq 0x42f0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0xbc, %edi
callq 0x4650
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x30(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x61a86
movq -0x28(%rbp), %rax
movq (%rax), %rax
cmpq $0x0, 0x30(%rax)
jne 0x61a92
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x62148
movl $0x5, %edi
xorl %esi, %esi
callq 0x6ede0
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x61abb
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x62148
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x1, %eax
jne 0x61b3a
movq -0x20(%rbp), %rax
movq 0x3b8(%rax), %rax
cmpl $0x1, (%rax)
jne 0x61b1e
movl $0x4, %edi
movl $0x27, %esi
callq 0x6ede0
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq %rax, 0x138(%rcx)
cmpq $0x0, %rax
jne 0x61b0a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x62148
movq -0x20(%rbp), %rax
movq 0x3b8(%rax), %rax
movl $0x27, 0x60(%rax)
jmp 0x61b38
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rcx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x138(%rax)
jmp 0x61b3a
cmpl $0x0, -0x2c(%rbp)
jle 0x61bcd
movl $0x4, %edi
movl $0xa, %esi
callq 0x6ede0
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq %rax, 0x50(%rcx)
cmpq $0x0, %rax
jne 0x61b71
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x62148
movq -0x28(%rbp), %rax
movl $0xa, 0xc8(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x50(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0xe0(%rax), %rsi
movq -0x28(%rbp), %rax
movq (%rax), %rax
addq 0x8(%rax), %rsi
movq -0x18(%rbp), %rcx
xorl %eax, %eax
subq 0xc0(%rcx), %rax
addq %rax, %rsi
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rdx
callq 0x5c2b0
cmpl $0x0, %eax
jge 0x61bcb
jmp 0x62141
jmp 0x61bcd
movl $0xc, -0x44(%rbp)
cmpl $0x27, -0x44(%rbp)
jae 0x61c13
movq -0x20(%rbp), %rax
movl -0x44(%rbp), %ecx
movq 0x280(%rax,%rcx,8), %rdi
movq -0x28(%rbp), %rsi
movl -0x44(%rbp), %edx
callq 0x64f40
cmpl $0x0, %eax
jge 0x61c06
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x62148
jmp 0x61c08
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x61bd4
movq -0x18(%rbp), %rax
cmpq $0x0, 0x270(%rax)
je 0x61c9d
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl $0x2f, 0x28(%rax)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0xcc(%rcx), %eax
jge 0x61c9b
movq -0x28(%rbp), %rax
movq 0xd0(%rax), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rsi
movl 0x28(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x28(%rsi)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movq 0xd0(%rax), %rax
movslq -0x4c(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x61c36
jmp 0x61c9d
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
cmpl 0x18(%rcx), %eax
jge 0x61d17
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x30(%rbp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x58(%rbp), %r8
leaq -0x40(%rbp), %r9
callq 0x651d0
cmpl $-0x1, %eax
jne 0x61d0a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x62148
jmp 0x61d0c
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x61cb3
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq 0x30(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx), %rcx
cmpl $0x7, 0x38(%rcx)
setl %cl
andb $0x1, %cl
movzbl %cl, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movl %ecx, 0x14(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movl 0x14(%rax), %ecx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movl %ecx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, 0x68(%rax)
je 0x61d90
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x68(%rax), %rdi
callq 0x6f940
movq -0x28(%rbp), %rax
movq 0x80(%rax), %rcx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x68(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x80(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, 0x60(%rax)
je 0x61dd6
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x60(%rax), %rdi
callq 0x6f940
movq -0x28(%rbp), %rax
movq 0x78(%rax), %rcx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x60(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x78(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, 0x58(%rax)
je 0x61e16
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x58(%rax), %rdi
callq 0x6f940
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rcx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x58(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x68(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, 0x70(%rax)
je 0x61e56
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x70(%rax), %rdi
callq 0x6f940
movq -0x28(%rbp), %rax
movq 0x88(%rax), %rcx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x70(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x88(%rax)
movl $0xc, -0x44(%rbp)
cmpl $0x27, -0x44(%rbp)
jae 0x61ee2
movq -0x20(%rbp), %rax
movl -0x44(%rbp), %ecx
cmpq $0x0, 0x280(%rax,%rcx,8)
je 0x61ed5
movq -0x20(%rbp), %rax
movl -0x44(%rbp), %ecx
movq 0x280(%rax,%rcx,8), %rax
cmpq $0x0, 0x48(%rax)
je 0x61ed5
movq -0x20(%rbp), %rax
movl -0x44(%rbp), %ecx
movq 0x280(%rax,%rcx,8), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movl -0x44(%rbp), %edx
movq 0x280(%rcx,%rdx,8), %rdi
callq *%rax
jmp 0x61ed7
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x61e83
movl $0x1, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
cmpl 0x28(%rcx), %eax
jae 0x61f8a
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x61f2a
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
cmpq (%rcx), %rax
jne 0x61f2c
jmp 0x61f7c
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
cmpl $0x0, 0x14(%rax)
jne 0x61f7a
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x30(%rax), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %edx
movq (%rax,%rdx,8), %rax
movl %ecx, 0x14(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %edx
movq (%rax,%rdx,8), %rax
movl %ecx, 0x10(%rax)
jmp 0x61f7c
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x61ee9
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x65b90
cmpl $-0x1, %eax
jne 0x61fac
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x62148
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rax
movslq 0x28(%rax), %rsi
shlq $0x2, %rsi
callq 0x4820
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x30(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
cmpq $0x0, 0x30(%rax)
jne 0x61ff3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x62148
movl $0x1, -0x60(%rbp)
movl $0x1, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
cmpl 0x28(%rcx), %eax
jge 0x620eb
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x5c(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x62044
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
cmpq (%rcx), %rax
jne 0x62049
jmp 0x620dd
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x0, 0x14(%rax)
jne 0x6208a
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x6f940
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x5c(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x620dd
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x60(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0xc(%rax), %edx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %rax
movl -0x60(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x62001
movl -0x60(%rbp), %ecx
subl $0x1, %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movl -0x60(%rbp), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl %ecx, 0x28(%rax)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x5c3f0
movq -0x28(%rbp), %rcx
movq %rax, 0x8(%rcx)
cmpq $0x0, %rax
jne 0x6212c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x62148
movl -0x34(%rbp), %edx
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0x62148
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)