// File: /workspace/github_c_makefile_star2_O1/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 0xc49f
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 0xc85e
movq 0x20(%r14), %rdi
callq 0x37249
testq %rax, %rax
je 0xc83d
movq %rax, %rdi
leaq 0xe0(%r14), %rax
movq %rax, 0x30(%rsp)
leaq 0x30(%r14), %rax
movq %rax, 0x28(%rsp)
movq %r14, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq %rdi, 0x40(%rsp)
callq 0x37576
testq %rax, %rax
je 0xc880
movq %rax, %rbp
movq %rax, %r13
cmpl $0x0, 0x58(%r12)
je 0xc590
movq 0x50(%r12), %rax
movl (%rax), %ecx
leal 0x1(%rcx), %edx
movl %edx, (%rax)
cmpl 0x4(%rax), %ecx
jl 0xc55e
testl %ecx, %ecx
js 0xc539
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 0xc85e
movq 0x50(%r12), %rax
movq 0x8(%rax), %rcx
movslq (%rax), %rax
shlq $0x4, %rax
movq 0x20010(%r13), %rdx
addq -0x20(%rcx,%rax), %rdx
movq %rdx, -0x10(%rcx,%rax)
movq 0x10008(%r13), %rdx
addq -0x18(%rcx,%rax), %rdx
movq %rdx, -0x8(%rcx,%rax)
movq 0x48(%r12), %rbx
cmpq $0x0, 0x128(%rbx)
je 0xc655
movq 0x20010(%r13), %r12
movq 0x10008(%r13), %r15
leaq 0xe0(%rbx), %rdi
movq %rdi, 0x18(%rsp)
callq 0x4750
movq 0x128(%rbx), %r14
movl 0x120(%rbx), %eax
testl %eax, %eax
je 0xc5e4
movq 0x118(%rbx), %rcx
cmpq 0x20(%r14), %rcx
ja 0xc89f
testl %eax, %eax
movq %r14, 0x38(%rsp)
jle 0xc65e
movq %r15, 0x20(%rsp)
xorl %r15d, %r15d
movq 0x20(%r14), %rcx
cmpq 0x118(%rbx), %rcx
jne 0xc661
testq %r12, %r12
je 0xc611
cmpq %r12, 0x18(%r14)
je 0xc7d7
movq 0x70(%rbx), %r8
movq 0x108(%rbx), %rdi
movl 0x10(%r14), %esi
movq (%r14), %rdx
movq 0x8(%r14), %rcx
shlq $0x10, %r8
addq 0x18(%r14), %r8
movl 0x14(%r14), %r9d
callq 0x16eed
testl %eax, %eax
js 0xc84f
incq %r15
movslq 0x120(%rbx), %rax
addq $0x28, %r14
cmpq %rax, %r15
jl 0xc5f5
jmp 0xc661
incq 0x118(%rbx)
jmp 0xc6a5
xorl %r15d, %r15d
movq 0x8(%rsp), %r12
movslq %r15d, %r14
leaq (%r14,%r14,4), %rax
movq 0x38(%rsp), %rdi
leaq (%rdi,%rax,8), %rsi
movslq 0x120(%rbx), %rax
subq %r14, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rdx
callq 0x4830
subl %r14d, 0x120(%rbx)
incq 0x118(%rbx)
movq 0x18(%rsp), %rdi
callq 0x4340
movq 0x40(%r12), %r15
movq 0x10008(%r13), %rbx
testb $0x2, 0x30(%r15)
jne 0xc6de
movq 0x18(%r15), %rsi
movq %rsi, %rax
subq 0x8(%r15), %rax
cmpq %rbx, %rax
jae 0xc6de
addq %rbx, %rsi
subq (%r15), %rsi
movq %r15, %rdi
callq 0xefdd
movq 0x18(%r15), %rax
movq %rax, 0x10(%r15)
addq $0x8, %rbp
movq 0x8(%r15), %rdi
movq 0x18(%r15), %r12
subq %rdi, %r12
cmpq %rbx, %r12
ja 0xc6f7
cmpq (%r15), %rdi
je 0xc723
cmpq %rbx, %r12
movq %rbx, %r14
cmovbq %r12, %r14
movq %rbp, %rsi
movq %r14, %rdx
callq 0x43c0
addq %r14, 0x8(%r15)
cmpq %rbx, %r12
jae 0xc736
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r14, %rcx
jmp 0xc72e
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0xf4c6
movq %rax, %r14
movq 0x8(%rsp), %r12
cmpq 0x10008(%r13), %r14
movq 0x10(%rsp), %r14
jne 0xc85e
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 0xc790
movq 0x40(%r12), %rdi
callq 0xf3e7
testl %eax, %eax
jne 0xc85e
movq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x37551
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq (%r14), %rdi
movq %r13, %rsi
callq 0x5a8fd
decl 0x58(%r14)
movq %rbx, %rdi
callq 0x4340
movq 0x20(%r14), %rdi
callq 0x37249
movq %rax, %rdi
testq %rax, %rax
jne 0xc4d3
jmp 0xc83d
decl %eax
cmpl %eax, %r15d
je 0xc7e8
cmpq 0x48(%r14), %rcx
jae 0xc8be
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 0x16eed
testl %eax, %eax
movq 0x8(%rsp), %r12
js 0xc81e
incl %r15d
jmp 0xc82e
movq 0x18(%rsp), %rdi
movl %eax, %r14d
callq 0x4340
movl %r14d, %eax
movq 0x10(%rsp), %r14
testl %eax, %eax
jns 0xc666
jmp 0xc85e
movq 0x40(%r12), %rdi
callq 0xf3e7
testl %eax, %eax
jne 0xc85e
xorl %ebx, %ebx
jmp 0xc865
movq 0x18(%rsp), %rdi
callq 0x4340
movq 0x10(%rsp), %r14
movq $-0x1, %rbx
movq 0x20(%r14), %rdi
callq 0x37316
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8ba5c(%rip), %rdi     # 0x982e3
leaq 0x8b352(%rip), %rsi     # 0x97be0
leaq 0x8ba50(%rip), %rcx     # 0x982e5
movl $0x582, %edx            # imm = 0x582
callq 0x42f0
leaq 0x8ba5c(%rip), %rdi     # 0x98302
leaq 0x8b333(%rip), %rsi     # 0x97be0
leaq 0x8ba94(%rip), %rcx     # 0x98348
movl $0xf7, %edx
callq 0x42f0
leaq 0x8baae(%rip), %rdi     # 0x98373
leaq 0x8b314(%rip), %rsi     # 0x97be0
leaq 0x8ba75(%rip), %rcx     # 0x98348
movl $0x10a, %edx            # imm = 0x10A
callq 0x42f0