// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r15
movq 0x28(%rdi), %r14
movq 0xd8(%r14), %r13
leaq 0x18(%r14), %r12
movq %r12, %rdi
callq 0x4750
movq 0x58(%r14), %rbx
testq %rbx, %rbx
movq %r13, 0x20(%rsp)
je 0x29c5a
movq (%rbx), %rax
movq %rax, 0x58(%r14)
jmp 0x29c5c
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x4340
testq %rbx, %rbx
jne 0x29c8b
movl $0x1, %edi
movl $0x30, %esi
callq 0x4300
testq %rax, %rax
je 0x29dd3
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq %r12, 0x18(%rsp)
movl 0x8(%r15), %eax
movl %eax, 0x8(%rbx)
xorl %eax, %eax
movq %rax, (%rbx)
movq %rax, (%rsp)
movslq 0x1c(%rbx), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x18(%r15)
jle 0x29d5a
xorl %r12d, %r12d
xorl %r13d, %r13d
movq (%r14), %rdi
movq 0x10(%r15), %rsi
addq %r12, %rsi
movq %rsp, %rdx
callq 0x2ae99
testl %eax, %eax
js 0x29d8f
movq (%rsp), %rax
addq $0x2, %rax
cmpq %rax, 0x8(%rsp)
jae 0x29d24
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %rdx
shrq %rdx
testq %rcx, %rcx
movl $0x0, %ebp
cmoveq %rdx, %rbp
addq %rax, %rbp
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x4840
testq %rax, %rax
je 0x29d46
movq %rax, 0x10(%rsp)
movq %rbp, 0x8(%rsp)
movq (%rsp), %rax
movq 0x10(%rsp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rsp)
movb $0xa, (%rcx,%rax)
movq (%rsp), %rax
movq 0x10(%rsp), %rcx
movb $0x0, (%rcx,%rax)
incq %r13
movslq 0x18(%r15), %rax
addq $0x50, %r12
cmpq %rax, %r13
jl 0x29cc3
movq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x4750
movl (%rsp), %eax
movl %eax, 0x18(%rbx)
movl 0x8(%rsp), %eax
movl %eax, 0x1c(%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x10(%rbx)
movq 0x20(%rsp), %rax
cmpq $0x0, 0x68(%rax)
je 0x29dbb
leaq 0x28(%rbx), %r14
jmp 0x29dc6
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
movl $0x5, %esi
cmovnel %eax, %esi
movq %r14, %rdi
callq 0x340e0
movq 0x10(%rbx), %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x45c0
jmp 0x29de0
movq 0x60(%r14), %rax
addq $0x60, %r14
movq %rax, (%r15)
movq %r15, (%r14)
movq %r12, %rdi
callq 0x4340
jmp 0x29de2
movq %r14, %rdi
movl $0xc, %esi
callq 0x340e0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq