// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq 0x10(%rdi), %r12
movq %rdi, 0x8(%rsp)
movq 0x20(%rdi), %rbx
leaq 0x18(%rbx), %rdi
movq %rdi, 0x20(%rsp)
callq 0x4750
movq 0x60(%rbx), %rbp
testq %rbp, %rbp
je 0x34905
movq (%rbp), %rax
movq %rax, 0x60(%rbx)
jmp 0x34907
xorl %ebp, %ebp
movq 0x20(%rsp), %rdi
callq 0x4340
testq %rbp, %rbp
je 0x34aaf
movq 0x8(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x8(%rbp)
movq $0x0, (%rbp)
movq 0x10(%rbp), %rax
testq %rax, %rax
je 0x34af9
movq %rax, 0x28(%rsp)
movq %rbx, 0x10(%rsp)
movq 0x8(%rsp), %rax
movslq 0x18(%rax), %rbx
addq %r12, %rbx
xorl %r13d, %r13d
movq %rbx, 0x38(%rsp)
movq %rbp, 0x30(%rsp)
cmpq %rbx, %r12
jae 0x34b22
movslq 0x1c(%rbp), %r15
cmpl %r15d, %r13d
jl 0x349e5
leal (%r15,%r15), %eax
movl %eax, 0x1c(%rbp)
movq 0x10(%rbp), %rdi
movq %r15, %rax
shlq $0x5, %rax
leaq (%rax,%rax,4), %rsi
callq 0x4840
movq %rax, %r14
testq %rax, %rax
je 0x349b9
leaq (%r15,%r15,4), %rdi
shlq $0x4, %rdi
addq %r14, %rdi
movslq 0x1c(%rbp), %rax
subq %r15, %rax
shlq $0x4, %rax
leaq (%rax,%rax,4), %rdx
xorl %esi, %esi
callq 0x4260
movq %r14, 0x10(%rbp)
jmp 0x349d7
movl 0x1c(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movl %ecx, 0x1c(%rbp)
movq 0x10(%rsp), %rdi
movl $0xc, %esi
callq 0x340e0
movq %r14, 0x28(%rsp)
testq %r14, %r14
je 0x34b06
movl %r13d, 0x1c(%rsp)
movq %r12, %rdi
movl $0xa, %esi
callq 0x4190
movq %rbx, %r15
movq %rbx, %rcx
testq %rax, %rax
je 0x34a1e
cmpq %r12, %rax
jbe 0x34a15
leaq -0x1(%rax), %rcx
cmpb $0xd, -0x1(%rax)
cmovneq %rax, %rcx
jmp 0x34a18
movq %rax, %rcx
incq %rax
movq %rax, %r15
movb $0x0, (%rcx)
subq %r12, %rcx
movq %rcx, 0x40(%rsp)
movq 0x8(%rsp), %rcx
movslq 0x1c(%rcx), %rax
movq %rax, 0x48(%rsp)
movq %r12, 0x50(%rsp)
movq %r12, %rbx
movq %rcx, %r12
movq 0x10(%rsp), %r14
movq (%r14), %rsi
movl 0x1c(%rsp), %r13d
movslq %r13d, %rax
leaq (%rax,%rax,4), %rdx
shlq $0x4, %rdx
addq 0x28(%rsp), %rdx
leaq 0x40(%rsp), %rdi
callq 0x280b4
movl %eax, %ebp
testl %eax, %eax
js 0x34a74
incl %r13d
jmp 0x34a98
movq %rbx, %r15
callq 0x4050
movl (%rax), %esi
testl %esi, %esi
movl $0x5, %eax
cmovel %eax, %esi
movq %r14, %rdi
callq 0x340e0
movq %r12, %rdi
callq 0x2ea4d
testl %ebp, %ebp
movq 0x30(%rsp), %rbp
movq 0x38(%rsp), %rbx
movq %r15, %r12
jns 0x3495d
jmp 0x34b06
movl $0x1, %edi
movl $0x30, %esi
callq 0x4300
testq %rax, %rax
je 0x34b0e
movq %rax, %rbp
movl $0x64, 0x1c(%rax)
movl $0x64, %edi
movl $0x50, %esi
callq 0x4300
movq %rax, 0x10(%rbp)
testq %rax, %rax
je 0x34af9
movl $0x0, 0x18(%rbp)
movq $0x0, 0x20(%rbp)
jmp 0x3491a
movq %rbx, %rdi
movl $0xc, %esi
callq 0x340e0
movq %rbp, %rdi
callq 0x29df4
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r13d, 0x18(%rbp)
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x10(%rsp), %rcx
movq 0x58(%rcx), %rax
movq 0x8(%rsp), %rdx
movq %rax, (%rdx)
movq %rdx, 0x58(%rcx)
movq %rbx, %rdi
callq 0x4340
jmp 0x34b10