// File: /workspace/github_c_makefile_star2_O2/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), %eax
leal -0x1(%rax), %ecx
cmpl $0x10, %ecx
ja 0x24e0e
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r12
leaq 0x5453e(%rip), %rdx     # 0x792d4
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq 0x40(%r12), %rdi
testq %rdi, %rdi
jne 0x24dce
xorl %ecx, %ecx
cmpl $0x11, %eax
sete %cl
leal 0x3e(,%rcx,2), %edi
callq 0x233be
movq %rax, %rdi
movq %rax, 0x40(%r12)
testq %rax, %rax
je 0x25159
leaq 0x10(%r12), %r15
movq %rbx, %rsi
movq %r15, %rdx
callq 0x24064
pushq $-0x1
popq %r14
testl %eax, %eax
js 0x2515d
testb $0x10, (%r12)
movq 0x38(%r12), %rdi
jne 0x25023
movq 0x10(%r12), %rdx
movq 0x20(%r12), %rsi
callq 0x213c7
jmp 0x25047
callq 0x4050
movl $0x9, (%rax)
pushq $-0x1
jmp 0x2515b
movabsq $0x400000001, %rax      # imm = 0x400000001
movq %rax, 0x48(%r12)
movq 0x38(%r12), %r13
cmpq $0x0, 0x68(%r12)
je 0x24fbc
movl 0x40(%rbx), %esi
movzbl 0xf(%rbx), %eax
subl %eax, %esi
addl $0x24, %esi
movq %r13, %rdi
callq 0xba3a
pushq $-0x1
popq %rbp
testl %eax, %eax
js 0x24fb4
cmpq $0x0, 0x48(%r13)
jne 0x24e80
movq 0x68(%r12), %rdi
movq 0x18(%r13), %rax
shlq $0x10, %rax
movzwl 0x10(%r13), %esi
orq %rax, %rsi
callq 0x13dfb
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1eac5
testl %eax, %eax
js 0x24fb4
movl %eax, %r14d
movq 0x68(%r12), %r12
movl 0x8(%rbx), %ebp
movq (%rbx), %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x1e518
movq 0x18(%r13), %rcx
shlq $0x10, %rcx
movzwl 0x10(%r13), %r9d
orq %rcx, %r9
movzwl 0x10(%rbx), %ecx
xorl %r10d, %r10d
testb $0x4, %cl
sete %r10b
subq $0x8, %rsp
movq %r13, %rdi
movq %r12, %rsi
movl %ebp, %edx
movq 0x10(%rsp), %rcx
movq %rax, %r8
pushq %r10
callq 0x9454
popq %rcx
popq %rdx
testl %eax, %eax
jns 0x2515d
movq 0x38(%rbx), %r14
movl 0x8(%rbx), %esi
movq %r15, %rdi
callq 0x6cf95
movq %rax, %r12
movl 0x8(%rbx), %esi
movq %r15, %rdi
callq 0x6cfd3
movzwl 0x10(%rbx), %r10d
movq (%rbx), %r11
incq %r11
leaq 0x55839(%rip), %rsi     # 0x7a759
leaq 0x54cf9(%rip), %rdx     # 0x79c20
pushq $0x1
popq %rdi
movq %r14, %rcx
movq %r12, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x110c6
addq $0x10, %rsp
pushq $-0x1
popq %rax
movl %eax, %r14d
jmp 0x2515d
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, 0x48(%r12)
movq 0x40(%r12), %r13
testq %r13, %r13
je 0x24fd5
movq (%r13), %rax
testq %rax, %rax
je 0x2505a
cmpq %r15, %rax
je 0x25090
leaq 0x54c55(%rip), %rsi     # 0x79bd8
leaq 0x54c59(%rip), %rdx     # 0x79be3
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
jmp 0x25159
movq 0x38(%r12), %rdi
movq %rbx, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3b076
movl %ebp, %r14d
jmp 0x2515d
movq %r13, %rdi
movq %rbx, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1eac5
leaq 0x10(%r12), %r13
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x23c7e
pushq $-0x1
popq %r14
testl %eax, %eax
js 0x2515d
pushq $0xa
popq %rdi
movq %r13, %rsi
callq 0x21551
testb $0x10, (%r12)
movq 0x38(%r12), %rdi
jne 0x2516f
movq 0x10(%r12), %rdx
movq 0x20(%r12), %rsi
callq 0x213c7
jmp 0x25190
movq (%r15), %rsi
callq 0xba3a
testl %eax, %eax
js 0x2515d
movq 0x38(%r12), %rdi
movq 0x10(%r12), %rdx
movq 0x20(%r12), %rsi
callq 0xba64
cmpq 0x10(%r12), %rax
jne 0x2515d
movq %rax, %r14
jmp 0x2515d
movq %r15, (%r13)
incl 0x40(%r15)
leaq 0x48(%r13), %rdi
leaq 0x304(%rip), %rdx       # 0x25371
xorl %esi, %esi
movq %r12, %rcx
callq 0x4920
testl %eax, %eax
jne 0x25159
movl $0x1, 0x50(%r13)
movq (%r13), %rax
jmp 0x24f73
movq 0x68(%r13), %r15
testq %r15, %r15
jne 0x250d3
leaq 0x18(%r13), %r14
movq %r14, %rdi
callq 0x4750
movq 0x60(%r13), %r15
testq %r15, %r15
je 0x25265
movq %r15, 0x68(%r13)
movq (%r15), %rax
movq %rax, 0x60(%r13)
andq $0x0, (%r15)
andl $0x0, 0x18(%r15)
andq $0x0, 0x20(%r15)
movq %r14, %rdi
callq 0x4340
movslq 0x18(%r15), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x18(%r15)
imulq $0x50, %rax, %rdi
addq 0x10(%r15), %rdi
movq %rbx, %rsi
callq 0x1dfcb
testq %rax, %rax
je 0x25159
movslq 0x40(%rbx), %rax
movq 0x20(%r15), %rcx
addq %rcx, %rax
addq $0x50, %rax
movq %rax, 0x20(%r15)
cmpl $0x3e8, 0x18(%r15)      # imm = 0x3E8
pushq $0x1
popq %r14
je 0x2511c
cmpq $0x2ee01, %rax          # imm = 0x2EE01
jb 0x2515d
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 0x2531b
movq %rbx, %rdi
callq 0x4340
xorl %r14d, %r14d
subl 0xd4(%r13), %r14d
jmp 0x2515d
pushq $-0x2
popq %r14
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r13), %rsi
callq 0xba3a
testl %eax, %eax
js 0x2515d
movq 0x38(%r12), %rdi
movq 0x10(%r12), %rdx
movq 0x20(%r12), %rsi
callq 0xba64
cmpq 0x10(%r12), %rax
jne 0x2515d
movq 0x68(%r12), %rbp
testq %rbp, %rbp
je 0x2525c
cmpl $0x2, 0x54(%r12)
jne 0x2520a
movq 0x38(%r12), %r12
movl 0x8(%rbx), %eax
movl %eax, 0x8(%rsp)
movq (%rbx), %rax
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
callq 0x1e518
movq 0x18(%r12), %rcx
shlq $0x10, %rcx
movzwl 0x10(%r12), %r9d
orq %rcx, %r9
movzwl 0x10(%rbx), %ecx
xorl %r10d, %r10d
testb $0x4, %cl
sete %r10b
subq $0x8, %rsp
movq %r12, %rdi
movq %rbp, %rsi
movl 0x10(%rsp), %edx
movq 0x18(%rsp), %rcx
movq %rax, %r8
pushq %r10
callq 0x9454
popq %rcx
popq %rdx
jmp 0x25258
movl 0x8(%rbx), %eax
movl %eax, 0x8(%rsp)
movq (%rbx), %rax
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
callq 0x1e518
movq 0x38(%r12), %rcx
movq 0x18(%rcx), %rdx
shlq $0x10, %rdx
movzwl 0x10(%rcx), %r8d
orq %rdx, %r8
movzwl 0x10(%rbx), %ecx
xorl %r9d, %r9d
testb $0x4, %cl
sete %r9b
movq %rbp, %rdi
movl 0x8(%rsp), %esi
movq 0x10(%rsp), %rdx
movq %rax, %rcx
callq 0x1390f
testl %eax, %eax
js 0x252c7
movl (%r13), %r14d
jmp 0x2515d
movq %r14, %rdi
callq 0x4340
pushq $0x1
popq %rdi
pushq $0x30
popq %rsi
callq 0x4300
pushq $-0x1
popq %r14
testq %rax, %rax
je 0x2515d
movq %rax, %r15
pushq $0x50
popq %rsi
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x4300
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x25364
movabsq $0x3e800000000, %rax    # imm = 0x3E800000000
movq %rax, 0x18(%r15)
andq $0x0, 0x20(%r15)
movq %r13, 0x28(%r15)
andl $0x0, 0x70(%r13)
movq %r15, 0x68(%r13)
jmp 0x250d3
movq 0x38(%rbx), %r12
movl 0x8(%rbx), %esi
movq %r15, %rdi
callq 0x6cf95
movq %rax, %r13
movl 0x8(%rbx), %esi
movq %r15, %rdi
callq 0x6cfd3
movzwl 0x10(%rbx), %r10d
movq (%rbx), %r11
incq %r11
leaq 0x548e2(%rip), %rsi     # 0x79bd8
leaq 0x54923(%rip), %rdx     # 0x79c20
pushq $0x1
popq %rdi
movq %r12, %rcx
movq %r13, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x110c6
popq %rax
popq %rcx
jmp 0x2515d
movq 0x8(%r13), %rdi
movq 0x40(%r13), %rsi
subq $0x8, %rsp
leaq -0x269d(%rip), %rdx     # 0x22c91
leaq 0x48e(%rip), %r8        # 0x257c3
leaq 0x48c(%rip), %r9        # 0x257c8
movq %r15, %rcx
pushq $0x0
callq 0x2c8b1
popq %rcx
popq %rdx
movl %eax, %ebp
movq %rbx, %rdi
callq 0x4340
testl %ebp, %ebp
js 0x24e19
andq $0x0, 0x68(%r13)
jmp 0x2515d
movq %r15, %rdi
callq 0x45c0
jmp 0x2515d