// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %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
je 0x22ce5
movq %r13, 0x8(%rsp)
movq (%rbx), %rax
movq %rax, 0x58(%r14)
movq %r12, 0x10(%rsp)
movq %r12, %rdi
callq 0x4340
movslq 0x1c(%rbx), %rcx
movq 0x10(%rbx), %rax
jmp 0x22d12
movq %r12, %rdi
callq 0x4340
pushq $0x1
popq %rdi
pushq $0x30
popq %rsi
callq 0x4300
testq %rax, %rax
je 0x22dee
movq %rax, %rbx
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
movl 0x8(%r15), %edx
movl %edx, 0x8(%rbx)
movq %rbx, 0x18(%rsp)
andq $0x0, (%rbx)
leaq 0x20(%rsp), %r13
andq $0x0, (%r13)
movq %rcx, 0x8(%r13)
movq %rax, 0x10(%r13)
xorl %r12d, %r12d
pushq $0xa
popq %rbp
xorl %ebx, %ebx
movslq 0x18(%r15), %rax
cmpq %rax, %rbx
jge 0x22d6e
movq (%r14), %rdi
movq 0x10(%r15), %rsi
addq %r12, %rsi
movq %r13, %rdx
callq 0x23c84
testl %eax, %eax
js 0x22da7
movl %ebp, %edi
movq %r13, %rsi
callq 0x21551
incq %rbx
addq $0x50, %r12
jmp 0x22d3c
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x4750
pshufd $0xe8, 0x20(%rsp), %xmm0 # xmm0 = mem[0,2,2,3]
movq 0x18(%rsp), %rbx
movq %xmm0, 0x18(%rbx)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rbx)
movq 0x8(%rsp), %rax
cmpq $0x0, 0x68(%rax)
je 0x22dd6
leaq 0x28(%rbx), %r14
jmp 0x22de1
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
pushq $0x5
popq %rsi
cmovnel %eax, %esi
movq %r14, %rdi
callq 0x29211
movq 0x18(%rsp), %rbx
movq 0x10(%rbx), %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x45c0
jmp 0x22df9
movq 0x60(%r14), %rax
addq $0x60, %r14
movq %rax, (%r15)
movq %r15, (%r14)
movq %r12, %rdi
callq 0x4340
jmp 0x22dfb
pushq $0xc
popq %rsi
movq %r14, %rdi
callq 0x29211
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq