// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/cram/cram_external.c
// Function: cram_update_cid2ds_map
cram_cid2ds_t *cram_update_cid2ds_map(cram_block_compression_hdr *hdr,
                                      cram_cid2ds_t *cid2ds) {
    cram_cid2ds_t *c2d = cid2ds;
    if (!c2d) {
        c2d = calloc(1, sizeof(*c2d));
        if (!c2d)
            return NULL;

        c2d->hash = kh_init(cid);
        if (!c2d->hash)
            goto err;
    }

    // Iterate through codecs.  Initially primary two-left ones in
    // rec_encoding_map, and then the three letter in tag_encoding_map.
    cram_codec_iter citer;
    cram_codec_iter_init(hdr, &citer);
    cram_codec *codec;
    int key;

    while ((codec = cram_codec_iter_next(&citer, &key))) {
        // Having got a codec, we can then use cram_codec_to_id to get
        // the block IDs utilised by that codec.  This is then our
        // map for allocating data blocks to data series, but for shared
        // blocks we can't separate out how much is used by each DS.
        int bnum[2];
        cram_codec_get_content_ids(codec, bnum);

        khiter_t k;
        int ret, i;
        for (i = 0; i < 2; i++) {
            if (bnum[i] > -2) {
                k = kh_put(cid, c2d->hash, bnum[i], &ret);
                if (ret < 0)
                    goto err;

                if (c2d->ds_idx >= c2d->ds_size) {
                    c2d->ds_size += 100;
                    c2d->ds_size *= 2;
                    ds_list *ds_new = realloc(c2d->ds,
                                              c2d->ds_size * sizeof(*ds_new));
                    if (!ds_new)
                        goto err;
                    c2d->ds = ds_new;
                }

                if (ret == 0) {
                    // Shared content_id, so add to list of DS

                    // Maybe data-series should be part of the hash key?
                    //
                    // So top-32 bit is content-id, bot-32 bit is key.
                    // Sort hash by key and then can group all the data-series
                    // known together. ??
                    //
                    // Brute force for now, scan to see if recorded.
                    // Typically this is minimal effort as we almost always
                    // have 1 data-series per block content-id, so the list to
                    // search is of size 1.
                    int dsi = kh_value(c2d->hash, k);
                    while (dsi >= 0) {
                        if (c2d->ds[dsi].data_series == key)
                            break;
                        dsi = c2d->ds[dsi].next;
                    }

                    if (dsi == -1) {
                        // Block content_id seen before, but not with this DS
                        c2d->ds[c2d->ds_idx].data_series = key;
                        c2d->ds[c2d->ds_idx].next = kh_value(c2d->hash, k);
                        kh_value(c2d->hash, k) = c2d->ds_idx;
                        c2d->ds_idx++;
                    }
                } else {
                    // First time this content id has been used
                    c2d->ds[c2d->ds_idx].data_series = key;
                    c2d->ds[c2d->ds_idx].next = -1;
                    kh_value(c2d->hash, k) = c2d->ds_idx;
                    c2d->ds_idx++;
                }
            }
        }
    }

    return c2d;

 err:
    if (c2d != cid2ds)
        cram_cid2ds_free(c2d);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r12
movq %rsi, %rbx
movq %rsi, 0x10(%rsp)
testq %rsi, %rsi
je 0x75803
leaq 0x20(%rsp), %rdi
movq %r12, (%rdi)
pxor %xmm0, %xmm0
movdqu %xmm0, 0x8(%rdi)
leaq 0x4(%rsp), %rsi
callq 0x75851
testq %rax, %rax
je 0x7583f
movq %rax, %rdi
leaq 0x18(%rsp), %rsi
callq 0x16640
movl 0x4(%rsp), %r15d
xorl %r14d, %r14d
cmpq $0x2, %r14
je 0x757f9
movl 0x18(%rsp,%r14,4), %r13d
cmpl $-0x1, %r13d
jl 0x757e0
movq 0x10(%rbx), %rbp
movl 0x8(%rbp), %eax
cmpl 0xc(%rbp), %eax
jb 0x7564b
movl (%rbp), %esi
movl 0x4(%rbp), %eax
addl %eax, %eax
cmpl %eax, %esi
jbe 0x75639
decl %esi
jmp 0x7563b
incl %esi
movq %rbp, %rdi
callq 0x764f3
testl %eax, %eax
js 0x7582e
movl (%rbp), %edx
leal -0x1(%rdx), %r8d
movl %r8d, %esi
andl %r13d, %esi
movq 0x10(%rbp), %rax
movl %esi, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %edi
leal (%rsi,%rsi), %ecx
shrl %cl, %edi
testb $0x2, %dil
je 0x75676
movl %esi, %edi
jmp 0x756ff
pushq $0x1
popq %r10
movl %esi, %r9d
movq %rdx, 0x8(%rsp)
movl %edx, %edi
movl %r9d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %r11d
leal (%r9,%r9), %ecx
andl $0x1e, %ecx
movl %r11d, %r12d
shrl %cl, %r12d
testb $0x2, %r12b
jne 0x756da
testb $0x1, %r12b
jne 0x756b4
movq 0x18(%rbp), %r12
movl %r9d, %edx
cmpl %r13d, (%r12,%rdx,4)
je 0x756da
btl %ecx, %r11d
cmovbl %r9d, %edi
addl %r10d, %r9d
andl %r8d, %r9d
incl %r10d
cmpl %esi, %r9d
jne 0x75684
movb $0x1, %cl
movq 0x8(%rsp), %rdx
movl %edx, %r8d
cmpl %edx, %edi
je 0x756ef
jmp 0x756ff
shrl %cl, %r11d
testb $0x2, %r11b
sete %cl
movl %edi, %r8d
movl %r9d, %esi
movq 0x8(%rsp), %rdx
cmpl %edx, %r8d
cmovel %esi, %r8d
testb %cl, %cl
cmovnel %esi, %r8d
movl %r8d, %edi
movl %edi, %edx
shrl $0x4, %edx
movl (%rax,%rdx,4), %esi
leal (%rdi,%rdi), %ecx
andl $0x1e, %ecx
shrl %cl, %esi
movl %edi, %edi
testb $0x2, %sil
jne 0x75737
movb $0x1, %r12b
testb $0x1, %sil
je 0x7575e
movq 0x18(%rbp), %rsi
movl %r13d, (%rsi,%rdi,4)
pushq $0x3
popq %rsi
shll %cl, %esi
notl %esi
andl %esi, (%rax,%rdx,4)
incl 0x4(%rbp)
jmp 0x7575b
movq 0x18(%rbp), %rsi
movl %r13d, (%rsi,%rdi,4)
pushq $0x3
popq %rsi
shll %cl, %esi
notl %esi
andl %esi, (%rax,%rdx,4)
movq 0x4(%rbp), %xmm0
psubd 0x2faca(%rip), %xmm0    # 0xa5220
movq %xmm0, 0x4(%rbp)
xorl %r12d, %r12d
movl 0x8(%rbx), %eax
cmpl %eax, 0xc(%rbx)
jl 0x75791
movq %rdi, %r13
leal 0xc8(,%rax,2), %eax
movl %eax, 0x8(%rbx)
movq (%rbx), %rdi
movslq %eax, %rsi
shlq $0x3, %rsi
callq 0x16380
testq %rax, %rax
je 0x7582e
movq %rax, (%rbx)
movq %r13, %rdi
testb %r12b, %r12b
je 0x757b8
movq 0x10(%rbx), %rax
movq 0x20(%rax), %rax
movl (%rax,%rdi,8), %ecx
movl %ecx, %edx
testl %edx, %edx
js 0x757db
movq (%rbx), %rsi
movl %edx, %edx
cmpl %r15d, (%rsi,%rdx,8)
je 0x757e0
movl 0x4(%rsi,%rdx,8), %edx
jmp 0x757a3
movq (%rbx), %rax
movslq 0xc(%rbx), %rdx
movl %r15d, (%rax,%rdx,8)
orl $-0x1, 0x4(%rax,%rdx,8)
movq 0x10(%rbx), %rax
movq 0x20(%rax), %rax
movq %rdx, (%rax,%rdi,8)
incl %edx
movl %edx, 0xc(%rbx)
jmp 0x757e0
cmpl $-0x1, %edx
je 0x757e8
incq %r14
jmp 0x75604
movq (%rbx), %rsi
movslq 0xc(%rbx), %rdx
movl %r15d, (%rsi,%rdx,8)
movl %ecx, 0x4(%rsi,%rdx,8)
jmp 0x757d0
leaq 0x20(%rsp), %rdi
jmp 0x755dc
pushq $0x1
popq %rdi
pushq $0x20
popq %rsi
callq 0x15f40
testq %rax, %rax
je 0x7583d
movq %rax, %rbx
pushq $0x1
popq %rdi
pushq $0x28
popq %rsi
callq 0x15f40
movq %rax, 0x10(%rbx)
testq %rax, %rax
jne 0x755cb
cmpq 0x10(%rsp), %rbx
je 0x7583d
movq %rbx, %rdi
callq 0x15450
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq