// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/bgzf.c
// Function: bgzf_mt_writer
static void *bgzf_mt_writer(void *vp) {
    BGZF *fp = (BGZF *)vp;
    mtaux_t *mt = fp->mt;
    hts_tpool_result *r;

    if (fp->idx_build_otf) {
        fp->idx->moffs = fp->idx->noffs = 1;
        fp->idx->offs = (bgzidx1_t*) calloc(fp->idx->moffs, sizeof(bgzidx1_t));
        if (!fp->idx->offs) goto err;
    }

    // Iterates until result queue is shutdown, where it returns NULL.
    while ((r = hts_tpool_next_result_wait(mt->out_queue))) {
        bgzf_job *j = (bgzf_job *)hts_tpool_result_data(r);
        assert(j);

        if (fp->idx_build_otf) {
            fp->idx->noffs++;
            if ( fp->idx->noffs > fp->idx->moffs )
            {
                fp->idx->moffs = fp->idx->noffs;
                kroundup32(fp->idx->moffs);
                fp->idx->offs = (bgzidx1_t*) realloc(fp->idx->offs, fp->idx->moffs*sizeof(bgzidx1_t));
                if ( !fp->idx->offs ) goto err;
            }
            fp->idx->offs[ fp->idx->noffs-1 ].uaddr = fp->idx->offs[ fp->idx->noffs-2 ].uaddr + j->uncomp_len;
            fp->idx->offs[ fp->idx->noffs-1 ].caddr = fp->idx->offs[ fp->idx->noffs-2 ].caddr + j->comp_len;
        }

        // Flush any cached hts_idx_push calls
        if (bgzf_idx_flush(fp, j->uncomp_len, j->comp_len) < 0)
            goto err;

        if (hwrite(fp->fp, j->comp_data, j->comp_len) != j->comp_len)
            goto err;

        // Update our local block_address.  Cannot be fp->block_address due to no
        // locking in bgzf_tell.
        pthread_mutex_lock(&mt->idx_m);
        mt->block_address += j->comp_len;
        pthread_mutex_unlock(&mt->idx_m);

        /*
         * Periodically call hflush (which calls fsync when on a file).
         * This avoids the fsync being done at the bgzf_close stage,
         * which can sometimes cause significant delays.  As this is in
         * a separate thread, spreading the sync delays throughout the
         * program execution seems better.
         * Frequency of 1/512 has been chosen by experimentation
         * across local XFS, NFS and Lustre tests.
         */
        if (++mt->flush_pending % 512 == 0)
            if (hflush(fp->fp) != 0)
                goto err;


        hts_tpool_delete_result(r, 0);

        // Also updated by main thread
        pthread_mutex_lock(&mt->job_pool_m);
        pool_free(mt->job_pool, j);
        mt->jobs_pending--;
        pthread_mutex_unlock(&mt->job_pool_m);
    }

    if (hflush(fp->fp) != 0)
        goto err;

    hts_tpool_process_destroy(mt->out_queue);

    return NULL;

 err:
    hts_tpool_process_destroy(mt->out_queue);
    return (void *)-1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r12
movq 0x48(%rdi), %r14
cmpl $0x0, 0x58(%rdi)
je 0xc3d3
movq 0x50(%r12), %rbx
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, (%rbx)
movl $0x1, %edi
movl $0x10, %esi
callq 0x4300
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0xc786
movq 0x20(%r14), %rdi
callq 0x35e75
testq %rax, %rax
je 0xc761
movq %rax, %rcx
leaq 0xe0(%r14), %rax
movq %rax, 0x30(%rsp)
leaq 0x30(%r14), %rax
movq %rax, 0x28(%rsp)
movq %r14, 0x8(%rsp)
movq %r12, 0x18(%rsp)
movq %rcx, 0x40(%rsp)
movq %rcx, %rdi
callq 0x361a4
testq %rax, %rax
je 0xc7ab
movq %rax, %rbp
movq %rax, %r13
cmpl $0x0, 0x58(%r12)
je 0xc442
movq 0x50(%r12), %rax
movl (%rax), %ecx
leal 0x1(%rcx), %edx
movl %edx, (%rax)
cmpl 0x4(%rax), %ecx
jge 0xc455
movq 0x8(%rax), %rax
jmp 0xc4ac
movq 0x20010(%r13), %r15
movq 0x10008(%r13), %rsi
jmp 0xc4db
testl %ecx, %ecx
js 0xc485
movl %ecx, %edx
shrl %edx
orl %ecx, %edx
movl %edx, %esi
shrl $0x2, %esi
orl %edx, %esi
movl %esi, %edx
shrl $0x4, %edx
orl %esi, %edx
movl %edx, %esi
shrl $0x8, %esi
orl %edx, %esi
movl %esi, %edx
shrl $0x10, %edx
orl %esi, %edx
shrl $0x1e, %ecx
xorl $0x1, %ecx
addl %edx, %ecx
movl %ecx, %edx
movl %edx, 0x4(%rax)
movq 0x8(%rax), %rdi
movslq %edx, %rsi
shlq $0x4, %rsi
callq 0x4840
movq 0x50(%r12), %rcx
movq %rax, 0x8(%rcx)
testq %rax, %rax
je 0xc786
movl (%rcx), %edx
movslq %edx, %rcx
shlq $0x4, %rcx
movq 0x20010(%r13), %r15
movq -0x20(%rax,%rcx), %rdx
addq %r15, %rdx
movq %rdx, -0x10(%rax,%rcx)
movq 0x10008(%r13), %rsi
movq -0x18(%rax,%rcx), %rdx
addq %rsi, %rdx
movq %rdx, -0x8(%rax,%rcx)
movq 0x48(%r12), %rbx
cmpq $0x0, 0x128(%rbx)
je 0xc5a5
movq %rsi, 0x20(%rsp)
leaq 0xe0(%rbx), %r14
movq %r14, %rdi
callq 0x4750
movq 0x128(%rbx), %rdx
movl 0x120(%rbx), %eax
xorl %r12d, %r12d
testl %eax, %eax
movq %r14, 0x10(%rsp)
movq %rdx, 0x38(%rsp)
je 0xc5b1
movq 0x118(%rbx), %rcx
cmpq 0x20(%rdx), %rcx
ja 0xc7ca
testl %eax, %eax
jle 0xc5ff
movq %rdx, %r14
xorl %r12d, %r12d
movq 0x20(%r14), %rcx
cmpq 0x118(%rbx), %rcx
jne 0xc5ff
movq 0x18(%r14), %r9
testq %r15, %r15
je 0xc562
cmpq %r15, %r9
je 0xc5b5
movq 0x70(%rbx), %r8
movq 0x108(%rbx), %rdi
movl 0x10(%r14), %esi
movq (%r14), %rdx
movq 0x8(%r14), %rcx
shlq $0x10, %r8
addq %r9, %r8
movl 0x14(%r14), %r9d
callq 0x1699a
testl %eax, %eax
js 0xc777
incq %r12
movslq 0x120(%rbx), %rax
addq $0x28, %r14
cmpq %rax, %r12
jl 0xc543
jmp 0xc5ff
incq 0x118(%rbx)
jmp 0xc63e
xorl %eax, %eax
jmp 0xc5ff
decl %eax
cmpl %eax, %r12d
je 0xc5c6
cmpq 0x48(%r14), %rcx
jae 0xc7e9
movq 0x20(%rsp), %r8
addq 0x70(%rbx), %r8
movq 0x108(%rbx), %rdi
movl 0x10(%r14), %esi
movq (%r14), %rdx
movq 0x8(%r14), %rcx
shlq $0x10, %r8
movl 0x14(%r14), %r9d
callq 0x1699a
testl %eax, %eax
js 0xc777
incl %r12d
movl 0x120(%rbx), %eax
movslq %r12d, %r14
leaq (%r14,%r14,4), %rcx
movq 0x38(%rsp), %rdi
leaq (%rdi,%rcx,8), %rsi
subl %r14d, %eax
cltq
shlq $0x3, %rax
leaq (%rax,%rax,4), %rdx
callq 0x4830
subl %r14d, 0x120(%rbx)
incq 0x118(%rbx)
movq 0x10(%rsp), %rdi
callq 0x4340
movq 0x18(%rsp), %r12
movq 0x40(%r12), %r15
movq 0x10008(%r13), %r12
testb $0x2, 0x30(%r15)
movq 0x18(%r15), %rbx
jne 0xc67a
movq %rbx, %rax
subq 0x8(%r15), %rax
cmpq %r12, %rax
jae 0xc67a
addq %r12, %rbx
subq (%r15), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0xee24
movq 0x18(%r15), %rbx
movq %rbx, 0x10(%r15)
addq $0x8, %rbp
movq 0x8(%r15), %rdi
subq %rdi, %rbx
cmpq %r12, %rbx
ja 0xc68f
cmpq (%r15), %rdi
je 0xc6af
cmpq %r12, %rbx
movq %r12, %r14
cmovbq %rbx, %r14
movq %rbp, %rsi
movq %r14, %rdx
callq 0x43c0
addq %r14, 0x8(%r15)
cmpq %r12, %rbx
jb 0xc6b2
jmp 0xc6c6
xorl %r14d, %r14d
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0xf300
movq %rax, %r14
cmpq 0x10008(%r13), %r14
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %r12
jne 0xc786
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x10008(%r13), %rax
addq %rax, 0x70(%r14)
movq %rbx, %rdi
callq 0x4340
movl 0x5c(%r14), %eax
incl %eax
movl %eax, 0x5c(%r14)
testl $0x1ff, %eax            # imm = 0x1FF
jne 0xc71c
movq 0x40(%r12), %rdi
callq 0xf22b
testl %eax, %eax
jne 0xc786
movq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x3617f
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq (%r14), %rdi
movq %r13, %rsi
callq 0x57e84
decl 0x58(%r14)
movq %rbx, %rdi
callq 0x4340
movq 0x20(%r14), %rdi
callq 0x35e75
movq %rax, %rcx
testq %rax, %rax
jne 0xc407
movq 0x40(%r12), %rdi
callq 0xf22b
testl %eax, %eax
jne 0xc786
addq $0x20, %r14
xorl %ebx, %ebx
jmp 0xc791
movq 0x10(%rsp), %rdi
callq 0x4340
movq 0x8(%rsp), %r14
addq $0x20, %r14
movq $-0x1, %rbx
movq (%r14), %rdi
callq 0x35f44
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x88b71(%rip), %rdi     # 0x95323
leaq 0x88467(%rip), %rsi     # 0x94c20
leaq 0x88b65(%rip), %rcx     # 0x95325
movl $0x582, %edx            # imm = 0x582
callq 0x42f0
leaq 0x88b71(%rip), %rdi     # 0x95342
leaq 0x88448(%rip), %rsi     # 0x94c20
leaq 0x88ba9(%rip), %rcx     # 0x95388
movl $0xf7, %edx
callq 0x42f0
leaq 0x88bc3(%rip), %rdi     # 0x953b3
leaq 0x88429(%rip), %rsi     # 0x94c20
leaq 0x88b8a(%rip), %rcx     # 0x95388
movl $0x10a, %edx            # imm = 0x10A
callq 0x42f0