// File: /workspace/github_c_makefile_star2_O3/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 0x90f4f
movq %rsi, %rbx
movq %rdi, %r14
movq %rdx, 0x18(%rsp)
movl (%r9), %r13d
callq 0x14900
movq %rax, %r15
movq %rax, %rdi
callq 0x14ed0
movl %eax, %r12d
movq %r14, %rdi
movq %r15, %rsi
callq 0x15160
movq %rax, %rbp
movq %rax, %rdi
movl %r13d, %esi
callq 0x15700
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x90f6f
movl %r12d, 0xc(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x14a40
testl %eax, %eax
jne 0x90f6f
movq %rbx, 0x20(%rsp)
movl 0x220(%r14), %ecx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq %rbp, %rdx
callq 0x15d90
movq %rax, 0x10(%rsp)
movq %rbp, %rdi
callq 0x153d0
movq 0x20(%r15), %rax
leaq 0x28(%rsp), %r13
movq %rax, (%r13)
movslq 0x14(%r15), %rbx
addq %rax, %rbx
leaq 0x34(%rsp), %r12
movl $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)
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
cmpl $0x0, (%r12)
jne 0x90f6f
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 0x14d70
movl 0x30(%rbx), %eax
movl %eax, 0x14(%rbx)
movl %eax, 0x10(%rbx)
movq %rbx, %rdi
callq 0x14ed0
movq 0x18(%rsp), %rsi
movl 0x30(%rsi), %ebp
subl 0xc(%rsp), %eax
movq 0x20(%rsp), %rbx
je 0x90f11
testl %ebp, %ebp
jle 0x90f0f
movq 0x38(%rsi), %rcx
xorl %edx, %edx
addl %eax, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rbp
jne 0x90f04
addl %eax, (%rsi)
movq %rbx, %rdi
callq 0x14120
testl %eax, %eax
jne 0x90f6f
movq %rbx, %rdi
movq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x15790
movq %r15, %rdi
callq 0x15090
movq %r12, %rdi
callq 0x15090
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x14970
movl %eax, %r13d
jmp 0x90f6f
leaq 0x44085(%rip), %rsi     # 0xd4fdb
leaq 0x44090(%rip), %rdx     # 0xd4fed
movl $0x1, %edi
xorl %eax, %eax
callq 0x157b0
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq