// File: /workspace/github_c_makefile_star2_O1/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 0x23429
movq %rsi, %r15
movq %rdi, %r14
movq 0x38(%rsi), %rdi
testq %rdi, %rdi
je 0x231c5
leaq 0x10(%rsp), %r13
movq %r13, %rsi
callq 0x87816
testl %eax, %eax
jne 0x23429
movq 0x10(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
je 0x23234
leaq 0x77515(%rip), %rsi     # 0x9a6c2
leaq 0x7751c(%rip), %rdx     # 0x9a6d0
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x2341f
movq 0x8(%r15), %rax
movq %rax, %rcx
shrq $0x20, %rcx
je 0x231f1
leaq 0x774e9(%rip), %rsi     # 0x9a6c2
leaq 0x774f0(%rip), %rdx     # 0x9a6d0
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x23429
shrq $0x1f, %rax
je 0x2322a
leaq 0x774c4(%rip), %r12     # 0x9a6c2
leaq 0x774ea(%rip), %rdx     # 0x9a6ef
movl $0x3, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x14378
leaq 0x77501(%rip), %rdx     # 0x9a71c
movl $0x3, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x14378
leaq 0x8(%r15), %r13
leaq 0x28(%r15), %rax
jmp 0x23272
shrq $0x1f, %rax
je 0x2326d
leaq 0x77481(%rip), %r12     # 0x9a6c2
leaq 0x774a7(%rip), %rdx     # 0x9a6ef
movl $0x3, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x14378
leaq 0x774be(%rip), %rdx     # 0x9a71c
movl $0x3, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x14378
leaq 0x20(%rsp), %rax
movq (%rax), %r12
movl (%r13), %eax
movl %eax, 0x8(%rsp)
leaq 0x773e2(%rip), %rsi     # 0x9a666
movl $0x4, %edx
movq %r14, %rdi
callq 0xd0bb
testq %rax, %rax
js 0x2341f
testb $0x8, 0x2(%r14)
jne 0x232e6
leaq 0x8(%rsp), %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0xd0bb
testq %rax, %rax
js 0x2341f
movl 0x8(%rsp), %edx
testq %rdx, %rdx
je 0x232d9
movq %r14, %rdi
movq %r12, %rsi
callq 0xd0bb
testq %rax, %rax
js 0x2341f
movl $0x4, %edx
movq %r14, %rdi
movq %r15, %rsi
jmp 0x2333a
movl 0x8(%rsp), %eax
bswapl %eax
leaq 0x4(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movq %r14, %rdi
callq 0xd0bb
testq %rax, %rax
js 0x2341f
movl 0x8(%rsp), %edx
testq %rdx, %rdx
je 0x23326
movq %r14, %rdi
movq %r12, %rsi
callq 0xd0bb
testq %rax, %rax
js 0x2341f
movl (%r15), %eax
bswapl %eax
leaq 0x4(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movq %r14, %rdi
callq 0xd0bb
testq %rax, %rax
js 0x2341f
movq 0x20(%rsp), %rdi
callq 0x45c0
cmpl $0x0, (%r15)
je 0x23410
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 0x2338e
movl $0x4, %edx
movq %r14, %rdi
leaq 0xc(%rsp), %rsi
jmp 0x233a1
bswapl %eax
movl %eax, 0x4(%rsp)
movl $0x4, %edx
movq %r14, %rdi
leaq 0x4(%rsp), %rsi
callq 0xd0bb
testq %rax, %rax
js 0x23429
movslq 0xc(%rsp), %rdx
movq %r14, %rdi
movq %rbp, %rsi
callq 0xd0bb
testq %rax, %rax
js 0x23429
testb $0x8, 0x2(%r14)
jne 0x233d8
movq 0x10(%r15), %rsi
addq %r12, %rsi
movl $0x4, %edx
movq %r14, %rdi
jmp 0x233f3
movq 0x10(%r15), %rax
movl (%rax,%r12), %eax
bswapl %eax
movl %eax, 0x4(%rsp)
movl $0x4, %edx
movq %r14, %rdi
leaq 0x4(%rsp), %rsi
callq 0xd0bb
testq %rax, %rax
js 0x23429
incq %r13
movl (%r15), %eax
addq $0x4, %r12
cmpq %rax, %r13
jne 0x23362
movq %r14, %rdi
callq 0xcc29
movl %eax, %ebx
sarl $0x1f, %ebx
jmp 0x23429
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