// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r14
movq 0x40(%rdi), %rbx
leaq 0x18(%rbx), %rax
movq %rax, 0x28(%rsp)
movq %rbx, 0x8(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x40(%rbx), %rdi
callq 0x2bca5
testq %rax, %rax
je 0x2577f
movq %rax, %r15
movq %rax, %rdi
callq 0x2bfc2
testq %rax, %rax
je 0x25797
movq %rax, %r13
cmpq $0x0, 0x68(%r14)
movq %r15, 0x30(%rsp)
je 0x25551
movq 0x28(%r13), %r14
xorl %r12d, %r12d
xorl %ecx, %ecx
movq %r14, (%rsp)
movl 0x18(%r13), %eax
cmpl %eax, %ecx
jge 0x25670
movslq %eax, %rsi
movslq %ecx, %rbp
movq %rbp, %rdx
movq 0x18(%rsp), %r8
cmpq %rdx, %rsi
je 0x2540d
movq 0x10(%r13), %rdi
cmpb $0xa, (%rdi,%rdx)
je 0x25411
incq %rdx
jmp 0x253f9
xorl %ebx, %ebx
jmp 0x2541b
xorl %ebx, %ebx
cmpq %rsi, %rdx
setl %bl
movl %edx, %eax
addl %eax, %ebx
testb $0x10, (%r8)
movq 0x38(%r8), %rdi
jne 0x2543f
addq 0x10(%r13), %rbp
movl %ebx, %eax
subl %ecx, %eax
movslq %eax, %r15
movq %rbp, %rsi
movq %r15, %rdx
callq 0x213c7
jmp 0x2546e
movl %ebx, %eax
subl %ecx, %eax
movslq %eax, %r15
movq %r15, %rsi
callq 0xba3a
testl %eax, %eax
js 0x25750
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rdi
addq 0x10(%r13), %rbp
movq %rbp, %rsi
movq %r15, %rdx
callq 0xba64
cmpq %r15, %rax
jne 0x25750
imulq $0x50, %r12, %rbp
incq %r12
movq %r12, 0x10(%rsp)
addq 0x10(%r14), %rbp
movq 0x18(%rsp), %r14
cmpl $0x2, 0x54(%r14)
jne 0x254ee
movq 0x38(%r14), %r15
movq 0x68(%r14), %rax
movq %rax, 0x20(%rsp)
movl 0x8(%rbp), %r12d
movq (%rbp), %r14
movq %rbp, %rdi
callq 0x1e518
movq 0x18(%r15), %rcx
shlq $0x10, %rcx
movzwl 0x10(%r15), %r9d
orq %rcx, %r9
movzwl 0x10(%rbp), %ecx
xorl %r10d, %r10d
testb $0x4, %cl
sete %r10b
subq $0x8, %rsp
movq %r15, %rdi
movq 0x28(%rsp), %rsi
movl %r12d, %edx
movq %r14, %rcx
movq %rax, %r8
pushq %r10
callq 0x9454
popq %rcx
popq %rdx
jmp 0x25539
movq 0x68(%r14), %r12
movl 0x8(%rbp), %eax
movl %eax, 0x20(%rsp)
movq (%rbp), %r15
movq %rbp, %rdi
callq 0x1e518
movq 0x38(%r14), %rcx
movq 0x18(%rcx), %rdx
shlq $0x10, %rdx
movzwl 0x10(%rcx), %r8d
orq %rdx, %r8
movzwl 0x10(%rbp), %ecx
xorl %r9d, %r9d
testb $0x4, %cl
sete %r9b
movq %r12, %rdi
movl 0x20(%rsp), %esi
movq %r15, %rdx
movq %rax, %rcx
callq 0x1390f
movl %ebx, %ecx
testl %eax, %eax
movq (%rsp), %r14
movq 0x10(%rsp), %r12
jns 0x253df
jmp 0x256e4
testb $0x10, (%r14)
movq 0x38(%r14), %rdi
jne 0x2557a
movq 0x10(%r13), %rsi
movslq 0x18(%r13), %rdx
callq 0x213c7
movslq 0x18(%r13), %rcx
cmpq %rcx, %rax
je 0x256a8
jmp 0x25750
xorl %r15d, %r15d
movq %rdi, (%rsp)
movl 0x18(%r13), %r12d
subl %r15d, %r12d
jle 0x256a8
movl 0x10(%rdi), %eax
movl $0xff00, %ebp           # imm = 0xFF00
subl %eax, %ebp
cmpl %r12d, %ebp
movl %r12d, %edx
cmovll %ebp, %edx
leal (%rdx,%r15), %ecx
movl %ecx, 0x10(%rsp)
movslq %ecx, %rcx
movslq %r15d, %rsi
negl %edx
decq %rcx
movl %edx, %r14d
cmpq %rsi, %rcx
jle 0x255cf
movq 0x10(%r13), %r8
leal 0x1(%r14), %edx
cmpb $0xa, (%r8,%rcx)
leaq -0x1(%rcx), %rcx
jne 0x255b4
cmpl $-0x1, %r14d
je 0x255fc
addq 0x10(%r13), %rsi
movl %r14d, %eax
negl %eax
movslq %eax, %rbx
movq %rbx, %rdx
callq 0xba64
movq (%rsp), %rdi
cmpq %rbx, %rax
jne 0x25750
subl %r14d, %r15d
movl 0x10(%rdi), %eax
movl %r15d, %ebx
cmpl %r12d, %ebp
setle %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x25634
movq 0x28(%rdi), %rcx
cltq
cmpb $0xa, -0x1(%rcx,%rax)
jne 0x25634
movl $0xff00, %esi           # imm = 0xFF00
callq 0xba3a
movq (%rsp), %rdi
testl %eax, %eax
js 0x25750
movl 0x10(%rsp), %ecx
movl %ecx, %eax
movl %ecx, %r15d
subl %ebx, %eax
jle 0x25581
movslq %ebx, %rsi
addq 0x10(%r13), %rsi
movslq %eax, %rbx
movq %rbx, %rdx
callq 0xba64
movq (%rsp), %rdi
movl 0x10(%rsp), %ecx
movl %ecx, %r15d
cmpq %rbx, %rax
je 0x25581
jmp 0x25750
cmpl %r12d, 0x18(%r14)
jne 0x257a4
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x8(%rsp), %rcx
movq 0x60(%rcx), %rax
movq %rax, (%r14)
movq 0x28(%r13), %rax
movq %rax, 0x60(%rcx)
andq $0x0, 0x28(%r13)
movq %rbx, %rdi
callq 0x4340
movq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x2bfa2
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x4750
movq 0x8(%rsp), %rbx
movq 0x58(%rbx), %rax
movq %rax, (%r13)
movq %r13, 0x58(%rbx)
movq %r14, %rdi
callq 0x4340
movq 0x18(%rsp), %r14
jmp 0x25399
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
pushq $0xc
popq %rsi
cmovnel %eax, %esi
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x29211
movq 0x38(%rbp), %rbx
movq (%r15), %rdi
movl 0x8(%rbp), %esi
callq 0x6cf95
movq %rax, %r14
movq (%r15), %rdi
movl 0x8(%rbp), %esi
callq 0x6cfd3
movzwl 0x10(%rbp), %r10d
movq (%rbp), %r11
incq %r11
leaq 0x55038(%rip), %rsi     # 0x7a768
leaq 0x544e9(%rip), %rdx     # 0x79c20
pushq $0x1
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x110c6
popq %rax
popq %rcx
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
pushq $0x5
popq %rsi
cmovnel %eax, %esi
movq 0x8(%rsp), %rdi
callq 0x29211
pushq $-0x1
popq %r14
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x29211
movq 0x40(%rbx), %rdi
callq 0x2bf17
jmp 0x2576d
pushq $0xc
popq %rsi
movq %rbx, %rdi
callq 0x29211
jmp 0x25750
leaq 0x54fd2(%rip), %rdi     # 0x7a77d
leaq 0x544cf(%rip), %rsi     # 0x79c81
leaq 0x54fd7(%rip), %rcx     # 0x7a790
movl $0xeb7, %edx            # imm = 0xEB7
callq 0x42f0