// File: /workspace/github_c_makefile_star2_O1/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 0x2dc56
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0x6c6cc(%rip), %rcx     # 0x9a2d8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpq $0x0, 0x40(%r12)
jne 0x2e07f
movl $0x1, %edi
movl $0x80, %esi
callq 0x4300
testq %rax, %rax
je 0x2e06f
cmpl $0x11, %ebx
sete %cl
movb $0x0, 0x6e(%rax)
movw $0x4342, 0x6c(%rax)     # imm = 0x4342
addb %cl, %cl
addb $0x3e, %cl
movb %cl, 0x78(%rax)
jmp 0x2e071
callq 0x4050
movl $0x9, (%rax)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x2e3dd
movabsq $0x400000001, %rax      # imm = 0x400000001
movq %rax, 0x48(%r12)
movq 0x38(%r12), %r13
cmpq $0x0, 0x68(%r12)
je 0x2de08
movl 0x40(%r14), %esi
movzbl 0xf(%r14), %eax
subl %eax, %esi
addl $0x24, %esi
movq %r13, %rdi
callq 0xd091
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x2e3dd
cmpq $0x0, 0x48(%r13)
jne 0x2dccf
movq 0x68(%r12), %rdi
movq 0x18(%r13), %rax
shlq $0x10, %rax
movzwl 0x10(%r13), %esi
orq %rax, %rsi
callq 0x173e3
movq %r13, %rdi
movq %r14, %rsi
callq 0x24209
testl %eax, %eax
js 0x2e3dd
movl %eax, %ebp
movq 0x68(%r12), %rbx
movl 0x8(%r14), %r12d
movq (%r14), %rax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x23b0a
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 %rbx, %rsi
movl %r12d, %edx
movq 0x10(%rsp), %rcx
movq %rax, %r8
pushq %r10
callq 0xa354
addq $0x10, %rsp
movl %ebp, %ebx
testl %eax, %eax
jns 0x2e3dd
movq 0x38(%r14), %rbx
movl 0x8(%r14), %esi
movq %r15, %rdi
callq 0x8bd81
movq %rax, %r12
movl 0x8(%r14), %esi
movq %r15, %rdi
callq 0x8bdc3
movzwl 0x10(%r14), %r10d
movq (%r14), %r11
incq %r11
leaq 0x6da74(%rip), %rsi     # 0x9b7eb
leaq 0x6cf34(%rip), %rdx     # 0x9acb2
movl $0x1, %edi
movq %rbx, %rcx
movq %r12, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x14378
addq $0x10, %rsp
jmp 0x2dc61
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, 0x48(%r12)
movq 0x40(%r12), %r13
testq %r13, %r13
je 0x2de21
cmpq $0x0, (%r13)
je 0x2df5d
cmpq %r15, (%r13)
je 0x2df94
leaq 0x6ce95(%rip), %rsi     # 0x9ac6a
leaq 0x6ce99(%rip), %rdx     # 0x9ac75
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x2e134
movq 0x38(%r12), %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x49e6a
movq %r13, %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x24209
leaq 0x10(%r12), %r13
movq $0x0, 0x10(%r12)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2ae99
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x2e3dd
movq %r13, 0x8(%rsp)
movq 0x10(%r12), %rax
addq $0x2, %rax
cmpq %rax, 0x18(%r12)
jae 0x2de95
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %rdx
shrq %rdx
xorl %r13d, %r13d
testq %rcx, %rcx
cmoveq %rdx, %r13
addq %rax, %r13
movq 0x20(%r12), %rdi
movq %r13, %rsi
callq 0x4840
testq %rax, %rax
je 0x2deba
movq %rax, 0x20(%r12)
movq %r13, 0x18(%r12)
movq 0x10(%r12), %rax
movq 0x20(%r12), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x10(%r12)
movb $0xa, (%rcx,%rax)
movq 0x10(%r12), %rax
movq 0x20(%r12), %rcx
movb $0x0, (%rcx,%rax)
testb $0x10, (%r12)
movq 0x38(%r12), %rbp
jne 0x2e171
movq 0x10(%r12), %r13
movq 0x20(%r12), %rcx
testb $0x2, 0x30(%rbp)
jne 0x2df07
movq 0x18(%rbp), %rsi
movq %rsi, %rax
subq 0x8(%rbp), %rax
cmpq %r13, %rax
jae 0x2df07
addq %r13, %rsi
subq (%rbp), %rsi
movq %rbp, %rdi
movq %rcx, %rbx
callq 0xefdd
movq %rbx, %rcx
movq 0x18(%rbp), %rax
movq %rax, 0x10(%rbp)
movq 0x8(%rbp), %rdi
movq 0x18(%rbp), %rbx
subq %rdi, %rbx
cmpq %r13, %rbx
ja 0x2df21
cmpq (%rbp), %rdi
je 0x2e245
cmpq %r13, %rbx
movq %r13, (%rsp)
cmovbq %rbx, %r13
movq %rcx, %rsi
movq %r13, %rdx
movq %rcx, 0x10(%rsp)
callq 0x43c0
movq (%rsp), %rdx
addq %r13, 0x8(%rbp)
cmpq %rdx, %rbx
jae 0x2e258
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rcx
jmp 0x2e250
movq %r15, (%r13)
incl 0x40(%r15)
leaq 0x48(%r13), %rdi
leaq 0x47e(%rip), %rdx       # 0x2e3ee
xorl %esi, %esi
movq %r12, %rcx
callq 0x4920
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
testl %eax, %eax
jne 0x2e3dd
movl $0x1, 0x50(%r13)
jmp 0x2ddc4
movq 0x68(%r13), %r15
testq %r15, %r15
jne 0x2dfda
leaq 0x18(%r13), %rbx
movq %rbx, %rdi
callq 0x4750
movq 0x60(%r13), %r15
testq %r15, %r15
je 0x2e1d6
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 0x2357d
testq %rax, %rax
je 0x2e134
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, %ebx
je 0x2e030
cmpq $0x2ee01, %rax          # imm = 0x2EE01
jb 0x2e3dd
movl 0x74(%r13), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x74(%r13)
movl %eax, 0x8(%r15)
leaq 0x78(%r13), %r14
movq %r14, %rdi
callq 0x4750
cmpl $0x0, 0xd4(%r13)
je 0x2e38a
movq %r14, %rdi
callq 0x4340
xorl %ebx, %ebx
subl 0xd4(%r13), %ebx
jmp 0x2e3dd
xorl %eax, %eax
movq %rax, 0x40(%r12)
testq %rax, %rax
je 0x2e134
movq 0x40(%r12), %rdi
leaq 0x10(%r12), %r15
movq %r14, %rsi
movq %r15, %rdx
callq 0x2be5e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x2e3dd
testb $0x10, (%r12)
movq 0x38(%r12), %r14
jne 0x2e13e
movq 0x10(%r12), %r13
movq 0x20(%r12), %r12
testb $0x2, 0x30(%r14)
jne 0x2e0e8
movq 0x18(%r14), %rsi
movq %rsi, %rax
subq 0x8(%r14), %rax
cmpq %r13, %rax
jae 0x2e0e8
addq %r13, %rsi
subq (%r14), %rsi
movq %r14, %rdi
callq 0xefdd
movq 0x18(%r14), %rax
movq %rax, 0x10(%r14)
movq 0x8(%r14), %rdi
movq 0x18(%r14), %rbx
subq %rdi, %rbx
cmpq %r13, %rbx
ja 0x2e101
cmpq (%r14), %rdi
je 0x2e1ad
cmpq %r13, %rbx
movq %r13, %rbp
cmovbq %rbx, %rbp
movq %r12, %rsi
movq %rbp, %rdx
callq 0x43c0
addq %rbp, 0x8(%r14)
cmpq %r13, %rbx
jae 0x2e1c0
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
jmp 0x2e1b8
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
jmp 0x2e3dd
movq (%r15), %rsi
movq %r14, %rdi
callq 0xd091
testl %eax, %eax
js 0x2e3dd
movq 0x38(%r12), %rdi
movq 0x10(%r12), %rdx
movq 0x20(%r12), %rsi
callq 0xd0bb
cmpq 0x10(%r12), %rax
je 0x2e1ce
jmp 0x2e3dd
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq %rbp, %rdi
callq 0xd091
testl %eax, %eax
js 0x2e3dd
movq 0x38(%r12), %rdi
movq 0x10(%r12), %rdx
movq 0x20(%r12), %rsi
callq 0xd0bb
cmpq 0x10(%r12), %rax
je 0x2e26b
jmp 0x2e3dd
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0xf4c6
movq %rax, %rbp
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq (%r15), %rbp
jne 0x2e3dd
movl (%r15), %ebx
jmp 0x2e3dd
movq %rbx, %rdi
callq 0x4340
movl $0x1, %edi
movl $0x30, %esi
callq 0x4300
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x2e3dd
movq %rax, %r15
movl $0x3e8, %edi            # imm = 0x3E8
movl $0x50, %esi
callq 0x4300
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x2e3d5
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 0x2dfda
movq %rbp, %rdi
movq %rcx, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0xf4c6
movq %rax, %r13
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rax
cmpq (%rax), %r13
jne 0x2e3dd
movq 0x68(%r12), %r13
testq %r13, %r13
je 0x2e327
cmpl $0x2, 0x54(%r12)
jne 0x2e2db
movq 0x38(%r12), %r12
movl 0x8(%r14), %ebp
movq (%r14), %rax
movq %rax, (%rsp)
movq %r14, %rdi
callq 0x23b0a
movq 0x18(%r12), %rcx
shlq $0x10, %rcx
movzwl 0x10(%r12), %r9d
orq %rcx, %r9
movzwl 0x10(%r14), %ecx
xorl %r10d, %r10d
testb $0x4, %cl
sete %r10b
subq $0x8, %rsp
movq %r12, %rdi
movq %r13, %rsi
movl %ebp, %edx
movq 0x8(%rsp), %rcx
movq %rax, %r8
pushq %r10
callq 0xa354
addq $0x10, %rsp
jmp 0x2e323
movl 0x8(%r14), %ebp
movq (%r14), %rax
movq %rax, (%rsp)
movq %r14, %rdi
callq 0x23b0a
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 %ebp, %esi
movq (%rsp), %rdx
movq %rax, %rcx
callq 0x16eed
testl %eax, %eax
js 0x2e333
movq 0x8(%rsp), %rax
movl (%rax), %ebx
jmp 0x2e3dd
movq 0x38(%r14), %r12
movl 0x8(%r14), %esi
movq %r15, %rdi
callq 0x8bd81
movq %rax, %r13
movl 0x8(%r14), %esi
movq %r15, %rdi
callq 0x8bdc3
movzwl 0x10(%r14), %r10d
movq (%r14), %r11
incq %r11
leaq 0x6c906(%rip), %rsi     # 0x9ac6a
leaq 0x6c947(%rip), %rdx     # 0x9acb2
movl $0x1, %edi
movq %r12, %rcx
movq %r13, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x14378
addq $0x10, %rsp
jmp 0x2e3dd
movq 0x8(%r13), %rdi
movq 0x40(%r13), %rsi
subq $0x8, %rsp
leaq -0x4782(%rip), %rdx     # 0x29c1b
leaq 0x6a4(%rip), %r8        # 0x2ea48
leaq 0x6a2(%rip), %r9        # 0x2ea4d
movq %r15, %rcx
pushq $0x0
callq 0x37f45
addq $0x10, %rsp
movl %eax, %ebp
movq %r14, %rdi
callq 0x4340
testl %ebp, %ebp
js 0x2dc61
movq $0x0, 0x68(%r13)
jmp 0x2e3dd
movq %r15, %rdi
callq 0x45c0
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq