// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/sam.c
// Function: sam_format_worker
static void *sam_format_worker(void *arg) {
    sp_bams *gb = (sp_bams *)arg;
    sp_lines *gl = NULL;
    int i;
    SAM_state *fd = gb->fd;
    htsFile *fp = fd->fp;

    // Use a block of SAM strings we had earlier if available.
    pthread_mutex_lock(&fd->lines_m);
    if (fd->lines) {
        gl = fd->lines;
        fd->lines = gl->next;
    }
    pthread_mutex_unlock(&fd->lines_m);

    if (gl == NULL) {
        gl = calloc(1, sizeof(*gl));
        if (!gl) {
            sam_state_err(fd, ENOMEM);
            return NULL;
        }
        gl->alloc = gl->data_size = 0;
        gl->data = NULL;
    }
    gl->serial = gb->serial;
    gl->next = NULL;

    kstring_t ks = {0, gl->alloc, gl->data};

    for (i = 0; i < gb->nbams; i++) {
        if (sam_format1_append(fd->h, &gb->bams[i], &ks) < 0) {
            sam_state_err(fd, errno ? errno : EIO);
            goto err;
        }
        kputc('\n', &ks);
    }

    pthread_mutex_lock(&fd->lines_m);
    gl->data_size = ks.l;
    gl->alloc = ks.m;
    gl->data = ks.s;

    if (fp->idx) {
        // Keep hold of the bam array a little longer as
        // sam_dispatcher_write needs to use them for building the index.
        gl->bams = gb;
    } else {
        // Add bam array to free-list
        gb->next = fd->bams;
        fd->bams = gb;
    }
    pthread_mutex_unlock(&fd->lines_m);

    return gl;

 err:
    // Possible race between this and fd->curr_bam.
    // Easier to not free and leave it on the input list so it
    // gets freed there instead?
    // sam_free_sp_bams(gb);
    if (gl) {
        free(gl->data);
        free(gl);
    }
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0xd8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
callq 0x4730
movq -0x30(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x3bc7a
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
callq 0x4330
cmpq $0x0, -0x20(%rbp)
jne 0x3bce5
movl $0x1, %edi
movl $0x30, %esi
callq 0x42f0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x3bcc3
movq -0x30(%rbp), %rdi
movl $0xc, %esi
callq 0x46a20
movq $0x0, -0x8(%rbp)
jmp 0x3be51
movq -0x20(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x10(%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 $0x0, -0x50(%rbp)
movq -0x20(%rbp), %rax
movslq 0x1c(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x3bdae
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x24(%rbp), %rax
imulq $0x50, %rax, %rax
addq %rax, %rsi
leaq -0x50(%rbp), %rdx
callq 0x3d9a0
cmpl $0x0, %eax
jge 0x3bd92
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
callq 0x4050
cmpl $0x0, (%rax)
je 0x3bd77
callq 0x4050
movl (%rax), %eax
movl %eax, -0x5c(%rbp)
jmp 0x3bd81
movl $0x5, %eax
movl %eax, -0x5c(%rbp)
jmp 0x3bd81
movq -0x58(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x46a20
jmp 0x3be2c
movl $0xa, %edi
leaq -0x50(%rbp), %rsi
callq 0x39080
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3bd25
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
callq 0x4730
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x40(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x68(%rax)
je 0x3bdfa
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x28(%rax)
jmp 0x3be15
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
callq 0x4330
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3be51
cmpq $0x0, -0x20(%rbp)
je 0x3be49
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x45a0
movq -0x20(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)