// File: /workspace/github_cpp_makefile_star2_O1/karel-brinda[P]ococo/ext/htslib/cram/cram_decode.c
// Function: cram_decode_aux
static int cram_decode_aux(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 = c->comp_hdr->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[3];
	cram_map *m;

	if (TN[0] == 'M' && TN[1] == 'D' && has_MD)
	    *has_MD = 1;
	if (TN[0] == 'N' && TN[1] == 'M' && has_NM)
	    *has_NM = 1;

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

	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;
    }
    
    return r;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl $0x1, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
movq 0x40(%rdi), %rax
movl 0x410(%rax), %r14d
testl $0x40004000, %r14d      # imm = 0x40004000
je 0x21596
movq %rsi, %r13
movq 0x380(%rax), %rsi
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %rsi, %rsi
je 0x215a0
movq %r9, %r12
movq %rdi, %rbp
movq %r8, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
leaq 0x20(%rsp), %r15
leaq 0x24(%rsp), %r8
movq %r13, %rdi
movq %rdx, 0x40(%rsp)
movq %r15, %rcx
callq *0x18(%rsi)
testl %eax, %eax
setne %cl
movl (%r15), %eax
testl %eax, %eax
sets %dl
orb %cl, %dl
jne 0x215a0
movl %eax, %eax
movq 0x40(%rbp), %rcx
cmpl 0x58(%rcx), %eax
jge 0x215a0
movq 0x60(%rcx), %rcx
movq (%rcx,%rax,8), %r15
movq %r15, %rdi
callq 0x4230
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq %rdx
movq 0x10(%rsp), %rsi
movl %edx, 0x38(%rsi)
movl $0x0, 0x40(%rsi)
movq 0x88(%r13), %rax
movl 0x30(%rax), %eax
movl %eax, 0x3c(%rsi)
xorl %ebx, %ebx
btl $0x1e, %r14d
jae 0x215a0
movq %r12, 0x28(%rsp)
cmpl $0x0, 0x38(%rsi)
movq 0x8(%rsp), %rdx
jle 0x215a0
xorl %r12d, %r12d
movq 0x315b3(%rip), %xmm1    # 0x52930
movapd 0x315bb(%rip), %xmm2    # 0x52940
movsd 0x3267b(%rip), %xmm3    # 0x53a08
movsd 0x31573(%rip), %xmm4    # 0x52908
movl $0x400, %r8d            # imm = 0x400
xorl %ebx, %ebx
movl $0x1, 0x4(%rsp)
cmpb $0x4d, (%r15)
jne 0x213bd
testq %rdx, %rdx
je 0x213bd
cmpb $0x44, 0x1(%r15)
jne 0x213bd
movl $0x1, (%rdx)
cmpb $0x4e, (%r15)
jne 0x213dd
cmpq $0x0, 0x28(%rsp)
je 0x213dd
cmpb $0x4d, 0x1(%r15)
jne 0x213dd
movq 0x28(%rsp), %rax
movl $0x1, (%rax)
movzbl (%r15), %r9d
movzbl 0x1(%r15), %edi
movzbl 0x2(%r15), %r10d
movq 0x40(%rbp), %rax
leal (%r9,%r9,2), %ecx
addl %edi, %ecx
andl $0x1f, %ecx
movq 0x180(%rax,%rcx,8), %r14
testq %r14, %r14
je 0x21425
movl %r9d, %ecx
shll $0x10, %ecx
movzbl %dil, %eax
shll $0x8, %eax
orl %ecx, %eax
orl %r10d, %eax
cmpl %eax, (%r14)
je 0x21425
movq 0x18(%r14), %r14
testq %r14, %r14
jne 0x21417
movl $0x1, %eax
testq %r14, %r14
je 0x2157f
movl %r10d, 0x1c(%rsp)
movq %r9, 0x30(%rsp)
movq %rdi, 0x38(%rsp)
movq 0x88(%r13), %rcx
movq 0x28(%rcx), %rax
movq 0x30(%rcx), %rdx
leaq 0x3(%rdx), %rsi
cmpq %rsi, %rax
ja 0x214e0
movq %rax, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd %xmm3, %xmm1
cvttsd2si %xmm1, %rdx
movq %rdx, %rdi
subsd %xmm4, %xmm1
cvttsd2si %xmm1, %rsi
sarq $0x3f, %rdi
andq %rdi, %rsi
orq %rdx, %rsi
testq %rax, %rax
cmoveq %r8, %rsi
movq %rsi, 0x28(%rcx)
movq 0x20(%rcx), %rdi
callq 0x48d0
movl $0x400, %r8d            # imm = 0x400
movsd 0x31450(%rip), %xmm4    # 0x52908
movsd 0x32548(%rip), %xmm3    # 0x53a08
movapd 0x31478(%rip), %xmm2    # 0x52940
movq 0x31460(%rip), %xmm1    # 0x52930
movq 0x88(%r13), %rcx
movq %rax, 0x20(%rcx)
jmp 0x21449
movq 0x20(%rcx), %rax
movq 0x30(%rsp), %rcx
movb %cl, (%rax,%rdx)
movq 0x38(%rsp), %rcx
movb %cl, 0x1(%rax,%rdx)
movl 0x1c(%rsp), %ecx
movb %cl, 0x2(%rax,%rdx)
movq 0x88(%r13), %rcx
addq $0x3, 0x30(%rcx)
movq 0x10(%r14), %rsi
testq %rsi, %rsi
je 0x21570
movq %r13, %rdi
movq 0x40(%rsp), %rdx
leaq 0x4(%rsp), %r8
callq *0x18(%rsi)
movl $0x400, %r8d            # imm = 0x400
movsd 0x313d8(%rip), %xmm4    # 0x52908
movsd 0x324d0(%rip), %xmm3    # 0x53a08
movapd 0x31400(%rip), %xmm2    # 0x52940
movq 0x313e8(%rip), %xmm1    # 0x52930
orl %eax, %ebx
movl $0x2, %eax
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
jne 0x2157f
movl 0x4(%rsp), %eax
movl 0x40(%rsi), %ecx
addl %ecx, %eax
addl $0x3, %eax
movl %eax, 0x40(%rsi)
xorl %ebx, %ebx
xorl %eax, %eax
jmp 0x2157f
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
movl $0x1, %eax
testl %eax, %eax
jne 0x215b1
addq $0x3, %r15
incl %r12d
cmpl 0x38(%rsi), %r12d
jl 0x2139d
jmp 0x215a0
movq $0x0, 0x3c(%rcx)
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x2, %eax
je 0x215a0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x215a0