// File: /workspace/github_c_makefile_star2_O3/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), %r12
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x4680
testq %rax, %rax
je 0x33152
movq %rax, %r14
leaq 0x78(%r12), %rbx
movq %rbx, %rdi
callq 0x4750
cmpl $0x1, 0xd0(%r12)
jne 0x33163
movq %r14, 0x10(%rsp)
leaq 0xa0(%r12), %rdi
callq 0x4040
movq %rbx, %rdi
callq 0x4340
xorl %r13d, %r13d
jmp 0x3353d
movq $0x0, 0x10(%rsp)
xorl %r13d, %r13d
jmp 0x33524
leaq 0x18(%r12), %rbp
movl $0x5dc, %eax            # imm = 0x5DC
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
movq %r12, 0x18(%rsp)
movq %rbx, 0x28(%rsp)
movq %rbp, 0x38(%rsp)
movq %rbx, %rdi
callq 0x4340
movq %rbp, %rdi
callq 0x4750
movq 0x58(%r12), %r13
testq %r13, %r13
movq %r14, 0x10(%rsp)
je 0x331ba
movq (%r13), %rax
movq %rax, 0x58(%r12)
movq %rbp, %rdi
callq 0x4340
movl 0x1c(%r13), %ecx
jmp 0x33200
movq %rbp, %rdi
callq 0x4340
movl $0x1, %edi
movl $0x30, %esi
callq 0x4300
testq %rax, %rax
je 0x3315b
movq %rax, %r13
movl $0x3a980, 0x1c(%rax)    # imm = 0x3A980
movl $0x3a988, %edi          # imm = 0x3A988
callq 0x4680
movq %rax, 0x10(%r13)
testq %rax, %rax
je 0x335e4
movq %r12, 0x20(%r13)
movl $0x3a980, %ecx          # imm = 0x3A980
movq $0x0, (%r13)
leal 0x1d4c0(%r15), %ebx
movq 0x10(%r13), %rax
cmpl %ebx, %ecx
jge 0x3323a
leal 0x1d4c8(%r15), %ecx
movslq %ecx, %rsi
movq %rax, %rdi
callq 0x4840
testq %rax, %rax
je 0x33524
movl %ebx, 0x1c(%r13)
movq %rax, 0x10(%r13)
movslq %r15d, %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x43c0
movl %r15d, 0x18(%r13)
movq %r13, 0x20(%rsp)
movslq %r15d, %rbx
movq 0x10(%r13), %r14
addq %rbx, %r14
movslq 0x1c(%r13), %rbp
subq %rbx, %rbp
movq 0x40(%rsp), %rax
testb $0x10, (%rax)
movq 0x38(%rax), %r13
jne 0x332b3
movq 0x8(%r13), %rsi
movq 0x10(%r13), %r15
subq %rsi, %r15
cmpq %rbp, %r15
movq %rbp, %r12
cmovbq %r15, %r12
movq %r14, %rdi
movq %r12, %rdx
callq 0x43c0
addq %r12, 0x8(%r13)
cmpq %rbp, %r15
jae 0x332c4
testb $0x2, 0x30(%r13)
je 0x332c4
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0xf129
jmp 0x332c1
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0xbefc
movq %rax, %r12
testq %r12, %r12
movq 0x20(%rsp), %r13
js 0x33492
je 0x334b2
movl 0x18(%r13), %ebp
leal (%r12,%rbp), %r15d
movl %r15d, 0x18(%r13)
movslq 0x1c(%r13), %rax
movq %rax, %rcx
subq %rbx, %rcx
cmpq %rcx, %r12
jne 0x33372
movq 0x10(%r13), %r14
movslq %r15d, %rcx
leaq (%r14,%rcx), %rdx
xorl %esi, %esi
movq %rsi, %r13
leaq (%rdx,%rsi), %rdi
decq %rdi
cmpq %r14, %rdi
jbe 0x33318
leaq -0x1(%r13), %rsi
cmpb $0xa, (%rdi)
jne 0x33300
movl $0x1, %edx
subq %rcx, %rdx
cmpq %r13, %rdx
jne 0x3337c
leaq 0x8(,%rax,2), %rsi
movq %r14, %rdi
callq 0x4840
testq %rax, %rax
je 0x3351a
movq 0x20(%rsp), %r13
movl 0x1c(%r13), %ecx
addl %ecx, %ecx
movl %ecx, 0x1c(%r13)
movq %rax, 0x10(%r13)
cmpl 0x18(%r13), %ecx
jl 0x3362f
cmpl %r15d, %ecx
jl 0x33610
testl %r15d, %r15d
jns 0x33251
jmp 0x335f1
xorl %r15d, %r15d
movq 0x10(%rsp), %r14
jmp 0x333f9
movq %r13, %r15
negq %r15
cmpq %r15, 0x30(%rsp)
jae 0x333c1
movq %r15, %rax
shrq $0x3e, %rax
movq %r15, %rcx
shrq %rcx
testq %rax, %rax
movl $0x0, %esi
cmoveq %rcx, %rsi
subq %r13, %rsi
movq 0x10(%rsp), %rdi
movq %rsi, 0x30(%rsp)
callq 0x4840
movq %rax, %rbx
testq %rax, %rax
jne 0x333c6
jmp 0x3351a
movq 0x10(%rsp), %rbx
addl %r12d, %ebp
movslq %ebp, %rax
addq %rax, %r14
addq %r13, %r14
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x43c0
movl %r13d, %r15d
negl %r15d
movq 0x20(%rsp), %rax
addl 0x1c(%rax), %r13d
movl %r13d, 0x18(%rax)
movq %rax, %r13
movq %rbx, %r14
movq 0x28(%rsp), %rbx
movq 0x18(%rsp), %r12
movl 0x74(%r12), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x74(%r12)
movl %eax, 0x8(%r13)
movq 0x8(%r12), %rdi
movq 0x40(%r12), %rsi
movl $0x0, (%rsp)
leaq 0x222(%rip), %rdx       # 0x3364e
movq %r13, %rcx
leaq -0x5620(%rip), %r8      # 0x2de16
leaq -0x562c(%rip), %r9      # 0x2de11
callq 0x36b0d
testl %eax, %eax
movq 0x38(%rsp), %rbp
js 0x335cd
movq %rbx, %rdi
callq 0x4750
movq %rbx, %r13
movl 0xd0(%r12), %ebx
movq %r13, %rdi
callq 0x4340
cmpl $0x1, %ebx
je 0x335d7
movq %r13, %rdi
callq 0x4750
cmpl $0x1, 0xd0(%r12)
movq %r13, %rbx
jne 0x33184
jmp 0x33130
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
movl $0x5, %esi
cmovnel %eax, %esi
movq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x32ea7
jmp 0x33524
movq 0x18(%rsp), %r12
movq 0x8(%r12), %rdi
movq 0x40(%r12), %rsi
leaq 0x3ec(%rip), %rdx       # 0x338b4
xorl %ecx, %ecx
callq 0x36af8
testl %eax, %eax
js 0x33524
leaq 0xa0(%r12), %r14
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movl 0xd0(%r12), %eax
testl %eax, %eax
jne 0x33507
movq %r14, %rdi
movq %rbx, %rsi
callq 0x47c0
movl 0xd0(%r12), %eax
cmpl $0x1, %eax
je 0x335b8
movq %rbx, %rdi
callq 0x4340
jmp 0x334e0
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %r13
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
movl $0xc, %esi
cmovnel %eax, %esi
movq %r12, %rdi
callq 0x32ea7
movq 0x40(%r12), %rdi
callq 0x360f4
leaq 0x78(%r12), %r14
movq %r14, %rdi
callq 0x4750
movl $0x2, 0xd0(%r12)
leaq 0xa0(%r12), %rdi
callq 0x4040
movq %r14, %rdi
callq 0x4340
testq %r13, %r13
je 0x3359d
leaq 0x18(%r12), %r14
movq %r14, %rdi
callq 0x4750
movq 0x58(%r12), %rax
movq %rax, (%r13)
movq %r13, 0x58(%r12)
movq %r14, %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
movq %r14, %rdi
callq 0x4040
movq %rbx, %rdi
callq 0x4340
jmp 0x3353d
movq %r14, 0x10(%rsp)
jmp 0x33524
movq %r14, 0x10(%rsp)
xorl %r13d, %r13d
jmp 0x33547
movq %r13, %rdi
callq 0x45c0
jmp 0x3315b
leaq 0x65179(%rip), %rdi     # 0x98771
leaq 0x64727(%rip), %rsi     # 0x97d26
leaq 0x65133(%rip), %rcx     # 0x98739
movl $0xe31, %edx            # imm = 0xE31
callq 0x42f0
leaq 0x65144(%rip), %rdi     # 0x9875b
leaq 0x64708(%rip), %rsi     # 0x97d26
leaq 0x65114(%rip), %rcx     # 0x98739
movl $0xe30, %edx            # imm = 0xE30
callq 0x42f0
leaq 0x650ea(%rip), %rdi     # 0x98720
leaq 0x646e9(%rip), %rsi     # 0x97d26
leaq 0x650f5(%rip), %rcx     # 0x98739
movl $0xe2f, %edx            # imm = 0xE2F
callq 0x42f0