// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
callq 0x4730
movq -0x40(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x476ba
movq -0x40(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
callq 0x4330
cmpq $0x0, -0x20(%rbp)
jne 0x4775a
movl $0x1, %edi
movl $0x30, %esi
callq 0x42f0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x476f9
movq $0x0, -0x8(%rbp)
jmp 0x479e4
movq -0x20(%rbp), %rax
movl $0x64, 0x1c(%rax)
movq -0x20(%rbp), %rax
movslq 0x1c(%rax), %rdi
movl $0x50, %esi
callq 0x42f0
movq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x47743
movq -0x40(%rbp), %rdi
movl $0xc, %esi
callq 0x46a20
jmp 0x479d3
movq -0x20(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x47799
movq -0x40(%rbp), %rdi
movl $0xc, %esi
callq 0x46a20
jmp 0x479d3
movl $0x0, -0x2c(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq 0x18(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x4798a
movl -0x2c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jl 0x4787a
movq -0x20(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x20(%rbp), %rax
movl 0x1c(%rax), %ecx
shll %ecx
movl %ecx, 0x1c(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
movslq 0x1c(%rax), %rax
imulq $0x50, %rax, %rsi
callq 0x4820
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x47848
movq -0x20(%rbp), %rax
movq %rax, -0x88(%rbp)
movl 0x1c(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x40(%rbp), %rdi
movl $0xc, %esi
callq 0x46a20
jmp 0x479d3
movq -0x38(%rbp), %rdi
movslq -0x54(%rbp), %rax
imulq $0x50, %rax, %rax
addq %rax, %rdi
movq -0x20(%rbp), %rax
movl 0x1c(%rax), %eax
subl -0x54(%rbp), %eax
cltq
imulq $0x50, %rax, %rdx
xorl %esi, %esi
callq 0x4250
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x48(%rbp), %rdi
movl $0xa, %esi
callq 0x4170
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x478cc
movq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x48(%rbp), %rax
jbe 0x478be
movq -0x68(%rbp), %rax
movsbl -0x1(%rax), %eax
cmpl $0xd, %eax
jne 0x478be
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x478d8
movq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
movq %rax, -0x60(%rbp)
movq -0x68(%rbp), %rax
movb $0x0, (%rax)
movq -0x68(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rax
movslq 0x1c(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rdx
movslq -0x2c(%rbp), %rax
imulq $0x50, %rax, %rax
addq %rax, %rdx
leaq -0x80(%rbp), %rdi
callq 0x39630
cmpl $0x0, %eax
jge 0x47974
movq -0x40(%rbp), %rax
movq %rax, -0x90(%rbp)
callq 0x4050
cmpl $0x0, (%rax)
je 0x4794a
callq 0x4050
movl (%rax), %eax
movl %eax, -0x94(%rbp)
jmp 0x47957
movl $0x5, %eax
movl %eax, -0x94(%rbp)
jmp 0x47957
movq -0x90(%rbp), %rdi
movl -0x94(%rbp), %esi
callq 0x46a20
movq -0x18(%rbp), %rdi
callq 0x409e0
jmp 0x479d3
movq -0x60(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x477bb
movl -0x2c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
callq 0x4730
movq -0x40(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
callq 0x4330
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x479e4
movq -0x20(%rbp), %rdi
callq 0x3be60
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)