// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/sam.c
// Function: sam_dispatcher_write
static void *sam_dispatcher_write(void *vp) {
    htsFile *fp = vp;
    SAM_state *fd = fp->state;
    hts_tpool_result *r;

    // Iterates until result queue is shutdown, where it returns NULL.
    while ((r = hts_tpool_next_result_wait(fd->q))) {
        sp_lines *gl = (sp_lines *)hts_tpool_result_data(r);
        if (!gl) {
            sam_state_err(fd, ENOMEM);
            goto err;
        }

        if (fp->idx) {
            sp_bams *gb = gl->bams;
            int i = 0, count = 0;
            while (i < gl->data_size) {
                int j = i;
                while (i < gl->data_size && gl->data[i] != '\n')
                    i++;
                if (i < gl->data_size)
                    i++;

                if (fp->is_bgzf) {
                    if (bgzf_flush_try(fp->fp.bgzf, i-j) < 0)
                        goto err;
                    if (bgzf_write(fp->fp.bgzf, &gl->data[j], i-j) != i-j)
                        goto err;
                } else {
                    if (hwrite(fp->fp.hfile, &gl->data[j], i-j) != i-j)
                        goto err;
                }

                bam1_t *b = &gb->bams[count++];
                if (fp->format.compression == bgzf) {
                    if (bgzf_idx_push(fp->fp.bgzf, fp->idx,
                                      b->core.tid, b->core.pos, bam_endpos(b),
                                      bgzf_tell(fp->fp.bgzf),
                                      !(b->core.flag&BAM_FUNMAP)) < 0) {
                        sam_state_err(fd, errno ? errno : ENOMEM);
                        hts_log_error("Read '%s' with ref_name='%s', ref_length=%"PRIhts_pos", flags=%d, pos=%"PRIhts_pos" cannot be indexed",
                                bam_get_qname(b), sam_hdr_tid2name(fd->h, b->core.tid), sam_hdr_tid2len(fd->h, b->core.tid), b->core.flag, b->core.pos+1);
                        goto err;
                    }
                } else {
                    if (hts_idx_push(fp->idx, b->core.tid, b->core.pos, bam_endpos(b),
                                     bgzf_tell(fp->fp.bgzf), !(b->core.flag&BAM_FUNMAP)) < 0) {
                        sam_state_err(fd, errno ? errno : ENOMEM);
                        hts_log_error("Read '%s' with ref_name='%s', ref_length=%"PRIhts_pos", flags=%d, pos=%"PRIhts_pos" cannot be indexed",
                                bam_get_qname(b), sam_hdr_tid2name(fd->h, b->core.tid), sam_hdr_tid2len(fd->h, b->core.tid), b->core.flag, b->core.pos+1);
                        goto err;
                    }
                }
            }

            assert(count == gb->nbams);

            // Add bam array to free-list
            pthread_mutex_lock(&fd->lines_m);
            gb->next = fd->bams;
            fd->bams = gl->bams;
            gl->bams = NULL;
            pthread_mutex_unlock(&fd->lines_m);
        } else {
            if (fp->is_bgzf) {
                // We keep track of how much in the current block we have
                // remaining => R.  We look for the last newline in input
                // [i] to [i+R], backwards => position N.
                //
                // If we find a newline, we write out bytes i to N.
                // We know we cannot fit the next record in this bgzf block,
                // so we flush what we have and copy input N to i+R into
                // the start of a new block, and recompute a new R for that.
                //
                // If we don't find a newline (i==N) then we cannot extend
                // the current block at all, so flush whatever is in it now
                // if it ends on a newline.
                // We still copy i(==N) to i+R to the next block and
                // continue as before with a new R.
                //
                // The only exception on the flush is when we run out of
                // data in the input.  In that case we skip it as we don't
                // yet know if the next record will fit.
                //
                // Both conditions share the same code here:
                // - Look for newline (pos N)
                // - Write i to N (which maybe 0)
                // - Flush if block ends on newline and not end of input
                // - write N to i+R

                int i = 0;
                BGZF *fb = fp->fp.bgzf;
                while (i < gl->data_size) {
                    // remaining space in block
                    int R = BGZF_BLOCK_SIZE - fb->block_offset;
                    int eod = 0;
                    if (R > gl->data_size-i)
                        R = gl->data_size-i, eod = 1;

                    // Find last newline in input data
                    int N = i + R;
                    while (--N > i) {
                        if (gl->data[N] == '\n')
                            break;
                    }

                    if (N != i) {
                        // Found a newline
                        N++;
                        if (bgzf_write(fb, &gl->data[i], N-i) != N-i)
                            goto err;
                    }

                    // Flush bgzf block
                    int b_off = fb->block_offset;
                    if (!eod && b_off &&
                        ((char *)fb->uncompressed_block)[b_off-1] == '\n')
                        if (bgzf_flush_try(fb, BGZF_BLOCK_SIZE) < 0)
                            goto err;

                    // Copy from N onwards into next block
                    if (i+R > N)
                        if (bgzf_write(fb, &gl->data[N], i+R - N)
                            != i+R - N)
                            goto err;

                    i = i+R;
                }
            } else {
                if (hwrite(fp->fp.hfile, gl->data, gl->data_size) != gl->data_size)
                    goto err;
            }
        }

        hts_tpool_delete_result(r, 0);

        // Also updated by main thread
        pthread_mutex_lock(&fd->lines_m);
        gl->next = fd->lines;
        fd->lines = gl;
        pthread_mutex_unlock(&fd->lines_m);
    }

    sam_state_err(fd, 0); // success
    hts_tpool_process_shutdown(fd->q);
    return NULL;

 err:
    sam_state_err(fd, errno ? errno : EIO);
    return (void *)-1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4cf90
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x40934
movq -0x28(%rbp), %rdi
callq 0x4d460
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x40275
movq -0x20(%rbp), %rdi
movl $0xc, %esi
callq 0x46a20
jmp 0x40956
movq -0x18(%rbp), %rax
cmpq $0x0, 0x68(%rax)
je 0x40734
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x3c(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x406bd
movl -0x3c(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x3c(%rbp), %ecx
movq -0x30(%rbp), %rdx
xorl %eax, %eax
cmpl 0x18(%rdx), %ecx
movb %al, -0x71(%rbp)
jge 0x402de
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x3c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
setne %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0x402e7
jmp 0x402f2
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x402b4
movl -0x3c(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x40307
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x40372
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdi
movl -0x3c(%rbp), %eax
subl -0x44(%rbp), %eax
movslq %eax, %rsi
callq 0xff00
cmpl $0x0, %eax
jge 0x40338
jmp 0x40956
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x44(%rbp), %rax
addq %rax, %rsi
movl -0x3c(%rbp), %eax
subl -0x44(%rbp), %eax
movslq %eax, %rdx
callq 0xffb0
movl -0x3c(%rbp), %ecx
subl -0x44(%rbp), %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
je 0x40370
jmp 0x40956
jmp 0x403ac
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x44(%rbp), %rax
addq %rax, %rsi
movl -0x3c(%rbp), %eax
subl -0x44(%rbp), %eax
movslq %eax, %rdx
callq 0x38db0
movl -0x3c(%rbp), %ecx
subl -0x44(%rbp), %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
je 0x403aa
jmp 0x40956
jmp 0x403ac
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x40(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x40(%rbp)
movslq %ecx, %rcx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x2, 0x54(%rax)
jne 0x40554
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x50(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x84(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x50(%rbp), %rdi
callq 0x33a20
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rsi
movl -0x84(%rbp), %edx
movq -0x80(%rbp), %rcx
movq %rax, %r8
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %r9
shlq $0x10, %r9
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movl 0x10(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %r9
movq -0x50(%rbp), %rax
movzwl 0x10(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, (%rsp)
callq 0xbec0
cmpl $0x0, %eax
jge 0x4054f
movq -0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
callq 0x4050
cmpl $0x0, (%rax)
je 0x404a7
callq 0x4050
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
jmp 0x404b4
movl $0xc, %eax
movl %eax, -0xa4(%rbp)
jmp 0x404b4
movq -0xa0(%rbp), %rdi
movl -0xa4(%rbp), %esi
callq 0x46a20
movq -0x50(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0xb8(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rax
movl 0x8(%rax), %esi
callq 0xdda00
movq %rax, -0xb0(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rax
movl 0x8(%rax), %esi
callq 0xdda90
movq -0xb8(%rbp), %rcx
movq -0xb0(%rbp), %r8
movq %rax, %r9
movq -0x50(%rbp), %rax
movzwl 0x10(%rax), %r10d
movq -0x50(%rbp), %rax
movq (%rax), %rax
addq $0x1, %rax
movl $0x1, %edi
leaq 0xb2800(%rip), %rsi     # 0xf2d33
leaq 0xb1ca9(%rip), %rdx     # 0xf21e3
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x1b030
jmp 0x40956
jmp 0x406b8
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0x50(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0xc4(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0x50(%rbp), %rdi
callq 0x33a20
movq -0xd0(%rbp), %rdi
movl -0xc4(%rbp), %esi
movq -0xc0(%rbp), %rdx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %r8
shlq $0x10, %r8
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movl 0x10(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %r8
movq -0x50(%rbp), %rax
movzwl 0x10(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x1f990
cmpl $0x0, %eax
jge 0x406b6
movq -0x20(%rbp), %rax
movq %rax, -0xd8(%rbp)
callq 0x4050
cmpl $0x0, (%rax)
je 0x4060e
callq 0x4050
movl (%rax), %eax
movl %eax, -0xdc(%rbp)
jmp 0x4061b
movl $0xc, %eax
movl %eax, -0xdc(%rbp)
jmp 0x4061b
movq -0xd8(%rbp), %rdi
movl -0xdc(%rbp), %esi
callq 0x46a20
movq -0x50(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0xf0(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rax
movl 0x8(%rax), %esi
callq 0xdda00
movq %rax, -0xe8(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rax
movl 0x8(%rax), %esi
callq 0xdda90
movq -0xf0(%rbp), %rcx
movq -0xe8(%rbp), %r8
movq %rax, %r9
movq -0x50(%rbp), %rax
movzwl 0x10(%rax), %r10d
movq -0x50(%rbp), %rax
movq (%rax), %rax
addq $0x1, %rax
movl $0x1, %edi
leaq 0xb2699(%rip), %rsi     # 0xf2d33
leaq 0xb1b42(%rip), %rdx     # 0xf21e3
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x1b030
jmp 0x40956
jmp 0x406b8
jmp 0x4029e
movl -0x40(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jne 0x406cb
jmp 0x406ea
leaq 0xb2676(%rip), %rdi     # 0xf2d48
leaq 0xb1b6b(%rip), %rsi     # 0xf2244
movl $0xeb7, %edx            # imm = 0xEB7
leaq 0xb2676(%rip), %rcx     # 0xf2d5b
callq 0x42e0
movq -0x20(%rbp), %rdi
addq $0x18, %rdi
callq 0x4730
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x20(%rbp), %rdi
addq $0x18, %rdi
callq 0x4330
jmp 0x408ef
movq -0x18(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x408bf
movl $0x0, -0x54(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x60(%rbp)
movl -0x54(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x408bd
movq -0x60(%rbp), %rcx
movl $0xff00, %eax           # imm = 0xFF00
subl 0x10(%rcx), %eax
movl %eax, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movl -0x64(%rbp), %eax
movq -0x30(%rbp), %rcx
movl 0x18(%rcx), %ecx
subl -0x54(%rbp), %ecx
cmpl %ecx, %eax
jle 0x407a7
movq -0x30(%rbp), %rax
movl 0x18(%rax), %eax
subl -0x54(%rbp), %eax
movl %eax, -0x64(%rbp)
movl $0x1, -0x68(%rbp)
movl -0x54(%rbp), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x6c(%rbp)
cmpl -0x54(%rbp), %eax
jle 0x407d7
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x6c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x407d5
jmp 0x407d7
jmp 0x407b0
movl -0x6c(%rbp), %eax
cmpl -0x54(%rbp), %eax
je 0x4081e
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x54(%rbp), %rax
addq %rax, %rsi
movl -0x6c(%rbp), %eax
subl -0x54(%rbp), %eax
movslq %eax, %rdx
callq 0xffb0
movl -0x6c(%rbp), %ecx
subl -0x54(%rbp), %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
je 0x4081c
jmp 0x40956
jmp 0x4081e
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x68(%rbp)
jne 0x40868
cmpl $0x0, -0x70(%rbp)
je 0x40868
movq -0x60(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x70(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x40868
movq -0x60(%rbp), %rdi
movl $0xff00, %esi           # imm = 0xFF00
callq 0xff00
cmpl $0x0, %eax
jge 0x40866
jmp 0x40956
jmp 0x40868
movl -0x54(%rbp), %eax
addl -0x64(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jle 0x408af
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x6c(%rbp), %rax
addq %rax, %rsi
movl -0x54(%rbp), %eax
addl -0x64(%rbp), %eax
subl -0x6c(%rbp), %eax
movslq %eax, %rdx
callq 0xffb0
movl -0x54(%rbp), %ecx
addl -0x64(%rbp), %ecx
subl -0x6c(%rbp), %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
je 0x408ad
jmp 0x40956
jmp 0x408af
movl -0x54(%rbp), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x54(%rbp)
jmp 0x4075c
jmp 0x408ed
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rax
movslq 0x18(%rax), %rdx
callq 0x38db0
movq -0x30(%rbp), %rcx
movslq 0x18(%rcx), %rcx
cmpq %rcx, %rax
je 0x408eb
jmp 0x40956
jmp 0x408ed
jmp 0x408ef
movq -0x28(%rbp), %rdi
xorl %esi, %esi
callq 0x4d410
movq -0x20(%rbp), %rdi
addq $0x18, %rdi
callq 0x4730
movq -0x20(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x20(%rbp), %rdi
addq $0x18, %rdi
callq 0x4330
jmp 0x40233
movq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0x46a20
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4d330
movq $0x0, -0x8(%rbp)
jmp 0x409a4
movq -0x20(%rbp), %rax
movq %rax, -0xf8(%rbp)
callq 0x4050
cmpl $0x0, (%rax)
je 0x4097a
callq 0x4050
movl (%rax), %eax
movl %eax, -0xfc(%rbp)
jmp 0x40987
movl $0x5, %eax
movl %eax, -0xfc(%rbp)
jmp 0x40987
movq -0xf8(%rbp), %rdi
movl -0xfc(%rbp), %esi
callq 0x46a20
movq $-0x1, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)