// File: /workspace/github_c_makefile_star2_O3/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
je 0x29058
movq %r13, 0x18(%rsp)
movq (%rbx), %rax
movq %rax, 0x58(%r14)
movq %r12, 0x20(%rsp)
movq %r12, %rdi
callq 0x4340
movslq 0x1c(%rbx), %rcx
movq 0x10(%rbx), %rax
jmp 0x29089
movq %r12, %rdi
callq 0x4340
movl $0x1, %edi
movl $0x30, %esi
callq 0x4300
testq %rax, %rax
je 0x291c4
movq %rax, %rbx
movq %r13, 0x18(%rsp)
movq %r12, 0x20(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
movl 0x8(%r15), %edx
movl %edx, 0x8(%rbx)
xorl %edx, %edx
movq %rdx, (%rbx)
movq %rdx, (%rsp)
movq %rcx, 0x8(%rsp)
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x18(%r15)
jle 0x2914d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq (%r14), %rdi
movq 0x10(%r15), %rsi
addq %r12, %rsi
movq %rsp, %rdx
callq 0x2a24a
testl %eax, %eax
js 0x29180
movq (%rsp), %rcx
leaq 0x2(%rcx), %rax
cmpq %rax, 0x8(%rsp)
jae 0x2911b
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 0x29139
movq %rax, 0x10(%rsp)
movq %rbp, 0x8(%rsp)
movq (%rsp), %rcx
jmp 0x29120
movq 0x10(%rsp), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%rsp)
movb $0xa, (%rax,%rcx)
movq (%rsp), %rax
movq 0x10(%rsp), %rcx
movb $0x0, (%rcx,%rax)
incq %r13
movslq 0x18(%r15), %rax
addq $0x50, %r12
cmpq %rax, %r13
jl 0x290b4
movq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x4750
pshufd $0xe8, (%rsp), %xmm0    # xmm0 = mem[0,2,2,3]
movq %xmm0, 0x18(%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x10(%rbx)
movq 0x18(%rsp), %rax
cmpq $0x0, 0x68(%rax)
je 0x291ac
leaq 0x28(%rbx), %r14
jmp 0x291b7
callq 0x4050
movl (%rax), %eax
testl %eax, %eax
movl $0x5, %esi
cmovnel %eax, %esi
movq %r14, %rdi
callq 0x32ea7
movq 0x10(%rbx), %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x45c0
jmp 0x291d1
movq 0x60(%r14), %rax
addq $0x60, %r14
movq %rax, (%r15)
movq %r15, (%r14)
movq %r12, %rdi
callq 0x4340
jmp 0x291d3
movq %r14, %rdi
movl $0xc, %esi
callq 0x32ea7
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq