// File: /workspace/github_c_makefile_star2_O1/zhou-lab[P]tbmate/htslib/sam.c
// Function: bam_hdr_write
int bam_hdr_write(BGZF *fp, const bam_hdr_t *h)
{
    char buf[4];
    int32_t i, name_len, x;
    // write "BAM1"
    strncpy(buf, "BAM\1", 4);
    if (bgzf_write(fp, buf, 4) < 0) return -1;
    // write plain text and the number of reference sequences
    if (fp->is_be) {
        x = ed_swap_4(h->l_text);
        if (bgzf_write(fp, &x, 4) < 0) return -1;
        if (h->l_text) {
            if (bgzf_write(fp, h->text, h->l_text) < 0) return -1;
        }
        x = ed_swap_4(h->n_targets);
        if (bgzf_write(fp, &x, 4) < 0) return -1;
    } else {
        if (bgzf_write(fp, &h->l_text, 4) < 0) return -1;
        if (h->l_text) {
            if (bgzf_write(fp, h->text, h->l_text) < 0) return -1;
        }
        if (bgzf_write(fp, &h->n_targets, 4) < 0) return -1;
    }
    // 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 $0x18, %rsp
movq %rsi, %r15
movq %rdi, %r14
leaq 0x14(%rsp), %rsi
movl $0x14d4142, (%rsi)      # imm = 0x14D4142
movl $0x4, %edx
callq 0x28994
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %rax, %rax
js 0x2dda9
leaq 0x8(%r15), %r12
testb $0xc, 0x2(%r14)
je 0x2dc8e
movl (%r12), %eax
bswapl %eax
leaq 0xc(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movq %r14, %rdi
callq 0x28994
testq %rax, %rax
js 0x2dda9
movl (%r12), %edx
testq %rdx, %rdx
je 0x2dc78
movq 0x28(%r15), %rsi
movq %r14, %rdi
callq 0x28994
testq %rax, %rax
js 0x2dda9
movl (%r15), %eax
bswapl %eax
leaq 0xc(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movq %r14, %rdi
jmp 0x2dcd0
movl $0x4, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x28994
testq %rax, %rax
js 0x2dda9
movl (%r12), %edx
testq %rdx, %rdx
je 0x2dcc5
movq 0x28(%r15), %rsi
movq %r14, %rdi
callq 0x28994
testq %rax, %rax
js 0x2dda9
movl $0x4, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x28994
testq %rax, %rax
js 0x2dda9
cmpl $0x0, (%r15)
je 0x2dd9c
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x20(%r15), %rax
movq (%rax,%r12,2), %rbp
movq %rbp, %rdi
callq 0x3380
incl %eax
movl %eax, 0x10(%rsp)
testb $0xc, 0x2(%r14)
je 0x2dd20
bswapl %eax
movl %eax, 0xc(%rsp)
movl $0x4, %edx
movq %r14, %rdi
leaq 0xc(%rsp), %rsi
jmp 0x2dd2d
movl $0x4, %edx
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x28994
testq %rax, %rax
js 0x2dda9
movslq 0x10(%rsp), %rdx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x28994
testq %rax, %rax
js 0x2dda9
testb $0xc, 0x2(%r14)
je 0x2dd70
movq 0x10(%r15), %rax
movl (%rax,%r12), %eax
bswapl %eax
movl %eax, 0xc(%rsp)
movl $0x4, %edx
movq %r14, %rdi
leaq 0xc(%rsp), %rsi
jmp 0x2dd7f
movq 0x10(%r15), %rsi
addq %r12, %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0x28994
testq %rax, %rax
js 0x2dda9
incq %r13
movl (%r15), %eax
addq $0x4, %r12
cmpq %rax, %r13
jne 0x2dcee
movq %r14, %rdi
callq 0x285a0
movl %eax, %ebx
sarl $0x1f, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq