// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/test/test-vcf-sweep.c
// Function: main
int main(int argc, char **argv)
{
    if ( argc!=2 )
    {
        fprintf(stderr,"Usage: test-vcf-sweep <file.bcf|file.vcf>\n");
        return 1;
    }

    // Init variables. The checksum is just for this test program to output
    // something and verify that all sites are read in both passes - fwd and
    // bwd.
    bcf_sweep_t *sw = bcf_sweep_init(argv[1]);
    bcf_hdr_t *hdr  = bcf_sweep_hdr(sw);
    int chksum = 0;

    // First we must sweep forward and read the whole file to build an index.
    // If this is undesirable, we can require the presence of a .gzi index
    // which can be created with `bgzip -r` from the samtools/htslib package
    bcf1_t *rec;
    while ( (rec = bcf_sweep_fwd(sw)) ) chksum += rec->pos+1;
    printf("fwd position chksum: %d\n", chksum);

    // Now sweep backward.
    chksum = 0;
    while ( (rec = bcf_sweep_bwd(sw)) ) chksum += rec->pos+1;
    printf("bwd position chksum: %d\n", chksum);

    // And forward and backward again, this time summing the PL vectors
    int i,j, mPLs = 0, nPLs;
    int32_t *PLs = NULL;
    chksum = 0;
    while ( (rec = bcf_sweep_fwd(sw)) )
    {
        // get copy of the PL vectors
        nPLs = bcf_get_format_int32(hdr, rec, "PL", &PLs, &mPLs);
        if ( nPLs <= 0 ) continue;  // PL not present

        // how many values are there per sample
        int nvals = nPLs / bcf_hdr_nsamples(hdr);

        int32_t *ptr = PLs;
        for (i=0; i<bcf_hdr_nsamples(hdr); i++)
        {
            for (j=0; j<nvals; j++)
            {
                // check for shorter vectors (haploid genotypes amongst diploids)
                if ( ptr[j]==bcf_int32_vector_end ) break;

                // skip missing values
                if ( ptr[j]==bcf_int32_missing ) continue;

                chksum += ptr[j];
            }
            ptr += nvals;
        }
    }
    printf("fwd PL chksum: %d\n", chksum);

    // And the same backwards..
    chksum = 0;
    while ( (rec = bcf_sweep_bwd(sw)) )
    {
        nPLs = bcf_get_format_int32(hdr, rec, "PL", &PLs, &mPLs);
        if ( nPLs <= 0 ) continue;
        int nvals = nPLs / bcf_hdr_nsamples(hdr);
        int32_t *ptr = PLs;
        for (i=0; i<bcf_hdr_nsamples(hdr); i++)
        {
            for (j=0; j<nvals; j++)
            {
                if ( ptr[j]==bcf_int32_vector_end ) break;
                if ( ptr[j]==bcf_int32_missing ) continue;
                chksum += ptr[j];
            }
            ptr += nvals;
        }
    }
    printf("bwd PL chksum: %d\n", chksum);

    // Clean up
    bcf_sweep_destroy(sw);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
cmpl $0x2, %edi
jne 0x60e0
movq 0x8(%rsi), %rdi
callq 0x60ec
movq %rax, %rbx
movq %rax, %rdi
callq 0x651b
movq %rax, %r14
movq %rbx, %rdi
callq 0x61d7
xorl %r12d, %r12d
movl $0x0, %r15d
testq %rax, %rax
je 0x5f38
xorl %r15d, %r15d
movl (%rax), %eax
addl %eax, %r15d
incl %r15d
movq %rbx, %rdi
callq 0x61d7
testq %rax, %rax
jne 0x5f23
leaq 0xa10f0(%rip), %rdi     # 0xa702f
movl %r15d, %esi
xorl %eax, %eax
callq 0x4060
movq %rbx, %rdi
callq 0x62cf
testq %rax, %rax
je 0x5f6e
xorl %r12d, %r12d
movl (%rax), %eax
addl %eax, %r12d
incl %r12d
movq %rbx, %rdi
callq 0x62cf
testq %rax, %rax
jne 0x5f59
leaq 0xa10d3(%rip), %rdi     # 0xa7048
xorl %r15d, %r15d
movl %r12d, %esi
xorl %eax, %eax
callq 0x4060
leaq 0x14(%rsp), %r12
movl $0x0, (%r12)
leaq 0x8(%rsp), %r13
movq $0x0, (%r13)
leaq 0xa10be(%rip), %rbp     # 0xa7061
movq %rbx, %rdi
callq 0x61d7
testq %rax, %rax
je 0x6019
movq %r14, %rdi
movq %rax, %rsi
movq %rbp, %rdx
movq %r13, %rcx
movq %r12, %r8
movl $0x1, %r9d
callq 0x1c77a
testl %eax, %eax
jle 0x5fa3
movl 0x8(%r14), %esi
testl %esi, %esi
jle 0x5fa3
movl %eax, %ecx
xorl %edi, %edi
xorl %edx, %edx
divl %esi
movq 0x8(%rsp), %rdx
cmpl %ecx, %esi
ja 0x600d
movq %rax, %r8
xorl %r9d, %r9d
movl (%rdx,%r9,4), %r10d
movl %r10d, %r11d
negl %r11d
jo 0x6005
cmpl $0x80000001, %r10d      # imm = 0x80000001
je 0x600d
addl %r10d, %r15d
incq %r9
decq %r8
jne 0x5fed
leaq (%rdx,%rax,4), %rdx
incl %edi
cmpl %esi, %edi
jne 0x5fe3
jmp 0x5fa3
leaq 0xa1044(%rip), %rdi     # 0xa7064
xorl %r12d, %r12d
movl %r15d, %esi
xorl %eax, %eax
callq 0x4060
leaq 0xa102d(%rip), %r15     # 0xa7061
leaq 0x8(%rsp), %r13
leaq 0x14(%rsp), %rbp
movq %rbx, %rdi
callq 0x62cf
testq %rax, %rax
je 0x60b4
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq %rbp, %r8
movl $0x1, %r9d
callq 0x1c77a
testl %eax, %eax
jle 0x603e
movl 0x8(%r14), %esi
testl %esi, %esi
jle 0x603e
movl %eax, %ecx
xorl %edi, %edi
xorl %edx, %edx
divl %esi
movq 0x8(%rsp), %rdx
cmpl %ecx, %esi
ja 0x60a8
movq %rax, %r8
xorl %r9d, %r9d
movl (%rdx,%r9,4), %r10d
movl %r10d, %r11d
negl %r11d
jo 0x60a0
cmpl $0x80000001, %r10d      # imm = 0x80000001
je 0x60a8
addl %r10d, %r12d
incq %r9
decq %r8
jne 0x6088
leaq (%rdx,%rax,4), %rdx
incl %edi
cmpl %esi, %edi
jne 0x607e
jmp 0x603e
leaq 0xa0fbc(%rip), %rdi     # 0xa7077
xorl %ebp, %ebp
movl %r12d, %esi
xorl %eax, %eax
callq 0x4060
movq %rbx, %rdi
callq 0x616b
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4a00
movl $0x1, %ebp
jmp 0x60cf