// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/sam.c
// Function: sam_parse_worker
static void *sam_parse_worker(void *arg) {
    sp_lines *gl = (sp_lines *)arg;
    sp_bams *gb = NULL;
    char *lines = gl->data;
    int i;
    bam1_t *b;
    SAM_state *fd = gl->fd;

    // Use a block of BAM structs we had earlier if available.
    pthread_mutex_lock(&fd->lines_m);
    if (fd->bams) {
        gb = fd->bams;
        fd->bams = gb->next;
    }
    pthread_mutex_unlock(&fd->lines_m);

    if (gb == NULL) {
        gb = calloc(1, sizeof(*gb));
        if (!gb) {
            return NULL;
        }
        gb->abams = 100;
        gb->bams = b = calloc(gb->abams, sizeof(*b));
        if (!gb->bams) {
            sam_state_err(fd, ENOMEM);
            goto err;
        }
        gb->nbams = 0;
        gb->bam_mem = 0;
    }
    gb->serial = gl->serial;
    gb->next = NULL;

    b = (bam1_t *)gb->bams;
    if (!b) {
        sam_state_err(fd, ENOMEM);
        goto err;
    }

    i = 0;
    char *cp = lines, *cp_end = lines + gl->data_size;
    while (cp < cp_end) {
        if (i >= gb->abams) {
            int old_abams = gb->abams;
            gb->abams *= 2;
            b = (bam1_t *)realloc(gb->bams, gb->abams*sizeof(bam1_t));
            if (!b) {
                gb->abams /= 2;
                sam_state_err(fd, ENOMEM);
                goto err;
            }
            memset(&b[old_abams], 0, (gb->abams - old_abams)*sizeof(*b));
            gb->bams = b;
        }

        // Ideally we'd get sam_parse1 to return the number of
        // bytes decoded and to be able to stop on newline as
        // well as \0.
        //
        // We can then avoid the additional strchr loop.
        // It's around 6% of our CPU cost, albeit threadable.
        //
        // However this is an API change so for now we copy.

        char *nl = strchr(cp, '\n');
        char *line_end;
        if (nl) {
            line_end = nl;
            if (line_end > cp && *(line_end - 1) == '\r')
                line_end--;
            nl++;
        } else {
            nl = line_end = cp_end;
        }
        *line_end = '\0';
        kstring_t ks = { line_end - cp, gl->alloc, cp };
        if (sam_parse1(&ks, fd->h, &b[i]) < 0) {
            sam_state_err(fd, errno ? errno : EIO);
            cleanup_sp_lines(gl);
            goto err;
        }

        cp = nl;
        i++;
    }
    gb->nbams = i;

    pthread_mutex_lock(&fd->lines_m);
    gl->next = fd->lines;
    fd->lines = gl;
    pthread_mutex_unlock(&fd->lines_m);
    return gb;

 err:
    sam_free_sp_bams(gb);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r15
movq 0x10(%rdi), %r13
movq 0x20(%rdi), %r14
leaq 0x18(%r14), %r12
movq %r12, %rdi
callq 0x4750
movq 0x60(%r14), %rcx
testq %rcx, %rcx
je 0x29a84
movq (%rcx), %rax
movq %rax, 0x60(%r14)
movq %rcx, %rbx
movq %r12, 0x10(%rsp)
movq %r12, %rdi
callq 0x4340
movq %rbx, %rcx
movq 0x10(%rbx), %rbp
movl 0x8(%r15), %eax
movl %eax, 0x8(%rcx)
andq $0x0, (%rcx)
testq %rbp, %rbp
movq %rcx, 0x8(%rsp)
je 0x29ad2
movslq 0x18(%r15), %rbx
addq %r13, %rbx
movq $0x0, 0x20(%rsp)
xorl %eax, %eax
movq %r14, 0x18(%rsp)
cmpq %rbx, %r13
jae 0x29ada
movslq 0x1c(%rcx), %r12
movq %rax, 0x28(%rsp)
cmpq %r12, %rax
jl 0x299fb
leal (%r12,%r12), %eax
movl %eax, 0x1c(%rcx)
movq %rcx, %r14
movq 0x10(%rcx), %rdi
cltq
imulq $0x50, %rax, %rsi
callq 0x4840
testq %rax, %rax
je 0x29b25
movq %rax, %rbp
imulq $0x50, %r12, %rdi
addq %rax, %rdi
movslq 0x1c(%r14), %rax
subq %r12, %rax
imulq $0x50, %rax, %rdx
xorl %esi, %esi
callq 0x4260
movq %rbp, 0x10(%r14)
movq 0x18(%rsp), %r14
movq %r13, %rdi
pushq $0xa
popq %rsi
callq 0x4190
movq %rbx, %r12
movq %rbx, %rcx
testq %rax, %rax
je 0x29a2d
cmpq %r13, %rax
jbe 0x29a24
leaq -0x1(%rax), %rcx
cmpb $0xd, -0x1(%rax)
cmovneq %rax, %rcx
jmp 0x29a27
movq %rax, %rcx
incq %rax
movq %rax, %r12
movb $0x0, (%rcx)
subq %r13, %rcx
movq %rcx, 0x30(%rsp)
movslq 0x1c(%r15), %rax
movq %rax, 0x38(%rsp)
movq %r13, 0x40(%rsp)
movq (%r14), %rsi
movq 0x20(%rsp), %r13
movq %rbp, %rdx
addq %r13, %rdx
leaq 0x30(%rsp), %rdi
callq 0x21889
testl %eax, %eax
js 0x29b04
movq 0x28(%rsp), %rax
incq %rax
addq $0x50, %r13
movq %r13, 0x20(%rsp)
movq %r12, %r13
movq 0x8(%rsp), %rcx
jmp 0x2999d
movq %r12, %rdi
callq 0x4340
pushq $0x1
popq %rdi
pushq $0x30
popq %rsi
callq 0x4300
testq %rax, %rax
je 0x29b4a
movq %r12, 0x10(%rsp)
movl $0x64, 0x1c(%rax)
movq %rax, 0x8(%rsp)
pushq $0x64
popq %rdi
pushq $0x50
popq %rsi
callq 0x4300
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%rcx)
testq %rax, %rax
je 0x29ad2
movq %rax, %rbp
jmp 0x2996d
pushq $0xc
popq %rsi
movq %r14, %rdi
jmp 0x29b3b
movl %eax, 0x18(%rcx)
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x58(%r14), %rax
movq %rax, (%r15)
movq %r15, 0x58(%r14)
movq %rbx, %rdi
callq 0x4340
movq 0x8(%rsp), %rax
jmp 0x29b4c
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
pushq $0x5
popq %rsi
cmovnel %eax, %esi
movq %r14, %rdi
callq 0x29211
movq %r15, %rdi
callq 0x257c8
jmp 0x29b40
movl 0x1c(%r14), %eax
pushq $0x2
popq %rcx
cltd
idivl %ecx
movl %eax, 0x1c(%r14)
pushq $0xc
popq %rsi
movq 0x18(%rsp), %rdi
callq 0x29211
movq 0x8(%rsp), %rdi
callq 0x22e0d
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq