// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/cram/cram_decode.c
// Function: cram_to_bam
int cram_to_bam(sam_hdr_t *sh, cram_fd *fd, cram_slice *s,
                cram_record *cr, int rec, bam_seq_t **bam) {
    int ret, rg_len;
    char name_a[1024], *name;
    int name_len;
    char *aux;
    char *seq, *qual;
    sam_hrecs_t *bfd = sh->hrecs;

    /* Assign names if not explicitly set */
    if (fd->required_fields & SAM_QNAME) {
        if (cr->name_len) {
            name = (char *)BLOCK_DATA(s->name_blk) + cr->name;
            name_len = cr->name_len;
        } else {
            name = name_a;
            if (cr->mate_line >= 0 && cr->mate_line < s->max_rec &&
                s->crecs[cr->mate_line].name_len > 0) {
                // Copy our mate if non-zero.
                memcpy(name_a, BLOCK_DATA(s->name_blk)+s->crecs[cr->mate_line].name,
                       s->crecs[cr->mate_line].name_len);
                name = name_a + s->crecs[cr->mate_line].name_len;
            } else {
                // Otherwise generate a name based on prefix
                name_len = strlen(fd->prefix);
                memcpy(name, fd->prefix, name_len);
                name += name_len;
                *name++ = ':';
                if (cr->mate_line >= 0 && cr->mate_line < rec) {
                    name = (char *)append_uint64((unsigned char *)name,
                                                 s->hdr->record_counter +
                                                 cr->mate_line + 1);
                } else {
                    name = (char *)append_uint64((unsigned char *)name,
                                                 s->hdr->record_counter +
                                                 rec + 1);
                }
            }
            name_len = name - name_a;
            name = name_a;
        }
    } else {
        name = "?";
        name_len = 1;
    }

    /* Generate BAM record */
    if (cr->rg < -1 || cr->rg >= bfd->nrg)
        return -1;
    rg_len = (cr->rg != -1) ? bfd->rg[cr->rg].name_len + 4 : 0;

    if (fd->required_fields & (SAM_SEQ | SAM_QUAL)) {
        if (!BLOCK_DATA(s->seqs_blk))
            return -1;
        seq = (char *)BLOCK_DATA(s->seqs_blk) + cr->seq;
    } else {
        seq = "*";
        cr->len = 0;
    }

    if (fd->required_fields & SAM_QUAL) {
        if (!BLOCK_DATA(s->qual_blk))
            return -1;
        qual = (char *)BLOCK_DATA(s->qual_blk) + cr->qual;
    } else {
        qual = NULL;
    }

    ret = bam_set1(*bam,
                   name_len, name,
                   cr->flags, cr->ref_id, cr->apos - 1, cr->mqual,
                   cr->ncigar, &s->cigar[cr->cigar],
                   cr->mate_ref_id, cr->mate_pos - 1, cr->tlen,
                   cr->len, seq, qual,
                   cr->aux_size + rg_len);
    if (ret < 0) {
        return ret;
    }

    aux = (char *)bam_aux(*bam);

    /* Auxiliary strings */
    if (cr->aux_size != 0) {
        memcpy(aux, BLOCK_DATA(s->aux_blk) + cr->aux, cr->aux_size);
        aux += cr->aux_size;
        (*bam)->l_data += cr->aux_size;
    }

    /* RG:Z: */
    if (rg_len > 0) {
        *aux++ = 'R'; *aux++ = 'G'; *aux++ = 'Z';
        int len = bfd->rg[cr->rg].name_len;
        memcpy(aux, bfd->rg[cr->rg].name, len);
        aux += len;
        *aux++ = 0;
        (*bam)->l_data += rg_len;
    }

    return (*bam)->l_data;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movq %r9, %r15
movl %r8d, %ebp
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %r12
movq 0x38(%rdi), %r8
testb $0x1, 0x24c(%rsi)
jne 0x329a4
leaq 0x475be(%rip), %rdx     # 0x79f5a
pushq $0x1
popq %rax
jmp 0x32a9f
movl 0x28(%r14), %eax
testl %eax, %eax
je 0x329bd
movq 0x68(%r13), %rcx
movslq 0x24(%r14), %rdx
addq 0x20(%rcx), %rdx
jmp 0x32a9f
movq %r8, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movl 0x2c(%r14), %r15d
testl %r15d, %r15d
movq %r13, %rcx
js 0x32a24
cmpl 0xe0(%rcx), %r15d
jge 0x32a24
movq 0x30(%rcx), %rbx
imulq $0x90, %r15, %r13
movslq 0x28(%rbx,%r13), %rdx
testq %rdx, %rdx
jle 0x32a24
leaq (%rbx,%r13), %rax
movq %rcx, %rbp
movq 0x68(%rcx), %rcx
movslq 0x24(%rax), %rsi
addq 0x20(%rcx), %rsi
leaq 0x30(%rsp), %rdi
callq 0x43c0
movslq 0x28(%rbx,%r13), %rax
addq %rsp, %rax
addq $0x30, %rax
movq 0x18(%rsp), %r15
movq %rbp, %r13
jmp 0x32a93
testl %r15d, %r15d
sets 0xc(%rsp)
movq 0x20(%r12), %r13
movq %r13, %rdi
movq %rcx, 0x20(%rsp)
callq 0x41d0
movslq %eax, %rbx
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x43c0
leaq (%rsp,%rbx), %rdi
addq $0x31, %rdi
movb $0x3a, -0x1(%rdi)
cmpl %ebp, %r15d
setge %cl
movq 0x20(%rsp), %r13
movq (%r13), %rax
movq 0x20(%rax), %rax
orb 0xc(%rsp), %cl
jne 0x32a7f
leaq (%r15,%rax), %rsi
incq %rsi
jmp 0x32a89
movslq %ebp, %rcx
leaq (%rcx,%rax), %rsi
incq %rsi
callq 0x32c82
movq 0x18(%rsp), %r15
leaq 0x30(%rsp), %rdx
subl %edx, %eax
movq 0x10(%rsp), %r8
movslq 0x20(%r14), %rsi
pushq $-0x1
popq %rcx
cmpq $-0x1, %rsi
jl 0x32c6e
cmpl 0x40(%r8), %esi
jge 0x32c6e
cmpl $-0x1, %esi
je 0x32ad2
movq 0x48(%r8), %rdi
imulq $0x18, %rsi, %rsi
movl 0x10(%rdi,%rsi), %r9d
addl $0x4, %r9d
jmp 0x32ad5
xorl %r9d, %r9d
movl 0x24c(%r12), %esi
testw $0x600, %si             # imm = 0x600
je 0x32afe
movq 0x70(%r13), %rdi
movq 0x20(%rdi), %r10
testq %r10, %r10
je 0x32c6e
movl 0x64(%r14), %edi
addq %rdi, %r10
jmp 0x32b0a
andl $0x0, 0x14(%r14)
leaq 0x47bee(%rip), %r10     # 0x7a6f8
btl $0xa, %esi
jb 0x32b1f
movl %r9d, 0xc(%rsp)
movq %r8, 0x10(%rsp)
xorl %r11d, %r11d
jmp 0x32b41
movq 0x78(%r13), %rsi
movq 0x20(%rsi), %r11
testq %r11, %r11
je 0x32c6e
movl %r9d, 0xc(%rsp)
movq %r8, 0x10(%rsp)
movl 0x68(%r14), %ecx
addq %rcx, %r11
movq (%r15), %rdi
movslq %eax, %rsi
movzwl 0xc(%r14), %ecx
movl 0x8(%r14), %r8d
movq 0x18(%r14), %r9
decq %r9
movzbl 0x80(%r14), %eax
movq %rax, 0x28(%rsp)
movslq 0x70(%r14), %rbx
movq %r15, 0x18(%rsp)
movl 0x6c(%r14), %r15d
shlq $0x2, %r15
movq %r13, 0x20(%rsp)
addq 0x38(%r13), %r15
movl 0x30(%r14), %r12d
movq 0x38(%r14), %r13
decq %r13
movslq 0x14(%r14), %rax
movl 0x58(%r14), %ebp
addl 0xc(%rsp), %ebp
pushq %rbp
pushq %r11
pushq %r10
pushq %rax
pushq 0x40(%r14)
pushq %r13
pushq %r12
pushq %r15
pushq %rbx
pushq 0x70(%rsp)
callq 0x1e077
addq $0x50, %rsp
testl %eax, %eax
js 0x32c67
movq 0x18(%rsp), %r15
movq (%r15), %rax
movl 0x14(%rax), %ecx
shll $0x2, %ecx
addq 0x38(%rax), %rcx
movzwl 0x12(%rax), %edx
movslq 0x18(%rax), %rsi
leal 0x1(%rsi), %edi
sarl %edi
movslq %edi, %r12
addq %rdx, %r12
addq %rsi, %r12
addq %rcx, %r12
movl 0x58(%r14), %edx
testq %rdx, %rdx
je 0x32c18
movq 0x20(%rsp), %rax
movq 0x90(%rax), %rax
movl 0x54(%r14), %esi
addq 0x20(%rax), %rsi
movq %r12, %rdi
callq 0x43c0
movl 0x58(%r14), %ecx
addq %rcx, %r12
movq (%r15), %rax
addl %ecx, 0x40(%rax)
movq 0x10(%rsp), %rcx
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
jle 0x32c6b
movw $0x4752, (%r12)         # imm = 0x4752
leaq 0x3(%r12), %rdi
movb $0x5a, 0x2(%r12)
movq 0x48(%rcx), %rax
movslq 0x20(%r14), %rcx
imulq $0x18, %rcx, %rcx
movslq 0x10(%rax,%rcx), %rbx
movq (%rax,%rcx), %rsi
movq %rbx, %rdx
callq 0x43c0
movb $0x0, 0x3(%r12,%rbx)
movq (%r15), %rax
addl 0x40(%rax), %ebp
movl %ebp, 0x40(%rax)
movl %ebp, %ecx
jmp 0x32c6e
movl %eax, %ecx
jmp 0x32c6e
movl 0x40(%rax), %ecx
movl %ecx, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq