// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/cram/cram_encode.c
// Function: cram_add_to_ref_MD
static int cram_add_to_ref_MD(bam1_t *b, char **ref, uint32_t (**hist)[5],
                              hts_pos_t ref_start, hts_pos_t *ref_end,
                              const uint8_t *MD) {
    uint8_t *seq = bam_get_seq(b);
    uint32_t *cigar = bam_get_cigar(b);
    uint32_t ncigar = b->core.n_cigar;
    uint32_t cig_op = 0, cig_len = 0, cig_ind = 0;

    int iseq = 0, next_op;
    hts_pos_t iref = b->core.pos - ref_start;

    // Skip INS, REF_SKIP, *CLIP, PAD. and BACK.
    static int cig_skip[16] = {0,1,0,1,1,1,1,0,0,1,1,1,1,1,1,1};
    while (iseq < b->core.l_qseq && *MD) {
        if (isdigit(*MD)) {
            // match
            int overflow = 0;
            int len = hts_str2uint((char *)MD, (char **)&MD, 31, &overflow);
            if (overflow ||
                extend_ref(ref, hist, iref+ref_start + len,
                           ref_start, ref_end) < 0)
                return -1;
            while (iseq < b->core.l_qseq && len) {
                // rewrite to have internal loops?
                if ((next_op = next_cigar_op(cigar, ncigar, cig_skip,
                                             &iseq, &cig_ind, &cig_op,
                                             &cig_len)) < 0)
                    return -1;

                if (next_op != BAM_CMATCH &&
                    next_op != BAM_CEQUAL) {
                    hts_log_info("MD:Z and CIGAR are incompatible for "
                                 "record %s", bam_get_qname(b));
                    return -1;
                }

                // Short-cut loop over same cigar op for efficiency
                cig_len++;
                do {
                    cig_len--;
                    (*ref)[iref++] = seq_nt16_str[bam_seqi(seq, iseq)];
                    iseq++;
                    len--;
                } while (cig_len && iseq < b->core.l_qseq && len);
            }
            if (len > 0)
                return -1; // MD is longer than seq
        } else if (*MD == '^') {
            // deletion
            MD++;
            while (isalpha(*MD)) {
                if (extend_ref(ref, hist, iref+ref_start, ref_start,
                               ref_end) < 0)
                    return -1;
                if ((next_op = next_cigar_op(cigar, ncigar, cig_skip,
                                             &iseq, &cig_ind, &cig_op,
                                             &cig_len)) < 0)
                    return -1;

                if (next_op != BAM_CDEL) {
                    hts_log_info("MD:Z and CIGAR are incompatible");
                    return -1;
                }

                (*ref)[iref++] = *MD++ & ~0x20;
            }
        } else {
            // substitution
            if (extend_ref(ref, hist, iref+ref_start, ref_start, ref_end) < 0)
                return -1;
            if ((next_op = next_cigar_op(cigar, ncigar, cig_skip,
                                         &iseq, &cig_ind, &cig_op,
                                         &cig_len)) < 0)
                return -1;

            if (next_op != BAM_CMATCH && next_op != BAM_CDIFF) {
                hts_log_info("MD:Z and CIGAR are incompatible");
                return -1;
            }

            (*ref)[iref++] = *MD++ & ~0x20;
            iseq++;
        }
    }

    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 %r9, -0x38(%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, -0x40(%rbp)
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)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
subq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x5c(%rbp), %ecx
movq -0x10(%rbp), %rdx
xorl %eax, %eax
cmpl 0x18(%rdx), %ecx
movb %al, -0x71(%rbp)
jge 0x648bc
movq -0x38(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0x648c8
jmp 0x64c81
callq 0x44f0
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movzbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x64a9d
movl $0x0, -0x6c(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x38(%rbp), %rsi
movl $0x1f, %edx
leaq -0x6c(%rbp), %rcx
callq 0x64ca0
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x6c(%rbp)
jne 0x6493b
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq -0x28(%rbp), %rdx
movslq -0x70(%rbp), %rax
addq %rax, %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x64370
cmpl $0x0, %eax
jge 0x64947
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x64c88
jmp 0x64949
movl -0x5c(%rbp), %ecx
movq -0x10(%rbp), %rdx
xorl %eax, %eax
cmpl 0x18(%rdx), %ecx
movb %al, -0x72(%rbp)
jge 0x64964
cmpl $0x0, -0x70(%rbp)
setne %al
movb %al, -0x72(%rbp)
movb -0x72(%rbp), %al
testb $0x1, %al
jne 0x64970
jmp 0x64a86
movq -0x48(%rbp), %rdi
movl -0x4c(%rbp), %esi
leaq 0xa8fc2(%rip), %rdx     # 0x10d940
leaq -0x5c(%rbp), %rcx
leaq -0x58(%rbp), %r8
leaq -0x50(%rbp), %r9
leaq -0x54(%rbp), %rax
movq %rax, (%rsp)
callq 0x64e60
movl %eax, -0x60(%rbp)
cmpl $0x0, %eax
jge 0x649ab
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x64c88
cmpl $0x0, -0x60(%rbp)
je 0x649e5
cmpl $0x7, -0x60(%rbp)
je 0x649e5
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
movl $0x4, %edi
leaq 0x8fce0(%rip), %rsi     # 0xf46ab
leaq 0x8fcec(%rip), %rdx     # 0xf46be
movb $0x0, %al
callq 0x1b030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x64c88
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x40(%rbp), %rax
movl -0x5c(%rbp), %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl -0x5c(%rbp), %ecx
xorl $-0x1, %ecx
andl $0x1, %ecx
shll $0x2, %ecx
sarl %cl, %eax
andl $0xf, %eax
movslq %eax, %rcx
leaq 0x8b20e(%rip), %rax     # 0xefc30
movb (%rax,%rcx), %dl
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x68(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x68(%rbp)
movb %dl, (%rax,%rcx)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl -0x70(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x70(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x54(%rbp)
movb %al, -0x73(%rbp)
je 0x64a76
movl -0x5c(%rbp), %ecx
movq -0x10(%rbp), %rdx
xorl %eax, %eax
cmpl 0x18(%rdx), %ecx
movb %al, -0x73(%rbp)
jge 0x64a76
cmpl $0x0, -0x70(%rbp)
setne %al
movb %al, -0x73(%rbp)
movb -0x73(%rbp), %al
testb $0x1, %al
jne 0x649ee
jmp 0x64949
cmpl $0x0, -0x70(%rbp)
jle 0x64a98
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x64c88
jmp 0x64c7c
movq -0x38(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x5e, %eax
jne 0x64bac
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
callq 0x44f0
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movzbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
je 0x64ba7
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq -0x28(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x64370
cmpl $0x0, %eax
jge 0x64b0b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x64c88
movq -0x48(%rbp), %rdi
movl -0x4c(%rbp), %esi
leaq 0xa8e27(%rip), %rdx     # 0x10d940
leaq -0x5c(%rbp), %rcx
leaq -0x58(%rbp), %r8
leaq -0x50(%rbp), %r9
leaq -0x54(%rbp), %rax
movq %rax, (%rsp)
callq 0x64e60
movl %eax, -0x60(%rbp)
cmpl $0x0, %eax
jge 0x64b46
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x64c88
cmpl $0x2, -0x60(%rbp)
je 0x64b72
movl $0x4, %edi
leaq 0x8fb53(%rip), %rsi     # 0xf46ab
leaq 0x8fb8d(%rip), %rdx     # 0xf46ec
movb $0x0, %al
callq 0x1b030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x64c88
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rbp)
movzbl (%rax), %eax
andl $-0x21, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x68(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x68(%rbp)
movb %dl, (%rax,%rcx)
jmp 0x64ab9
jmp 0x64c7a
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq -0x28(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x64370
cmpl $0x0, %eax
jge 0x64bda
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x64c88
movq -0x48(%rbp), %rdi
movl -0x4c(%rbp), %esi
leaq 0xa8d58(%rip), %rdx     # 0x10d940
leaq -0x5c(%rbp), %rcx
leaq -0x58(%rbp), %r8
leaq -0x50(%rbp), %r9
leaq -0x54(%rbp), %rax
movq %rax, (%rsp)
callq 0x64e60
movl %eax, -0x60(%rbp)
cmpl $0x0, %eax
jge 0x64c12
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x64c88
cmpl $0x0, -0x60(%rbp)
je 0x64c41
cmpl $0x8, -0x60(%rbp)
je 0x64c41
movl $0x4, %edi
leaq 0x8fa81(%rip), %rsi     # 0xf46ab
leaq 0x8fabb(%rip), %rdx     # 0xf46ec
movb $0x0, %al
callq 0x1b030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x64c88
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rbp)
movzbl (%rax), %eax
andl $-0x21, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x68(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x68(%rbp)
movb %dl, (%rax,%rcx)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x64c7c
jmp 0x6489b
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)