// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
pushq $-0x1
popq %rbx
testq %rsi, %rsi
je 0x1de77
movq %rsi, %r15
movq %rdi, %r14
movq 0x38(%rsi), %rdi
testq %rdi, %rdi
je 0x1dc4f
leaq 0x10(%rsp), %r13
movq %r13, %rsi
callq 0x6a067
testl %eax, %eax
jne 0x1de77
movq 0x10(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
je 0x1dcb9
leaq 0x5b9e9(%rip), %rsi     # 0x79622
leaq 0x5b9f0(%rip), %rdx     # 0x79630
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
jmp 0x1de6d
movq 0x8(%r15), %rax
movq %rax, %rcx
shrq $0x20, %rcx
je 0x1dc79
leaq 0x5b9bf(%rip), %rsi     # 0x79622
leaq 0x5b9c6(%rip), %rdx     # 0x79630
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
jmp 0x1de77
shrq $0x1f, %rax
je 0x1dcaf
leaq 0x5b99c(%rip), %r12     # 0x79622
leaq 0x5b9c2(%rip), %rdx     # 0x7964f
pushq $0x3
popq %rbp
movl %ebp, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x110c6
leaq 0x5b9d9(%rip), %rdx     # 0x7967c
movl %ebp, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x110c6
leaq 0x8(%r15), %r13
leaq 0x28(%r15), %rax
jmp 0x1dcf4
shrq $0x1f, %rax
je 0x1dcef
leaq 0x5b95c(%rip), %r12     # 0x79622
leaq 0x5b982(%rip), %rdx     # 0x7964f
pushq $0x3
popq %rbp
movl %ebp, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x110c6
leaq 0x5b999(%rip), %rdx     # 0x7967c
movl %ebp, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x110c6
leaq 0x20(%rsp), %rax
movq (%rax), %r12
movq (%r13), %r13
movl %r13d, 0x8(%rsp)
leaq 0x5b8bf(%rip), %rsi     # 0x795c6
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0xba64
testq %rax, %rax
js 0x1de6d
testb $0x8, 0x2(%r14)
jne 0x1dd63
leaq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0xba64
testq %rax, %rax
js 0x1de6d
movl 0x8(%rsp), %edx
testq %rdx, %rdx
je 0x1dd58
movq %r14, %rdi
movq %r12, %rsi
callq 0xba64
testq %rax, %rax
js 0x1de6d
pushq $0x4
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
jmp 0x1ddb1
movl %r13d, %eax
bswapl %eax
leaq 0x4(%rsp), %rsi
movl %eax, (%rsi)
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0xba64
testq %rax, %rax
js 0x1de6d
testl %r13d, %r13d
je 0x1dd9f
movl %r13d, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0xba64
testq %rax, %rax
js 0x1de6d
movl (%r15), %eax
bswapl %eax
leaq 0x4(%rsp), %rsi
movl %eax, (%rsi)
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0xba64
testq %rax, %rax
js 0x1de6d
movq 0x20(%rsp), %rdi
callq 0x45c0
xorl %ebp, %ebp
xorl %r13d, %r13d
movl (%r15), %eax
cmpq %rax, %r13
je 0x1de88
movq 0x20(%r15), %rax
movq (%rax,%rbp,2), %r12
movq %r12, %rdi
callq 0x41d0
incl %eax
movl %eax, 0xc(%rsp)
testb $0x8, 0x2(%r14)
jne 0x1de01
movq %r14, %rdi
leaq 0xc(%rsp), %rsi
jmp 0x1de0f
bswapl %eax
movl %eax, 0x4(%rsp)
movq %r14, %rdi
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0xba64
testq %rax, %rax
js 0x1de77
movslq 0xc(%rsp), %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0xba64
testq %rax, %rax
js 0x1de77
movq 0x10(%r15), %rsi
addq %rbp, %rsi
testb $0x8, 0x2(%r14)
jne 0x1de44
movq %r14, %rdi
jmp 0x1de54
movl (%rsi), %eax
bswapl %eax
movl %eax, 0x4(%rsp)
movq %r14, %rdi
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0xba64
testq %rax, %rax
js 0x1de77
incq %r13
addq $0x4, %rbp
jmp 0x1ddce
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
movq %r14, %rdi
callq 0xb609
movl %eax, %ebx
sarl $0x1f, %ebx
jmp 0x1de77