// File: /workspace/github_c_makefile_star2_O2/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 0x5cc6
movq 0x8(%rsi), %rdi
callq 0x5ce8
movq %rax, %rbx
movq %rax, %rdi
callq 0x60fa
movq %rax, %r14
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x5dc9
testq %rax, %rax
je 0x5b24
movl (%rax), %eax
addl %eax, %r15d
incl %r15d
jmp 0x5b0d
leaq 0x7c504(%rip), %rdi     # 0x8202f
xorl %r12d, %r12d
movl %r15d, %esi
xorl %eax, %eax
callq 0x4060
movq %rbx, %rdi
callq 0x5ecd
testq %rax, %rax
je 0x5b4f
movl (%rax), %eax
addl %eax, %r12d
incl %r12d
jmp 0x5b38
leaq 0x7c4f2(%rip), %rdi     # 0x82048
xorl %r15d, %r15d
movl %r12d, %esi
xorl %eax, %eax
callq 0x4060
leaq 0x14(%rsp), %r12
andl $0x0, (%r12)
leaq 0x8(%rsp), %r13
andq $0x0, (%r13)
pushq $0x1
popq %rbp
movq %rbx, %rdi
callq 0x5dc9
testq %rax, %rax
je 0x5bf9
movq %r14, %rdi
movq %rax, %rsi
leaq 0x7c4cd(%rip), %rdx     # 0x82061
movq %r13, %rcx
movq %r12, %r8
movl %ebp, %r9d
callq 0x14004
testl %eax, %eax
jle 0x5b7a
movl 0x8(%r14), %ecx
cltd
idivl %ecx
movq 0x8(%rsp), %rdx
cltq
xorl %esi, %esi
testl %eax, %eax
movl $0x0, %edi
cmovgl %eax, %edi
testl %ecx, %ecx
cmovlel %esi, %ecx
shlq $0x2, %rax
cmpl %ecx, %esi
je 0x5b7a
xorl %r8d, %r8d
cmpq %r8, %rdi
je 0x5bf2
movl (%rdx,%r8,4), %r9d
movl %r9d, %r10d
negl %r10d
jo 0x5bed
cmpl $0x80000001, %r9d       # imm = 0x80000001
je 0x5bf2
addl %r9d, %r15d
incq %r8
jmp 0x5bd0
incl %esi
addq %rax, %rdx
jmp 0x5bc9
leaq 0x7c464(%rip), %rdi     # 0x82064
xorl %r12d, %r12d
movl %r15d, %esi
xorl %eax, %eax
callq 0x4060
leaq 0x8(%rsp), %r13
leaq 0x14(%rsp), %rbp
pushq $0x1
popq %r15
movq %rbx, %rdi
callq 0x5ecd
testq %rax, %rax
je 0x5c9a
movq %r14, %rdi
movq %rax, %rsi
leaq 0x7c42c(%rip), %rdx     # 0x82061
movq %r13, %rcx
movq %rbp, %r8
movl %r15d, %r9d
callq 0x14004
testl %eax, %eax
jle 0x5c1b
movl 0x8(%r14), %ecx
cltd
idivl %ecx
movq 0x8(%rsp), %rdx
cltq
xorl %esi, %esi
testl %eax, %eax
movl $0x0, %edi
cmovgl %eax, %edi
testl %ecx, %ecx
cmovlel %esi, %ecx
shlq $0x2, %rax
cmpl %ecx, %esi
je 0x5c1b
xorl %r8d, %r8d
cmpq %r8, %rdi
je 0x5c93
movl (%rdx,%r8,4), %r9d
movl %r9d, %r10d
negl %r10d
jo 0x5c8e
cmpl $0x80000001, %r9d       # imm = 0x80000001
je 0x5c93
addl %r9d, %r12d
incq %r8
jmp 0x5c71
incl %esi
addq %rax, %rdx
jmp 0x5c6a
leaq 0x7c3d6(%rip), %rdi     # 0x82077
xorl %ebp, %ebp
movl %r12d, %esi
xorl %eax, %eax
callq 0x4060
movq %rbx, %rdi
callq 0x5d61
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa8313(%rip), %rax     # 0xadfe0
movq (%rax), %rcx
leaq 0x7c32d(%rip), %rdi     # 0x82004
pushq $0x2a
popq %rsi
pushq $0x1
popq %rdx
callq 0x49c0
pushq $0x1
popq %rbp
jmp 0x5cb5
nop