// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq 0x10(%rdi), %r13
movq 0x20(%rdi), %rbx
leaq 0x18(%rbx), %r14
movq %r14, %rdi
callq 0x4750
movq 0x60(%rbx), %r15
testq %r15, %r15
je 0x336a9
movq (%r15), %rax
movq %rax, 0x60(%rbx)
movq %r14, %rdi
callq 0x4340
movq 0x10(%r15), %rbp
movl 0x8(%r12), %eax
movl %eax, 0x8(%r15)
movq $0x0, (%r15)
testq %rbp, %rbp
jne 0x336fb
jmp 0x33819
movq %r14, %rdi
callq 0x4340
movl $0x1, %edi
movl $0x30, %esi
callq 0x4300
testq %rax, %rax
je 0x33884
movq %rax, %r15
movl $0x64, 0x1c(%rax)
movl $0x64, %edi
movl $0x50, %esi
callq 0x4300
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x33819
movq %rax, %rbp
movl 0x8(%r12), %eax
movl %eax, 0x8(%r15)
movq %r14, 0x20(%rsp)
movslq 0x18(%r12), %rax
testq %rax, %rax
jle 0x33828
movq %r12, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movq %r15, 0x18(%rsp)
addq %r13, %rax
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %rax, 0x28(%rsp)
movq 0x18(%rsp), %r15
movslq 0x1c(%r15), %r12
cmpq %r12, %r14
jl 0x33784
leal (%r12,%r12), %eax
movl %eax, 0x1c(%r15)
movq 0x10(%r15), %rdi
cltq
shlq $0x4, %rax
leaq (%rax,%rax,4), %rsi
callq 0x4840
testq %rax, %rax
je 0x33899
movq %rax, %rbp
leaq (%r12,%r12,4), %rdi
shlq $0x4, %rdi
addq %rax, %rdi
movslq 0x1c(%r15), %rax
subq %r12, %rax
shlq $0x4, %rax
leaq (%rax,%rax,4), %rdx
xorl %esi, %esi
callq 0x4260
movq %rbp, 0x10(%r15)
movq %r13, %rdi
movl $0xa, %esi
callq 0x4190
movq 0x28(%rsp), %r15
movq %r15, %r12
movq %r15, %rcx
testq %rax, %rax
je 0x337bd
cmpq %r13, %rax
jbe 0x337b4
leaq -0x1(%rax), %rcx
cmpb $0xd, -0x1(%rax)
cmovneq %rax, %rcx
jmp 0x337b7
movq %rax, %rcx
incq %rax
movq %rax, %r12
movb $0x0, (%rcx)
subq %r13, %rcx
movq %rcx, 0x30(%rsp)
movq 0x10(%rsp), %rax
movslq 0x1c(%rax), %rax
movq %rax, 0x38(%rsp)
movq %r13, 0x40(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rsi
leaq (%rbx,%rbp), %rdx
leaq 0x30(%rsp), %rdi
callq 0x275aa
testl %eax, %eax
js 0x33852
incq %r14
addq $0x50, %rbx
movq %r12, %r13
cmpq %r15, %r12
jb 0x3372a
movq 0x18(%rsp), %r15
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %r12
jmp 0x3382b
movq %rbx, %rdi
movl $0xc, %esi
callq 0x32ea7
jmp 0x3387c
xorl %r14d, %r14d
movl %r14d, 0x18(%r15)
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x4750
movq 0x58(%rbx), %rax
movq %rax, (%r12)
movq %r12, 0x58(%rbx)
movq %r14, %rdi
callq 0x4340
jmp 0x33887
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
movl $0x5, %esi
cmovnel %eax, %esi
movq 0x8(%rsp), %rdi
callq 0x32ea7
movq 0x10(%rsp), %rdi
callq 0x2de16
movq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x291e5
xorl %r15d, %r15d
movq %r15, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x1c(%r15), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movl %ecx, 0x1c(%r15)
movq 0x8(%rsp), %rdi
jmp 0x3381c