// File: /workspace/github_c_makefile_star2_O2/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, %rbp
movq 0x48(%rdi), %r15
cmpl $0x0, 0x58(%rdi)
je 0xaf9e
movq 0x50(%rbp), %rbx
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, (%rbx)
pushq $0x1
popq %rdi
pushq $0x10
popq %rsi
callq 0x4300
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0xb29d
leaq 0x20(%r15), %r14
leaq 0xe0(%r15), %rax
movq %rax, 0x30(%rsp)
leaq 0x30(%r15), %rax
movq %rax, 0x28(%rsp)
movq %r14, 0x38(%rsp)
movq %r15, 0x8(%rsp)
movq %rbp, 0x18(%rsp)
movq (%r14), %rdi
callq 0x2bca5
testq %rax, %rax
je 0xb2c1
movq %rax, %r13
movq %rax, %rdi
callq 0x2bfc2
testq %rax, %rax
je 0xb2d2
movq %rax, %r12
cmpl $0x0, 0x58(%rbp)
je 0xb00a
movq 0x50(%rbp), %rax
movl (%rax), %ecx
leal 0x1(%rcx), %edx
movl %edx, (%rax)
cmpl 0x4(%rax), %ecx
jge 0xb01f
movq 0x8(%rax), %rax
jmp 0xb075
movq 0x20010(%r12), %r14
movq 0x10008(%r12), %rsi
jmp 0xb0a6
testl %ecx, %ecx
js 0xb04f
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(%rbp), %rcx
movq %rax, 0x8(%rcx)
testq %rax, %rax
je 0xb29d
movl (%rcx), %edx
movslq %edx, %rcx
shlq $0x4, %rcx
movq 0x20010(%r12), %r14
movq -0x20(%rax,%rcx), %rdx
addq %r14, %rdx
movq %rdx, -0x10(%rax,%rcx)
movq 0x10008(%r12), %rsi
movq -0x18(%rax,%rcx), %rdx
addq %rsi, %rdx
movq %rdx, -0x8(%rax,%rcx)
movq 0x48(%rbp), %rbx
cmpq $0x0, 0x128(%rbx)
je 0xb15f
movq %rsi, 0x20(%rsp)
leaq 0xe0(%rbx), %rdi
movq %rdi, 0x10(%rsp)
callq 0x4750
movq 0x128(%rbx), %r15
movl 0x120(%rbx), %eax
testl %eax, %eax
je 0xb0f0
movq 0x118(%rbx), %rcx
cmpq 0x20(%r15), %rcx
ja 0xb2f1
movq %r15, 0x40(%rsp)
xorl %ebp, %ebp
movslq %eax, %rcx
cmpq %rcx, %rbp
jge 0xb1b3
movq 0x20(%r15), %rcx
cmpq 0x118(%rbx), %rcx
jne 0xb1b3
movq 0x18(%r15), %r9
testq %r14, %r14
je 0xb122
cmpq %r14, %r9
je 0xb16b
movq 0x70(%rbx), %r8
movq 0x108(%rbx), %rdi
movl 0x10(%r15), %esi
movq (%r15), %rdx
movq 0x8(%r15), %rcx
shlq $0x10, %r8
addq %r9, %r8
movl 0x14(%r15), %r9d
callq 0x1390f
testl %eax, %eax
js 0xb28e
incq %rbp
movl 0x120(%rbx), %eax
addq $0x28, %r15
jmp 0xb0f7
incq 0x118(%rbx)
jmp 0xb1f2
decl %eax
cmpl %eax, %ebp
je 0xb17b
cmpq 0x48(%r15), %rcx
jae 0xb310
movq 0x20(%rsp), %r8
addq 0x70(%rbx), %r8
movq 0x108(%rbx), %rdi
movl 0x10(%r15), %esi
movq (%r15), %rdx
movq 0x8(%r15), %rcx
shlq $0x10, %r8
movl 0x14(%r15), %r9d
callq 0x1390f
testl %eax, %eax
js 0xb28e
incl %ebp
movl 0x120(%rbx), %eax
movslq %ebp, %r14
imulq $0x28, %r14, %rsi
movq 0x40(%rsp), %rdi
addq %rdi, %rsi
subl %r14d, %eax
cltq
imulq $0x28, %rax, %rdx
callq 0x4830
subl %r14d, 0x120(%rbx)
incq 0x118(%rbx)
movq 0x10(%rsp), %rdi
callq 0x4340
movq 0x8(%rsp), %r15
movq 0x18(%rsp), %rbp
movq 0x40(%rbp), %rdi
movq %r12, %rsi
addq $0x8, %rsi
movq 0x10008(%r12), %rdx
callq 0xb9a0
cmpq 0x10008(%r12), %rax
movq 0x38(%rsp), %r14
jne 0xb29d
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x10008(%r12), %rax
addq %rax, 0x70(%r15)
movq %rbx, %rdi
callq 0x4340
movl 0x5c(%r15), %eax
incl %eax
movl %eax, 0x5c(%r15)
testw $0x1ff, %ax             # imm = 0x1FF
jne 0xb25b
movq 0x40(%rbp), %rdi
callq 0xd64f
testl %eax, %eax
jne 0xb29d
movq %r13, %rdi
xorl %esi, %esi
callq 0x2bfa2
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq (%r15), %rdi
movq %r12, %rsi
callq 0x4924e
decl 0x58(%r15)
movq %rbx, %rdi
callq 0x4340
jmp 0xafc6
movq 0x10(%rsp), %rdi
callq 0x4340
movq 0x8(%rsp), %r15
addq $0x20, %r15
pushq $-0x1
popq %rbx
movq %r15, %r14
movq (%r14), %rdi
callq 0x2bd67
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x40(%rbp), %rdi
callq 0xd64f
testl %eax, %eax
jne 0xb29d
xorl %ebx, %ebx
jmp 0xb2a7
leaq 0x6bffa(%rip), %rdi     # 0x772d3
leaq 0x6b8f0(%rip), %rsi     # 0x76bd0
leaq 0x6bfee(%rip), %rcx     # 0x772d5
movl $0x582, %edx            # imm = 0x582
callq 0x42f0
leaq 0x6bffa(%rip), %rdi     # 0x772f2
leaq 0x6b8d1(%rip), %rsi     # 0x76bd0
leaq 0x6c032(%rip), %rcx     # 0x77338
movl $0xf7, %edx
callq 0x42f0
leaq 0x6c04c(%rip), %rdi     # 0x77363
leaq 0x6b8b2(%rip), %rsi     # 0x76bd0
leaq 0x6c013(%rip), %rcx     # 0x77338
movl $0x10a, %edx            # imm = 0x10A
callq 0x42f0