// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/cram/cram_encode.c
// Function: cram_add_to_ref
static int cram_add_to_ref(bam1_t *b, char **ref, uint32_t (**hist)[5],
                           hts_pos_t ref_start, hts_pos_t *ref_end) {
    const uint8_t *MD = bam_aux_get(b, "MD");
    int ret = 0;
    if (MD && *MD == 'Z') {
        // We can use MD to directly compute the reference
        int ret = cram_add_to_ref_MD(b, ref, hist, ref_start, ref_end, MD+1);

        if (ret > 0)
            return ret;
    }

    // Otherwise we just use SEQ+CIGAR and build a consensus which we later
    // turn into a fake reference
    uint32_t *cigar = bam_get_cigar(b);
    uint32_t ncigar = b->core.n_cigar;
    uint32_t i, j;
    hts_pos_t iseq = 0, iref = b->core.pos - ref_start;
    uint8_t *seq = bam_get_seq(b);
    for (i = 0; i < ncigar; i++) {
        switch (bam_cigar_op(cigar[i])) {
        case BAM_CSOFT_CLIP:
        case BAM_CINS:
            iseq += bam_cigar_oplen(cigar[i]);
            break;

        case BAM_CMATCH:
        case BAM_CEQUAL:
        case BAM_CDIFF: {
            int len = bam_cigar_oplen(cigar[i]);
            // Maps an nt16 (A=1 C=2 G=4 T=8 bits) to 0123 plus N=4
            static uint8_t L16[16] = {4,0,1,4, 2,4,4,4, 3,4,4,4, 4,4,4,4};

            if (extend_ref(ref, hist, iref+ref_start + len,
                           ref_start, ref_end) < 0)
                return -1;
            if (iseq + len <= b->core.l_qseq) {
                // Nullify failed MD:Z if appropriate
                if (ret < 0)
                    memset(&(*ref)[iref], 0, len);

                for (j = 0; j < len; j++, iref++, iseq++)
                    (*hist)[iref][L16[bam_seqi(seq, iseq)]]++;
            } else {
                // Probably a 2ndary read with seq "*"
                iseq += len;
                iref += len;
            }
            break;
        }

        case BAM_CDEL:
        case BAM_CREF_SKIP:
            iref += bam_cigar_oplen(cigar[i]);
        }
    }

    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x8bbf8(%rip), %rsi     # 0xf0162
callq 0x3e760
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x645c4
movq -0x38(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x5a, %eax
jne 0x645c4
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
addq $0x1, %r9
callq 0x64800
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jle 0x645c2
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x647e5
jmp 0x645c4
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x4c(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
subq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x14(%rcx), %ecx
shll $0x2, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jae 0x647de
movq -0x48(%rbp), %rax
movl -0x50(%rbp), %ecx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
movl %eax, -0x78(%rbp)
je 0x646a0
jmp 0x6464f
movl -0x78(%rbp), %eax
subl $0x1, %eax
je 0x64684
jmp 0x64659
movl -0x78(%rbp), %eax
addl $-0x2, %eax
subl $0x2, %eax
jb 0x647b7
jmp 0x6466a
movl -0x78(%rbp), %eax
subl $0x4, %eax
je 0x64684
jmp 0x64674
movl -0x78(%rbp), %eax
addl $-0x7, %eax
subl $0x2, %eax
jb 0x646a0
jmp 0x647ce
movq -0x48(%rbp), %rax
movl -0x50(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x647ce
movq -0x48(%rbp), %rax
movl -0x50(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, -0x74(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq -0x28(%rbp), %rdx
movslq -0x74(%rbp), %rax
addq %rax, %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x64370
cmpl $0x0, %eax
jge 0x646e5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x647e5
movq -0x60(%rbp), %rax
movslq -0x74(%rbp), %rcx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
movslq 0x18(%rcx), %rcx
cmpq %rcx, %rax
jg 0x6479d
cmpl $0x0, -0x3c(%rbp)
jge 0x6471d
movq -0x18(%rbp), %rax
movq (%rax), %rdi
addq -0x68(%rbp), %rdi
movslq -0x74(%rbp), %rdx
xorl %esi, %esi
callq 0x4250
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x74(%rbp), %eax
jae 0x6479b
movq -0x20(%rbp), %rax
movq (%rax), %rax
imulq $0x14, -0x68(%rbp), %rcx
addq %rcx, %rax
movq -0x70(%rbp), %rcx
movq -0x60(%rbp), %rdx
sarq %rdx
movzbl (%rcx,%rdx), %edx
movq -0x60(%rbp), %rcx
xorq $-0x1, %rcx
andq $0x1, %rcx
shlq $0x2, %rcx
sarl %cl, %edx
movl %edx, %ecx
andl $0xf, %ecx
movslq %ecx, %rdx
leaq 0xa91c5(%rip), %rcx     # 0x10d930
movzbl (%rcx,%rdx), %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x64724
jmp 0x647b5
movslq -0x74(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movslq -0x74(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x647ce
movq -0x48(%rbp), %rax
movl -0x50(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, %eax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x647d0
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x6462f
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)