// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq -0x1c(%rbp), %rdi
leaq 0x318b4(%rip), %rsi     # 0x7660f
movl $0x4, %edx
callq 0x36f0
movq -0x10(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
movl $0x4, %edx
callq 0x3bef0
cmpq $0x0, %rax
jge 0x44d89
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x44e40
movq -0x18(%rbp), %rax
movl 0x8(%rax), %edi
callq 0x45040
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x3bef0
cmpq $0x0, %rax
jge 0x44dd1
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
movq -0x18(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x44e09
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
callq 0x3bef0
cmpq $0x0, %rax
jge 0x44e07
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
jmp 0x44e09
movq -0x18(%rbp), %rax
movl (%rax), %edi
callq 0x45040
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x3bef0
cmpq $0x0, %rax
jge 0x44e3b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
jmp 0x44ec6
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
movl $0x4, %edx
callq 0x3bef0
cmpq $0x0, %rax
jge 0x44e68
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
movq -0x18(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x44ea0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
callq 0x3bef0
cmpq $0x0, %rax
jge 0x44e9e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
jmp 0x44ea0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0x3bef0
cmpq $0x0, %rax
jge 0x44ec4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
jmp 0x44ec6
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
je 0x45018
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x3380
addq $0x1, %rax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x44f42
movl -0x24(%rbp), %edi
callq 0x45040
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x3bef0
cmpq $0x0, %rax
jge 0x44f40
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
jmp 0x44f68
movq -0x10(%rbp), %rdi
leaq -0x24(%rbp), %rsi
movl $0x4, %edx
callq 0x3bef0
cmpq $0x0, %rax
jge 0x44f66
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
jmp 0x44f68
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movslq -0x24(%rbp), %rdx
callq 0x3bef0
cmpq $0x0, %rax
jge 0x44f8b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x44fd6
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x45040
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x3bef0
cmpq $0x0, %rax
jge 0x44fd4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
jmp 0x45008
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x20(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movl $0x4, %edx
callq 0x3bef0
cmpq $0x0, %rax
jge 0x45006
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
jmp 0x45008
jmp 0x4500a
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x44ecd
movq -0x10(%rbp), %rdi
callq 0x3b870
cmpl $0x0, %eax
jge 0x4502f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x45036
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop