// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %rbx
movl $0x1, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl 0xd8(%rdx), %r15d
testl $0x40004000, %r15d      # imm = 0x40004000
je 0x3ace7
movq %rsi, %r13
movq 0x48(%rsi), %rax
movq 0x380(%rax), %rsi
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rsi, %rsi
je 0x3acf1
movq %rdx, %r12
movq %rdi, 0x30(%rsp)
movq %r9, 0x18(%rsp)
leaq 0x28(%rsp), %r14
leaq 0x2c(%rsp), %r8
movq %rdx, %rdi
movq %rcx, 0x40(%rsp)
movq %rcx, %rdx
movq %r14, %rcx
callq *0x28(%rsi)
testl %eax, %eax
setne %cl
movl (%r14), %eax
testl %eax, %eax
sets %dl
orb %cl, %dl
jne 0x3acf1
movl %eax, %eax
movq 0x48(%r13), %rcx
cmpl 0x58(%rcx), %eax
jge 0x3acf1
movq 0x60(%rcx), %rcx
movq (%rcx,%rax,8), %rdi
movq %rdi, %r14
callq 0x41d0
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq %rdx
movl %edx, 0x50(%rbx)
movl $0x0, 0x58(%rbx)
movq 0x90(%r12), %rax
movl 0x30(%rax), %eax
movl %eax, 0x54(%rbx)
xorl %ebp, %ebp
btl $0x1e, %r15d
jae 0x3acf1
testl %edx, %edx
movq 0x18(%rsp), %r8
movq 0x30(%rsp), %rdi
jle 0x3acf1
movq %r14, %r15
xorl %r14d, %r14d
movl $0x1, 0x14(%rsp)
movb (%r15), %cl
cmpb $0x4d, %cl
jne 0x3a8ea
movb $0x4d, %cl
testq %r8, %r8
je 0x3a92d
cmpb $0x44, 0x1(%r15)
jne 0x3a92d
movq 0x90(%r12), %rax
movl 0x30(%rax), %eax
movl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
subl %eax, %ecx
addl $0x3, %eax
cmpb $0x2a, 0x2(%r15)
cmovel %ecx, %eax
movl %eax, (%r8)
movb (%r15), %cl
cmpb $0x4e, %cl
jne 0x3a92d
movb $0x4e, %cl
cmpq $0x0, 0x80(%rsp)
je 0x3a92d
cmpb $0x4d, 0x1(%r15)
jne 0x3a92d
movq 0x90(%r12), %rax
movl 0x30(%rax), %eax
movl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
subl %eax, %ecx
addl $0x3, %eax
cmpb $0x2a, 0x2(%r15)
cmovel %ecx, %eax
movq 0x80(%rsp), %rcx
movl %eax, (%rcx)
movb (%r15), %cl
movb %cl, 0x9(%rsp)
movzbl 0x1(%r15), %eax
movb %al, 0xa(%rsp)
movzbl 0x2(%r15), %esi
movb %sil, 0xb(%rsp)
movzbl %cl, %edx
cmpl $0x400, 0xc(%rdi)       # imm = 0x400
movq %r15, 0x20(%rsp)
movl %r14d, 0x10(%rsp)
jl 0x3a994
cmpb $0x2a, %sil
jne 0x3a994
cmpb $0x52, %cl
je 0x3a9f4
cmpl $0x4e, %edx
jne 0x3aa56
cmpl $0x4d, %eax
jne 0x3aa56
leaq 0xb(%rsp), %rax
movb $0x0, 0x4(%rax)
movl $0x49, (%rax)
movl $0x7, %eax
jmp 0x3aa60
movq 0x48(%r13), %rcx
leal (%rdx,%rdx,2), %edi
addl %eax, %edi
andl $0x1f, %edi
movq 0x180(%rcx,%rdi,8), %rbp
testq %rbp, %rbp
je 0x3ad1c
shll $0x10, %edx
shll $0x8, %eax
orl %edx, %eax
orl %esi, %eax
cmpl %eax, (%rbp)
je 0x3a9ce
movq 0x18(%rbp), %rbp
testq %rbp, %rbp
jne 0x3a9bb
jmp 0x3ad1c
movq 0x90(%r12), %r14
movq 0x28(%r14), %rax
movq 0x30(%r14), %rcx
leaq 0x3(%rcx), %r15
cmpq %r15, %rax
jbe 0x3aae8
movq 0x20(%r14), %rax
jmp 0x3ab24
cmpl $0x47, %eax
jne 0x3aa56
movq 0x18(%rdi), %rdi
movl 0x20(%rbx), %edx
leaq 0x5d6f1(%rip), %rsi     # 0x980f8
movq %r8, %rbp
callq 0x88cf7
movq %rbp, %r8
testq %rax, %rax
je 0x3ac22
movq %rax, 0x38(%rsp)
movq %rax, %rdi
callq 0x41d0
movq %rax, %r15
movb $0x5a, 0xb(%rsp)
movq 0x90(%r12), %r14
movq 0x28(%r14), %rax
movq 0x30(%r14), %rcx
leaq 0x3(%rcx), %rbp
cmpq %rbp, %rax
jbe 0x3ac3f
movq 0x20(%r14), %rax
jmp 0x3ac7b
movb $0x5a, 0xb(%rsp)
movl $0x3, %eax
movq 0x90(%r12), %r14
movl %eax, %ebp
movq 0x28(%r14), %rax
movq 0x30(%r14), %rdi
leaq (%rdi,%rbp), %r15
cmpq %r15, %rax
jbe 0x3aa81
movq 0x20(%r14), %rax
jmp 0x3aabd
leaq 0x320(%rax), %rcx
shrq $0x2, %rcx
addq %rcx, %rax
addq $0x320, %rax            # imm = 0x320
cmpq %r15, %rax
cmovaq %rax, %r15
movq 0x20(%r14), %rdi
movq %r15, %rsi
callq 0x4840
testq %rax, %rax
je 0x3ad1c
movq %r15, 0x28(%r14)
movq %rax, 0x20(%r14)
movq 0x30(%r14), %rdi
addq %rax, %rdi
leaq 0x9(%rsp), %rsi
movq %rbp, %rdx
callq 0x43c0
addq %rbp, 0x30(%r14)
addl %ebp, 0x58(%rbx)
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r15
movl 0x10(%rsp), %r14d
jmp 0x3ac04
leaq 0x320(%rax), %rcx
shrq $0x2, %rcx
addq %rcx, %rax
addq $0x320, %rax            # imm = 0x320
cmpq %r15, %rax
cmovaq %rax, %r15
movq 0x20(%r14), %rdi
movq %r15, %rsi
callq 0x4840
testq %rax, %rax
je 0x3ad1c
movq %r15, 0x28(%r14)
movq %rax, 0x20(%r14)
movq 0x30(%r14), %rcx
movb 0xb(%rsp), %dl
movb %dl, 0x2(%rax,%rcx)
movzwl 0x9(%rsp), %edx
movw %dx, (%rax,%rcx)
addq $0x3, 0x30(%r14)
movq 0x10(%rbp), %rsi
testq %rsi, %rsi
je 0x3ad1c
movq 0x90(%r12), %rcx
movq %r12, %rdi
movq 0x40(%rsp), %rdx
leaq 0x14(%rsp), %r8
callq *0x28(%rsi)
testl %eax, %eax
jne 0x3ad23
movl 0x14(%rsp), %ecx
movl 0x58(%rbx), %eax
leal (%rcx,%rax), %edx
addl $0x3, %edx
movl %edx, 0x58(%rbx)
movq 0x20(%rsp), %r15
cmpb $0x63, (%r15)
movq 0x18(%rsp), %r8
movl 0x10(%rsp), %r14d
jne 0x3ac04
cmpb $0x46, 0x1(%r15)
jne 0x3ac04
cmpb $0x43, 0x2(%r15)
sete %dl
cmpl $0x1, %ecx
sete %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x3ac04
movq 0x90(%r12), %rdx
movq 0x20(%rdx), %rcx
movq 0x30(%rdx), %rsi
movzbl -0x1(%rcx,%rsi), %ecx
addq $-0x4, %rsi
movq %rsi, 0x30(%rdx)
movl %eax, 0x58(%rbx)
testq %r8, %r8
je 0x3abe1
movl %ecx, %eax
andl $0x1, %eax
je 0x3abe1
cmpl $0x0, (%r8)
jne 0x3abe1
movl $0x1, (%r8)
cmpq $0x0, 0x80(%rsp)
je 0x3ac04
andl $0x2, %ecx
je 0x3ac04
movq 0x80(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x3ac04
movl $0x1, (%rax)
movq 0x90(%r12), %rax
movl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
addq $-0x7ffffffd, %rcx      # imm = 0x80000003
cmpq %rcx, 0x30(%rax)
ja 0x3ad02
addq $0x3, %r15
incl %r14d
xorl %ebp, %ebp
cmpl 0x50(%rbx), %r14d
movq 0x30(%rsp), %rdi
jl 0x3a8a9
jmp 0x3acf1
leaq 0x320(%rax), %rcx
shrq $0x2, %rcx
addq %rcx, %rax
addq $0x320, %rax            # imm = 0x320
cmpq %rbp, %rax
cmovaq %rax, %rbp
movq 0x20(%r14), %rdi
movq %rbp, %rsi
callq 0x4840
testq %rax, %rax
je 0x3ad1c
movq %rbp, 0x28(%r14)
movq %rax, 0x20(%r14)
movq 0x30(%r14), %rcx
movb 0xb(%rsp), %dl
movb %dl, 0x2(%rax,%rcx)
movzwl 0x9(%rsp), %edx
movw %dx, (%rax,%rcx)
addq $0x3, 0x30(%r14)
movq 0x90(%r12), %rdi
movq 0x38(%rsp), %rsi
movq %r15, %r14
movq %r15, %rdx
callq 0x372dc
testl %eax, %eax
js 0x3ad1c
movq 0x90(%r12), %rdi
xorl %esi, %esi
callq 0x37361
testl %eax, %eax
js 0x3ad1c
movl 0x58(%rbx), %eax
addl %r14d, %eax
addl $0x4, %eax
movl %eax, 0x58(%rbx)
movl $0xffffffff, 0x20(%rbx) # imm = 0xFFFFFFFF
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r15
movl 0x10(%rsp), %r14d
jmp 0x3ac22
movq $0x0, 0x54(%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5e81c(%rip), %rsi     # 0x99525
leaq 0x5e825(%rip), %rdx     # 0x99535
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x3acf1
movl %eax, %ebp
jmp 0x3acf1