// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/sam.c
// Function: sam_hdr_write
int sam_hdr_write(htsFile *fp, const sam_hdr_t *h)
{
    if (!fp || !h) {
        errno = EINVAL;
        return -1;
    }

    switch (fp->format.format) {
    case binary_format:
        fp->format.category = sequence_data;
        fp->format.format = bam;
        /* fall-through */
    case bam:
        if (bam_hdr_write(fp->fp.bgzf, h) < 0) return -1;
        break;

    case cram: {
        cram_fd *fd = fp->fp.cram;
        if (cram_set_header2(fd, h) < 0) return -1;
        if (fp->fn_aux)
            cram_load_reference(fd, fp->fn_aux);
        if (cram_write_SAM_hdr(fd, fd->header) < 0) return -1;
        }
        break;

    case text_format:
        fp->format.category = sequence_data;
        fp->format.format = sam;
        /* fall-through */
    case sam: {
        if (!h->hrecs && !h->text)
            return 0;
        char *text;
        kstring_t hdr_ks = { 0, 0, NULL };
        size_t l_text;
        ssize_t bytes;
        int r = 0, no_sq = 0;

        if (h->hrecs) {
            if (sam_hrecs_rebuild_text(h->hrecs, &hdr_ks) != 0)
                return -1;
            text = hdr_ks.s;
            l_text = hdr_ks.l;
        } else {
            const char *p = NULL;
            do {
                const char *q = p == NULL ? h->text : p + 4;
                p = strstr(q, "@SQ\t");
            } while (!(p == NULL || p == h->text || *(p - 1) == '\n'));
            no_sq = p == NULL;
            text = h->text;
            l_text = h->l_text;
        }

        if (fp->is_bgzf) {
            bytes = bgzf_write(fp->fp.bgzf, text, l_text);
        } else {
            bytes = hwrite(fp->fp.hfile, text, l_text);
        }
        free(hdr_ks.s);
        if (bytes != l_text)
            return -1;

        if (no_sq) {
            int i;
            for (i = 0; i < h->n_targets; ++i) {
                fp->line.l = 0;
                r |= kputsn("@SQ\tSN:", 7, &fp->line) < 0;
                r |= kputs(h->target_name[i], &fp->line) < 0;
                r |= kputsn("\tLN:", 4, &fp->line) < 0;
                r |= kputw(h->target_len[i], &fp->line) < 0;
                r |= kputc('\n', &fp->line) < 0;
                if (r != 0)
                    return -1;

                if (fp->is_bgzf) {
                    bytes = bgzf_write(fp->fp.bgzf, fp->line.s, fp->line.l);
                } else {
                    bytes = hwrite(fp->fp.hfile, fp->line.s, fp->line.l);
                }
                if (bytes != fp->line.l)
                    return -1;
            }
        }
        if (fp->is_bgzf) {
            if (bgzf_flush(fp->fp.bgzf) != 0) return -1;
        } else {
            if (hflush(fp->fp.hfile) != 0) return -1;
        }
        }
        break;

    case fastq_format:
    case fasta_format:
        // Nothing to output; FASTQ has no file headers.
        break;

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
testq %rdi, %rdi
setne %al
testq %rsi, %rsi
setne %cl
testb %cl, %al
je 0x211af
movq %rdi, %rbx
movl 0x4c(%rdi), %eax
decl %eax
cmpl $0x10, %eax
ja 0x211c2
movq %rsi, %r14
leaq 0x5807e(%rip), %rcx     # 0x79200
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0x400000001, %rax      # imm = 0x400000001
movq %rax, 0x48(%rbx)
movq 0x38(%rbx), %rdi
movq %r14, %rsi
callq 0x1dbd4
testl %eax, %eax
jns 0x213b4
jmp 0x211ba
callq 0x4050
movl $0x16, (%rax)
pushq $-0x1
popq %rbp
jmp 0x213b6
callq 0x4050
movl $0x9, (%rax)
jmp 0x211ba
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, 0x48(%rbx)
movq 0x38(%r14), %rdi
testq %rdi, %rdi
je 0x21256
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r12
movaps %xmm0, (%r12)
andq $0x0, 0x10(%r12)
movq %r12, %rsi
callq 0x6a067
testl %eax, %eax
jne 0x211ba
movb $0x1, %bpl
movq 0x20(%rsp), %r15
jmp 0x212a8
movq 0x38(%rbx), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x40aa3
pushq $-0x1
popq %rbp
testl %eax, %eax
js 0x213b6
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x2123d
movq %r15, %rdi
callq 0x41b73
movq 0x18(%r15), %rsi
movq %r15, %rdi
callq 0x43d90
testl %eax, %eax
jns 0x213b4
jmp 0x213b6
movq 0x28(%r14), %r15
testq %r15, %r15
je 0x213b4
andq $0x0, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorl %eax, %eax
leaq 0x5863a(%rip), %r12     # 0x798b4
leaq 0x4(%rax), %rdi
testq %rax, %rax
cmoveq %r15, %rdi
movq %r12, %rsi
callq 0x40e0
testq %rax, %rax
setne %bpl
cmpq %r15, %rax
je 0x212a4
testq %rax, %rax
je 0x212a4
cmpb $0xa, -0x1(%rax)
jne 0x2127a
leaq 0x8(%r14), %r12
movq (%r12), %r12
testb $0x10, (%rbx)
movq 0x38(%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
jne 0x212c2
callq 0x213c7
jmp 0x212c7
callq 0xba64
movq %rax, %r15
movq 0x20(%rsp), %rdi
callq 0x45c0
cmpq %r12, %r15
jne 0x211ba
testb %bpl, %bpl
je 0x212f9
testb $0x10, (%rbx)
movq 0x38(%rbx), %rdi
jne 0x213a7
callq 0xd64f
jmp 0x213ac
leaq 0x10(%rbx), %r15
xorl %r12d, %r12d
movslq (%r14), %rax
cmpq %rax, %r12
jge 0x212e2
andq $0x0, (%r15)
leaq 0x585a6(%rip), %rdi     # 0x798b9
pushq $0x7
popq %rsi
movq %r15, %rdx
callq 0x21461
movl %eax, 0xc(%rsp)
movq 0x20(%r14), %rax
movq (%rax,%r12,8), %rdi
movq %r15, %rsi
callq 0x214c5
movl %eax, %ebp
leaq 0x58586(%rip), %rdi     # 0x798c1
pushq $0x4
popq %rsi
movq %r15, %rdx
callq 0x21461
movl %eax, %r13d
orl 0xc(%rsp), %r13d
orl %ebp, %r13d
movq 0x10(%r14), %rax
movl (%rax,%r12,4), %edi
movq %r15, %rsi
callq 0x21504
movl %eax, %ebp
orl %r13d, %ebp
pushq $0xa
popq %rdi
movq %r15, %rsi
callq 0x21551
orl %ebp, %eax
js 0x211ba
testb $0x10, (%rbx)
movq 0x38(%rbx), %rdi
movq 0x10(%rbx), %rdx
movq 0x20(%rbx), %rsi
jne 0x21391
callq 0x213c7
jmp 0x21396
callq 0xba64
incq %r12
cmpq (%r15), %rax
je 0x21300
jmp 0x211ba
callq 0xb609
testl %eax, %eax
jne 0x211ba
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq