// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/sam.c
// Function: sam_dispatcher_read
static void *sam_dispatcher_read(void *vp) {
    htsFile *fp = vp;
    kstring_t line = {0};
    int line_frag = 0;
    SAM_state *fd = fp->state;
    sp_lines *l = NULL;

    // Pre-allocate buffer for left-over bits of line (exact size doesn't
    // matter as it will grow if necessary).
    if (ks_resize(&line, 1000) < 0)
        goto err;

    for (;;) {
        // Check for command
        pthread_mutex_lock(&fd->command_m);
        switch (fd->command) {

        case SAM_CLOSE:
            pthread_cond_signal(&fd->command_c);
            pthread_mutex_unlock(&fd->command_m);
            hts_tpool_process_shutdown(fd->q);
            goto tidyup;

        default:
            break;
        }
        pthread_mutex_unlock(&fd->command_m);

        pthread_mutex_lock(&fd->lines_m);
        if (fd->lines) {
            // reuse existing line buffer
            l = fd->lines;
            fd->lines = l->next;
        }
        pthread_mutex_unlock(&fd->lines_m);

        if (l == NULL) {
            // none to reuse, to create a new one
            l = calloc(1, sizeof(*l));
            if (!l)
                goto err;
            l->alloc = SAM_NBYTES;
            l->data = malloc(l->alloc+8); // +8 for optimisation in sam_parse1
            if (!l->data) {
                free(l);
                l = NULL;
                goto err;
            }
            l->fd = fd;
        }
        l->next = NULL;

        if (l->alloc < line_frag+SAM_NBYTES/2) {
            char *rp = realloc(l->data, line_frag+SAM_NBYTES/2 +8);
            if (!rp)
                goto err;
            l->alloc = line_frag+SAM_NBYTES/2;
            l->data = rp;
        }
        memcpy(l->data, line.s, line_frag);

        l->data_size = line_frag;
        ssize_t nbytes;
    longer_line:
        if (fp->is_bgzf)
            nbytes = bgzf_read(fp->fp.bgzf, l->data + line_frag, l->alloc - line_frag);
        else
            nbytes = hread(fp->fp.hfile, l->data + line_frag, l->alloc - line_frag);
        if (nbytes < 0) {
            sam_state_err(fd, errno ? errno : EIO);
            goto err;
        } else if (nbytes == 0)
            break; // EOF
        l->data_size += nbytes;

        // trim to last \n. Maybe \r\n, but that's still fine
        if (nbytes == l->alloc - line_frag) {
            char *cp_end = l->data + l->data_size;
            char *cp = cp_end-1;

            while (cp > (char *)l->data && *cp != '\n')
                cp--;

            // entire buffer is part of a single line
            if (cp == l->data) {
                line_frag = l->data_size;
                char *rp = realloc(l->data, l->alloc * 2 + 8);
                if (!rp)
                    goto err;
                l->alloc *= 2;
                l->data = rp;
                assert(l->alloc >= l->data_size);
                assert(l->alloc >= line_frag);
                assert(l->alloc >= l->alloc - line_frag);
                goto longer_line;
            }
            cp++;

            // line holds the remainder of our line.
            if (ks_resize(&line, cp_end - cp) < 0)
                goto err;
            memcpy(line.s, cp, cp_end - cp);
            line_frag = cp_end - cp;
            l->data_size = l->alloc - line_frag;
        } else {
            // out of buffer
            line_frag = 0;
        }

        l->serial = fd->serial++;
        //fprintf(stderr, "Dispatching %p, %d bytes, serial %d\n", l, l->data_size, l->serial);
        if (hts_tpool_dispatch3(fd->p, fd->q, sam_parse_worker, l,
                                cleanup_sp_lines, cleanup_sp_bams, 0) < 0)
            goto err;
        pthread_mutex_lock(&fd->command_m);
        if (fd->command == SAM_CLOSE) {
            pthread_mutex_unlock(&fd->command_m);
            l = NULL;
            goto tidyup;
        }
        l = NULL;  // Now "owned" by sam_parse_worker()
        pthread_mutex_unlock(&fd->command_m);
    }

    // Submit a NULL sp_bams entry to act as an EOF marker
    if (hts_tpool_dispatch(fd->p, fd->q, sam_parse_eof, NULL) < 0)
        goto err;

    // At EOF, wait for close request.
    // (In future if we add support for seek, this is where we need to catch it.)
    for (;;) {
        pthread_mutex_lock(&fd->command_m);
        if (fd->command == SAM_NONE)
            pthread_cond_wait(&fd->command_c, &fd->command_m);
        switch (fd->command) {
        case SAM_CLOSE:
            pthread_cond_signal(&fd->command_c);
            pthread_mutex_unlock(&fd->command_m);
            hts_tpool_process_shutdown(fd->q);
            goto tidyup;

        default:
            pthread_mutex_unlock(&fd->command_m);
            break;
        }
    }

 tidyup:
    pthread_mutex_lock(&fd->command_m);
    fd->command = SAM_CLOSE_DONE;
    pthread_cond_signal(&fd->command_c);
    pthread_mutex_unlock(&fd->command_m);

    if (l) {
        pthread_mutex_lock(&fd->lines_m);
        l->next = fd->lines;
        fd->lines = l;
        pthread_mutex_unlock(&fd->lines_m);
    }
    free(line.s);

    return NULL;

 err:
    sam_state_err(fd, errno ? errno : ENOMEM);
    hts_tpool_process_shutdown(fd->q);
    goto tidyup;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rax
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq %rax, 0x38(%rsp)
movq 0x40(%rax), %r14
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x24d14
testl %eax, %eax
js 0x29825
leaq 0x78(%r14), %r12
leaq 0x18(%r14), %r13
leaq 0x40(%r14), %rax
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
movq %r14, 0x28(%rsp)
movq %r12, 0x18(%rsp)
movq %r13, 0x30(%rsp)
movq %r12, %rdi
callq 0x4750
cmpl $0x1, 0xd0(%r14)
je 0x297fc
movq %r12, %rdi
callq 0x4340
movq %r13, %rdi
callq 0x4750
movq 0x58(%r14), %rcx
testq %rcx, %rcx
je 0x294ea
movq (%rcx), %rax
movq %rax, 0x58(%r14)
movq %r13, %rdi
movq %rcx, %r15
callq 0x4340
movq %r15, %rdi
movl 0x1c(%r15), %ecx
jmp 0x29533
movq %r13, %rdi
callq 0x4340
pushq $0x1
popq %rdi
pushq $0x30
popq %rsi
callq 0x4300
testq %rax, %rax
je 0x29825
movl $0x3a980, 0x1c(%rax)    # imm = 0x3A980
movl $0x3a988, %edi          # imm = 0x3A988
movq %rax, %r15
callq 0x4680
movq %r15, %rdi
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x29820
movq %r14, 0x20(%rdi)
movl $0x3a980, %ecx          # imm = 0x3A980
andq $0x0, (%rdi)
leal 0x1d4c0(%rbx), %ebp
movq 0x10(%rdi), %rax
cmpl %ebp, %ecx
movq %rdi, 0x10(%rsp)
jge 0x29573
leal 0x1d4c8(%rbx), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
callq 0x4840
testq %rax, %rax
je 0x2982e
movq 0x10(%rsp), %r15
movl %ebp, 0x1c(%r15)
movq %rax, 0x10(%r15)
jmp 0x29576
movq %rdi, %r15
movq 0x50(%rsp), %rsi
movslq %ebx, %rdx
movq %rax, %rdi
callq 0x43c0
movl %ebx, 0x18(%r15)
movq %r15, %rdx
movslq %ebx, %r12
movq 0x10(%rdx), %rbp
addq %r12, %rbp
movslq 0x1c(%rdx), %r13
subq %r12, %r13
movq 0x38(%rsp), %rax
testb $0x10, (%rax)
movq 0x38(%rax), %rbx
jne 0x295f3
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %r14
subq %rsi, %r14
cmpq %r13, %r14
movq %r13, %r15
cmovbq %r14, %r15
movq %rbp, %rdi
movq %r15, %rdx
callq 0x43c0
addq %r15, 0x8(%rbx)
cmpq %r13, %r14
jae 0x29606
testb $0x2, 0x30(%rbx)
movq 0x28(%rsp), %r14
je 0x2960b
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0xd55e
jmp 0x29601
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0xab95
movq %rax, %r15
jmp 0x2960b
movq 0x28(%rsp), %r14
testq %r15, %r15
js 0x29770
je 0x2978c
movq 0x10(%rsp), %rcx
movl 0x18(%rcx), %r13d
leal (%r15,%r13), %ebx
movl %ebx, 0x18(%rcx)
movslq 0x1c(%rcx), %rax
movq %rax, %rdx
subq %r12, %rdx
cmpq %rdx, %r15
jne 0x296b2
movq 0x10(%rcx), %rbp
movslq %ebx, %rcx
leaq (%rcx,%rbp), %rdx
xorl %esi, %esi
movq %rsi, %r12
leaq (%rdx,%rsi), %rdi
decq %rdi
cmpq %rbp, %rdi
jbe 0x2965f
leaq -0x1(%r12), %rsi
cmpb $0xa, (%rdi)
jne 0x29646
pushq $0x1
popq %rdx
subq %rcx, %rdx
cmpq %r12, %rdx
jne 0x296b6
leaq 0x8(,%rax,2), %rsi
movq %rbp, %rdi
callq 0x4840
testq %rax, %rax
je 0x2982e
movq 0x10(%rsp), %rdx
movl 0x1c(%rdx), %ecx
addl %ecx, %ecx
movl %ecx, 0x1c(%rdx)
movq %rax, 0x10(%rdx)
cmpl 0x18(%rdx), %ecx
jl 0x298c0
cmpl %ebx, %ecx
jl 0x298fe
testl %ebx, %ebx
jns 0x2958d
jmp 0x298df
xorl %ebx, %ebx
jmp 0x296ff
movq %r12, %rbx
negq %rbx
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x24d14
testl %eax, %eax
js 0x2982e
addl %r15d, %r13d
movslq %r13d, %rax
addq %rax, %rbp
addq %r12, %rbp
movq 0x50(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x43c0
movl %r12d, %ebx
negl %ebx
movq 0x10(%rsp), %rcx
addl 0x1c(%rcx), %r12d
movl %r12d, 0x18(%rcx)
movq 0x18(%rsp), %r12
movl 0x74(%r14), %eax
leal 0x1(%rax), %edx
movl %edx, 0x74(%r14)
movl %eax, 0x8(%rcx)
movq 0x8(%r14), %rdi
movq 0x40(%r14), %rsi
andl $0x0, (%rsp)
leaq 0x1f8(%rip), %rdx       # 0x2991d
leaq -0x3f64(%rip), %r8      # 0x257c8
leaq -0x3f70(%rip), %r9      # 0x257c3
callq 0x2c8b1
testl %eax, %eax
movl $0x0, %r15d
movq 0x30(%rsp), %r13
js 0x2982e
movq %r12, %rdi
callq 0x4750
movl 0xd0(%r14), %ebp
movq %r12, %rdi
callq 0x4340
cmpl $0x1, %ebp
jne 0x294a0
jmp 0x29856
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
pushq $0x5
popq %rsi
cmovnel %eax, %esi
movq %r14, %rdi
callq 0x29211
jmp 0x2982e
movq 0x8(%r14), %rdi
movq 0x40(%r14), %rsi
leaq 0x3c0(%rip), %rdx       # 0x29b5b
xorl %ecx, %ecx
callq 0x2c89f
testl %eax, %eax
js 0x2982e
leaq 0xa0(%r14), %r15
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movl 0xd0(%r14), %eax
testl %eax, %eax
jne 0x297db
movq %r15, %rdi
movq %rbx, %rsi
callq 0x47c0
movl 0xd0(%r14), %eax
cmpl $0x1, %eax
je 0x297ea
movq %rbx, %rdi
callq 0x4340
jmp 0x297b6
movq %r15, %rdi
callq 0x4040
movq %rbx, %rdi
callq 0x4340
jmp 0x29819
leaq 0xa0(%r14), %rdi
callq 0x4040
movq %r12, %rdi
callq 0x4340
movq $0x0, 0x10(%rsp)
movq 0x20(%rsp), %rax
jmp 0x29849
callq 0x45c0
movq $0x0, 0x10(%rsp)
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
pushq $0xc
popq %rsi
cmovnel %eax, %esi
movq %r14, %rdi
callq 0x29211
leaq 0x40(%r14), %rax
movq (%rax), %rdi
callq 0x2bf17
movq 0x10(%rsp), %r15
leaq 0x78(%r14), %rbx
movq %rbx, %rdi
callq 0x4750
movl $0x2, 0xd0(%r14)
leaq 0xa0(%r14), %rdi
callq 0x4040
movq %rbx, %rdi
callq 0x4340
testq %r15, %r15
je 0x298a5
leaq 0x18(%r14), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x58(%r14), %rax
movq %rax, (%r15)
movq %r15, 0x58(%r14)
movq %rbx, %rdi
callq 0x4340
movq 0x50(%rsp), %rdi
callq 0x45c0
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x50db4(%rip), %rdi     # 0x7a67b
leaq 0x503b3(%rip), %rsi     # 0x79c81
leaq 0x50dbf(%rip), %rcx     # 0x7a694
movl $0xe2f, %edx            # imm = 0xE2F
callq 0x42f0
leaq 0x50de6(%rip), %rdi     # 0x7a6cc
leaq 0x50394(%rip), %rsi     # 0x79c81
leaq 0x50da0(%rip), %rcx     # 0x7a694
movl $0xe31, %edx            # imm = 0xE31
callq 0x42f0
leaq 0x50db1(%rip), %rdi     # 0x7a6b6
leaq 0x50375(%rip), %rsi     # 0x79c81
leaq 0x50d81(%rip), %rcx     # 0x7a694
movl $0xe30, %edx            # imm = 0xE30
callq 0x42f0