// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
testq %rdi, %rdi
setne %al
testq %rsi, %rsi
setne %cl
testb %cl, %al
je 0x27600
movq %rdi, %rbx
movl 0x4c(%rdi), %eax
decl %eax
cmpl $0x10, %eax
ja 0x27615
movq %rsi, %r14
leaq 0x72b9d(%rip), %rcx     # 0x9a170
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 0x23143
testl %eax, %eax
jns 0x27bc6
jmp 0x2760b
callq 0x4050
movl $0x16, (%rax)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x27bc8
callq 0x4050
movl $0x9, (%rax)
jmp 0x2760b
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, 0x48(%rbx)
movq 0x38(%r14), %rdi
testq %rdi, %rdi
je 0x276ae
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
leaq 0x20(%rsp), %r15
movq %r15, %rsi
callq 0x87816
testl %eax, %eax
jne 0x2760b
movb $0x1, %r12b
movq 0x30(%rsp), %rdx
jmp 0x27713
movq 0x38(%rbx), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x50ad8
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x27bc8
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x27695
movq %r15, %rdi
callq 0x51ef0
movq 0x18(%r15), %rsi
movq %r15, %rdi
callq 0x547d9
testl %eax, %eax
jns 0x27bc6
jmp 0x27bc8
cmpq $0x0, 0x28(%r14)
je 0x27bc6
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
testq %rdi, %rdi
jne 0x2764a
xorl %eax, %eax
leaq 0x73278(%rip), %r15     # 0x9a954
testq %rax, %rax
je 0x276e7
addq $0x4, %rax
jmp 0x276eb
movq 0x28(%r14), %rax
movq %rax, %rdi
movq %r15, %rsi
callq 0x40e0
testq %rax, %rax
setne %r12b
je 0x2770b
cmpq 0x28(%r14), %rax
je 0x2770b
cmpb $0xa, -0x1(%rax)
jne 0x276dc
movq 0x28(%r14), %rdx
leaq 0x8(%r14), %r15
movq (%r15), %r15
testb $0x10, (%rbx)
movq 0x38(%rbx), %r13
jne 0x277b0
testb $0x2, 0x30(%r13)
jne 0x27757
movq 0x18(%r13), %rsi
movq %rsi, %rax
subq 0x8(%r13), %rax
cmpq %r15, %rax
jae 0x27757
addq %r15, %rsi
subq (%r13), %rsi
movq %r13, %rdi
movq %rdx, %rbp
callq 0xefdd
movq %rbp, %rdx
movq 0x18(%r13), %rax
movq %rax, 0x10(%r13)
movq 0x8(%r13), %rdi
movq %r15, %rcx
movq 0x18(%r13), %r15
subq %rdi, %r15
movq %rcx, %rbp
cmpq %rcx, %r15
ja 0x27773
cmpq (%r13), %rdi
je 0x277c0
movq %rbp, 0x10(%rsp)
cmpq %rbp, %r15
cmovbq %r15, %rbp
movq %rdx, %rsi
movq %rdx, 0x8(%rsp)
movq %rbp, %rdx
callq 0x43c0
addq %rbp, 0x8(%r13)
movq 0x10(%rsp), %rax
cmpq %rax, %r15
movq %rax, %r15
jae 0x277d6
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
movq %rbp, %rcx
jmp 0x277ce
movq %r13, %rdi
movq %rdx, %rsi
movq %r15, %rdx
callq 0xd0bb
jmp 0x277d3
movq %r13, %rdi
movq %rdx, %rsi
movq %rbp, %r15
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0xf4c6
movq %rax, %rbp
movq 0x30(%rsp), %rdi
callq 0x45c0
cmpq %r15, %rbp
jne 0x2760b
testb %r12b, %r12b
jne 0x27ba9
cmpl $0x0, (%r14)
jle 0x27ba9
leaq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
movq $0x0, 0x10(%rbx)
cmpq $0x8, 0x18(%rbx)
ja 0x27839
movq 0x20(%rbx), %rdi
movl $0xd, %esi
callq 0x4840
testq %rax, %rax
je 0x278d7
movq %rax, 0x20(%rbx)
movq $0xd, 0x18(%rbx)
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
movl $0x3a4e5309, 0x3(%rcx,%rax) # imm = 0x3A4E5309
movl $0x9515340, (%rcx,%rax) # imm = 0x9515340
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
leaq 0x7(%rax), %rdx
movq %rdx, 0x10(%rbx)
movb $0x0, 0x7(%rcx,%rax)
movl $0x7, 0x18(%rsp)
movq 0x20(%r14), %rax
movq (%rax,%rbp,8), %r13
testq %r13, %r13
je 0x278b7
movq %r13, %rdi
callq 0x41d0
movq %rax, %r12
movq 0x10(%rsp), %rax
movq (%rax), %rcx
leaq (%r12,%rcx), %rax
addq $0x2, %rax
cmpq %rcx, %rax
jbe 0x278c4
cmpq %rax, 0x18(%rbx)
jae 0x27900
movabsq $0x3fffffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFFFF
cmpq %rcx, %rax
ja 0x278e1
movq %rax, %r15
shrq %r15
jmp 0x278e4
callq 0x4050
movl $0xe, (%rax)
jmp 0x278cf
callq 0x4050
movl $0x4b, (%rax)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x27926
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
jmp 0x2786d
xorl %r15d, %r15d
addq %rax, %r15
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x4840
testq %rax, %rax
je 0x278cf
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%rbx)
movq 0x20(%rbx), %rdi
addq 0x10(%rbx), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x43c0
movq 0x10(%rbx), %rax
addq %r12, %rax
movq %rax, 0x10(%rbx)
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rax
subq $-0x6, %rax
jae 0x2799f
cmpq %rax, 0x18(%rbx)
jae 0x27973
movq %rax, %rcx
shrq %rcx
movabsq $0x3fffffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFFFF
cmpq %rdx, %rax
movl $0x0, %r15d
cmovbeq %rcx, %r15
addq %rax, %r15
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x4840
testq %rax, %rax
je 0x279aa
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%rbx)
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
movl $0x3a4e4c09, (%rcx,%rax) # imm = 0x3A4E4C09
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
leaq 0x4(%rax), %rdx
movq %rdx, 0x10(%rbx)
movb $0x0, 0x4(%rcx,%rax)
movl $0x4, %r13d
jmp 0x279b0
callq 0x4050
movl $0x4b, (%rax)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x10(%r14), %rax
movq %rbp, 0x8(%rsp)
movl (%rax,%rbp,4), %ebp
testl %ebp, %ebp
jns 0x27a21
movq 0x10(%rbx), %rax
addq $0x3, %rax
cmpq %rax, 0x18(%rbx)
jae 0x27a0b
movq %rax, %rcx
shrq %rcx
movabsq $0x3fffffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFFFF
cmpq %rdx, %rax
movl $0x0, %r15d
cmovbeq %rcx, %r15
addq %rax, %r15
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x4840
testq %rax, %rax
je 0x27b9f
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%rbx)
negl %ebp
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x10(%rbx)
movb $0x2d, (%rcx,%rax)
movl %ebp, %edi
movq 0x10(%rsp), %rsi
callq 0x32cef
movl %eax, %ebp
movq 0x10(%rbx), %rax
addq $0x2, %rax
cmpq %rax, 0x18(%rbx)
jae 0x27a7a
movq %rax, %rcx
shrq %rcx
movabsq $0x3fffffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFFFF
cmpq %rdx, %rax
movl $0x0, %r15d
cmovbeq %rcx, %r15
addq %rax, %r15
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x4840
testq %rax, %rax
je 0x27b5e
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%rbx)
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x10(%rbx)
movb $0xa, (%rcx,%rax)
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
movl $0xa, %eax
orl 0x18(%rsp), %r12d
orl %r13d, %r12d
orl %ebp, %r12d
orl %eax, %r12d
movq 0x8(%rsp), %rbp
js 0x2760b
testb $0x10, (%rbx)
movq 0x10(%rbx), %r15
jne 0x27b49
movq 0x20(%rbx), %rcx
movq 0x38(%rbx), %rbp
testb $0x2, 0x30(%rbp)
jne 0x27b00
movq 0x18(%rbp), %rsi
movq %rsi, %rax
subq 0x8(%rbp), %rax
cmpq %r15, %rax
jae 0x27b00
addq %r15, %rsi
subq (%rbp), %rsi
movq %rbp, %rdi
movq %rcx, %r12
callq 0xefdd
movq %r12, %rcx
movq 0x18(%rbp), %rax
movq %rax, 0x10(%rbp)
movq 0x8(%rbp), %rdi
movq 0x18(%rbp), %r13
subq %rdi, %r13
cmpq %r15, %r13
ja 0x27b16
cmpq (%rbp), %rdi
je 0x27b68
cmpq %r15, %r13
movq %r15, %r12
cmovbq %r13, %r12
movq %rcx, %rsi
movq %r12, %rdx
movq %rcx, 0x18(%rsp)
callq 0x43c0
addq %r12, 0x8(%rbp)
cmpq %r15, %r13
jae 0x27b7b
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
jmp 0x27b73
movq 0x20(%rbx), %rsi
movq 0x38(%rbx), %rdi
movq %r15, %rdx
callq 0xd0bb
movq %rax, %r12
jmp 0x27b80
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x27a9f
movq %rbp, %rdi
movq %rcx, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xf4c6
movq %rax, %r12
movq 0x8(%rsp), %rbp
movq 0x10(%rsp), %rax
cmpq (%rax), %r12
jne 0x2760b
incq %rbp
movslq (%r14), %rax
cmpq %rax, %rbp
jl 0x27807
jmp 0x27ba9
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x27a2f
testb $0x10, (%rbx)
movq 0x38(%rbx), %rdi
jne 0x27bb9
callq 0xf3e7
jmp 0x27bbe
callq 0xcc29
testl %eax, %eax
jne 0x2760b
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq