// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq 0x40(%rdi), %r15
movl $0x5dc, %eax            # imm = 0x5DC
movq %rax, 0x38(%rsp)
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x4680
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x347da
leaq 0x78(%r15), %rcx
leaq 0x18(%r15), %rax
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
xorl %r12d, %r12d
movq %r15, 0x30(%rsp)
movq %rcx, 0x8(%rsp)
movq %rcx, %rbx
movq %rcx, %rdi
callq 0x4750
cmpl $0x1, 0xd0(%r15)
je 0x347a5
movq %rbx, %rdi
callq 0x4340
movq 0x28(%rsp), %rdi
callq 0x4750
movq 0x58(%r15), %rax
testq %rax, %rax
je 0x343d8
movq (%rax), %rcx
movq %rcx, 0x58(%r15)
movq %rax, %r14
movq 0x28(%rsp), %rdi
callq 0x4340
testq %r14, %r14
jne 0x34424
movl $0x1, %edi
movl $0x30, %esi
callq 0x4300
testq %rax, %rax
je 0x347da
movq %rax, %r14
movl $0x3a980, 0x1c(%rax)    # imm = 0x3A980
movl $0x3a988, %edi          # imm = 0x3A988
callq 0x4680
movq %rax, 0x10(%r14)
testq %rax, %rax
je 0x347d2
movq %r15, 0x20(%r14)
movq $0x0, (%r14)
leal 0x1d4c0(%r12), %ebx
cmpl %ebx, 0x1c(%r14)
jge 0x3445e
movq 0x10(%r14), %rdi
leal 0x1d4c8(%r12), %eax
movslq %eax, %rsi
callq 0x4840
testq %rax, %rax
je 0x347dd
movl %ebx, 0x1c(%r14)
movq %rax, 0x10(%r14)
movq 0x10(%r14), %rdi
movslq %r12d, %rdx
movq 0x10(%rsp), %rsi
callq 0x43c0
movl %r12d, 0x18(%r14)
movq %r14, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movslq %r12d, %r13
movq 0x10(%r14), %rbp
addq %r13, %rbp
movslq 0x1c(%r14), %r14
subq %r13, %r14
movq 0x40(%rsp), %rax
testb $0x10, (%rax)
jne 0x344e3
movq 0x38(%rax), %rbx
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %r15
subq %rsi, %r15
cmpq %r14, %r15
movq %r14, %r12
cmovbq %r15, %r12
movq %rbp, %rdi
movq %r12, %rdx
callq 0x43c0
addq %r12, 0x8(%rbx)
cmpq %r14, %r15
jae 0x344f7
testb $0x2, 0x30(%rbx)
movq 0x30(%rsp), %r15
je 0x344fc
movq %rbx, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0xf2cd
jmp 0x344f2
movq 0x38(%rax), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0xbf5d
movq %rax, %r12
jmp 0x344fc
movq 0x30(%rsp), %r15
testq %r12, %r12
movq 0x18(%rsp), %r14
js 0x34665
je 0x34685
movl 0x18(%r14), %ebx
addl %r12d, %ebx
movl %ebx, 0x18(%r14)
movslq 0x1c(%r14), %rax
movq %rax, %rcx
subq %r13, %rcx
cmpq %rcx, %r12
jne 0x34699
movq 0x10(%r14), %rdi
movslq %ebx, %rcx
leaq (%rdi,%rcx), %rbp
xorl %edx, %edx
movq %rdx, %r14
leaq (%rdx,%rbp), %rsi
decq %rsi
cmpq %rdi, %rsi
jbe 0x34553
leaq -0x1(%r14), %rdx
cmpb $0xa, (%rsi)
jne 0x3453b
movl $0x1, %edx
subq %rcx, %rdx
cmpq %r14, %rdx
je 0x345ae
movq %r14, %r12
negq %r12
cmpq %r12, 0x38(%rsp)
jae 0x345f6
movq %r12, %rax
shrq $0x3e, %rax
movq %r12, %rcx
shrq %rcx
testq %rax, %rax
movl $0x0, %ebx
cmoveq %rcx, %rbx
subq %r14, %rbx
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x4840
testq %rax, %rax
je 0x3460e
xorl %ecx, %ecx
movq %rbx, 0x38(%rsp)
movq %rax, %rdi
jmp 0x34613
leaq 0x8(,%rax,2), %rsi
callq 0x4840
testq %rax, %rax
je 0x345ff
movq 0x18(%rsp), %r14
movl 0x1c(%r14), %ecx
addl %ecx, %ecx
movl %ecx, 0x1c(%r14)
movq %rax, 0x10(%r14)
cmpl 0x18(%r14), %ecx
jl 0x348aa
cmpl %ebx, %ecx
jl 0x3488b
movl $0x7, %eax
movl %ebx, %r12d
testl %ebx, %ebx
jns 0x34653
jmp 0x3486c
xorl %ecx, %ecx
movq 0x10(%rsp), %rdi
jmp 0x34613
movl $0x2, %eax
movl %ebx, %r12d
movq 0x18(%rsp), %r14
jmp 0x34653
movb $0x1, %cl
movq %r13, %rdi
movl $0x2, %eax
testb %cl, %cl
movq %rdi, 0x10(%rsp)
je 0x3462d
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %r12
jmp 0x34653
addq %r14, %rbp
movq %rbp, %rsi
movq %r12, %rdx
callq 0x43c0
movl %r14d, %r12d
negl %r12d
movq 0x18(%rsp), %rax
addl 0x1c(%rax), %r14d
movl %r14d, 0x18(%rax)
movq %rax, %r14
xorl %eax, %eax
cmpl $0x7, %eax
je 0x34478
testl %eax, %eax
je 0x3469c
jmp 0x34714
callq 0x4050
movl (%rax), %esi
testl %esi, %esi
movl $0x5, %eax
cmovel %eax, %esi
movq %r15, %rdi
callq 0x340e0
movl $0x2, %eax
jmp 0x3468a
movl $0x3, %eax
movq 0x8(%rsp), %rcx
movq 0x20(%rsp), %r12
jmp 0x34719
xorl %r12d, %r12d
movl 0x74(%r15), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x74(%r15)
movl %eax, 0x8(%r14)
movq 0x8(%r15), %rdi
movq 0x40(%r15), %rsi
movl $0x0, (%rsp)
leaq 0x208(%rip), %rdx       # 0x348c9
movq %r14, %rcx
leaq -0x5c7e(%rip), %r8      # 0x2ea4d
leaq -0x5c8a(%rip), %r9      # 0x2ea48
callq 0x37f45
testl %eax, %eax
js 0x3470f
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x4750
movl 0xd0(%r15), %ebx
movq %r14, %rdi
callq 0x4340
movq %r14, %rcx
cmpl $0x1, %ebx
movl $0x0, %eax
movl $0x6, %edx
cmovel %edx, %eax
xorl %r14d, %r14d
jmp 0x34719
movl $0x2, %eax
movq 0x8(%rsp), %rcx
testl %eax, %eax
je 0x3439a
cmpl $0x2, %eax
je 0x347dd
cmpl $0x6, %eax
je 0x34802
cmpl $0x3, %eax
jne 0x3485b
movq 0x8(%r15), %rdi
movq 0x40(%r15), %rsi
leaq 0x407(%rip), %rdx       # 0x34b52
xorl %ecx, %ecx
callq 0x37f30
testl %eax, %eax
js 0x347dd
movq %r14, %r12
leaq 0x40(%r15), %rbx
leaq 0xa0(%r15), %r14
movq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x4750
cmpl $0x0, 0xd0(%r15)
jne 0x3478c
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x47c0
cmpl $0x1, 0xd0(%r15)
je 0x347bb
movq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x4340
jmp 0x3476d
leaq 0xa0(%r15), %rdi
callq 0x4040
movq %rbx, %rdi
callq 0x4340
jmp 0x347f6
movq %r14, %rdi
callq 0x4040
movq 0x8(%rsp), %rdi
callq 0x4340
movq %r12, %r14
jmp 0x347fa
movq %r14, %rdi
callq 0x45c0
xorl %r14d, %r14d
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
movl $0xc, %esi
cmovnel %eax, %esi
movq %r15, %rdi
callq 0x340e0
leaq 0x40(%r15), %rbx
movq (%rbx), %rdi
callq 0x374c6
leaq 0x78(%r15), %rbx
movq %rbx, %rdi
callq 0x4750
movl $0x2, 0xd0(%r15)
leaq 0xa0(%r15), %rdi
callq 0x4040
movq %rbx, %rdi
callq 0x4340
testq %r14, %r14
je 0x34851
leaq 0x18(%r15), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x58(%r15), %rax
movq %rax, (%r14)
movq %r14, 0x58(%r15)
movq %rbx, %rdi
callq 0x4340
movq 0x10(%rsp), %rdi
callq 0x45c0
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x66ef7(%rip), %rdi     # 0x9b76a
leaq 0x66499(%rip), %rsi     # 0x9ad13
leaq 0x66eb1(%rip), %rcx     # 0x9b732
movl $0xe31, %edx            # imm = 0xE31
callq 0x42f0
leaq 0x66ec2(%rip), %rdi     # 0x9b754
leaq 0x6647a(%rip), %rsi     # 0x9ad13
leaq 0x66e92(%rip), %rcx     # 0x9b732
movl $0xe30, %edx            # imm = 0xE30
callq 0x42f0
leaq 0x66e68(%rip), %rdi     # 0x9b719
leaq 0x6645b(%rip), %rsi     # 0x9ad13
leaq 0x66e73(%rip), %rcx     # 0x9b732
movl $0xe2f, %edx            # imm = 0xE2F
callq 0x42f0