// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4e8, %rsp            # imm = 0x4E8
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movl %r8d, -0x54(%rbp)
movq %r9, -0x60(%rbp)
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x4a0(%rbp)
movq -0x40(%rbp), %rax
movl 0x24c(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x582c4
movq -0x50(%rbp), %rax
cmpl $0x0, 0x28(%rax)
je 0x580eb
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rax
movq 0x20(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x24(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x478(%rbp)
movq -0x50(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x47c(%rbp)
jmp 0x582c2
leaq -0x470(%rbp), %rax
movq %rax, -0x478(%rbp)
movq -0x50(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
jl 0x581cd
movq -0x50(%rbp), %rax
movl 0x2c(%rax), %eax
movq -0x48(%rbp), %rcx
cmpl 0xe0(%rcx), %eax
jge 0x581cd
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x2c(%rcx), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x28(%rax)
jle 0x581cd
leaq -0x470(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rax
movq 0x20(%rax), %rsi
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x2c(%rcx), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movslq 0x24(%rax), %rax
addq %rax, %rsi
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x2c(%rcx), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movslq 0x28(%rax), %rdx
callq 0x43b0
leaq -0x470(%rbp), %rax
movq -0x48(%rbp), %rcx
movq 0x30(%rcx), %rcx
movq -0x50(%rbp), %rdx
movslq 0x2c(%rdx), %rdx
imulq $0x90, %rdx, %rdx
addq %rdx, %rcx
movslq 0x28(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x478(%rbp)
jmp 0x5829d
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x41b0
movl %eax, -0x47c(%rbp)
movq -0x478(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rsi
movslq -0x47c(%rbp), %rdx
callq 0x43b0
movl -0x47c(%rbp), %ecx
movq -0x478(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x478(%rbp)
movq -0x478(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x478(%rbp)
movb $0x3a, (%rax)
movq -0x50(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
jl 0x58272
movq -0x50(%rbp), %rax
movl 0x2c(%rax), %eax
cmpl -0x54(%rbp), %eax
jge 0x58272
movq -0x478(%rbp), %rdi
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rsi
movq -0x50(%rbp), %rax
movslq 0x2c(%rax), %rax
addq %rax, %rsi
addq $0x1, %rsi
callq 0x586f0
movq %rax, -0x478(%rbp)
jmp 0x5829b
movq -0x478(%rbp), %rdi
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rsi
movslq -0x54(%rbp), %rax
addq %rax, %rsi
addq $0x1, %rsi
callq 0x586f0
movq %rax, -0x478(%rbp)
jmp 0x5829d
movq -0x478(%rbp), %rax
leaq -0x470(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x47c(%rbp)
leaq -0x470(%rbp), %rax
movq %rax, -0x478(%rbp)
jmp 0x582dc
leaq 0x9a252(%rip), %rax     # 0xf251d
movq %rax, -0x478(%rbp)
movl $0x1, -0x47c(%rbp)
movq -0x50(%rbp), %rax
cmpl $-0x1, 0x20(%rax)
jl 0x582f9
movq -0x50(%rbp), %rax
movl 0x20(%rax), %eax
movq -0x4a0(%rbp), %rcx
cmpl 0x40(%rcx), %eax
jl 0x58305
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
jmp 0x586cc
movq -0x50(%rbp), %rax
cmpl $-0x1, 0x20(%rax)
je 0x58337
movq -0x4a0(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x20(%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
addl $0x4, %eax
movl %eax, -0x4a8(%rbp)
jmp 0x58341
xorl %eax, %eax
movl %eax, -0x4a8(%rbp)
jmp 0x58341
movl -0x4a8(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x40(%rbp), %rax
movl 0x24c(%rax), %eax
andl $0x600, %eax            # imm = 0x600
cmpl $0x0, %eax
je 0x58398
movq -0x48(%rbp), %rax
movq 0x70(%rax), %rax
cmpq $0x0, 0x20(%rax)
jne 0x58379
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
jmp 0x586cc
movq -0x48(%rbp), %rax
movq 0x70(%rax), %rax
movq 0x20(%rax), %rax
movq -0x50(%rbp), %rcx
movl 0x64(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x490(%rbp)
jmp 0x583b1
leaq 0x9a924(%rip), %rax     # 0xf2cc3
movq %rax, -0x490(%rbp)
movq -0x50(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x40(%rbp), %rax
movl 0x24c(%rax), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
je 0x583ff
movq -0x48(%rbp), %rax
movq 0x78(%rax), %rax
cmpq $0x0, 0x20(%rax)
jne 0x583e0
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
jmp 0x586cc
movq -0x48(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x20(%rax), %rax
movq -0x50(%rbp), %rcx
movl 0x68(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x498(%rbp)
jmp 0x5840a
movq $0x0, -0x498(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movslq -0x47c(%rbp), %rsi
movq -0x478(%rbp), %rdx
movq -0x50(%rbp), %rax
movl 0xc(%rax), %eax
movw %ax, %cx
movq -0x50(%rbp), %rax
movl 0x8(%rax), %r8d
movq -0x50(%rbp), %rax
movq 0x18(%rax), %r9
subq $0x1, %r9
movq -0x50(%rbp), %rax
movl 0x80(%rax), %eax
movb %al, -0x4b9(%rbp)
movq -0x50(%rbp), %rax
movslq 0x70(%rax), %rax
movq %rax, -0x4b8(%rbp)
movq -0x48(%rbp), %rax
movq 0x38(%rax), %r13
movq -0x50(%rbp), %rax
movl 0x6c(%rax), %eax
shlq $0x2, %rax
addq %rax, %r13
movq -0x50(%rbp), %rax
movl 0x30(%rax), %r12d
movq -0x50(%rbp), %rax
movq 0x38(%rax), %r15
subq $0x1, %r15
movq -0x50(%rbp), %rax
movq 0x40(%rax), %r14
movq -0x50(%rbp), %rax
movslq 0x14(%rax), %rbx
movq -0x490(%rbp), %r11
movq -0x498(%rbp), %r10
movq -0x50(%rbp), %rax
movl 0x58(%rax), %eax
addl -0x68(%rbp), %eax
movl %eax, %eax
movq %rax, -0x4b0(%rbp)
movb -0x4b9(%rbp), %al
movzwl %cx, %ecx
movzbl %al, %eax
movl %eax, (%rsp)
movq -0x4b8(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x4b0(%rbp), %rax
movq %r13, 0x10(%rsp)
movl %r12d, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
movq %r11, 0x38(%rsp)
movq %r10, 0x40(%rsp)
movq %rax, 0x48(%rsp)
callq 0x331d0
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jge 0x5851a
movl -0x64(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x586cc
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq 0x38(%rax), %rax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
movl 0x14(%rcx), %ecx
shll $0x2, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
movzwl 0x12(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
movl 0x18(%rcx), %ecx
addl $0x1, %ecx
sarl %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
movslq 0x18(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x488(%rbp)
movq -0x50(%rbp), %rax
cmpl $0x0, 0x58(%rax)
je 0x585d8
movq -0x488(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x90(%rax), %rax
movq 0x20(%rax), %rsi
movq -0x50(%rbp), %rax
movl 0x54(%rax), %eax
addq %rax, %rsi
movq -0x50(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, %edx
callq 0x43b0
movq -0x50(%rbp), %rax
movl 0x58(%rax), %ecx
movq -0x488(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x488(%rbp)
movq -0x50(%rbp), %rax
movl 0x58(%rax), %ecx
movq -0x60(%rbp), %rax
movq (%rax), %rax
addl 0x40(%rax), %ecx
movl %ecx, 0x40(%rax)
cmpl $0x0, -0x68(%rbp)
jle 0x586bf
movq -0x488(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x488(%rbp)
movb $0x52, (%rax)
movq -0x488(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x488(%rbp)
movb $0x47, (%rax)
movq -0x488(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x488(%rbp)
movb $0x5a, (%rax)
movq -0x4a0(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x20(%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, -0x4a4(%rbp)
movq -0x488(%rbp), %rdi
movq -0x4a0(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x20(%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movslq -0x4a4(%rbp), %rdx
callq 0x43b0
movl -0x4a4(%rbp), %ecx
movq -0x488(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x488(%rbp)
movq -0x488(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x488(%rbp)
movb $0x0, (%rax)
movl -0x68(%rbp), %ecx
movq -0x60(%rbp), %rax
movq (%rax), %rax
addl 0x40(%rax), %ecx
movl %ecx, 0x40(%rax)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl 0x40(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)