// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x58(%rax)
je 0xee72
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl $0x1, (%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl $0x1, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movslq 0x4(%rax), %rdi
movl $0x10, %esi
callq 0x42f0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, 0x8(%rax)
jne 0xee70
jmp 0xf26c
jmp 0xee72
jmp 0xee74
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x4cf90
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0xf241
movq -0x28(%rbp), %rdi
callq 0x4d460
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xeea5
jmp 0xeec4
leaq 0xe0641(%rip), %rdi     # 0xef4ed
leaq 0xdff32(%rip), %rsi     # 0xeede5
movl $0x582, %edx            # imm = 0x582
leaq 0xe0630(%rip), %rcx     # 0xef4ef
callq 0x42e0
movq -0x18(%rbp), %rax
cmpl $0x0, 0x58(%rax)
je 0xf138
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl (%rax), %eax
movq -0x18(%rbp), %rcx
movq 0x50(%rcx), %rcx
cmpl 0x4(%rcx), %eax
jle 0xf088
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl (%rax), %ecx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
cmpl $0x0, 0x4(%rax)
jle 0xf040
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x4(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x4(%rax), %ecx
sarl $0x0, %ecx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
orl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x4(%rax), %ecx
sarl %ecx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
orl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x4(%rax), %ecx
sarl $0x2, %ecx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
orl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x4(%rax), %ecx
sarl $0x4, %ecx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
orl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x4(%rax), %ecx
sarl $0x8, %ecx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
orl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x4(%rax), %ecx
sarl $0x10, %ecx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
orl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
movq 0x50(%rcx), %rcx
imull $0x0, 0x4(%rcx), %edx
addl $0x1, %edx
xorl %ecx, %ecx
subl %edx, %ecx
cmpl $0x0, %ecx
setg %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
movslq %ecx, %rdx
movl $0x1f, %ecx
subq %rdx, %rcx
sarl %cl, %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
addl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x4(%rax), %eax
jmp 0xf042
jmp 0xf042
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movslq 0x4(%rax), %rsi
shlq $0x4, %rsi
callq 0x4820
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, 0x8(%rax)
jne 0xf086
jmp 0xf26c
jmp 0xf088
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x50(%rcx), %rcx
movl (%rcx), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x30(%rbp), %rax
addq 0x20010(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rdx
movq 0x50(%rdx), %rdx
movl (%rdx), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x50(%rcx), %rcx
movl (%rcx), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
addq 0x10008(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rdx
movq 0x50(%rdx), %rdx
movl (%rdx), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x20010(%rax), %rsi
movq -0x30(%rbp), %rax
movq 0x10008(%rax), %rdx
callq 0x12e00
cmpl $0x0, %eax
jge 0xf161
jmp 0xf26c
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x8, %rsi
movq -0x30(%rbp), %rax
movq 0x10008(%rax), %rdx
callq 0xfdc0
movq -0x30(%rbp), %rcx
cmpq 0x10008(%rcx), %rax
je 0xf193
jmp 0xf26c
movq -0x20(%rbp), %rdi
addq $0xe0, %rdi
callq 0x4730
movq -0x30(%rbp), %rax
movq 0x10008(%rax), %rcx
movq -0x20(%rbp), %rax
addq 0x70(%rax), %rcx
movq %rcx, 0x70(%rax)
movq -0x20(%rbp), %rdi
addq $0xe0, %rdi
callq 0x4330
movq -0x20(%rbp), %rcx
movl 0x5c(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rcx)
movl $0x200, %ecx            # imm = 0x200
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0xf1fa
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x143b0
cmpl $0x0, %eax
je 0xf1f8
jmp 0xf26c
jmp 0xf1fa
movq -0x28(%rbp), %rdi
xorl %esi, %esi
callq 0x4d410
movq -0x20(%rbp), %rdi
addq $0x30, %rdi
callq 0x4730
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x822f0
movq -0x20(%rbp), %rax
movl 0x58(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x58(%rax)
movq -0x20(%rbp), %rdi
addq $0x30, %rdi
callq 0x4330
jmp 0xee74
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x143b0
cmpl $0x0, %eax
je 0xf255
jmp 0xf26c
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x4d090
movq $0x0, -0x8(%rbp)
jmp 0xf284
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x4d090
movq $-0x1, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nop