// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/bgzf.c
// Function: bgzf_idx_flush
static int bgzf_idx_flush(BGZF *fp,
                          size_t block_uncomp_len, size_t block_comp_len) {
    mtaux_t *mt = fp->mt;

    if (!mt->idx_cache.e) {
        mt->block_written++;
        return 0;
    }

    pthread_mutex_lock(&mt->idx_m);

    hts_idx_cache_entry *e = mt->idx_cache.e;
    int i;

    assert(mt->idx_cache.nentries == 0 || mt->block_written <= e[0].block_number);

    for (i = 0; i < mt->idx_cache.nentries && e[i].block_number == mt->block_written; i++) {
        if (block_uncomp_len > 0 && e[i].offset == block_uncomp_len) {
            /*
             * If the virtual offset is at the end of the current block,
             * adjust it to point to the start of the next one.  This
             * is needed when on-the-fly indexing has recorded a virtual
             * offset just before a new block has been started, and makes
             * on-the-fly and standard indexing give exactly the same results.
             *
             * In theory the two virtual offsets are equivalent, but pointing
             * to the end of a block is inefficient, and caused problems with
             * versions of HTSlib before 1.11 where bgzf_read() would
             * incorrectly return EOF.
             */

            // Assert that this is the last entry for the current block_number
            assert(i == mt->idx_cache.nentries - 1
                   || e[i].block_number < e[i + 1].block_number);

            // Work out where the next block starts.  For this entry, the
            // offset will be zero.
            uint64_t next_block_addr = mt->block_address + block_comp_len;
            if (hts_idx_push(mt->hts_idx, e[i].tid, e[i].beg, e[i].end,
                             next_block_addr << 16, e[i].is_mapped) < 0) {
                pthread_mutex_unlock(&mt->idx_m);
                return -1;
            }
            // Count this entry and drop out of the loop
            i++;
            break;
        }

        if (hts_idx_push(mt->hts_idx, e[i].tid, e[i].beg, e[i].end,
                         (mt->block_address << 16) + e[i].offset,
                         e[i].is_mapped) < 0) {
            pthread_mutex_unlock(&mt->idx_m);
            return -1;
        }
    }

    memmove(&e[0], &e[i], (mt->idx_cache.nentries - i) * sizeof(*e));
    mt->idx_cache.nentries -= i;
    mt->block_written++;

    pthread_mutex_unlock(&mt->idx_m);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x128(%rax)
jne 0x12e50
movq -0x28(%rbp), %rax
movq 0x118(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x118(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x1313c
movq -0x28(%rbp), %rdi
addq $0xe0, %rdi
callq 0x4730
movq -0x28(%rbp), %rax
movq 0x128(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x120(%rax)
je 0x12e91
movq -0x28(%rbp), %rax
movq 0x118(%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x20(%rcx), %rax
ja 0x12e93
jmp 0x12eb2
leaq 0xdc672(%rip), %rdi     # 0xef50c
leaq 0xdbf44(%rip), %rsi     # 0xeede5
movl $0xf7, %edx
leaq 0xdc6a5(%rip), %rcx     # 0xef552
callq 0x42e0
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %ecx
movq -0x28(%rbp), %rdx
xorl %eax, %eax
cmpl 0x120(%rdx), %ecx
movb %al, -0x41(%rbp)
jge 0x12ef1
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x118(%rcx), %rax
sete %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x12efd
jmp 0x130cf
cmpq $0x0, -0x18(%rbp)
jbe 0x13028
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
cmpq -0x18(%rbp), %rax
jne 0x13028
movl -0x34(%rbp), %eax
movq -0x28(%rbp), %rcx
movl 0x120(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x12f66
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movq -0x30(%rbp), %rcx
movl -0x34(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x20(%rcx), %rax
jae 0x12f68
jmp 0x12f87
leaq 0xdc60e(%rip), %rdi     # 0xef57d
leaq 0xdbe6f(%rip), %rsi     # 0xeede5
movl $0x10a, %edx            # imm = 0x10A
leaq 0xdc5d0(%rip), %rcx     # 0xef552
callq 0x42e0
movq -0x28(%rbp), %rax
movq 0x70(%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq 0x108(%rax), %rdi
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %esi
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x40(%rbp), %r8
shlq $0x10, %r8
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %r9
imulq $0x28, %r9, %r9
addq %r9, %rax
movl 0x14(%rax), %r9d
callq 0x1f990
cmpl $0x0, %eax
jge 0x1301a
movq -0x28(%rbp), %rdi
addq $0xe0, %rdi
callq 0x4330
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1313c
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x130cf
movq -0x28(%rbp), %rax
movq 0x108(%rax), %rdi
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %esi
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq 0x70(%rax), %r8
shlq $0x10, %r8
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %r9
imulq $0x28, %r9, %r9
addq %r9, %rax
addq 0x18(%rax), %r8
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %r9
imulq $0x28, %r9, %r9
addq %r9, %rax
movl 0x14(%rax), %r9d
callq 0x1f990
cmpl $0x0, %eax
jge 0x130bf
movq -0x28(%rbp), %rdi
addq $0xe0, %rdi
callq 0x4330
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1313c
jmp 0x130c1
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x12eb9
movq -0x30(%rbp), %rdi
movq -0x30(%rbp), %rsi
movslq -0x34(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movq -0x28(%rbp), %rax
movl 0x120(%rax), %eax
subl -0x34(%rbp), %eax
cltq
imulq $0x28, %rax, %rdx
callq 0x4810
movl -0x34(%rbp), %edx
movq -0x28(%rbp), %rax
movl 0x120(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x120(%rax)
movq -0x28(%rbp), %rax
movq 0x118(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x118(%rax)
movq -0x28(%rbp), %rdi
addq $0xe0, %rdi
callq 0x4330
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)