// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4250
cmpq $0x0, -0x18(%rbp)
jne 0x32893
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
movq -0x18(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x32952
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0xd8c10
cmpl $0x0, %eax
je 0x328c4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x40(%rbp)
jbe 0x328fe
movl $0x1, %edi
leaq 0xbf312(%rip), %rsi     # 0xf1bed
leaq 0xbf319(%rip), %rdx     # 0xf1bfb
movb $0x0, %al
callq 0x1b030
movq -0x30(%rbp), %rdi
callq 0x45a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
cmpq $0x7fffffff, -0x40(%rbp) # imm = 0x7FFFFFFF
jbe 0x3293c
movl $0x3, %edi
leaq 0xbf2d9(%rip), %rsi     # 0xf1bed
leaq 0xbf2ff(%rip), %rdx     # 0xf1c1a
movb $0x0, %al
callq 0x1b030
movl $0x3, %edi
leaq 0xbf2bf(%rip), %rsi     # 0xf1bed
leaq 0xbf312(%rip), %rdx     # 0xf1c47
movb $0x0, %al
callq 0x1b030
jmp 0x3293e
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movl %eax, -0x4c(%rbp)
jmp 0x329e2
movq -0x18(%rbp), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, 0x8(%rax)
jbe 0x32987
movl $0x1, %edi
leaq 0xbf280(%rip), %rsi     # 0xf1bed
leaq 0xbf287(%rip), %rdx     # 0xf1bfb
movb $0x0, %al
callq 0x1b030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
movq -0x18(%rbp), %rax
cmpq $0x7fffffff, 0x8(%rax)  # imm = 0x7FFFFFFF
jbe 0x329c9
movl $0x3, %edi
leaq 0xbf24c(%rip), %rsi     # 0xf1bed
leaq 0xbf272(%rip), %rdx     # 0xf1c1a
movb $0x0, %al
callq 0x1b030
movl $0x3, %edi
leaq 0xbf232(%rip), %rsi     # 0xf1bed
leaq 0xbf285(%rip), %rdx     # 0xf1c47
movb $0x0, %al
callq 0x1b030
jmp 0x329cb
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rdi
leaq 0xbd57f(%rip), %rsi     # 0xeff6c
movl $0x4, %edx
callq 0xffb0
cmpq $0x0, %rax
jge 0x32a12
movq -0x30(%rbp), %rdi
callq 0x45a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x13, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x32ad4
movl -0x4c(%rbp), %edi
callq 0x32cf0
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x24(%rbp), %rsi
movl $0x4, %edx
callq 0xffb0
cmpq $0x0, %rax
jge 0x32a5f
movq -0x30(%rbp), %rdi
callq 0x45a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
cmpl $0x0, -0x4c(%rbp)
je 0x32a94
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0x4c(%rbp), %eax
movl %eax, %edx
callq 0xffb0
cmpq $0x0, %rax
jge 0x32a92
movq -0x30(%rbp), %rdi
callq 0x45a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
jmp 0x32a94
movq -0x18(%rbp), %rax
movl (%rax), %edi
callq 0x32cf0
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x24(%rbp), %rsi
movl $0x4, %edx
callq 0xffb0
cmpq $0x0, %rax
jge 0x32acf
movq -0x30(%rbp), %rdi
callq 0x45a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
jmp 0x32b65
movq -0x10(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
movl $0x4, %edx
callq 0xffb0
cmpq $0x0, %rax
jge 0x32b01
movq -0x30(%rbp), %rdi
callq 0x45a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
cmpl $0x0, -0x4c(%rbp)
je 0x32b36
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0x4c(%rbp), %eax
movl %eax, %edx
callq 0xffb0
cmpq $0x0, %rax
jge 0x32b34
movq -0x30(%rbp), %rdi
callq 0x45a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
jmp 0x32b36
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0xffb0
cmpq $0x0, %rax
jge 0x32b63
movq -0x30(%rbp), %rdi
callq 0x45a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
jmp 0x32b65
movq -0x30(%rbp), %rdi
callq 0x45a0
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
je 0x32cc0
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
callq 0x41b0
addq $0x1, %rax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x13, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x32bea
movl -0x20(%rbp), %edi
callq 0x32cf0
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x24(%rbp), %rsi
movl $0x4, %edx
callq 0xffb0
cmpq $0x0, %rax
jge 0x32be8
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
jmp 0x32c10
movq -0x10(%rbp), %rdi
leaq -0x20(%rbp), %rsi
movl $0x4, %edx
callq 0xffb0
cmpq $0x0, %rax
jge 0x32c0e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
jmp 0x32c10
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
movslq -0x20(%rbp), %rdx
callq 0xffb0
cmpq $0x0, %rax
jge 0x32c33
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x13, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x32c7e
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x32cf0
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x24(%rbp), %rsi
movl $0x4, %edx
callq 0xffb0
cmpq $0x0, %rax
jge 0x32c7c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
jmp 0x32cb0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x1c(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movl $0x4, %edx
callq 0xffb0
cmpq $0x0, %rax
jge 0x32cae
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
jmp 0x32cb0
jmp 0x32cb2
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x32b75
movq -0x10(%rbp), %rdi
callq 0xf820
cmpl $0x0, %eax
jge 0x32cd7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32cde
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)