// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/cram/cram_external.c
// Function: cram_transcode_rg
int cram_transcode_rg(cram_fd *in, cram_fd *out,
                      cram_container *c,
                      int nrg, int *in_rg, int *out_rg) {
    int new_rg = *out_rg, old_size, new_size;
    cram_block *o_blk, *n_blk;
    cram_block_compression_hdr *ch;

    if (nrg != 1) {
        hts_log_error("CRAM transcode supports only a single RG");
        return -2;
    }

    // Produce a new block holding the updated compression header,
    // with RG transcoded to a new value. (Single only supported.)
    o_blk = cram_read_block(in);
    old_size = cram_block_size(o_blk);
    ch = cram_decode_compression_header(in, o_blk);
    if (cram_block_compression_hdr_set_rg(ch, new_rg) != 0)
        return -1;
    if (cram_block_compression_hdr_decoder2encoder(in, ch) != 0)
        return -1;
    n_blk = cram_encode_compression_header(in, c, ch, in->embed_ref);
    cram_free_compression_header(ch);

    /*
     * Warning: this has internal knowledge of the cram compression
     * header format.
     *
     * The decoder doesn't set c->tags_used, so the encoder puts a two
     * byte blank segment.  This means n_blk is too short.  We skip
     * through the decoded old block (o_blk) and copy from there.
     */
    char *cp = cram_block_get_data(o_blk);
    char *op = cp;
    char *endp = cp + cram_block_get_uncomp_size(o_blk);
    //fprintf(stderr, "sz = %d\n", (int)(endp-cp));
    int32_t i32, err = 0;

    i32 = in->vv.varint_get32(&cp, endp, &err);
    cp += i32;
    i32 = in->vv.varint_get32(&cp, endp, &err);
    cp += i32;
    op = cp;
    i32 = in->vv.varint_get32(&cp, endp, &err);
    i32 += (cp-op);
    if (err)
        return -2;

    //fprintf(stderr, "remaining %d bytes\n", i32);
    cram_block_set_size(n_blk, cram_block_get_size(n_blk)-2);
    cram_block_append(n_blk, op, i32);
    cram_block_update_size(n_blk);

    new_size = cram_block_size(n_blk);

    //fprintf(stderr, "size %d -> %d\n", old_size, new_size);

    // Now we've constructedthe updated compression header,
    // amend the container too (it may have changed size).
    int32_t *landmarks, num_landmarks;
    landmarks = cram_container_get_landmarks(c, &num_landmarks);

    if (old_size != new_size) {
        int diff = new_size - old_size, j;

        for (j = 0; j < num_landmarks; j++)
            landmarks[j] += diff;
        //cram_container_set_landmarks(c, num_landmarks, landmarks);
        cram_container_set_length(c, cram_container_get_length(c) + diff);
    }

    // Finally write it all out; container, compression header,
    // and then all the remaining slice blocks.
    if (cram_write_container(out, c) != 0)
        return -2;

    cram_write_block(out, n_blk);
    cram_free_block(o_blk);
    cram_free_block(n_blk);

    // Container num_blocks can be invalid, due to a bug.
    // Instead we iterate in slice context instead.
    return cram_copy_slice(in, out, num_landmarks);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
cmpl $0x1, %ecx
jne 0x7646e
movq %rsi, %rbx
movq %rdi, %r14
movq %rdx, 0x18(%rsp)
movl (%r9), %r13d
callq 0x14930
movq %rax, %r15
movq %rax, %rdi
callq 0x14f30
movl %eax, %r12d
movq %r14, %rdi
movq %r15, %rsi
callq 0x151d0
movq %rax, %rbp
movq %rax, %rdi
movl %r13d, %esi
callq 0x15780
pushq $-0x1
popq %r13
testl %eax, %eax
jne 0x7648a
movl %r12d, 0xc(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x14a80
testl %eax, %eax
jne 0x7648a
movq %rbx, 0x20(%rsp)
movl 0x220(%r14), %ecx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq %rbp, %rdx
callq 0x15e40
movq %rax, 0x10(%rsp)
movq %rbp, %rdi
callq 0x15440
movq 0x20(%r15), %rax
leaq 0x28(%rsp), %r13
movq %rax, (%r13)
movslq 0x14(%r15), %rbx
addq %rax, %rbx
leaq 0x34(%rsp), %r12
andl $0x0, (%r12)
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq *0x89a8(%r14)
cltq
addq %rax, (%r13)
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq *0x89a8(%r14)
movslq %eax, %rbp
addq (%r13), %rbp
movq %rbp, (%r13)
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq *0x89a8(%r14)
pushq $-0x2
popq %r13
cmpl $0x0, (%r12)
jne 0x7648a
subl %ebp, %eax
addl 0x28(%rsp), %eax
movq 0x10(%rsp), %rbx
movl 0x30(%rbx), %ecx
addl $-0x2, %ecx
movslq %ecx, %rcx
movq %rcx, 0x30(%rbx)
movq %rbx, %rdi
movq %rbp, %rsi
movl %eax, %edx
callq 0x14dc0
movl 0x30(%rbx), %eax
movl %eax, 0x14(%rbx)
movl %eax, 0x10(%rbx)
movq %rbx, %rdi
callq 0x14f30
movq 0x18(%rsp), %r12
movl 0x30(%r12), %ebx
subl 0xc(%rsp), %eax
movq 0x20(%rsp), %rbp
je 0x764a0
movq 0x38(%r12), %rcx
xorl %edx, %edx
testl %ebx, %ebx
movl $0x0, %esi
cmovgl %ebx, %esi
cmpq %rdx, %rsi
je 0x7649c
addl %eax, (%rcx,%rdx,4)
incq %rdx
jmp 0x76461
leaq 0x35896(%rip), %rsi     # 0xabd0b
leaq 0x358a1(%rip), %rdx     # 0xabd1d
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x15830
pushq $-0x2
popq %r13
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl %eax, (%r12)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x14130
testl %eax, %eax
jne 0x7648a
movq %rbp, %rdi
movq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x15810
movq %r15, %rdi
callq 0x15100
movq %r12, %rdi
callq 0x15100
movq %r14, %rdi
movq %rbp, %rsi
movl %ebx, %edx
callq 0x149b0
movl %eax, %r13d
jmp 0x7648a