// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x5b20
movq 0x1204dd(%rip), %rax    # 0x125fe0
movq (%rax), %rdi
leaq 0xfe4f7(%rip), %rsi     # 0x104004
movb $0x0, %al
callq 0x4700
movl $0x1, -0x4(%rbp)
jmp 0x5dd7
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x5de0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6390
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5f50
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x5b6f
movq -0x30(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movslq -0x24(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x24(%rbp)
jmp 0x5b45
movl -0x24(%rbp), %esi
leaq 0xfe4b6(%rip), %rdi     # 0x10402f
movb $0x0, %al
callq 0x4060
movl $0x0, -0x24(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6140
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x5bb1
movq -0x30(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movslq -0x24(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x24(%rbp)
jmp 0x5b87
movl -0x24(%rbp), %esi
leaq 0xfe48d(%rip), %rdi     # 0x104048
movb $0x0, %al
callq 0x4060
movl $0x0, -0x3c(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5f50
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x5cbb
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
leaq 0xfe463(%rip), %rdx     # 0x104061
leaq -0x48(%rbp), %rcx
leaq -0x3c(%rbp), %r8
movl $0x1, %r9d
callq 0x1c760
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jg 0x5c1c
jmp 0x5bd8
movl -0x40(%rbp), %eax
movq -0x20(%rbp), %rcx
cltd
idivl 0x8(%rcx)
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x5cb6
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x5c96
movq -0x58(%rbp), %rax
movslq -0x38(%rbp), %rcx
cmpl $0x80000001, (%rax,%rcx,4) # imm = 0x80000001
jne 0x5c67
jmp 0x5c96
movq -0x58(%rbp), %rax
movslq -0x38(%rbp), %rcx
cmpl $0x80000000, (%rax,%rcx,4) # imm = 0x80000000
jne 0x5c7a
jmp 0x5c8b
movq -0x58(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x5c4c
movl -0x4c(%rbp), %ecx
movq -0x58(%rbp), %rax
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5c39
jmp 0x5bd8
movl -0x24(%rbp), %esi
leaq 0xfe39f(%rip), %rdi     # 0x104064
movb $0x0, %al
callq 0x4060
movl $0x0, -0x24(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6140
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x5db6
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
leaq 0xfe368(%rip), %rdx     # 0x104061
leaq -0x48(%rbp), %rcx
leaq -0x3c(%rbp), %r8
movl $0x1, %r9d
callq 0x1c760
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jg 0x5d17
jmp 0x5cd3
movl -0x40(%rbp), %eax
movq -0x20(%rbp), %rcx
cltd
idivl 0x8(%rcx)
movl %eax, -0x5c(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x5db1
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x5d91
movq -0x68(%rbp), %rax
movslq -0x38(%rbp), %rcx
cmpl $0x80000001, (%rax,%rcx,4) # imm = 0x80000001
jne 0x5d62
jmp 0x5d91
movq -0x68(%rbp), %rax
movslq -0x38(%rbp), %rcx
cmpl $0x80000000, (%rax,%rcx,4) # imm = 0x80000000
jne 0x5d75
jmp 0x5d86
movq -0x68(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x5d47
movl -0x5c(%rbp), %ecx
movq -0x68(%rbp), %rax
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5d34
jmp 0x5cd3
movl -0x24(%rbp), %esi
leaq 0xfe2b7(%rip), %rdi     # 0x104077
movb $0x0, %al
callq 0x4060
movq -0x18(%rbp), %rdi
callq 0x5eb0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq