// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdi, %r12
movq 0x40(%rdi), %rax
movq %rax, 0x18(%rsp)
addq $0x18, %rax
movq %rax, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
movq 0x18(%rsp), %rax
movq 0x40(%rax), %rdi
callq 0x37249
testq %rax, %rax
je 0x2e9fb
movq %rax, %rbx
movq %rax, %rdi
callq 0x37576
testq %rax, %rax
je 0x2e732
movq %rax, %r9
cmpq $0x0, 0x68(%r12)
movq %rax, 0x8(%rsp)
movq %rbx, 0x38(%rsp)
je 0x2e74b
movq 0x28(%r9), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x10(%rsp)
xorl %r14d, %r14d
movl 0x18(%r9), %eax
cmpl %eax, %r14d
jge 0x2e7de
movl %r14d, %r13d
movq 0x10(%r9), %rcx
movslq %r14d, %rsi
movb $0x1, %dil
cmpb $0xa, (%rcx,%rsi)
movl %r14d, %edx
je 0x2e4b8
movslq %eax, %rdi
leaq -0x1(%rdi), %r8
cmpq %rsi, %r8
je 0x2e4b4
leaq 0x1(%rsi), %rdx
cmpb $0xa, 0x1(%rcx,%rsi)
movq %rdx, %rsi
jne 0x2e498
cmpq %rdi, %rdx
setl %dil
jmp 0x2e4b8
xorl %edi, %edi
movl %eax, %edx
movzbl %dil, %r14d
addl %edx, %r14d
testb $0x10, (%r12)
jne 0x2e54c
movq 0x38(%r12), %rbx
movslq %r13d, %r15
movl %r14d, %eax
subl %r13d, %eax
movslq %eax, %rbp
addq 0x10(%r9), %r15
testb $0x2, 0x30(%rbx)
jne 0x2e50b
movq 0x18(%rbx), %rsi
movq %rsi, %rax
subq 0x8(%rbx), %rax
cmpq %rbp, %rax
jae 0x2e50b
addq %rbp, %rsi
subq (%rbx), %rsi
movq %rbx, %rdi
callq 0xefdd
movq 0x18(%rbx), %rax
movq %rax, 0x10(%rbx)
movq 0x8(%rbx), %rdi
movq 0x18(%rbx), %r12
subq %rdi, %r12
cmpq %rbp, %r12
ja 0x2e520
cmpq (%rbx), %rdi
je 0x2e592
cmpq %rbp, %r12
movq %rbp, %r13
cmovbq %r12, %r13
movq %r15, %rsi
movq %r13, %rdx
callq 0x43c0
addq %r13, 0x8(%rbx)
cmpq %rbp, %r12
jae 0x2e5a5
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %rcx
jmp 0x2e59d
movq 0x38(%r12), %rdi
movl %r14d, %eax
subl %r13d, %eax
movslq %eax, %rbx
movq %rbx, %rsi
callq 0xd091
movl $0x4, %ebp
testl %eax, %eax
js 0x2e63d
movq 0x38(%r12), %rdi
movslq %r13d, %rsi
movq 0x8(%rsp), %rax
addq 0x10(%rax), %rsi
movq %rbx, %rdx
callq 0xd0bb
cmpq %rbx, %rax
je 0x2e5b3
jmp 0x2e63d
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0xf4c6
movq %rax, %r13
movq 0x20(%rsp), %r12
cmpq %rbp, %r13
jne 0x2e638
movl 0x10(%rsp), %ecx
movslq %ecx, %rax
incl %ecx
movl %ecx, 0x10(%rsp)
leaq (%rax,%rax,4), %r13
shlq $0x4, %r13
movq 0x30(%rsp), %rax
addq 0x10(%rax), %r13
cmpl $0x2, 0x54(%r12)
jne 0x2e645
movq 0x38(%r12), %rbx
movq 0x68(%r12), %r15
movl 0x8(%r13), %ebp
movq (%r13), %rax
movq %rax, 0x40(%rsp)
movq %r13, %rdi
callq 0x23b0a
movq 0x18(%rbx), %rcx
shlq $0x10, %rcx
movzwl 0x10(%rbx), %r9d
orq %rcx, %r9
movzwl 0x10(%r13), %ecx
xorl %r10d, %r10d
testb $0x4, %cl
sete %r10b
subq $0x8, %rsp
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %edx
movq 0x48(%rsp), %rcx
movq %rax, %r8
pushq %r10
callq 0xa354
addq $0x10, %rsp
jmp 0x2e68e
movl $0x4, %ebp
xorl %r13d, %r13d
jmp 0x2e71f
movq 0x68(%r12), %rbx
movl 0x8(%r13), %ebp
movq (%r13), %r15
movq %r13, %rdi
callq 0x23b0a
movq 0x38(%r12), %rcx
movq 0x18(%rcx), %rdx
shlq $0x10, %rdx
movzwl 0x10(%rcx), %r8d
orq %rdx, %r8
movzwl 0x10(%r13), %ecx
xorl %r9d, %r9d
testb $0x4, %cl
sete %r9b
movq %rbx, %rdi
movl %ebp, %esi
movq %r15, %rdx
movq %rax, %rcx
callq 0x16eed
testl %eax, %eax
js 0x2e69c
xorl %ebp, %ebp
movb $0x1, %r13b
jmp 0x2e71f
callq 0x4050
movl (%rax), %esi
testl %esi, %esi
movl $0xc, %eax
cmovel %eax, %esi
movq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x340e0
movq 0x38(%r13), %rbx
movq (%r12), %rdi
movl 0x8(%r13), %esi
callq 0x8bd81
movq %rax, %r15
movq (%r12), %rdi
movq 0x20(%rsp), %r12
movl 0x8(%r13), %esi
callq 0x8bdc3
movzwl 0x10(%r13), %r10d
movq (%r13), %r11
incq %r11
xorl %r13d, %r13d
movl $0x1, %edi
leaq 0x6d0ff(%rip), %rsi     # 0x9b7fa
leaq 0x6c5b0(%rip), %rdx     # 0x9acb2
movq %rbx, %rcx
movq %r15, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x14378
addq $0x10, %rsp
movl $0x4, %ebp
movq 0x8(%rsp), %r9
testb %r13b, %r13b
jne 0x2e46e
jmp 0x2e82c
movq 0x18(%rsp), %rdi
movl $0xc, %esi
callq 0x340e0
movl $0x4, %ebp
jmp 0x2e866
testb $0x10, (%r12)
jne 0x2e873
movq 0x38(%r12), %r13
movq 0x10(%r9), %rbp
movslq 0x18(%r9), %r15
testb $0x2, 0x30(%r13)
jne 0x2e791
movq 0x18(%r13), %rsi
movq %rsi, %rax
subq 0x8(%r13), %rax
cmpq %r15, %rax
jae 0x2e791
addq %r15, %rsi
subq (%r13), %rsi
movq %r13, %rdi
callq 0xefdd
movq 0x18(%r13), %rax
movq %rax, 0x10(%r13)
movq 0x8(%r13), %rdi
movq 0x18(%r13), %r14
subq %rdi, %r14
cmpq %r15, %r14
ja 0x2e7ab
cmpq (%r13), %rdi
je 0x2e9a3
cmpq %r15, %r14
movq %r15, %rbx
cmovbq %r14, %rbx
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x43c0
addq %rbx, 0x8(%r13)
cmpq %r15, %r14
jae 0x2e9b6
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %rbx, %rcx
jmp 0x2e9ae
movq 0x30(%rsp), %r14
movl 0x10(%rsp), %eax
cmpl 0x18(%r14), %eax
jne 0x2ea29
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x18(%rsp), %rcx
movq 0x60(%rcx), %rax
movq %rax, (%r14)
movq 0x8(%rsp), %r14
movq 0x28(%r14), %rax
movq %rax, 0x60(%rcx)
movq $0x0, 0x28(%r14)
movq %rbx, %rdi
callq 0x4340
movq %r14, %r9
xorl %ebp, %ebp
testl %ebp, %ebp
jne 0x2e866
xorl %ebp, %ebp
movq 0x38(%rsp), %rdi
xorl %esi, %esi
movq %r9, %r14
callq 0x37551
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x18(%rsp), %rcx
movq 0x58(%rcx), %rax
movq %rax, (%r14)
movq %r14, 0x58(%rcx)
movq %rbx, %rdi
callq 0x4340
testl %ebp, %ebp
je 0x2e416
jmp 0x2e9d2
movq 0x38(%r12), %rdi
xorl %r14d, %r14d
movq %rdi, 0x10(%rsp)
movl 0x18(%r9), %ebp
subl %r14d, %ebp
jle 0x2e82a
movl $0xff00, %r13d          # imm = 0xFF00
subl 0x10(%rdi), %r13d
cmpl %ebp, %r13d
movl %ebp, %ecx
cmovll %r13d, %ecx
leal (%rcx,%r14), %ebx
movslq %ebx, %rax
movslq %r14d, %rsi
addl %r14d, %ecx
decl %ecx
cmpl %ecx, %r14d
cmovll %r14d, %ecx
decq %rax
movl %ebx, %r15d
cmpq %rsi, %rax
jle 0x2e8d4
movq 0x10(%r9), %rdx
decl %r15d
cmpb $0xa, (%rdx,%rax)
leaq -0x1(%rax), %rax
jne 0x2e8b8
leal 0x1(%r15), %eax
jmp 0x2e8da
movl %r15d, %eax
movl %ecx, %r15d
cmpl %r14d, %r15d
je 0x2e913
addq 0x10(%r9), %rsi
movl %eax, %r15d
subl %r14d, %eax
movl %ebx, %r12d
movslq %eax, %rbx
movq 0x10(%rsp), %rdi
movq %rbx, %rdx
callq 0xd0bb
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %r9
cmpq %rbx, %rax
movl %r12d, %ebx
movq 0x20(%rsp), %r12
jne 0x2e989
cmpl %ebp, %r13d
setle %cl
movl 0x10(%rdi), %eax
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x2e952
cltq
movq 0x28(%rdi), %rcx
cmpb $0xa, -0x1(%rcx,%rax)
jne 0x2e952
movl $0xff00, %esi           # imm = 0xFF00
movq 0x10(%rsp), %rdi
callq 0xd091
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %r9
testl %eax, %eax
js 0x2e989
movl %ebx, %eax
subl %r15d, %eax
jle 0x2e983
movslq %r15d, %rsi
addq 0x10(%r9), %rsi
movl %ebx, %ebp
movslq %eax, %rbx
movq 0x10(%rsp), %rdi
movq %rbx, %rdx
callq 0xd0bb
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %r9
cmpq %rbx, %rax
movl %ebp, %ebx
jne 0x2e989
xorl %ebp, %ebp
movb $0x1, %al
jmp 0x2e993
movl $0x4, %ebp
xorl %eax, %eax
movl %r14d, %ebx
movl %ebx, %r14d
testb %al, %al
jne 0x2e880
jmp 0x2e82c
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xf4c6
movq %rax, %rbx
movq 0x8(%rsp), %r9
movslq 0x18(%r9), %rax
movl $0x4, %ebp
cmpq %rax, %rbx
je 0x2e830
jmp 0x2e866
cmpl $0x4, %ebp
jne 0x2ea17
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
movl $0x5, %esi
cmovnel %eax, %esi
movq 0x18(%rsp), %rdi
callq 0x340e0
movq $-0x1, %rbx
jmp 0x2ea17
xorl %ebx, %ebx
movq 0x18(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x340e0
movq 0x40(%r14), %rdi
callq 0x374c6
jmp 0x2ea17
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6cddf(%rip), %rdi     # 0x9b80f
leaq 0x6c2dc(%rip), %rsi     # 0x9ad13
leaq 0x6cde4(%rip), %rcx     # 0x9b822
movl $0xeb7, %edx            # imm = 0xEB7
callq 0x42f0