// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %rcx, %r14
movl $0x1, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
movq 0x40(%rdi), %rax
movl 0x410(%rax), %r12d
testl $0x40004000, %r12d      # imm = 0x40004000
je 0x20faf
movq %rsi, %r13
movq 0x380(%rax), %rsi
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %rsi, %rsi
je 0x20fb9
movq %rdi, %rbp
movq %r8, 0x28(%rsp)
movq %r9, 0x30(%rsp)
leaq 0x18(%rsp), %r15
leaq 0x1c(%rsp), %r8
movq %r13, %rdi
movq %rdx, 0x20(%rsp)
movq %r15, %rcx
callq *0x18(%rsi)
testl %eax, %eax
setne %cl
movl (%r15), %eax
testl %eax, %eax
sets %dl
orb %cl, %dl
jne 0x20fb9
movl %eax, %eax
movq 0x40(%rbp), %rcx
cmpl 0x58(%rcx), %eax
jge 0x20fb9
movq 0x60(%rcx), %rcx
movq (%rcx,%rax,8), %rdi
movq %rdi, %r15
callq 0x4230
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq %rdx
movl %edx, 0x38(%r14)
movl $0x0, 0x40(%r14)
movq 0x88(%r13), %rax
movl 0x30(%rax), %eax
movl %eax, 0x3c(%r14)
xorl %ebx, %ebx
btl $0x1e, %r12d
jae 0x20fb9
testl %edx, %edx
jle 0x20fb9
movq %r15, %r12
xorl %edx, %edx
movq 0x2fbe4(%rip), %xmm1    # 0x50980
movapd 0x2fbec(%rip), %xmm2    # 0x50990
movsd 0x30cb4(%rip), %xmm3    # 0x51a60
movsd 0x2fb54(%rip), %xmm4    # 0x50908
movl $0x400, %r9d            # imm = 0x400
movl %edx, 0x14(%rsp)
movl $0x1, 0xc(%rsp)
movb (%r12), %sil
cmpb $0x4d, %sil
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rcx
jne 0x20df4
movb $0x4d, %sil
testq %rcx, %rcx
je 0x20e14
cmpb $0x44, 0x1(%r12)
jne 0x20e14
movl $0x1, (%rcx)
movb (%r12), %sil
cmpb $0x4e, %sil
jne 0x20e14
movb $0x4e, %sil
testq %rax, %rax
je 0x20e14
cmpb $0x4d, 0x1(%r12)
jne 0x20e14
movl $0x1, (%rax)
movb (%r12), %sil
movzbl 0x1(%r12), %r15d
movzbl %sil, %eax
movq 0x40(%rbp), %rcx
leal (%rax,%rax,2), %edx
addl %r15d, %edx
andl $0x1f, %edx
movq 0x180(%rcx,%rdx,8), %rbx
testq %rbx, %rbx
je 0x20fca
movzbl 0x2(%r12), %edx
addq $0x3, %r12
shll $0x10, %eax
movzbl %r15b, %ecx
shll $0x8, %ecx
orl %eax, %ecx
orl %edx, %ecx
cmpl %ecx, (%rbx)
je 0x20e66
movq 0x18(%rbx), %rbx
testq %rbx, %rbx
jne 0x20e54
jmp 0x20fca
movl %edx, 0x10(%rsp)
movb %sil, 0xb(%rsp)
movq 0x88(%r13), %rcx
movq 0x20(%rcx), %rax
movq 0x28(%rcx), %rdx
movq 0x30(%rcx), %rsi
leaq 0x3(%rsi), %rdi
cmpq %rdi, %rdx
ja 0x20f27
addq $0x28, %rcx
movq %rdx, %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, %rdi
movq %rdi, %r8
sarq $0x3f, %r8
subsd %xmm4, %xmm1
cvttsd2si %xmm1, %rsi
andq %r8, %rsi
orq %rdi, %rsi
testq %rdx, %rdx
cmoveq %r9, %rsi
movq %rsi, (%rcx)
movq %rax, %rdi
callq 0x48d0
movl $0x400, %r9d            # imm = 0x400
movsd 0x2fa1d(%rip), %xmm4    # 0x50908
movsd 0x30b6d(%rip), %xmm3    # 0x51a60
movapd 0x2fa95(%rip), %xmm2    # 0x50990
movq 0x2fa7d(%rip), %xmm1    # 0x50980
movq 0x88(%r13), %rsi
movq %rax, 0x20(%rsi)
leaq 0x28(%rsi), %rcx
movq 0x28(%rsi), %rdx
movq 0x30(%rsi), %rsi
leaq 0x3(%rsi), %rdi
cmpq %rdi, %rdx
jbe 0x20e93
movb 0xb(%rsp), %cl
movb %cl, (%rax,%rsi)
movb %r15b, 0x1(%rax,%rsi)
movl 0x10(%rsp), %ecx
movb %cl, 0x2(%rax,%rsi)
movq 0x88(%r13), %rcx
addq $0x3, 0x30(%rcx)
movq 0x10(%rbx), %rsi
testq %rsi, %rsi
je 0x20fca
movq %r13, %rdi
movq 0x20(%rsp), %rdx
leaq 0xc(%rsp), %r8
callq *0x18(%rsi)
testl %eax, %eax
movl 0x14(%rsp), %edx
jne 0x20fd1
movl 0xc(%rsp), %eax
movl 0x40(%r14), %ecx
addl %ecx, %eax
addl $0x3, %eax
movl %eax, 0x40(%r14)
incl %edx
xorl %ebx, %ebx
cmpl 0x38(%r14), %edx
movq 0x2f9f7(%rip), %xmm1    # 0x50980
movapd 0x2f9ff(%rip), %xmm2    # 0x50990
movsd 0x30ac7(%rip), %xmm3    # 0x51a60
movsd 0x2f967(%rip), %xmm4    # 0x50908
movl $0x400, %r9d            # imm = 0x400
jl 0x20dba
jmp 0x20fb9
movq $0x0, 0x3c(%r14)
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x20fb9
movl %eax, %ebx
jmp 0x20fb9