// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4250
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
leaq -0x28(%rbp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x3f730
cmpl $0x0, %eax
jge 0x46f2a
jmp 0x47546
jmp 0x46f2c
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
callq 0x4730
movq -0x38(%rbp), %rax
movl 0xd0(%rax), %eax
subl $0x1, %eax
jne 0x46f79
jmp 0x46f4a
movq -0x38(%rbp), %rdi
addq $0xa0, %rdi
callq 0x4040
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
callq 0x4330
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4d330
jmp 0x474be
jmp 0x46f7b
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
callq 0x4330
movq -0x38(%rbp), %rdi
addq $0x18, %rdi
callq 0x4730
movq -0x38(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x46fbb
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x38(%rbp), %rdi
addq $0x18, %rdi
callq 0x4330
cmpq $0x0, -0x40(%rbp)
jne 0x47043
movl $0x1, %edi
movl $0x30, %esi
callq 0x42f0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x46fee
jmp 0x47546
movq -0x40(%rbp), %rax
movl $0x3a980, 0x1c(%rax)    # imm = 0x3A980
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %eax
addl $0x8, %eax
movslq %eax, %rdi
callq 0x4650
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x47037
movq -0x40(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x40(%rbp)
jmp 0x47546
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x40(%rbp), %rax
movq $0x0, (%rax)
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %eax
movl -0x2c(%rbp), %ecx
addl $0x1d4c0, %ecx          # imm = 0x1D4C0
cmpl %ecx, %eax
jge 0x470a9
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x2c(%rbp), %eax
addl $0x1d4c0, %eax          # imm = 0x1D4C0
addl $0x8, %eax
movslq %eax, %rsi
callq 0x4820
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x4708d
jmp 0x47546
movl -0x2c(%rbp), %ecx
addl $0x1d4c0, %ecx          # imm = 0x1D4C0
movq -0x40(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rsi
movslq -0x2c(%rbp), %rdx
callq 0x43b0
movl -0x2c(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x47108
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x2c(%rbp), %rax
addq %rax, %rsi
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %eax
subl -0x2c(%rbp), %eax
movslq %eax, %rdx
callq 0xe620
movq %rax, -0x50(%rbp)
jmp 0x47135
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x2c(%rbp), %rax
addq %rax, %rsi
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %eax
subl -0x2c(%rbp), %eax
movslq %eax, %rdx
callq 0x475a0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jge 0x47175
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
callq 0x4050
cmpl $0x0, (%rax)
je 0x4715a
callq 0x4050
movl (%rax), %eax
movl %eax, -0x74(%rbp)
jmp 0x47164
movl $0x5, %eax
movl %eax, -0x74(%rbp)
jmp 0x47164
movq -0x70(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0x46a20
jmp 0x47546
cmpq $0x0, -0x50(%rbp)
jne 0x47181
jmp 0x47414
jmp 0x47183
movq -0x50(%rbp), %rdx
movq -0x40(%rbp), %rax
movslq 0x18(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x18(%rax)
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
movl 0x1c(%rcx), %ecx
subl -0x2c(%rbp), %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jne 0x47368
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movslq 0x18(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rcx
movq -0x40(%rbp), %rdx
xorl %eax, %eax
cmpq 0x10(%rdx), %rcx
movb %al, -0x75(%rbp)
jbe 0x471f5
movq -0x60(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
setne %al
movb %al, -0x75(%rbp)
movb -0x75(%rbp), %al
testb $0x1, %al
jne 0x471fe
jmp 0x4720c
movq -0x60(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x471d2
movq -0x60(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x47305
movq -0x40(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %eax
shll %eax
addl $0x8, %eax
movslq %eax, %rsi
callq 0x4820
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x47254
jmp 0x47546
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %ecx
shll %ecx
movl %ecx, 0x1c(%rax)
movq -0x68(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x40(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jl 0x4727e
jmp 0x4729d
leaq 0xab9c1(%rip), %rdi     # 0xf2c46
leaq 0xaafb8(%rip), %rsi     # 0xf2244
movl $0xe2f, %edx            # imm = 0xE2F
leaq 0xab9c7(%rip), %rcx     # 0xf2c5f
callq 0x42e0
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %eax
cmpl -0x2c(%rbp), %eax
jl 0x472ab
jmp 0x472ca
leaq 0xab9cf(%rip), %rdi     # 0xf2c81
leaq 0xaaf8b(%rip), %rsi     # 0xf2244
movl $0xe30, %edx            # imm = 0xE30
leaq 0xab99a(%rip), %rcx     # 0xf2c5f
callq 0x42e0
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x40(%rbp), %rcx
movl 0x1c(%rcx), %ecx
subl -0x2c(%rbp), %ecx
cmpl %ecx, %eax
jl 0x472e1
jmp 0x47300
leaq 0xab9af(%rip), %rdi     # 0xf2c97
leaq 0xaaf55(%rip), %rsi     # 0xf2244
movl $0xe31, %edx            # imm = 0xE31
leaq 0xab964(%rip), %rcx     # 0xf2c5f
callq 0x42e0
jmp 0x470c8
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rax
subq %rax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x3f730
cmpl $0x0, %eax
jge 0x4732f
jmp 0x47546
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rax
subq %rax, %rdx
callq 0x43b0
movq -0x58(%rbp), %rax
movq -0x60(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x2c(%rbp)
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %ecx
subl -0x2c(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0x4736f
movl $0x0, -0x2c(%rbp)
movq -0x38(%rbp), %rax
movl 0x74(%rax), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x74(%rax)
movq -0x40(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rsi
movq -0x40(%rbp), %rcx
leaq 0x2b0(%rip), %rdx       # 0x47650
leaq -0x69c7(%rip), %r8      # 0x409e0
leaq -0x69ee(%rip), %r9      # 0x409c0
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x4e2e0
cmpl $0x0, %eax
jge 0x473c6
jmp 0x47546
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
callq 0x4730
movq -0x38(%rbp), %rax
cmpl $0x1, 0xd0(%rax)
jne 0x473fa
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
callq 0x4330
movq $0x0, -0x40(%rbp)
jmp 0x474be
movq $0x0, -0x40(%rbp)
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
callq 0x4330
jmp 0x46f2c
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rsi
leaq 0x5d5(%rip), %rdx       # 0x47a00
xorl %eax, %eax
movl %eax, %ecx
callq 0x4e290
cmpl $0x0, %eax
jge 0x4743e
jmp 0x47546
jmp 0x47440
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
callq 0x4730
movq -0x38(%rbp), %rax
cmpl $0x0, 0xd0(%rax)
jne 0x47472
movq -0x38(%rbp), %rdi
addq $0xa0, %rdi
movq -0x38(%rbp), %rsi
addq $0x78, %rsi
callq 0x47a0
movq -0x38(%rbp), %rax
movl 0xd0(%rax), %eax
subl $0x1, %eax
jne 0x474af
jmp 0x47483
movq -0x38(%rbp), %rdi
addq $0xa0, %rdi
callq 0x4040
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
callq 0x4330
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4d330
jmp 0x474be
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
callq 0x4330
jmp 0x47440
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
callq 0x4730
movq -0x38(%rbp), %rax
movl $0x2, 0xd0(%rax)
movq -0x38(%rbp), %rdi
addq $0xa0, %rdi
callq 0x4040
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
callq 0x4330
cmpq $0x0, -0x40(%rbp)
je 0x47532
movq -0x38(%rbp), %rdi
addq $0x18, %rdi
callq 0x4730
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x38(%rbp), %rdi
addq $0x18, %rdi
callq 0x4330
movq -0x18(%rbp), %rdi
callq 0x45a0
xorl %eax, %eax
addq $0x90, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
callq 0x4050
cmpl $0x0, (%rax)
je 0x47567
callq 0x4050
movl (%rax), %eax
movl %eax, -0x84(%rbp)
jmp 0x47574
movl $0xc, %eax
movl %eax, -0x84(%rbp)
jmp 0x47574
movq -0x80(%rbp), %rdi
movl -0x84(%rbp), %esi
callq 0x46a20
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4d330
jmp 0x474be
nopw %cs:(%rax,%rax)