// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/cram/cram_decode.c
// Function: cram_decode_aux
static int cram_decode_aux(cram_fd *fd,
                           cram_container *c, cram_slice *s,
                           cram_block *blk, cram_record *cr,
                           int *has_MD, int *has_NM) {
    int i, r = 0, out_sz = 1;
    int32_t TL = 0;
    unsigned char *TN;
    uint32_t ds = s->data_series;

    if (!(ds & (CRAM_TL|CRAM_aux))) {
        cr->aux = 0;
        cr->aux_size = 0;
        return 0;
    }

    if (!c->comp_hdr->codecs[DS_TL]) return -1;
    r |= c->comp_hdr->codecs[DS_TL]->decode(s, c->comp_hdr->codecs[DS_TL], blk,
                                            (char *)&TL, &out_sz);
    if (r || TL < 0 || TL >= c->comp_hdr->nTL)
        return -1;

    TN = c->comp_hdr->TL[TL];
    cr->ntags = strlen((char *)TN)/3; // optimise to remove strlen

    //printf("TC=%d\n", cr->ntags);
    cr->aux_size = 0;
    cr->aux = BLOCK_SIZE(s->aux_blk);

    if (!(ds & CRAM_aux))
        return 0;

    for (i = 0; i < cr->ntags; i++) {
        int32_t id, out_sz = 1;
        unsigned char tag_data[7];
        cram_map *m;

        if (TN[0] == 'M' && TN[1] == 'D' && has_MD)
            *has_MD = (BLOCK_SIZE(s->aux_blk)+3) * (TN[2] == '*' ? -1 : 1);
        if (TN[0] == 'N' && TN[1] == 'M' && has_NM)
            *has_NM = (BLOCK_SIZE(s->aux_blk)+3) * (TN[2] == '*' ? -1 : 1);;

        //printf("Tag %d/%d\n", i+1, cr->ntags);
        tag_data[0] = TN[0];
        tag_data[1] = TN[1];
        tag_data[2] = TN[2];
        id = (tag_data[0]<<16) | (tag_data[1]<<8) | tag_data[2];

        if (CRAM_MAJOR_VERS(fd->version) >= 4 && TN[2] == '*') {
            // Place holder, fill out contents later.
            int tag_data_size;
            if (TN[0] == 'N' && TN[1] == 'M') {
                // Use a fixed size, so we can allocate room for it now.
                memcpy(&tag_data[2], "I\0\0\0\0", 5);
                tag_data_size = 7;
            } else if (TN[0] == 'R' && TN[1] == 'G') {
                // RG is variable size, but known already.  Insert now
                TN += 3;
                // Equiv to fd->header->hrecs->rg[cr->rg], but this is the
                // new header API equivalent.
                const char *rg = sam_hdr_line_name(fd->header, "RG", cr->rg);
                if (!rg)
                    continue;

                size_t rg_len = strlen(rg);
                tag_data[2] = 'Z';
                BLOCK_APPEND(s->aux_blk, (char *)tag_data, 3);
                BLOCK_APPEND(s->aux_blk, rg, rg_len);
                BLOCK_APPEND_CHAR(s->aux_blk, '\0');
                cr->aux_size += 3 + rg_len + 1;
                cr->rg = -1; // prevents auto-add later
                continue;
            } else {
                // Unknown size.  We'll insert MD into stream later.
                tag_data[2] = 'Z';
                tag_data_size = 3;
            }
            BLOCK_APPEND(s->aux_blk, (char *)tag_data, tag_data_size);
            cr->aux_size += tag_data_size;
            TN += 3;
        } else {
            TN += 3;
            m = map_find(c->comp_hdr->tag_encoding_map, tag_data, id);
            if (!m)
                return -1;

            BLOCK_APPEND(s->aux_blk, (char *)tag_data, 3);

            if (!m->codec) return -1;
            r |= m->codec->decode(s, m->codec, blk, (char *)s->aux_blk, &out_sz);
            if (r) break;
            cr->aux_size += out_sz + 3;

            // cF CRAM flags.
            if (TN[-3]=='c' && TN[-2]=='F' && TN[-1]=='C' && out_sz == 1) {
                // Remove cF tag
                uint8_t cF = BLOCK_END(s->aux_blk)[-1];
                BLOCK_SIZE(s->aux_blk) -= out_sz+3;
                cr->aux_size -= out_sz+3;

                // bit 1 => don't auto-decode MD.
                // Pretend MD is present verbatim, so we don't auto-generate
                if ((cF & 1) && has_MD && *has_MD == 0)
                    *has_MD = 1;

                // bit 1 => don't auto-decode NM
                if ((cF & 2) && has_NM && *has_NM == 0)
                    *has_NM = 1;
            }
        }

        // We could go to 2^32 fine, but we shouldn't be hitting this anyway,
        // and it's protecting against memory hogs too.
        if (BLOCK_SIZE(s->aux_blk) > (1u<<31)) {
            hts_log_error("CRAM->BAM aux block size overflow");
            goto block_err;
        }
    }

    return r;

 block_err:
    return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x1, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movq -0x20(%rbp), %rax
movl 0xd8(%rax), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
andl $0x40004000, %eax       # imm = 0x40004000
cmpl $0x0, %eax
jne 0x54788
movq -0x30(%rbp), %rax
movl $0x0, 0x54(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0x58(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x54d27
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x380(%rax)
jne 0x547a6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x54d27
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x380(%rax), %rax
movq 0x28(%rax), %rax
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq 0x48(%rcx), %rcx
movq 0x380(%rcx), %rsi
movq -0x28(%rbp), %rdx
leaq -0x48(%rbp), %rcx
leaq -0x44(%rbp), %r8
callq *%rax
orl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jne 0x547fc
cmpl $0x0, -0x48(%rbp)
jl 0x547fc
movl -0x48(%rbp), %eax
movq -0x18(%rbp), %rcx
movq 0x48(%rcx), %rcx
cmpl 0x58(%rcx), %eax
jl 0x54808
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x54d27
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x60(%rax), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x41b0
movl $0x3, %ecx
xorl %edx, %edx
divq %rcx
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x50(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0x58(%rax)
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movq 0x30(%rax), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x54(%rax)
movl -0x54(%rbp), %eax
andl $0x40000000, %eax       # imm = 0x40000000
cmpl $0x0, %eax
jne 0x54878
movl $0x0, -0x4(%rbp)
jmp 0x54d27
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x50(%rcx), %eax
jge 0x54d18
movl $0x1, -0x5c(%rbp)
movq -0x50(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x4d, %eax
jne 0x548f0
movq -0x50(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x44, %eax
jne 0x548f0
cmpq $0x0, -0x38(%rbp)
je 0x548f0
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movq 0x30(%rax), %rax
addq $0x3, %rax
movq -0x50(%rbp), %rcx
movzbl 0x2(%rcx), %esi
movl $0x1, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0x2a, %esi
cmovel %edx, %ecx
movslq %ecx, %rcx
imulq %rcx, %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, (%rax)
movq -0x50(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x4e, %eax
jne 0x5494a
movq -0x50(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x4d, %eax
jne 0x5494a
cmpq $0x0, 0x10(%rbp)
je 0x5494a
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movq 0x30(%rax), %rax
addq $0x3, %rax
movq -0x50(%rbp), %rcx
movzbl 0x2(%rcx), %esi
movl $0x1, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0x2a, %esi
cmovel %edx, %ecx
movslq %ecx, %rcx
imulq %rcx, %rax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x50(%rbp), %rax
movb (%rax), %al
movb %al, -0x63(%rbp)
movq -0x50(%rbp), %rax
movb 0x1(%rax), %al
movb %al, -0x62(%rbp)
movq -0x50(%rbp), %rax
movb 0x2(%rax), %al
movb %al, -0x61(%rbp)
movzbl -0x63(%rbp), %eax
shll $0x10, %eax
movzbl -0x62(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x61(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x4, %eax
jl 0x54b24
movq -0x50(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x2a, %eax
jne 0x54b24
movq -0x50(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x4e, %eax
jne 0x549db
movq -0x50(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x4d, %eax
jne 0x549db
movl 0x9ecf2(%rip), %eax     # 0xf36b5
movl %eax, -0x61(%rbp)
movb 0x9eced(%rip), %al      # 0xf36b9
movb %al, -0x5d(%rbp)
movl $0x7, -0x74(%rbp)
jmp 0x54ae4
movq -0x50(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x52, %eax
jne 0x54ad7
movq -0x50(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x47, %eax
jne 0x54ad7
movq -0x50(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rax
movl 0x20(%rax), %edx
leaq 0x9dbf8(%rip), %rsi     # 0xf2616
callq 0xdd110
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0x54a33
jmp 0x54d0a
movq -0x80(%rbp), %rdi
callq 0x41b0
movq %rax, -0x88(%rbp)
movb $0x5a, -0x61(%rbp)
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rdi
leaq -0x63(%rbp), %rsi
movl $0x3, %edx
callq 0x4eff0
cmpl $0x0, %eax
jge 0x54a6a
jmp 0x54d20
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rdi
movq -0x80(%rbp), %rsi
movq -0x88(%rbp), %rdx
callq 0x4eff0
cmpl $0x0, %eax
jge 0x54a8f
jmp 0x54d20
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rdi
xorl %esi, %esi
callq 0x4f070
cmpl $0x0, %eax
jge 0x54aab
jmp 0x54d20
movq -0x88(%rbp), %rdx
addq $0x3, %rdx
addq $0x1, %rdx
movq -0x30(%rbp), %rax
movl 0x58(%rax), %ecx
addq %rdx, %rcx
movl %ecx, 0x58(%rax)
movq -0x30(%rbp), %rax
movl $0xffffffff, 0x20(%rax) # imm = 0xFFFFFFFF
jmp 0x54d0a
movb $0x5a, -0x61(%rbp)
movl $0x3, -0x74(%rbp)
jmp 0x54ae4
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rdi
leaq -0x63(%rbp), %rsi
movslq -0x74(%rbp), %rdx
callq 0x4eff0
cmpl $0x0, %eax
jge 0x54b06
jmp 0x54d20
movl -0x74(%rbp), %ecx
movq -0x30(%rbp), %rax
addl 0x58(%rax), %ecx
movl %ecx, 0x58(%rax)
movq -0x50(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x50(%rbp)
jmp 0x54cd6
movq -0x50(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x180, %rdi            # imm = 0x180
leaq -0x63(%rbp), %rsi
movl -0x58(%rbp), %edx
callq 0x59df0
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0x54b62
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x54d27
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rdi
leaq -0x63(%rbp), %rsi
movl $0x3, %edx
callq 0x4eff0
cmpl $0x0, %eax
jge 0x54b85
jmp 0x54d20
movq -0x70(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x54b9c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x54d27
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x28(%rax), %rax
movq -0x20(%rbp), %rdi
movq -0x70(%rbp), %rcx
movq 0x10(%rcx), %rsi
movq -0x28(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq 0x90(%rcx), %rcx
leaq -0x5c(%rbp), %r8
callq *%rax
orl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x54bda
jmp 0x54d18
movl -0x5c(%rbp), %ecx
addl $0x3, %ecx
movq -0x30(%rbp), %rax
addl 0x58(%rax), %ecx
movl %ecx, 0x58(%rax)
movq -0x50(%rbp), %rax
movzbl -0x3(%rax), %eax
cmpl $0x63, %eax
jne 0x54cd4
movq -0x50(%rbp), %rax
movzbl -0x2(%rax), %eax
cmpl $0x46, %eax
jne 0x54cd4
movq -0x50(%rbp), %rax
movzbl -0x1(%rax), %eax
cmpl $0x43, %eax
jne 0x54cd4
cmpl $0x1, -0x5c(%rbp)
jne 0x54cd4
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movq 0x20(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x90(%rcx), %rcx
movq 0x30(%rcx), %rcx
movb -0x1(%rax,%rcx), %al
movb %al, -0x89(%rbp)
movl -0x5c(%rbp), %eax
addl $0x3, %eax
movslq %eax, %rdx
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movq 0x30(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x30(%rax)
movl -0x5c(%rbp), %edx
addl $0x3, %edx
movq -0x30(%rbp), %rax
movl 0x58(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x58(%rax)
movzbl -0x89(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x54ca9
cmpq $0x0, -0x38(%rbp)
je 0x54ca9
movq -0x38(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x54ca9
movq -0x38(%rbp), %rax
movl $0x1, (%rax)
movzbl -0x89(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x54cd2
cmpq $0x0, 0x10(%rbp)
je 0x54cd2
movq 0x10(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x54cd2
movq 0x10(%rbp), %rax
movl $0x1, (%rax)
jmp 0x54cd4
jmp 0x54cd6
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movl $0x80000000, %ecx       # imm = 0x80000000
cmpq %rcx, 0x30(%rax)
jbe 0x54d08
movl $0x1, %edi
leaq 0x9ed85(%rip), %rsi     # 0xf3a7d
leaq 0x9ed8e(%rip), %rdx     # 0xf3a8d
movb $0x0, %al
callq 0x1b030
jmp 0x54d20
jmp 0x54d0a
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x5487f
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x54d27
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)