// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/sam.c
// Function: sam_write1
int sam_write1(htsFile *fp, const sam_hdr_t *h, const bam1_t *b)
{
    switch (fp->format.format) {
    case binary_format:
        fp->format.category = sequence_data;
        fp->format.format = bam;
        /* fall-through */
    case bam:
        return bam_write_idx1(fp, h, b);

    case cram:
        return cram_put_bam_seq(fp->fp.cram, (bam1_t *)b);

    case text_format:
        fp->format.category = sequence_data;
        fp->format.format = sam;
        /* fall-through */
    case sam:
        if (fp->state) {
            SAM_state *fd = (SAM_state *)fp->state;

            // Threaded output
            if (!fd->h) {
                // NB: discard const.  We don't actually modify sam_hdr_t here,
                // just data pointed to by it (which is a bit weasely still),
                // but out cached pointer must be non-const as we want to
                // destroy it later on and sam_hdr_destroy takes non-const.
                //
                // We do this because some tools do sam_hdr_destroy; sam_close
                // while others do sam_close; sam_hdr_destroy.  The former is
                // an issue as we need the header still when flushing.
                fd->h = (sam_hdr_t *)h;
                fd->h->ref_count++;

                if (pthread_create(&fd->dispatcher, NULL, sam_dispatcher_write,
                                   fp) != 0)
                    return -2;
                fd->dispatcher_set = 1;
            }

            if (fd->h != h) {
                hts_log_error("SAM multi-threaded decoding does not support changing header");
                return -2;
            }

            // Find a suitable BAM array to copy to
            sp_bams *gb = fd->curr_bam;
            if (!gb) {
                pthread_mutex_lock(&fd->lines_m);
                if (fd->bams) {
                    fd->curr_bam = gb = fd->bams;
                    fd->bams = gb->next;
                    gb->next = NULL;
                    gb->nbams = 0;
                    gb->bam_mem = 0;
                    pthread_mutex_unlock(&fd->lines_m);
                } else {
                    pthread_mutex_unlock(&fd->lines_m);
                    if (!(gb = calloc(1, sizeof(*gb)))) return -1;
                    if (!(gb->bams = calloc(SAM_NBAM, sizeof(*gb->bams)))) {
                        free(gb);
                        return -1;
                    }
                    gb->nbams = 0;
                    gb->abams = SAM_NBAM;
                    gb->bam_mem = 0;
                    gb->fd = fd;
                    fd->curr_idx = 0;
                    fd->curr_bam = gb;
                }
            }

            if (!bam_copy1(&gb->bams[gb->nbams++], b))
                return -2;
            gb->bam_mem += b->l_data + sizeof(*b);

            // Dispatch if full
            if (gb->nbams == SAM_NBAM || gb->bam_mem > SAM_NBYTES*0.8) {
                gb->serial = fd->serial++;
                pthread_mutex_lock(&fd->command_m);
                if (fd->errcode != 0) {
                    pthread_mutex_unlock(&fd->command_m);
                    return -fd->errcode;
                }
                if (hts_tpool_dispatch3(fd->p, fd->q, sam_format_worker, gb,
                                        cleanup_sp_bams,
                                        cleanup_sp_lines, 0) < 0) {
                    pthread_mutex_unlock(&fd->command_m);
                    return -1;
                }
                pthread_mutex_unlock(&fd->command_m);
                fd->curr_bam = NULL;
            }

            // Dummy value as we don't know how long it really is.
            // We could track file sizes via a SAM_state field, but I don't think
            // it is necessary.
            return 1;
        } else {
            if (sam_format1(h, b, &fp->line) < 0) return -1;
            kputc('\n', &fp->line);
            if (fp->is_bgzf) {
                if (bgzf_flush_try(fp->fp.bgzf, fp->line.l) < 0)
                    return -1;
                if ( bgzf_write(fp->fp.bgzf, fp->line.s, fp->line.l) != fp->line.l ) return -1;
            } else {
                if ( hwrite(fp->fp.hfile, fp->line.s, fp->line.l) != fp->line.l ) return -1;
            }

            if (fp->idx) {
                if (fp->format.compression == bgzf) {
                    if (bgzf_idx_push(fp->fp.bgzf, fp->idx, b->core.tid, b->core.pos, bam_endpos(b),
                                      bgzf_tell(fp->fp.bgzf), !(b->core.flag&BAM_FUNMAP)) < 0) {
                        hts_log_error("Read '%s' with ref_name='%s', ref_length=%"PRIhts_pos", flags=%d, pos=%"PRIhts_pos" cannot be indexed",
                                bam_get_qname(b), sam_hdr_tid2name(h, b->core.tid), sam_hdr_tid2len(h, b->core.tid), b->core.flag, b->core.pos+1);
                        return -1;
                    }
                } else {
                    if (hts_idx_push(fp->idx, b->core.tid, b->core.pos, bam_endpos(b),
                                     bgzf_tell(fp->fp.bgzf), !(b->core.flag&BAM_FUNMAP)) < 0) {
                        hts_log_error("Read '%s' with ref_name='%s', ref_length=%"PRIhts_pos", flags=%d, pos=%"PRIhts_pos" cannot be indexed",
                                bam_get_qname(b), sam_hdr_tid2name(h, b->core.tid), sam_hdr_tid2len(h, b->core.tid), b->core.flag, b->core.pos+1);
                        return -1;
                    }
                }
            }

            return fp->line.l;
        }


    case fasta_format:
    case fastq_format: {
        fastq_state *x = (fastq_state *)fp->state;
        if (!x) {
            if (!(fp->state = fastq_state_init(fp->format.format
                                               == fastq_format ? '@' : '>')))
                return -2;
        }

        if (fastq_format1(fp->state, b, &fp->line) < 0)
            return -1;
        if (fp->is_bgzf) {
            if (bgzf_flush_try(fp->fp.bgzf, fp->line.l) < 0)
                return -1;
            if (bgzf_write(fp->fp.bgzf, fp->line.s, fp->line.l) != fp->line.l)
                return -1;
        } else {
            if (hwrite(fp->fp.hfile, fp->line.s, fp->line.l) != fp->line.l)
                return -1;
        }
        return fp->line.l;
    }

    default:
        errno = EBADF;
        return -1;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl 0x4c(%rdi), %ebx
leal -0x1(%rbx), %eax
cmpl $0x10, %eax
ja 0x2d151
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0x6a345(%rip), %rcx     # 0x973a4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x40(%r12), %rdi
testq %rdi, %rdi
jne 0x2d0a9
movl $0x1, %edi
movl $0x80, %esi
callq 0x4300
testq %rax, %rax
je 0x2d4e7
movq %rax, %rdi
cmpl $0x11, %ebx
sete %al
movb $0x0, 0x6e(%rdi)
movw $0x4342, 0x6c(%rdi)     # imm = 0x4342
addb %al, %al
addb $0x3e, %al
movb %al, 0x78(%rdi)
movq %rdi, 0x40(%r12)
leaq 0x10(%r12), %r15
movq %r14, %rsi
movq %r15, %rdx
callq 0x2b3b9
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x2d823
testb $0x10, (%r12)
movq 0x38(%r12), %r14
jne 0x2d39b
movq 0x10(%r12), %rbx
movq 0x20(%r12), %r12
testb $0x2, 0x30(%r14)
movq 0x18(%r14), %rbp
jne 0x2d110
movq %rbp, %rax
subq 0x8(%r14), %rax
cmpq %rbx, %rax
jae 0x2d110
addq %rbx, %rbp
subq (%r14), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0xee24
movq 0x18(%r14), %rbp
movq %rbp, 0x10(%r14)
movq 0x8(%r14), %rdi
subq %rdi, %rbp
cmpq %rbx, %rbp
ja 0x2d125
cmpq (%r14), %rdi
je 0x2d5f1
cmpq %rbx, %rbp
movq %rbx, %r13
cmovbq %rbp, %r13
movq %r12, %rsi
movq %r13, %rdx
callq 0x43c0
addq %r13, 0x8(%r14)
cmpq %rbx, %rbp
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jb 0x2d5f9
jmp 0x2d60d
callq 0x4050
movl $0x9, (%rax)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x2d823
movabsq $0x400000001, %rax      # imm = 0x400000001
movq %rax, 0x48(%r12)
movq 0x38(%r12), %r13
cmpq $0x0, 0x68(%r12)
je 0x2d302
movl 0x40(%r14), %esi
movzbl 0xf(%r14), %eax
subl %eax, %esi
addl $0x24, %esi
movq %r13, %rdi
callq 0xcfdb
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x2d823
cmpq $0x0, 0x48(%r13)
jne 0x2d1ca
movq 0x68(%r12), %rdi
movq 0x18(%r13), %rax
shlq $0x10, %rax
movzwl 0x10(%r13), %esi
orq %rax, %rsi
callq 0x16eb2
movq %r13, %rdi
movq %r14, %rsi
callq 0x23833
testl %eax, %eax
js 0x2d823
movl %eax, %ebx
movq 0x68(%r12), %r12
movl 0x8(%r14), %ebp
movq (%r14), %rax
movq %rax, (%rsp)
movq %r14, %rdi
callq 0x23104
movq 0x18(%r13), %rcx
shlq $0x10, %rcx
movzwl 0x10(%r13), %r9d
orq %rcx, %r9
movzwl 0x10(%r14), %ecx
xorl %r10d, %r10d
testb $0x4, %cl
sete %r10b
subq $0x8, %rsp
movq %r13, %rdi
movq %r12, %rsi
movl %ebp, %edx
movq 0x8(%rsp), %rcx
movq %rax, %r8
pushq %r10
callq 0xa408
addq $0x10, %rsp
movl %ebx, %ebp
testl %eax, %eax
jns 0x2d823
movq 0x38(%r14), %rbx
movl 0x8(%r14), %esi
movq %r15, %rdi
callq 0x8943c
movq %rax, %r12
movl 0x8(%r14), %esi
movq %r15, %rdi
callq 0x8947e
movzwl 0x10(%r14), %r10d
movq (%r14), %r11
incq %r11
leaq 0x6b582(%rip), %rsi     # 0x987f2
leaq 0x6aa4e(%rip), %rdx     # 0x97cc5
movl $0x1, %edi
movq %rbx, %rcx
movq %r12, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x13de5
addq $0x10, %rsp
jmp 0x2d15c
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, 0x48(%r12)
movq 0x40(%r12), %r13
testq %r13, %r13
je 0x2d31b
movq (%r13), %rax
testq %rax, %rax
je 0x2d3d5
cmpq %r15, %rax
je 0x2d410
leaq 0x6a9ae(%rip), %rsi     # 0x97c7d
leaq 0x6a9b2(%rip), %rdx     # 0x97c88
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
jmp 0x2d4f0
movq 0x38(%r12), %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4772f
movq %r13, %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x23833
leaq 0x10(%r12), %r13
movq $0x0, 0x10(%r12)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2a24a
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x2d823
movq 0x10(%r12), %rcx
leaq 0x2(%rcx), %rax
cmpq %rax, 0x18(%r12)
jae 0x2d4fa
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %rdx
shrq %rdx
xorl %ebx, %ebx
testq %rcx, %rcx
cmoveq %rdx, %rbx
addq %rax, %rbx
movq 0x20(%r12), %rdi
movq %rbx, %rsi
callq 0x4840
testq %rax, %rax
je 0x2d51a
movq %rax, 0x20(%r12)
movq %rbx, 0x18(%r12)
movq 0x10(%r12), %rcx
jmp 0x2d4ff
movq (%r15), %rsi
movq %r14, %rdi
callq 0xcfdb
testl %eax, %eax
js 0x2d823
movq 0x38(%r12), %rdi
movq 0x10(%r12), %rdx
movq 0x20(%r12), %rsi
callq 0xd005
movq %rax, %r13
cmpq 0x10(%r12), %rax
je 0x2d616
jmp 0x2d823
movq %r15, (%r13)
incl 0x40(%r15)
leaq 0x48(%r13), %rdi
leaq 0x44c(%rip), %rdx       # 0x2d834
xorl %esi, %esi
movq %r12, %rcx
callq 0x4920
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
testl %eax, %eax
jne 0x2d823
movl $0x1, 0x50(%r13)
movq (%r13), %rax
jmp 0x2d2bf
movq 0x68(%r13), %r15
testq %r15, %r15
jne 0x2d456
leaq 0x18(%r13), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x60(%r13), %r15
testq %r15, %r15
je 0x2d61e
movq %r15, 0x68(%r13)
movq (%r15), %rax
movq %rax, 0x60(%r13)
xorl %eax, %eax
movq %rax, (%r15)
movl $0x0, 0x18(%r15)
movq %rax, 0x20(%r15)
movq %rbx, %rdi
callq 0x4340
movslq 0x18(%r15), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x18(%r15)
leaq (%rax,%rax,4), %rdi
shlq $0x4, %rdi
addq 0x10(%r15), %rdi
movq %r14, %rsi
callq 0x22b64
testq %rax, %rax
je 0x2d4f0
movslq 0x40(%r14), %rax
movq 0x20(%r15), %rcx
addq %rcx, %rax
addq $0x50, %rax
movq %rax, 0x20(%r15)
cmpl $0x3e8, 0x18(%r15)      # imm = 0x3E8
movl $0x1, %ebp
je 0x2d4a8
cmpq $0x2ee01, %rax          # imm = 0x2EE01
jb 0x2d823
movl 0x74(%r13), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x74(%r13)
movl %eax, 0x8(%r15)
leaq 0x78(%r13), %rbx
movq %rbx, %rdi
callq 0x4750
cmpl $0x0, 0xd4(%r13)
je 0x2d7cb
movq %rbx, %rdi
callq 0x4340
xorl %ebp, %ebp
subl 0xd4(%r13), %ebp
jmp 0x2d823
movq $0x0, 0x40(%r12)
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x2d823
movq 0x20(%r12), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x10(%r12)
movb $0xa, (%rax,%rcx)
movq 0x10(%r12), %rax
movq 0x20(%r12), %rcx
movb $0x0, (%rcx,%rax)
testb $0x10, (%r12)
movq 0x38(%r12), %rbp
movq %r13, (%rsp)
jne 0x2d5bf
movq 0x10(%r12), %rdx
movq 0x20(%r12), %rcx
testb $0x2, 0x30(%rbp)
movq 0x18(%rbp), %r13
movq %rcx, 0x8(%rsp)
jne 0x2d578
movq %r13, %rax
subq 0x8(%rbp), %rax
cmpq %rdx, %rax
jae 0x2d578
addq %rdx, %r13
subq (%rbp), %r13
movq %rbp, %rdi
movq %r13, %rsi
movq %rdx, %rbx
callq 0xee24
movq 0x8(%rsp), %rcx
movq %rbx, %rdx
movq 0x18(%rbp), %r13
movq %r13, 0x10(%rbp)
movq 0x8(%rbp), %rdi
subq %rdi, %r13
cmpq %rdx, %r13
ja 0x2d58e
cmpq (%rbp), %rdi
je 0x2d68d
cmpq %rdx, %r13
movq %rdx, %rbx
cmovbq %r13, %rbx
movq %rcx, %rsi
movq %rdx, 0x10(%rsp)
movq %rbx, %rdx
callq 0x43c0
movq 0x10(%rsp), %rdx
addq %rbx, 0x8(%rbp)
cmpq %rdx, %r13
jb 0x2d68f
jmp 0x2d6a2
movq (%r13), %rsi
movq %rbp, %rdi
callq 0xcfdb
testl %eax, %eax
js 0x2d15c
movq 0x38(%r12), %rdi
movq 0x10(%r12), %rdx
movq 0x20(%r12), %rsi
callq 0xd005
cmpq 0x10(%r12), %rax
jmp 0x2d6aa
xorl %r13d, %r13d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0xf300
movq %rax, %r13
cmpq (%r15), %r13
jne 0x2d823
movl %r13d, %ebp
jmp 0x2d823
movq %rbx, %rdi
callq 0x4340
movl $0x1, %edi
movl $0x30, %esi
callq 0x4300
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x2d823
movq %rax, %r15
movl $0x3e8, %edi            # imm = 0x3E8
movl $0x50, %esi
callq 0x4300
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x2d81b
movabsq $0x3e800000000, %rax    # imm = 0x3E800000000
movq %rax, 0x18(%r15)
movq $0x0, 0x20(%r15)
movq %r13, 0x28(%r15)
movl $0x0, 0x70(%r13)
movq %r15, 0x68(%r13)
jmp 0x2d456
xorl %ebx, %ebx
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rcx
callq 0xf300
movq %rax, %rbx
movq (%rsp), %r13
cmpq (%r13), %rbx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jne 0x2d823
movq 0x68(%r12), %r13
testq %r13, %r13
je 0x2d769
cmpl $0x2, 0x54(%r12)
jne 0x2d71d
movq 0x38(%r12), %rbx
movl 0x8(%r14), %ebp
movq (%r14), %r12
movq %r14, %rdi
callq 0x23104
movq 0x18(%rbx), %rcx
shlq $0x10, %rcx
movzwl 0x10(%rbx), %r9d
orq %rcx, %r9
movzwl 0x10(%r14), %ecx
xorl %r10d, %r10d
testb $0x4, %cl
sete %r10b
subq $0x8, %rsp
movq %rbx, %rdi
movq %r13, %rsi
movl %ebp, %edx
movq %r12, %rcx
movq %rax, %r8
pushq %r10
callq 0xa408
addq $0x10, %rsp
jmp 0x2d760
movl 0x8(%r14), %ebx
movq (%r14), %rbp
movq %r14, %rdi
callq 0x23104
movq 0x38(%r12), %rcx
movq 0x18(%rcx), %rdx
shlq $0x10, %rdx
movzwl 0x10(%rcx), %r8d
orq %rdx, %r8
movzwl 0x10(%r14), %ecx
xorl %r9d, %r9d
testb $0x4, %cl
sete %r9b
movq %r13, %rdi
movl %ebx, %esi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x1699a
testl %eax, %eax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
js 0x2d774
movq (%rsp), %rax
movl (%rax), %ebp
jmp 0x2d823
movq 0x38(%r14), %rbx
movl 0x8(%r14), %esi
movq %r15, %rdi
callq 0x8943c
movq %rax, %r12
movl 0x8(%r14), %esi
movq %r15, %rdi
callq 0x8947e
movzwl 0x10(%r14), %r10d
movq (%r14), %r11
incq %r11
leaq 0x6a4d8(%rip), %rsi     # 0x97c7d
leaq 0x6a519(%rip), %rdx     # 0x97cc5
movl $0x1, %edi
movq %rbx, %rcx
movq %r12, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x13de5
addq $0x10, %rsp
jmp 0x2d823
movq 0x8(%r13), %rdi
movq 0x40(%r13), %rsi
subq $0x8, %rsp
leaq -0x47da(%rip), %rdx     # 0x29004
leaq 0x62c(%rip), %r8        # 0x2de11
leaq 0x62a(%rip), %r9        # 0x2de16
movq %r15, %rcx
pushq $0x0
callq 0x36b0d
addq $0x10, %rsp
movl %eax, %ebp
movq %rbx, %rdi
callq 0x4340
testl %ebp, %ebp
js 0x2d15c
movq $0x0, 0x68(%r13)
movl $0x1, %ebp
jmp 0x2d823
movq %r15, %rdi
callq 0x45c0
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq