// File: /workspace/github_c_makefile_star2_O1/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 0x60d2
movq 0x8(%rsi), %rdi
callq 0x60e0
movq %rax, %rbx
movq %rax, %rdi
callq 0x650c
movq %rax, %r14
movq %rbx, %rdi
callq 0x61d2
xorl %r12d, %r12d
movl $0x0, %r15d
testq %rax, %rax
je 0x5f28
xorl %r15d, %r15d
movl (%rax), %eax
addl %eax, %r15d
incl %r15d
movq %rbx, %rdi
callq 0x61d2
testq %rax, %rax
jne 0x5f13
leaq 0xa4100(%rip), %rdi     # 0xaa02f
movl %r15d, %esi
xorl %eax, %eax
callq 0x4060
movq %rbx, %rdi
callq 0x62c8
testq %rax, %rax
je 0x5f5e
xorl %r12d, %r12d
movl (%rax), %eax
addl %eax, %r12d
incl %r12d
movq %rbx, %rdi
callq 0x62c8
testq %rax, %rax
jne 0x5f49
leaq 0xa40e3(%rip), %rdi     # 0xaa048
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 0xa40ce(%rip), %rbp     # 0xaa061
movq %rbx, %rdi
callq 0x61d2
testq %rax, %rax
je 0x600a
movq %r14, %rdi
movq %rax, %rsi
movq %rbp, %rdx
movq %r13, %rcx
movq %r12, %r8
movl $0x1, %r9d
callq 0x1c5bf
testl %eax, %eax
jle 0x5f93
movl 0x8(%r14), %ecx
testl %ecx, %ecx
jle 0x5f93
cltd
idivl %ecx
movq 0x8(%rsp), %rdx
leaq (,%rax,4), %rsi
xorl %edi, %edi
testl %eax, %eax
jle 0x5fff
xorl %r8d, %r8d
movl (%rdx,%r8,4), %r9d
movl %r9d, %r10d
negl %r10d
jo 0x5ff7
cmpl $0x80000001, %r9d       # imm = 0x80000001
je 0x5fff
addl %r9d, %r15d
incq %r8
cmpq %r8, %rax
jne 0x5fdf
incl %edi
addq %rsi, %rdx
cmpl %ecx, %edi
jne 0x5fd8
jmp 0x5f93
leaq 0xa4053(%rip), %rdi     # 0xaa064
xorl %r12d, %r12d
movl %r15d, %esi
xorl %eax, %eax
callq 0x4060
leaq 0xa403c(%rip), %r15     # 0xaa061
leaq 0x8(%rsp), %r13
leaq 0x14(%rsp), %rbp
movq %rbx, %rdi
callq 0x62c8
testq %rax, %rax
je 0x60a6
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq %rbp, %r8
movl $0x1, %r9d
callq 0x1c5bf
testl %eax, %eax
jle 0x602f
movl 0x8(%r14), %ecx
testl %ecx, %ecx
jle 0x602f
cltd
idivl %ecx
movq 0x8(%rsp), %rdx
leaq (,%rax,4), %rsi
xorl %edi, %edi
testl %eax, %eax
jle 0x609b
xorl %r8d, %r8d
movl (%rdx,%r8,4), %r9d
movl %r9d, %r10d
negl %r10d
jo 0x6093
cmpl $0x80000001, %r9d       # imm = 0x80000001
je 0x609b
addl %r9d, %r12d
incq %r8
cmpq %r8, %rax
jne 0x607b
incl %edi
addq %rsi, %rdx
cmpl %ecx, %edi
jne 0x6074
jmp 0x602f
leaq 0xa3fca(%rip), %rdi     # 0xaa077
xorl %ebp, %ebp
movl %r12d, %esi
xorl %eax, %eax
callq 0x4060
movq %rbx, %rdi
callq 0x6166
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 0x60c1
nop