// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/bgzf.c
// Function: cache_block
static void cache_block(BGZF *fp, int size)
{
    int ret;
    khint_t k, k_orig;
    uint8_t *block = NULL;
    cache_t *p;
    //fprintf(stderr, "Cache block at %llx\n", (int)fp->block_address);
    khash_t(cache) *h = fp->cache->h;
    if (BGZF_MAX_BLOCK_SIZE >= fp->cache_size) return;
    if (fp->block_length < 0 || fp->block_length > BGZF_MAX_BLOCK_SIZE) return;
    if ((kh_size(h) + 1) * BGZF_MAX_BLOCK_SIZE > (uint32_t)fp->cache_size) {
        /* Remove uniformly from any position in the hash by a simple
         * round-robin approach.  An alternative strategy would be to
         * remove the least recently accessed block, but the round-robin
         * removal is simpler and is not expected to have a big impact
         * on performance */
        if (fp->cache->last_pos >= kh_end(h)) fp->cache->last_pos = kh_begin(h);
        k_orig = k = fp->cache->last_pos;
        if (++k >= kh_end(h)) k = kh_begin(h);
        while (k != k_orig) {
            if (kh_exist(h, k))
                break;
            if (++k == kh_end(h))
                k = kh_begin(h);
        }
        fp->cache->last_pos = k;

        if (k != k_orig) {
            block = kh_val(h, k).block;
            kh_del(cache, h, k);
        }
    } else {
        block = (uint8_t*)malloc(BGZF_MAX_BLOCK_SIZE);
    }
    if (!block) return;
    k = kh_put(cache, h, fp->block_address, &ret);
    if (ret <= 0) { // kh_put failed, or in there already (shouldn't happen)
        free(block);
        return;
    }
    p = &kh_val(h, k);
    p->size = fp->block_length;
    p->end_offset = fp->block_address + size;
    p->block = block;
    memcpy(p->block, fp->uncompressed_block, p->size);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl 0x4(%rdi), %ecx
cmpl $0x10001, %ecx          # imm = 0x10001
jl 0xbd2d
movq %rdi, %rbx
cmpl $0x10000, 0x8(%rdi)     # imm = 0x10000
ja 0xbd2d
movl %esi, %ebp
movq 0x38(%rbx), %rax
movq (%rax), %r14
movl 0x4(%r14), %r13d
movl %r13d, %edx
shll $0x10, %edx
addl $0x10000, %edx          # imm = 0x10000
cmpl %ecx, %edx
jbe 0xbcfd
movl 0x8(%rax), %esi
movl (%r14), %edx
cmpl %edx, %esi
jb 0xbcb5
movl $0x0, 0x8(%rax)
xorl %esi, %esi
leal 0x1(%rsi), %r8d
xorl %r9d, %r9d
cmpl %edx, %r8d
cmovael %r9d, %r8d
cmpl %esi, %r8d
je 0xbcf8
movq 0x10(%r14), %rdi
leal (%r8,%r8), %ecx
andl $0x1e, %ecx
movl $0x3, %r11d
shll %cl, %r11d
movl %r8d, %r10d
shrl $0x4, %r10d
testl %r11d, (%rdi,%r10,4)
je 0xbd3c
incl %r8d
cmpl %edx, %r8d
cmovel %r9d, %r8d
cmpl %esi, %r8d
jne 0xbccc
movl %esi, 0x8(%rax)
jmp 0xbd2d
movl $0x10000, %edi          # imm = 0x10000
callq 0x4680
testq %rax, %rax
je 0xbd2d
movq %rax, (%rsp)
movq 0x18(%rbx), %r12
movl 0x8(%r14), %eax
cmpl 0xc(%r14), %eax
jb 0xbd84
movl (%r14), %esi
addl %r13d, %r13d
cmpl %r13d, %esi
jbe 0xbd72
decl %esi
jmp 0xbd74
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r8d, 0x8(%rax)
movq 0x20(%r14), %rax
movl %r8d, %esi
leaq (%rsi,%rsi,2), %rsi
movq 0x8(%rax,%rsi,8), %rax
cmpl %r8d, %edx
je 0xbd07
movl (%rdi,%r10,4), %edx
testl %r11d, %edx
jne 0xbd07
btsl %ecx, %edx
movl %edx, (%rdi,%r10,4)
movl 0x4(%r14), %r13d
decl %r13d
movl %r13d, 0x4(%r14)
jmp 0xbd07
incl %esi
movq %r14, %rdi
callq 0xe804
testl %eax, %eax
js 0xbe58
movl (%r14), %edx
leal -0x1(%rdx), %r8d
movq %r12, %rdi
shrq $0x21, %rdi
movl %r12d, %eax
shll $0xb, %eax
xorl %eax, %edi
xorl %r12d, %edi
andl %r8d, %edi
movq 0x10(%r14), %rax
movl %edi, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %esi
leal (%rdi,%rdi), %ecx
shrl %cl, %esi
testb $0x2, %sil
je 0xbdbb
movl %edi, %esi
jmp 0xbe38
movl $0x1, %r10d
movl %edi, %r9d
movl %edx, %esi
movl %r9d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %r11d
leal (%r9,%r9), %ecx
andl $0x1e, %ecx
movl %r11d, %r13d
shrl %cl, %r13d
testb $0x2, %r13b
jne 0xbe18
testb $0x1, %r13b
jne 0xbdf7
movq 0x18(%r14), %r13
movl %r9d, %r15d
cmpq %r12, (%r13,%r15,8)
je 0xbe18
btl %ecx, %r11d
cmovbl %r9d, %esi
addl %r10d, %r9d
andl %r8d, %r9d
incl %r10d
cmpl %edi, %r9d
jne 0xbdc6
movb $0x1, %cl
movl %edx, %r8d
cmpl %edx, %esi
je 0xbe28
jmp 0xbe38
shrl %cl, %r11d
testb $0x2, %r11b
sete %cl
movl %esi, %r8d
movl %r9d, %edi
cmpl %edx, %r8d
cmovel %edi, %r8d
testb %cl, %cl
cmovnel %edi, %r8d
movl %r8d, %esi
movl %esi, %edx
shrl $0x4, %edx
movl (%rax,%rdx,4), %edi
leal (%rsi,%rsi), %ecx
andl $0x1e, %ecx
movl %edi, %r8d
shrl %cl, %r8d
testb $0x2, %r8b
jne 0xbe6f
testb $0x1, %r8b
jne 0xbea1
movq (%rsp), %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x45c0
movq 0x18(%r14), %r8
movl %esi, %esi
movl $0x3, %r9d
shll %cl, %r9d
movq %r12, (%r8,%rsi,8)
notl %r9d
andl %r9d, %edi
movl %edi, (%rax,%rdx,4)
movq 0x4(%r14), %xmm0
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x4(%r14)
jmp 0xbec1
movq 0x18(%r14), %r8
movl $0x3, %r9d
shll %cl, %r9d
movl %esi, %esi
movq %r12, (%r8,%rsi,8)
notl %r9d
andl %r9d, %edi
movl %edi, (%rax,%rdx,4)
incl 0x4(%r14)
movq 0x20(%r14), %rax
leaq (%rsi,%rsi,2), %rcx
movslq 0x8(%rbx), %rdx
movl %edx, (%rax,%rcx,8)
movslq %ebp, %rsi
addq 0x18(%rbx), %rsi
movq %rsi, 0x10(%rax,%rcx,8)
movq (%rsp), %rdi
movq %rdi, 0x8(%rax,%rcx,8)
movq 0x28(%rbx), %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x43c0