// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x1, -0x24(%rbp)
je 0xc4d67
movl $0x1, %edi
leaq 0x76836(%rip), %rsi     # 0x13b583
leaq 0x76841(%rip), %rdx     # 0x13b595
movb $0x0, %al
callq 0x15920
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0xc500c
movq -0x10(%rbp), %rdi
callq 0x149c0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x15010
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x152b0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x15870
cmpl $0x0, %eax
je 0xc4dae
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc500c
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x14b20
cmpl $0x0, %eax
je 0xc4dcc
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc500c
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x10(%rbp), %rax
movl 0x220(%rax), %ecx
callq 0x15f60
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rdi
callq 0x15530
movq -0x50(%rbp), %rdi
callq 0x15c10
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x50(%rbp), %rdi
callq 0x152d0
movl %eax, %ecx
movq -0xa0(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x89a8(%rax), %rax
movq -0x78(%rbp), %rsi
leaq -0x68(%rbp), %rdi
leaq -0x80(%rbp), %rdx
callq *%rax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %ecx
movq -0x68(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0x89a8(%rax), %rax
movq -0x78(%rbp), %rsi
leaq -0x68(%rbp), %rdi
leaq -0x80(%rbp), %rdx
callq *%rax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %ecx
movq -0x68(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq 0x89a8(%rax), %rax
movq -0x78(%rbp), %rsi
leaq -0x68(%rbp), %rdi
leaq -0x80(%rbp), %rdx
callq *%rax
movl %eax, -0x7c(%rbp)
movq -0x68(%rbp), %rcx
movq -0x70(%rbp), %rax
subq %rax, %rcx
movslq -0x7c(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x80(%rbp)
je 0xc4edc
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0xc500c
movq -0x58(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x58(%rbp), %rdi
callq 0x14a70
movq -0xa8(%rbp), %rdi
movl %eax, %esi
subl $0x2, %esi
callq 0x140d0
movq -0x58(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl -0x7c(%rbp), %edx
callq 0x14e70
movq -0x58(%rbp), %rdi
callq 0x14f70
movq -0x58(%rbp), %rdi
callq 0x15010
movl %eax, -0x44(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x8c(%rbp), %rsi
callq 0x14040
movq %rax, -0x88(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x44(%rbp), %eax
je 0xc4fbc
movl -0x44(%rbp), %eax
subl -0x40(%rbp), %eax
movl %eax, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jge 0xc4f94
movl -0x90(%rbp), %edx
movq -0x88(%rbp), %rax
movslq -0x94(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0xc4f5b
movq -0x20(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x20(%rbp), %rdi
callq 0x14e10
movq -0xb0(%rbp), %rdi
movl %eax, %esi
addl -0x90(%rbp), %esi
callq 0x14d90
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x14170
cmpl $0x0, %eax
je 0xc4fd7
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0xc500c
movq -0x18(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x15900
movq -0x50(%rbp), %rdi
callq 0x151e0
movq -0x58(%rbp), %rdi
callq 0x151e0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x8c(%rbp), %edx
callq 0x14a40
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax,%rax)