// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/vcf.c
// Function: bcf_write
int bcf_write(htsFile *hfp, bcf_hdr_t *h, bcf1_t *v)
{
    if ( h->dirty ) {
        if (bcf_hdr_sync(h) < 0) return -1;
    }
    if ( bcf_hdr_nsamples(h)!=v->n_sample )
    {
        hts_log_error("Broken VCF record, the number of columns at %s:%"PRIhts_pos" does not match the number of samples (%d vs %d)",
            bcf_seqname_safe(h,v), v->pos+1, v->n_sample, bcf_hdr_nsamples(h));
        return -1;
    }

    if ( hfp->format.format == vcf || hfp->format.format == text_format )
        return vcf_write(hfp,h,v);

    if ( v->errcode & ~BCF_ERR_LIMITS ) // todo: unsure about the other BCF_ERR_LIMITS branches in vcf_parse_format_alloc4()
    {
        // vcf_parse1() encountered a new contig or tag, undeclared in the
        // header.  At this point, the header must have been printed,
        // proceeding would lead to a broken BCF file. Errors must be checked
        // and cleared by the caller before we can proceed.
        char errdescription[1024] = "";
        hts_log_error("Unchecked error (%d %s) at %s:%"PRIhts_pos, v->errcode, bcf_strerror(v->errcode, errdescription, sizeof(errdescription)), bcf_seqname_safe(h,v), v->pos+1);
        return -1;
    }
    bcf1_sync(v);   // check if the BCF record was modified

    if ( v->unpacked & BCF_IS_64BIT )
    {
        hts_log_error("Data at %s:%"PRIhts_pos" contains 64-bit values not representable in BCF. Please use VCF instead", bcf_seqname_safe(h,v), v->pos+1);
        return -1;
    }

    BGZF *fp = hfp->fp.bgzf;
    uint8_t x[32];
    u32_to_le(v->shared.l + 24, x); // to include six 32-bit integers
    u32_to_le(v->indiv.l, x + 4);
    i32_to_le(v->rid, x + 8);
    u32_to_le(v->pos, x + 12);
    u32_to_le(v->rlen, x + 16);
    float_to_le(v->qual, x + 20);
    u16_to_le(v->n_info, x + 24);
    u16_to_le(v->n_allele, x + 26);
    u32_to_le((uint32_t)v->n_fmt<<24 | (v->n_sample & 0xffffff), x + 28);
    if ( bgzf_write(fp, x, 32) != 32 ) return -1;
    if ( bgzf_write(fp, v->shared.s, v->shared.l) != v->shared.l ) return -1;
    if ( bgzf_write(fp, v->indiv.s, v->indiv.l) != v->indiv.l ) return -1;

    if (hfp->idx) {
        if (bgzf_idx_push(fp, hfp->idx, v->rid, v->pos, v->pos + v->rlen,
                          bgzf_tell(fp), 1) < 0)
            return -1;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x410, %rsp            # imm = 0x410
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
cmpl $0x0, 0x54(%rsi)
je 0x31614
movq %r15, %rdi
callq 0x2ab97
testl %eax, %eax
js 0x316a4
movl 0x8(%r15), %eax
movq 0x18(%rbx), %r9
shrq $0x28, %r9
cmpl %r9d, %eax
jne 0x31654
movl 0x4c(%r14), %eax
cmpl $0x8, %eax
je 0x31637
cmpl $0x2, %eax
jne 0x316bb
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
addq $0x410, %rsp            # imm = 0x410
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x318ea
movslq 0x10(%rbx), %rcx
testq %rcx, %rcx
js 0x31671
cmpl %ecx, 0x4(%r15)
jle 0x31671
movq 0x18(%r15), %rdx
shlq $0x4, %rcx
movq (%rdx,%rcx), %rdx
jmp 0x31673
xorl %edx, %edx
testq %rdx, %rdx
leaq 0x7b292(%rip), %rcx     # 0xac90f
cmovneq %rdx, %rcx
movq (%rbx), %r8
incq %r8
movl %eax, (%rsp)
leaq 0x7a3ff(%rip), %rsi     # 0xaba90
leaq 0x7a402(%rip), %rdx     # 0xaba9a
movl $0x1, %edi
xorl %eax, %eax
callq 0x942d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x410, %rsp            # imm = 0x410
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl 0xcc(%rbx), %ebp
testl $0xfffffff7, %ebp       # imm = 0xFFFFFFF7
je 0x31717
leaq 0x10(%rsp), %r14
xorl %r12d, %r12d
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
xorl %esi, %esi
callq 0x4250
movl $0x400, %edx            # imm = 0x400
movl %ebp, %edi
movq %r14, %rsi
callq 0x31afd
movslq 0x10(%rbx), %rcx
testq %rcx, %rcx
js 0x3181f
cmpl %ecx, 0x4(%r15)
jle 0x3181c
movq 0x18(%r15), %rdx
shlq $0x4, %rcx
movq (%rdx,%rcx), %r12
jmp 0x3181f
movq %rbx, %rdi
callq 0x30aea
testb $0x40, 0xbf(%rbx)
jne 0x3185b
movq 0x38(%r14), %r15
movl 0x20(%rbx), %eax
addl $0x18, %eax
leaq 0x10(%rsp), %rsi
movl %eax, (%rsi)
movl 0x38(%rbx), %eax
movl %eax, 0x4(%rsi)
movl 0x10(%rbx), %eax
movl %eax, 0x8(%rsi)
movdqu (%rbx), %xmm0
pshufd $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3]
movq %xmm0, 0xc(%rsi)
movss 0x14(%rbx), %xmm0
movss %xmm0, 0x14(%rsi)
movq 0x18(%rbx), %rax
movw %ax, 0x18(%rsi)
movl %eax, %ecx
shrl $0x10, %ecx
movw %cx, 0x1a(%rsi)
movq %rax, %rcx
shrq $0x8, %rcx
andl $0xff000000, %ecx       # imm = 0xFF000000
shrq $0x28, %rax
orl %ecx, %eax
movl %eax, 0x1c(%rsi)
movl $0x20, %edx
movq %r15, %rdi
callq 0x8caa5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq $0x20, %rax
jne 0x316a9
movq 0x20(%rbx), %rdx
movq 0x30(%rbx), %rsi
movq %r15, %rdi
callq 0x8caa5
cmpq 0x20(%rbx), %rax
jne 0x316a9
movq 0x38(%rbx), %rdx
movq 0x48(%rbx), %rsi
movq %r15, %rdi
callq 0x8caa5
cmpq 0x38(%rbx), %rax
jne 0x316a9
movq 0x68(%r14), %rsi
testq %rsi, %rsi
je 0x31815
movl 0x10(%rbx), %edx
movq (%rbx), %rcx
movq 0x8(%rbx), %r8
addq %rcx, %r8
movq 0x18(%r15), %rax
shlq $0x10, %rax
movzwl 0x10(%r15), %r9d
orq %rax, %r9
movl $0x1, (%rsp)
movq %r15, %rdi
callq 0x89ea8
testl %eax, %eax
js 0x316a9
xorl %ebp, %ebp
jmp 0x316a9
xorl %r12d, %r12d
testq %r12, %r12
leaq 0x7b0e6(%rip), %r9      # 0xac90f
cmovneq %r12, %r9
movq (%rbx), %rcx
incq %rcx
movq %rcx, (%rsp)
leaq 0x7a252(%rip), %rsi     # 0xaba90
leaq 0x7a2b8(%rip), %rdx     # 0xabafd
movl $0x1, %edi
movl %ebp, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x942d
jmp 0x316a4
movslq 0x10(%rbx), %rax
testq %rax, %rax
js 0x31878
cmpl %eax, 0x4(%r15)
jle 0x31878
movq 0x18(%r15), %rcx
shlq $0x4, %rax
movq (%rcx,%rax), %rax
jmp 0x3187a
xorl %eax, %eax
testq %rax, %rax
leaq 0x7b08b(%rip), %rcx     # 0xac90f
cmovneq %rax, %rcx
movq (%rbx), %r8
incq %r8
leaq 0x7a1fb(%rip), %rsi     # 0xaba90
leaq 0x7a283(%rip), %rdx     # 0xabb1f
movl $0x1, %edi
xorl %eax, %eax
callq 0x942d
jmp 0x316a4