// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq 0x40(%rdi), %r14
movq 0x40(%r14), %rdi
callq 0x35e75
testq %rax, %rax
je 0x2dd23
movq %rax, %rbx
leaq 0x18(%r14), %rax
movq %rax, 0x20(%rsp)
movq %r14, 0x10(%rsp)
movq %r12, 0x18(%rsp)
movq %rbx, %rdi
callq 0x361a4
movq %rax, (%rsp)
testq %rax, %rax
je 0x2ddb1
cmpq $0x0, 0x68(%r12)
movq %rbx, 0x30(%rsp)
je 0x2dac4
movq (%rsp), %rdx
movq 0x28(%rdx), %r15
movl 0x18(%rdx), %ecx
testl %ecx, %ecx
jle 0x2db5b
movq %r15, 0x28(%rsp)
xorl %eax, %eax
xorl %r13d, %r13d
movq %rax, 0x8(%rsp)
movl %r13d, %eax
movq 0x10(%rdx), %rbp
movslq %r13d, %r15
movslq %ecx, %rdx
leal 0x1(%r15), %ecx
cmpl %ecx, %edx
cmovgl %edx, %ecx
movq %r15, %rdi
movb (%rbp,%rdi), %sil
cmpb $0xa, %sil
je 0x2d8e7
incq %rdi
cmpq %rdx, %rdi
jl 0x2d8d2
jmp 0x2d8e9
movl %edi, %ecx
xorl %r13d, %r13d
cmpb $0xa, %sil
sete %r13b
addl %ecx, %r13d
testb $0x10, (%r12)
movq 0x38(%r12), %rbx
movl %r13d, %ecx
jne 0x2d96e
subl %eax, %ecx
movslq %ecx, %r12
testb $0x2, 0x30(%rbx)
movq 0x18(%rbx), %r14
jne 0x2d93a
movq %r14, %rax
subq 0x8(%rbx), %rax
cmpq %r12, %rax
jae 0x2d93a
addq %r12, %r14
subq (%rbx), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0xee24
movq 0x18(%rbx), %r14
movq %r14, 0x10(%rbx)
addq %r15, %rbp
movq 0x8(%rbx), %rdi
subq %rdi, %r14
cmpq %r12, %r14
ja 0x2d94e
cmpq (%rbx), %rdi
je 0x2d9ac
cmpq %r12, %r14
movq %r12, %r15
cmovbq %r14, %r15
movq %rbp, %rsi
movq %r15, %rdx
callq 0x43c0
addq %r15, 0x8(%rbx)
cmpq %r12, %r14
jb 0x2d9af
jmp 0x2d9c3
subl %eax, %ecx
movslq %ecx, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0xcfdb
testl %eax, %eax
movq (%rsp), %rcx
js 0x2ddbe
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rdi
addq 0x10(%rcx), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0xd005
cmpq %r14, %rax
je 0x2d9cc
jmp 0x2ddbe
xorl %r15d, %r15d
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0xf300
movq %rax, %r15
cmpq %r12, %r15
jne 0x2ddbe
movq 0x8(%rsp), %rax
leaq (%rax,%rax,4), %rbp
shlq $0x4, %rbp
movq 0x28(%rsp), %rax
addq 0x10(%rax), %rbp
movq 0x18(%rsp), %r12
cmpl $0x2, 0x54(%r12)
jne 0x2da4c
movq 0x38(%r12), %rbx
movq 0x68(%r12), %r14
movl 0x8(%rbp), %r15d
movq (%rbp), %r12
movq %rbp, %rdi
callq 0x23104
movq 0x18(%rbx), %rcx
shlq $0x10, %rcx
movzwl 0x10(%rbx), %r9d
orq %rcx, %r9
movzwl 0x10(%rbp), %ecx
xorl %r10d, %r10d
testb $0x4, %cl
sete %r10b
subq $0x8, %rsp
movq %rbx, %rdi
movq %r14, %rsi
movl %r15d, %edx
movq %r12, %rcx
movq 0x20(%rsp), %r12
movq %rax, %r8
pushq %r10
callq 0xa408
addq $0x10, %rsp
jmp 0x2da95
movq 0x68(%r12), %rbx
movl 0x8(%rbp), %r14d
movq (%rbp), %r15
movq %rbp, %rdi
callq 0x23104
movq 0x38(%r12), %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 %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
movq %rax, %rcx
callq 0x1699a
testl %eax, %eax
js 0x2dd3d
movq 0x8(%rsp), %rax
incq %rax
movq (%rsp), %rdx
movl 0x18(%rdx), %ecx
cmpl %ecx, %r13d
jl 0x2d8b4
movq 0x10(%rsp), %r14
movq 0x28(%rsp), %r15
jmp 0x2db5d
testb $0x10, (%r12)
movq 0x38(%r12), %r9
movq %r9, 0x8(%rsp)
jne 0x2db9f
movq (%rsp), %rax
movq 0x10(%rax), %rbx
movslq 0x18(%rax), %r15
testb $0x2, 0x30(%r9)
movq 0x18(%r9), %r14
jne 0x2db1a
movq %r14, %rax
subq 0x8(%r9), %rax
cmpq %r15, %rax
jae 0x2db1a
addq %r15, %r14
subq (%r9), %r14
movq %r9, %rdi
movq %r14, %rsi
callq 0xee24
movq 0x8(%rsp), %r9
movq 0x18(%r9), %r14
movq %r14, 0x10(%r9)
movq 0x8(%r9), %rdi
subq %rdi, %r14
cmpq %r15, %r14
ja 0x2db2f
cmpq (%r9), %rdi
je 0x2dcad
cmpq %r15, %r14
movq %r15, %r12
cmovbq %r14, %r12
movq %rbx, %rsi
movq %r12, %rdx
callq 0x43c0
movq 0x8(%rsp), %r9
addq %r12, 0x8(%r9)
cmpq %r15, %r14
jb 0x2dcb0
jmp 0x2dcc4
xorl %eax, %eax
cmpl 0x18(%r15), %eax
jne 0x2ddf2
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x60(%r14), %rax
movq %rax, (%r15)
movq (%rsp), %rcx
movq 0x28(%rcx), %rax
movq %rax, 0x60(%r14)
movq $0x0, 0x28(%rcx)
movq %rbx, %rdi
movq %rcx, %rbx
callq 0x4340
jmp 0x2dcd5
movq (%rsp), %rbx
movl 0x18(%rbx), %r12d
testl %r12d, %r12d
jle 0x2dcd5
xorl %r14d, %r14d
movl %r14d, %r15d
movl 0x10(%r9), %eax
movl $0xff00, %r13d          # imm = 0xFF00
subl %eax, %r13d
subl %r14d, %r12d
cmpl %r12d, %r13d
movl %r12d, %ecx
cmovll %r13d, %ecx
leal (%rcx,%r15), %r14d
movslq %r14d, %rdi
movslq %r15d, %rsi
leal (%rcx,%rsi), %edx
decl %edx
cmpl %edx, %esi
cmovll %r15d, %edx
decq %rdi
movl %ecx, %ebp
cmpq %rsi, %rdi
jle 0x2dc06
movq 0x10(%rbx), %r8
leal -0x1(%rbp), %ecx
cmpb $0xa, (%r8,%rdi)
leaq -0x1(%rdi), %rdi
jne 0x2dbe8
addl %r15d, %ecx
movl %ecx, %edx
cmpl %r15d, %edx
je 0x2dc36
addq 0x10(%rbx), %rsi
movslq %ebp, %rbx
movq %r9, %rdi
movq %rbx, %rdx
callq 0xd005
movq 0x8(%rsp), %r9
cmpq %rbx, %rax
movq (%rsp), %rbx
jne 0x2ddbe
addl %ebp, %r15d
movl 0x10(%r9), %eax
cmpl %r12d, %r13d
setle %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x2dc6f
movq 0x28(%r9), %rcx
cltq
cmpb $0xa, -0x1(%rcx,%rax)
jne 0x2dc6f
movl $0xff00, %esi           # imm = 0xFF00
movq %r9, %rdi
callq 0xcfdb
movq 0x8(%rsp), %r9
testl %eax, %eax
js 0x2ddbe
movl %r14d, %eax
subl %r15d, %eax
jle 0x2dc9e
movslq %r15d, %rsi
addq 0x10(%rbx), %rsi
movslq %eax, %rbx
movq %r9, %rdi
movq %rbx, %rdx
callq 0xd005
movq 0x8(%rsp), %r9
cmpq %rbx, %rax
movq (%rsp), %rbx
jne 0x2ddbe
movl 0x18(%rbx), %r12d
cmpl %r12d, %r14d
jl 0x2dbb3
jmp 0x2dcd5
xorl %r12d, %r12d
movq %r9, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0xf300
movq %rax, %r12
movq (%rsp), %rbx
movslq 0x18(%rbx), %rax
cmpq %rax, %r12
jne 0x2ddbe
movq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x3617f
movq %rbx, %r15
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x10(%rsp), %r14
movq 0x58(%r14), %rax
movq %rax, (%r15)
movq %r15, 0x58(%r14)
movq %rbx, %rdi
callq 0x4340
movq 0x40(%r14), %rdi
callq 0x35e75
movq %rax, %rbx
testq %rax, %rax
movq 0x18(%rsp), %r12
jne 0x2d871
xorl %ebx, %ebx
movq %r14, %rdi
xorl %esi, %esi
callq 0x32ea7
movq 0x40(%r14), %rdi
callq 0x360f4
jmp 0x2dde0
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
movl $0xc, %esi
cmovnel %eax, %esi
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x32ea7
movq 0x38(%rbp), %rbx
movq (%r15), %rdi
movl 0x8(%rbp), %esi
callq 0x8943c
movq %rax, %r14
movq (%r15), %rdi
movl 0x8(%rbp), %esi
callq 0x8947e
movzwl 0x10(%rbp), %r10d
movq (%rbp), %r11
incq %r11
leaq 0x6aa76(%rip), %rsi     # 0x98801
leaq 0x69f33(%rip), %rdx     # 0x97cc5
movl $0x1, %edi
movq %rbx, %rcx
movq %r14, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x13de5
addq $0x10, %rsp
jmp 0x2ddbe
movq %r14, %rdi
movl $0xc, %esi
callq 0x32ea7
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
movl $0x5, %esi
cmovnel %eax, %esi
movq 0x10(%rsp), %rdi
callq 0x32ea7
movq $-0x1, %rbx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6aa1d(%rip), %rdi     # 0x98816
leaq 0x69f26(%rip), %rsi     # 0x97d26
leaq 0x6aa22(%rip), %rcx     # 0x98829
movl $0xeb7, %edx            # imm = 0xEB7
callq 0x42f0