// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %r8, %r12
movl $0x1, 0x44(%rsp)
movl $0x0, 0x40(%rsp)
movl 0xd8(%rdx), %ebx
testl $0x40004000, %ebx       # imm = 0x40004000
je 0x3c8df
movq %r9, %r13
movq %rsi, %r14
movq 0x48(%rsi), %rax
movq 0x380(%rax), %rsi
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
testq %rsi, %rsi
je 0x3c8f8
movq %rdi, %rbp
leaq 0x40(%rsp), %r15
leaq 0x44(%rsp), %r8
movq %rdx, 0x10(%rsp)
movq %rdx, %rdi
movq %rcx, 0x50(%rsp)
movq %rcx, %rdx
movq %r15, %rcx
callq *0x28(%rsi)
testl %eax, %eax
setne %cl
movl (%r15), %eax
testl %eax, %eax
sets %dl
orb %cl, %dl
jne 0x3c8f2
movl %eax, %eax
movq 0x48(%r14), %rcx
cmpl 0x58(%rcx), %eax
jge 0x3c8f2
movq 0x60(%rcx), %rcx
movq (%rcx,%rax,8), %r15
movq %r15, %rdi
callq 0x41d0
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq %rdx
movl %edx, 0x50(%r12)
movl $0x0, 0x58(%r12)
movq 0x10(%rsp), %rdi
movq 0x90(%rdi), %rax
movl 0x30(%rax), %eax
movl %eax, 0x54(%r12)
xorl %r9d, %r9d
btl $0x1e, %ebx
jae 0x3c8f8
cmpl $0x0, 0x50(%r12)
jle 0x3c8f8
xorl %ebx, %ebx
xorl %r9d, %r9d
movq %r13, 0x28(%rsp)
movq %r14, 0x60(%rsp)
movq %rbp, 0x58(%rsp)
movq %r12, 0x38(%rsp)
movl $0x1, 0x34(%rsp)
cmpb $0x4d, (%r15)
jne 0x3c3e1
testq %r13, %r13
je 0x3c3e1
cmpb $0x44, 0x1(%r15)
jne 0x3c3e1
movq 0x90(%rdi), %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, (%r13)
cmpb $0x4e, (%r15)
jne 0x3c41f
cmpq $0x0, 0xa0(%rsp)
je 0x3c41f
cmpb $0x4d, 0x1(%r15)
jne 0x3c41f
movq 0x90(%rdi), %rax
movl 0x30(%rax), %eax
movl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
subl %eax, %ecx
addl $0x3, %eax
cmpb $0x2a, 0x2(%r15)
cmovel %ecx, %eax
movq 0xa0(%rsp), %rcx
movl %eax, (%rcx)
movzbl (%r15), %ecx
movb %cl, 0x21(%rsp)
movzbl 0x1(%r15), %eax
movb %al, 0x22(%rsp)
movzbl 0x2(%r15), %edx
movb %dl, 0x23(%rsp)
cmpl $0x400, 0xc(%rbp)       # imm = 0x400
jl 0x3c488
cmpb $0x2a, 0x2(%r15)
jne 0x3c488
movzbl (%r15), %eax
cmpl $0x52, %eax
movl %r9d, 0xc(%rsp)
je 0x3c683
cmpl $0x4e, %eax
jne 0x3c7b2
cmpb $0x4d, 0x1(%r15)
jne 0x3c67b
leaq 0x23(%rsp), %rax
movb $0x0, 0x4(%rax)
movl $0x49, (%rax)
movl $0x7, %eax
jmp 0x3c7bc
movq 0x48(%r14), %rsi
leal (%rcx,%rcx,2), %r8d
addl %eax, %r8d
andl $0x1f, %r8d
movq 0x180(%rsi,%r8,8), %r14
testq %r14, %r14
je 0x3c4bc
shll $0x10, %ecx
shll $0x8, %eax
orl %ecx, %eax
orl %edx, %eax
cmpl %eax, (%r14)
je 0x3c4bc
movq 0x18(%r14), %r14
testq %r14, %r14
jne 0x3c4ae
leaq 0x3(%r15), %rax
movl $0x1, %ebp
testq %r14, %r14
je 0x3c59e
movq %rax, 0x18(%rsp)
movq 0x90(%rdi), %r12
movq 0x28(%r12), %rax
movq 0x30(%r12), %r13
addq $0x3, %r13
cmpq %r13, %rax
ja 0x3c537
movl %r9d, 0xc(%rsp)
leaq 0x320(%rax), %rcx
shrq $0x2, %rcx
addq %rcx, %rax
addq $0x320, %rax            # imm = 0x320
cmpq %r13, %rax
cmovaq %rax, %r13
movq 0x20(%r12), %rdi
movq %r13, %rsi
callq 0x4840
testq %rax, %rax
je 0x3c658
movq %r13, 0x28(%r12)
movq %rax, 0x20(%r12)
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %r9d
movq 0x20(%r12), %rax
movq 0x30(%r12), %rcx
movb 0x23(%rsp), %dl
movb %dl, 0x2(%rax,%rcx)
movzwl 0x21(%rsp), %edx
movw %dx, (%rax,%rcx)
addq $0x3, 0x30(%r12)
movq 0x10(%r14), %rsi
testq %rsi, %rsi
je 0x3c5a3
movq 0x90(%rdi), %rcx
movq 0x50(%rsp), %rdx
leaq 0x34(%rsp), %r8
movl %r9d, %ebp
callq *0x28(%rsi)
movl %ebp, %r9d
orl %eax, %r9d
movl $0x2, %ebp
movq 0x38(%rsp), %r12
movq 0x28(%rsp), %r13
je 0x3c5b7
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %rdi
jmp 0x3c89e
movq %rax, %r15
jmp 0x3c5ad
movq 0x18(%rsp), %r15
movq 0x38(%rsp), %r12
movq 0x28(%rsp), %r13
jmp 0x3c89e
movl 0x34(%rsp), %ecx
movl 0x58(%r12), %eax
leal (%rcx,%rax), %edx
addl $0x3, %edx
movl %edx, 0x58(%r12)
xorl %r9d, %r9d
cmpb $0x63, (%r15)
movq 0x10(%rsp), %rdi
jne 0x3c64e
cmpb $0x46, 0x1(%r15)
jne 0x3c64e
cmpb $0x43, 0x2(%r15)
sete %dl
cmpl $0x1, %ecx
sete %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x3c64e
movq 0x90(%rdi), %rdx
movq 0x20(%rdx), %rcx
movq 0x30(%rdx), %rsi
movzbl -0x1(%rcx,%rsi), %ecx
addq $-0x4, %rsi
movq %rsi, 0x30(%rdx)
movl %eax, 0x58(%r12)
testq %r13, %r13
movq 0xa0(%rsp), %rdx
je 0x3c639
movl %ecx, %eax
andl $0x1, %eax
je 0x3c639
cmpl $0x0, (%r13)
jne 0x3c639
movl $0x1, (%r13)
testq %rdx, %rdx
je 0x3c64e
andl $0x2, %ecx
je 0x3c64e
cmpl $0x0, (%rdx)
jne 0x3c64e
movl $0x1, (%rdx)
movq 0x18(%rsp), %rdx
jmp 0x3c851
movl $0x5, %ebp
movq 0x18(%rsp), %r15
movq 0x38(%rsp), %r12
movq 0x28(%rsp), %r13
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %r9d
jmp 0x3c89e
cmpb $0x52, %al
jne 0x3c7b2
cmpb $0x47, 0x1(%r15)
jne 0x3c7b2
addq $0x3, %r15
movq 0x18(%rbp), %rdi
movl 0x20(%r12), %edx
leaq 0x5ea43(%rip), %rsi     # 0x9b0e5
callq 0x8b559
movl $0x4, %ebp
testq %rax, %rax
je 0x3c7aa
movq %rax, %rdi
movq %rax, %r14
callq 0x41d0
movq %r14, %rsi
movb $0x5a, 0x23(%rsp)
movq 0x10(%rsp), %r8
movq 0x90(%r8), %r14
movq 0x28(%r14), %rdx
movq 0x30(%r14), %r13
addq $0x3, %r13
cmpq %r13, %rdx
movq %rax, 0x18(%rsp)
ja 0x3c737
movq %rsi, 0x48(%rsp)
leaq 0x320(%rdx), %rcx
shrq $0x2, %rcx
leaq (%rcx,%rdx), %rax
addq $0x320, %rax            # imm = 0x320
cmpq %r13, %rax
cmovaq %rax, %r13
movq 0x20(%r14), %rdi
movq %r13, %rsi
callq 0x4840
testq %rax, %rax
je 0x3c832
movq %r13, 0x28(%r14)
movq %rax, 0x20(%r14)
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rax
movq 0x48(%rsp), %rsi
movq 0x20(%r14), %rdi
movq 0x30(%r14), %rcx
movb 0x23(%rsp), %dl
movb %dl, 0x2(%rdi,%rcx)
movzwl 0x21(%rsp), %edx
movw %dx, (%rdi,%rcx)
addq $0x3, 0x30(%r14)
movq 0x90(%r8), %rdi
movq %rax, %rdx
movq %r8, %r14
callq 0x386e4
testl %eax, %eax
js 0x3c8c0
movq 0x90(%r14), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x38769
testl %eax, %eax
movq 0x28(%rsp), %r13
js 0x3c8d5
movl 0x58(%r12), %eax
movq 0x18(%rsp), %rcx
addl %ecx, %eax
addl $0x4, %eax
movl %eax, 0x58(%r12)
movl $0xffffffff, 0x20(%r12) # imm = 0xFFFFFFFF
xorl %r14d, %r14d
jmp 0x3c83f
movb $0x5a, 0x23(%rsp)
movl $0x3, %eax
movq 0x90(%rdi), %r14
movl %eax, %ebp
movq 0x30(%r14), %r13
addq %rbp, %r13
movq 0x28(%r14), %rax
cmpq %r13, %rax
ja 0x3c809
leaq 0x320(%rax), %rcx
shrq $0x2, %rcx
addq %rcx, %rax
addq $0x320, %rax            # imm = 0x320
cmpq %r13, %rax
cmovaq %rax, %r13
movq 0x20(%r14), %rdi
movq %r13, %rsi
callq 0x4840
testq %rax, %rax
je 0x3c832
movq %r13, 0x28(%r14)
movq %rax, 0x20(%r14)
movq 0x20(%r14), %rdi
addq 0x30(%r14), %rdi
leaq 0x21(%rsp), %rsi
movq %rbp, %rdx
callq 0x43c0
addq %rbp, 0x30(%r14)
addl %ebp, 0x58(%r12)
addq $0x3, %r15
xorl %ebp, %ebp
movb $0x1, %r14b
jmp 0x3c83a
movl $0x5, %ebp
xorl %r14d, %r14d
movq 0x28(%rsp), %r13
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %r9d
movq %r15, %rdx
testb %r14b, %r14b
je 0x3c89e
movq 0x90(%rdi), %rax
movl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
addq $-0x7ffffffd, %rcx      # imm = 0x80000003
xorl %ebp, %ebp
cmpq %rcx, 0x30(%rax)
jbe 0x3c89b
movl $0x1, %edi
leaq 0x5fc31(%rip), %rsi     # 0x9c4a9
movq %rdx, %r15
leaq 0x5fc37(%rip), %rdx     # 0x9c4b9
xorl %eax, %eax
movl %r9d, %ebp
callq 0x14378
movl %ebp, %r9d
movq 0x10(%rsp), %rdi
movl $0x5, %ebp
jmp 0x3c89e
movq %rdx, %r15
testl %ebp, %ebp
je 0x3c8a7
cmpl $0x4, %ebp
jne 0x3c8ed
incl %ebx
cmpl 0x50(%r12), %ebx
movq 0x60(%rsp), %r14
movq 0x58(%rsp), %rbp
jl 0x3c3a7
jmp 0x3c8f8
movq %r14, %rdi
xorl %r14d, %r14d
movl $0x5, %ebp
movq 0x28(%rsp), %r13
jmp 0x3c844
movl $0x5, %ebp
jmp 0x3c83f
movq $0x0, 0x54(%r12)
xorl %r9d, %r9d
jmp 0x3c8f8
cmpl $0x2, %ebp
je 0x3c8f8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %r9d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq