// File: /workspace/github_c_makefile_star2_O3/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 0x26a7c
movq %rdi, %rbx
movl 0x4c(%rdi), %eax
decl %eax
cmpl $0x10, %eax
ja 0x26a91
movq %rsi, %r14
leaq 0x70801(%rip), %rcx     # 0x97250
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 0x22738
testl %eax, %eax
jns 0x2705b
jmp 0x26a87
callq 0x4050
movl $0x16, (%rax)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x2705d
callq 0x4050
movl $0x9, (%rax)
jmp 0x26a87
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, 0x48(%rbx)
movq 0x38(%r14), %rdi
testq %rdi, %rdi
je 0x26b2a
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r12
movaps %xmm0, (%r12)
movq $0x0, 0x10(%r12)
movq %r12, %rsi
callq 0x85247
testl %eax, %eax
jne 0x26a87
movb $0x1, %r13b
movq 0x30(%rsp), %r15
jmp 0x26b7f
movq 0x38(%rbx), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x4e179
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x2705d
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x26b11
movq %r15, %rdi
callq 0x4f438
movq 0x18(%r15), %rsi
movq %r15, %rdi
callq 0x51cb1
testl %eax, %eax
jns 0x2705b
jmp 0x2705d
movq 0x28(%r14), %r15
testq %r15, %r15
je 0x2705b
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
xorl %eax, %eax
leaq 0x70e13(%rip), %r12     # 0x97964
leaq 0x4(%rax), %rdi
testq %rax, %rax
cmoveq %r15, %rdi
movq %r12, %rsi
callq 0x40e0
testq %rax, %rax
setne %r13b
cmpq %r15, %rax
je 0x26b7b
testq %rax, %rax
je 0x26b7b
cmpb $0xa, -0x1(%rax)
jne 0x26b51
leaq 0x8(%r14), %r12
movq (%r12), %r12
testb $0x10, (%rbx)
movq 0x38(%rbx), %rcx
jne 0x26c06
testb $0x2, 0x30(%rcx)
movq 0x18(%rcx), %rbp
jne 0x26bc1
movq %rbp, %rax
subq 0x8(%rcx), %rax
cmpq %r12, %rax
jae 0x26bc1
addq %r12, %rbp
subq (%rcx), %rbp
movq %rcx, %rdi
movq %rbp, %rsi
movq %rcx, %rbp
callq 0xee24
movq %rbp, %rcx
movq 0x18(%rbp), %rbp
movq %rbp, 0x10(%rcx)
movq 0x8(%rcx), %rdi
subq %rdi, %rbp
cmpq %r12, %rbp
ja 0x26bd2
cmpq (%rcx), %rdi
je 0x26c19
cmpq %r12, %rbp
movq %r12, 0x18(%rsp)
cmovbq %rbp, %r12
movq %r15, %rsi
movq %r12, %rdx
movq %rcx, 0x10(%rsp)
callq 0x43c0
movq 0x10(%rsp), %rcx
addq %r12, 0x8(%rcx)
movq 0x18(%rsp), %rax
cmpq %rax, %rbp
movq %rax, %rbp
jb 0x26c1f
jmp 0x26c33
movq %rcx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xd005
movq %r12, %rbp
jmp 0x26c30
movq %r12, %rbp
xorl %r12d, %r12d
movq %rcx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0xf300
movq %rax, %r12
movq 0x30(%rsp), %rdi
callq 0x45c0
cmpq %rbp, %r12
jne 0x26a87
testb %r13b, %r13b
jne 0x2703e
cmpl $0x0, (%r14)
jle 0x2703e
leaq 0x10(%rbx), %r15
xorl %ebp, %ebp
movq %r15, 0x8(%rsp)
movq $0x0, 0x10(%rbx)
movq 0x20(%rbx), %rax
cmpq $0x8, 0x18(%rbx)
ja 0x26c9f
movl $0xd, %esi
movq %rax, %rdi
callq 0x4840
testq %rax, %rax
je 0x26d62
movq %rax, 0x20(%rbx)
movq $0xd, 0x18(%rbx)
movq 0x10(%rbx), %rcx
jmp 0x26ca1
xorl %ecx, %ecx
movl $0x3a4e5309, 0x3(%rax,%rcx) # imm = 0x3A4E5309
movl $0x9515340, (%rax,%rcx) # 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, 0x10(%rsp)
movq 0x20(%r14), %rax
movq (%rax,%rbp,8), %r13
testq %r13, %r13
je 0x26d42
movq %r13, %rdi
callq 0x41d0
movq %rax, %r12
movq (%r15), %rdi
addq %rdi, %rax
addq $0x2, %rax
cmpq %rdi, %rax
jbe 0x26d4f
cmpq %rax, 0x18(%rbx)
jae 0x26d6f
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 0x2702e
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%rbx)
movq 0x10(%rbx), %rdi
movq 0x8(%rsp), %r15
jmp 0x26d73
callq 0x4050
movl $0xe, (%rax)
jmp 0x26d5a
callq 0x4050
movl $0x4b, (%rax)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x26d94
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
jmp 0x26ccd
movq 0x20(%rbx), %rax
addq %rax, %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 (%r15), %rcx
movq %rcx, %rax
subq $-0x6, %rax
jae 0x26dee
cmpq %rax, 0x18(%rbx)
jae 0x26e01
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 0x26fd2
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%rbx)
movq 0x10(%rbx), %rcx
movq 0x8(%rsp), %r15
jmp 0x26e05
callq 0x4050
movl $0x4b, (%rax)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x26e27
movq 0x20(%rbx), %rax
movl $0x3a4e4c09, (%rax,%rcx) # 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
movq 0x10(%r14), %rax
movq %rbp, 0x18(%rsp)
movl (%rax,%rbp,4), %ebp
testl %ebp, %ebp
jns 0x26e9e
movq 0x10(%rbx), %rcx
leaq 0x3(%rcx), %rax
cmpq %rax, 0x18(%rbx)
jae 0x26e8d
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 0x2701f
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%rbx)
movq 0x10(%rbx), %rcx
movq 0x8(%rsp), %r15
jmp 0x26e91
movq 0x20(%rbx), %rax
negl %ebp
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movb $0x2d, (%rax,%rcx)
movl %ebp, %edi
movq %r15, %rsi
callq 0x31b51
movl %eax, %ebp
movq 0x10(%rbx), %rcx
leaq 0x2(%rcx), %rax
cmpq %rax, 0x18(%rbx)
jae 0x26f00
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 0x26fc3
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%rbx)
movq 0x10(%rbx), %rcx
movq 0x8(%rsp), %r15
jmp 0x26f04
movq 0x20(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x10(%rbx)
movb $0xa, (%rax,%rcx)
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
movl $0xa, %eax
orl 0x10(%rsp), %r12d
orl %r13d, %r12d
orl %ebp, %r12d
orl %eax, %r12d
js 0x26a87
testb $0x10, (%rbx)
movq 0x38(%rbx), %r13
movq 0x10(%rbx), %rbp
movq 0x20(%rbx), %rsi
jne 0x26fb6
testb $0x2, 0x30(%r13)
movq 0x18(%r13), %r15
jne 0x26f7d
movq %r15, %rax
subq 0x8(%r13), %rax
cmpq %rbp, %rax
jae 0x26f7d
addq %rbp, %r15
subq (%r13), %r15
movq %r13, %rdi
movq %rsi, %r12
movq %r15, %rsi
callq 0xee24
movq %r12, %rsi
movq 0x18(%r13), %r15
movq %r15, 0x10(%r13)
movq 0x8(%r13), %rdi
subq %rdi, %r15
cmpq %rbp, %r15
movq %rsi, 0x10(%rsp)
ja 0x26f94
cmpq (%r13), %rdi
je 0x26fe2
cmpq %rbp, %r15
movq %rbp, %r12
cmovbq %r15, %r12
movq %r12, %rdx
callq 0x43c0
addq %r12, 0x8(%r13)
cmpq %rbp, %r15
movq 0x8(%rsp), %r15
jb 0x26fea
jmp 0x27000
movq %r13, %rdi
movq %rbp, %rdx
callq 0xd005
jmp 0x26ffd
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %r15
jmp 0x26f21
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %r15
jmp 0x26e27
xorl %r12d, %r12d
movq 0x8(%rsp), %r15
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0xf300
movq %rax, %r12
cmpq (%r15), %r12
jne 0x26a87
movq 0x18(%rsp), %rbp
incq %rbp
movslq (%r14), %rax
cmpq %rax, %rbp
jl 0x26c64
jmp 0x2703e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %r15
jmp 0x26eaa
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %r15
jmp 0x26d94
testb $0x10, (%rbx)
movq 0x38(%rbx), %rdi
jne 0x2704e
callq 0xf22b
jmp 0x27053
callq 0xcb85
testl %eax, %eax
jne 0x26a87
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq