// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/sam.c
// Function: bam_hdr_write
int bam_hdr_write(BGZF *fp, const sam_hdr_t *h)
{
    int32_t i, name_len, x;
    kstring_t hdr_ks = { 0, 0, NULL };
    char *text;
    uint32_t l_text;

    if (!h) return -1;

    if (h->hrecs) {
        if (sam_hrecs_rebuild_text(h->hrecs, &hdr_ks) != 0) return -1;
        if (hdr_ks.l > UINT32_MAX) {
            hts_log_error("Header too long for BAM format");
            free(hdr_ks.s);
            return -1;
        } else if (hdr_ks.l > INT32_MAX) {
            hts_log_warning("Header too long for BAM specification (>2GB)");
            hts_log_warning("Output file may not be portable");
        }
        text = hdr_ks.s;
        l_text = hdr_ks.l;
    } else {
        if (h->l_text > UINT32_MAX) {
            hts_log_error("Header too long for BAM format");
            return -1;
        } else if (h->l_text > INT32_MAX) {
            hts_log_warning("Header too long for BAM specification (>2GB)");
            hts_log_warning("Output file may not be portable");
        }
        text = h->text;
        l_text = h->l_text;
    }
    // write "BAM1"
    if (bgzf_write(fp, "BAM\1", 4) < 0) { free(hdr_ks.s); return -1; }
    // write plain text and the number of reference sequences
    if (fp->is_be) {
        x = ed_swap_4(l_text);
        if (bgzf_write(fp, &x, 4) < 0) { free(hdr_ks.s); return -1; }
        if (l_text) {
            if (bgzf_write(fp, text, l_text) < 0) { free(hdr_ks.s); return -1; }
        }
        x = ed_swap_4(h->n_targets);
        if (bgzf_write(fp, &x, 4) < 0) { free(hdr_ks.s); return -1; }
    } else {
        if (bgzf_write(fp, &l_text, 4) < 0) { free(hdr_ks.s); return -1; }
        if (l_text) {
            if (bgzf_write(fp, text, l_text) < 0) { free(hdr_ks.s); return -1; }
        }
        if (bgzf_write(fp, &h->n_targets, 4) < 0) { free(hdr_ks.s); return -1; }
    }
    free(hdr_ks.s);
    // write sequence names and lengths
    for (i = 0; i != h->n_targets; ++i) {
        char *p = h->target_name[i];
        name_len = strlen(p) + 1;
        if (fp->is_be) {
            x = ed_swap_4(name_len);
            if (bgzf_write(fp, &x, 4) < 0) return -1;
        } else {
            if (bgzf_write(fp, &name_len, 4) < 0) return -1;
        }
        if (bgzf_write(fp, p, name_len) < 0) return -1;
        if (fp->is_be) {
            x = ed_swap_4(h->target_len[i]);
            if (bgzf_write(fp, &x, 4) < 0) return -1;
        } else {
            if (bgzf_write(fp, &h->target_len[i], 4) < 0) return -1;
        }
    }
    if (bgzf_flush(fp) < 0) return -1;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %rsi, %rsi
je 0x22a17
movq %rsi, %r15
movq %rdi, %r14
movq 0x38(%rsi), %rdi
testq %rdi, %rdi
je 0x227ba
leaq 0x10(%rsp), %r13
movq %r13, %rsi
callq 0x85247
testl %eax, %eax
jne 0x22a17
movq 0x10(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
je 0x22829
leaq 0x74f30(%rip), %rsi     # 0x976d2
leaq 0x74f37(%rip), %rdx     # 0x976e0
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
jmp 0x22a0d
movq 0x8(%r15), %rax
movq %rax, %rcx
shrq $0x20, %rcx
je 0x227e6
leaq 0x74f04(%rip), %rsi     # 0x976d2
leaq 0x74f0b(%rip), %rdx     # 0x976e0
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
jmp 0x22a17
shrq $0x1f, %rax
je 0x2281f
leaq 0x74edf(%rip), %r12     # 0x976d2
leaq 0x74f05(%rip), %rdx     # 0x976ff
movl $0x3, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x13de5
leaq 0x74f1c(%rip), %rdx     # 0x9772c
movl $0x3, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x13de5
leaq 0x8(%r15), %r13
leaq 0x28(%r15), %rax
jmp 0x22867
shrq $0x1f, %rax
je 0x22862
leaq 0x74e9c(%rip), %r12     # 0x976d2
leaq 0x74ec2(%rip), %rdx     # 0x976ff
movl $0x3, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x13de5
leaq 0x74ed9(%rip), %rdx     # 0x9772c
movl $0x3, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x13de5
leaq 0x20(%rsp), %rax
movq (%rax), %r12
movq (%r13), %r13
movl %r13d, 0x8(%rsp)
leaq 0x74dfc(%rip), %rsi     # 0x97676
movl $0x4, %edx
movq %r14, %rdi
callq 0xd005
testq %rax, %rax
js 0x22a0d
testb $0x8, 0x2(%r14)
jne 0x228dc
leaq 0x8(%rsp), %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0xd005
testq %rax, %rax
js 0x22a0d
movl 0x8(%rsp), %edx
testq %rdx, %rdx
je 0x228cf
movq %r14, %rdi
movq %r12, %rsi
callq 0xd005
testq %rax, %rax
js 0x22a0d
movl $0x4, %edx
movq %r14, %rdi
movq %r15, %rsi
jmp 0x2292e
movl %r13d, %eax
bswapl %eax
leaq 0x4(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movq %r14, %rdi
callq 0xd005
testq %rax, %rax
js 0x22a0d
testl %r13d, %r13d
je 0x2291a
movl %r13d, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0xd005
testq %rax, %rax
js 0x22a0d
movl (%r15), %eax
bswapl %eax
leaq 0x4(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movq %r14, %rdi
callq 0xd005
testq %rax, %rax
js 0x22a0d
movq 0x20(%rsp), %rdi
callq 0x45c0
cmpl $0x0, (%r15)
je 0x229fe
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x20(%r15), %rax
movq (%rax,%r12,2), %rbp
movq %rbp, %rdi
callq 0x41d0
incl %eax
movl %eax, 0xc(%rsp)
testb $0x8, 0x2(%r14)
jne 0x22982
movl $0x4, %edx
movq %r14, %rdi
leaq 0xc(%rsp), %rsi
jmp 0x22995
bswapl %eax
movl %eax, 0x4(%rsp)
movl $0x4, %edx
movq %r14, %rdi
leaq 0x4(%rsp), %rsi
callq 0xd005
testq %rax, %rax
js 0x22a17
movslq 0xc(%rsp), %rdx
movq %r14, %rdi
movq %rbp, %rsi
callq 0xd005
testq %rax, %rax
js 0x22a17
movq 0x10(%r15), %rsi
addq %r12, %rsi
testb $0x8, 0x2(%r14)
jne 0x229cc
movl $0x4, %edx
movq %r14, %rdi
jmp 0x229e1
movl (%rsi), %eax
bswapl %eax
movl %eax, 0x4(%rsp)
movl $0x4, %edx
movq %r14, %rdi
leaq 0x4(%rsp), %rsi
callq 0xd005
testq %rax, %rax
js 0x22a17
incq %r13
movl (%r15), %eax
addq $0x4, %r12
cmpq %rax, %r13
jne 0x22956
movq %r14, %rdi
callq 0xcb85
movl %eax, %ebx
sarl $0x1f, %ebx
jmp 0x22a17
movq 0x20(%rsp), %rdi
callq 0x45c0
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq