// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/test/test-bcf-sr.c
// Function: main
int main(int argc, char *argv[])
{
    static struct option loptions[] =
    {
        {"help",no_argument,NULL,'h'},
        {"output-fmt",required_argument,NULL,'O'},
        {"pair",required_argument,NULL,'p'},
        {"regions",required_argument,NULL,'r'},
        {"targets",required_argument,NULL,'t'},
        {"no-index",no_argument,NULL,1000},
        {"args",no_argument,NULL,1001},
        {"usefptr",no_argument,NULL,'u'},
        {NULL,0,NULL,0}
    };

    int c, pair = 0, use_index = 1, use_fofn = 1, usefptr = 0;
    enum htsExactFormat out_fmt = text_format; // for original pos + alleles
    const char *out_fn = NULL, *regions = NULL, *targets = NULL;
    htsFile **htsfp = NULL;
    while ((c = getopt_long(argc, argv, "o:O:p:r:t:hu", loptions, NULL)) >= 0)
    {
        switch (c)
        {
            case 'o':
                out_fn = optarg;
                break;
            case 'O':
                if (!strcasecmp(optarg, "vcf"))          out_fmt = vcf;
                else if (!strcasecmp(optarg, "bcf"))     out_fmt = bcf;
                else if (!strcasecmp(optarg, "summary")) out_fmt = text_format;
                else error("Unknown output format \"%s\"\n", optarg);
                break;
            case 'p':
                if ( !strcmp(optarg,"snps") )            pair |= BCF_SR_PAIR_SNPS;
                else if ( !strcmp(optarg,"snp+ref") )    pair |= BCF_SR_PAIR_SNPS|BCF_SR_PAIR_SNP_REF;
                else if ( !strcmp(optarg,"snps+ref") )   pair |= BCF_SR_PAIR_SNPS|BCF_SR_PAIR_SNP_REF;
                else if ( !strcmp(optarg,"indels") )     pair |= BCF_SR_PAIR_INDELS;
                else if ( !strcmp(optarg,"indel+ref") )  pair |= BCF_SR_PAIR_INDELS|BCF_SR_PAIR_INDEL_REF;
                else if ( !strcmp(optarg,"indels+ref") ) pair |= BCF_SR_PAIR_INDELS|BCF_SR_PAIR_INDEL_REF;
                else if ( !strcmp(optarg,"both") )       pair |= BCF_SR_PAIR_BOTH;
                else if ( !strcmp(optarg,"both+ref") )   pair |= BCF_SR_PAIR_BOTH_REF;
                else if ( !strcmp(optarg,"any") )        pair |= BCF_SR_PAIR_ANY;
                else if ( !strcmp(optarg,"all") )        pair |= BCF_SR_PAIR_ANY;
                else if ( !strcmp(optarg,"some") )       pair |= BCF_SR_PAIR_SOME;
                else if ( !strcmp(optarg,"exact") )      pair  = BCF_SR_PAIR_EXACT;
                else error("The --pair logic \"%s\" not recognised.\n", optarg);
                break;
            case 'r':
                regions = optarg;
                break;
            case 't':
                targets = optarg;
                break;
            case 1000:
                use_index = 0;
                break;
            case 1001:
                use_fofn = 0;
                break;
            case 'u':
                usefptr = 1;    //use htsfile interface instead of fname i/f
                break;
            case 'h':
                usage(EXIT_SUCCESS);
            default: usage(EXIT_FAILURE);
        }
    }
    if ( !pair ) pair = BCF_SR_PAIR_EXACT;
    if ( optind == argc ) usage(EXIT_FAILURE);

    int i, nvcf;
    char **vcfs = NULL;
    if (use_fofn) {
        vcfs = hts_readlist(argv[optind], 1, &nvcf);
        if ( !vcfs ) error("Could not parse %s\n", argv[optind]);
    } else {
        vcfs = &argv[optind];
        nvcf = argc - optind;
    }

    bcf_srs_t *sr = bcf_sr_init();
    if (!sr) error("bcf_sr_init() failed\n");
    bcf_sr_set_opt(sr, BCF_SR_PAIR_LOGIC, pair);
    if (use_index) {
        bcf_sr_set_opt(sr, BCF_SR_REQUIRE_IDX);
    } else {
        bcf_sr_set_opt(sr, BCF_SR_ALLOW_NO_IDX);
    }

    if (regions)
    {
        if (bcf_sr_set_regions(sr, regions, 0) != 0)
            error("Failed to set regions\n");
    }

    if (targets)
    {
        if (bcf_sr_set_targets(sr, targets, 0, 0) != 0)
            error("Failed to set targets\n");
    }

    if (usefptr && !(htsfp = malloc(sizeof(htsFile*) * nvcf))) {
        error("Failed to allocate memory\n");
    }

    for (i=0; i<nvcf; i++) {
        if (!usefptr) {
            if ( !bcf_sr_add_reader(sr,vcfs[i]) ) {
                error("Failed to open %s: %s\n", vcfs[i],
                    bcf_sr_strerror(sr->errnum));
            }
        } else {    //use htsfile i/f
            if (!(htsfp[i] = hts_open(vcfs[i], "r"))) {
                error("Failed to open %s: %s\n", vcfs[i],
                    bcf_sr_strerror(sr->errnum));
            }
            /*with name, index can be anywhere, named as anything
              w/o name it has to be along with file with default naming*/

            const char *idxname = strstr(vcfs[i], HTS_IDX_DELIM);
            idxname += idxname ? sizeof(HTS_IDX_DELIM) - 1 : 0;
            if ( !bcf_sr_add_hreader(sr, htsfp[i], 1, idxname) ) {
                error("Failed to add reader %s: %s\n", vcfs[i],
                    bcf_sr_strerror(sr->errnum));
            }
        }
    }

    if (!sr->readers || sr->nreaders < 1)
        error("No readers set, even though one was added\n");

    if (out_fmt == text_format) {
        FILE *out = stdout;
        if (out_fn)
        {
            out = fopen(out_fn, "w");
            if (!out) error("Couldn't open \"%s\" for writing: %s\n",
                            out_fn, strerror(errno));
        }
        write_summary_format(sr, out);
        if (out_fn)
        {
            if (fclose(out) != 0)
                error("Error on closing %s : %s\n",
                      out_fn, strerror(errno));
        }
    } else {
        const char *fmt_type = out_fmt == vcf ? "VCF" : "BCF";

        bcf_hdr_t *hdr = bcf_sr_get_header(sr, 0);
        if (!hdr) error("%s output, but don't have a header\n", fmt_type);

        if (!out_fn) { out_fn = "-"; }
        vcfFile *vcf_out = vcf_open(out_fn, out_fmt == vcf ? "w" : "wb");
        if (!vcf_out) error("Couldn't open \"%s\" for writing: %s\n",
                            out_fn, strerror(errno));
        write_vcf_bcf_format(sr, hdr, vcf_out, fmt_type);
        if (vcf_close(vcf_out) != 0)
            error("Error on closing \"%s\"\n", out_fn);
    }

    if (sr->errnum) error("Synced reader error: %s\n",
                          bcf_sr_strerror(sr->errnum));

    bcf_sr_destroy(sr);
    if (use_fofn)
    {
        for (i=0; i<nvcf; i++)
            free(vcfs[i]);
        free(vcfs);
    }
    if (usefptr) {
        //files are closed along with sr destroy
        free(htsfp);
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movl %edi, %ebp
leaq 0xaaff0(%rip), %rdx     # 0xb1355
leaq 0xd21a4(%rip), %rcx     # 0xd8510
xorl %r8d, %r8d
callq 0x4890
testl %eax, %eax
js 0x6688
movl $0x2, 0x10(%rsp)
movl $0x1, 0x8(%rsp)
movq $0x0, (%rsp)
leaq 0xaac69(%rip), %r12     # 0xb1004
leaq 0xaafb3(%rip), %r13     # 0xb1355
leaq 0xd2167(%rip), %rbx     # 0xd8510
movl $0x1, 0x14(%rsp)
movl $0x0, 0x18(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x20(%rsp)
cmpl $0x3e7, %eax            # imm = 0x3E7
jg 0x6400
leal -0x68(%rax), %ecx
cmpl $0xd, %ecx
ja 0x6423
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movq 0xd1bcd(%rip), %rax     # 0xd7fc0
movq (%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x660e
cmpl $0x3e8, %eax            # imm = 0x3E8
je 0x65dc
cmpl $0x3e9, %eax            # imm = 0x3E9
jne 0x69fb
movl $0x0, 0x14(%rsp)
jmp 0x660e
cmpl $0x4f, %eax
jne 0x69fb
movq 0xd1b8d(%rip), %rax     # 0xd7fc0
movq (%rax), %r15
movq %r15, %rdi
leaq 0xabde3(%rip), %rsi     # 0xb2223
callq 0x4520
testl %eax, %eax
je 0x65fc
movq %r15, %rdi
leaq 0xabdd1(%rip), %rsi     # 0xb2228
callq 0x4520
testl %eax, %eax
je 0x6606
movq %r15, %rdi
leaq 0xaaef4(%rip), %rsi     # 0xb1362
callq 0x4520
movl $0x2, 0x10(%rsp)
testl %eax, %eax
je 0x660e
jmp 0x6a2f
movq 0xd1b31(%rip), %rax     # 0xd7fc0
movq (%rax), %r15
movq %r15, %rdi
leaq 0xaaeea(%rip), %rsi     # 0xb1386
callq 0x46f0
testl %eax, %eax
je 0x65ef
movq %r15, %rdi
leaq 0xaaed8(%rip), %rsi     # 0xb138b
callq 0x46f0
testl %eax, %eax
je 0x65e6
movq %r15, %rdi
leaq 0xaaec9(%rip), %rsi     # 0xb1393
callq 0x46f0
testl %eax, %eax
je 0x65e6
movq %r15, %rdi
leaq 0xaaebb(%rip), %rsi     # 0xb139c
callq 0x46f0
testl %eax, %eax
je 0x6634
movq %r15, %rdi
leaq 0xaaeab(%rip), %rsi     # 0xb13a3
callq 0x46f0
testl %eax, %eax
je 0x662b
movq %r15, %rdi
leaq 0xaae9e(%rip), %rsi     # 0xb13ad
callq 0x46f0
testl %eax, %eax
je 0x662b
movq %r15, %rdi
leaq 0xaae92(%rip), %rsi     # 0xb13b8
callq 0x46f0
testl %eax, %eax
je 0x663d
movq %r15, %rdi
leaq 0xaae80(%rip), %rsi     # 0xb13bd
callq 0x46f0
testl %eax, %eax
je 0x664f
movq %r15, %rdi
leaq 0xaae72(%rip), %rsi     # 0xb13c6
callq 0x46f0
testl %eax, %eax
je 0x6646
movq %r15, %rdi
leaq 0xab91e(%rip), %rsi     # 0xb1e89
callq 0x46f0
testl %eax, %eax
je 0x6646
movq %r15, %rdi
leaq 0xaae48(%rip), %rsi     # 0xb13ca
callq 0x46f0
testl %eax, %eax
je 0x6658
movq %r15, %rdi
leaq 0xaae36(%rip), %rsi     # 0xb13cf
callq 0x46f0
movl $0x40, %ecx
movq %rcx, (%rsp)
testl %eax, %eax
je 0x660e
jmp 0x6aad
movl $0x1, 0x18(%rsp)
jmp 0x660e
movq 0xd19ff(%rip), %rax     # 0xd7fc0
movq (%rax), %rax
movq %rax, 0x28(%rsp)
jmp 0x660e
movq 0xd19ee(%rip), %rax     # 0xd7fc0
movq (%rax), %rax
movq %rax, 0x30(%rsp)
jmp 0x660e
movl $0x0, 0x8(%rsp)
jmp 0x660e
movq (%rsp), %rax
orl $0x11, %eax
jmp 0x65f6
movq (%rsp), %rax
orl $0x1, %eax
movq %rax, (%rsp)
jmp 0x660e
movl $0x8, 0x10(%rsp)
jmp 0x660e
movl $0x9, 0x10(%rsp)
movl %ebp, %edi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x4890
testl %eax, %eax
jns 0x63d4
jmp 0x6661
movq (%rsp), %rax
orl $0x22, %eax
jmp 0x65f6
movq (%rsp), %rax
orl $0x2, %eax
jmp 0x65f6
movq (%rsp), %rax
orl $0x3, %eax
jmp 0x65f6
movq (%rsp), %rax
orl $0x4, %eax
jmp 0x65f6
movq (%rsp), %rax
orl $0x33, %eax
jmp 0x65f6
movq (%rsp), %rax
orl $0x8, %eax
jmp 0x65f6
cmpl $0x0, 0x14(%rsp)
sete %cl
xorl %r15d, %r15d
cmpl $0x0, 0x8(%rsp)
sete %r15b
cmpl $0x0, 0x18(%rsp)
sete 0x8(%rsp)
addl %r15d, %r15d
movq (%rsp), %rax
jmp 0x66b7
movq $0x0, 0x20(%rsp)
movb $0x1, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
xorl %ecx, %ecx
xorl %r15d, %r15d
xorl %eax, %eax
testl %eax, %eax
movl $0x40, %r13d
cmovnel %eax, %r13d
movq 0xd1916(%rip), %r12     # 0xd7fe0
movslq (%r12), %rax
cmpl %eax, %ebp
je 0x69fb
leaq (%r14,%rax,8), %rbx
testb %cl, %cl
movq %rcx, 0x18(%rsp)
je 0x66f0
subl %eax, %ebp
movl %ebp, 0xc(%rsp)
movb 0x8(%rsp), %bpl
jmp 0x6713
movq (%rbx), %rdi
leaq 0xc(%rsp), %rdx
movl $0x1, %esi
callq 0xb842
movq %rax, %rbx
testq %rax, %rax
movb 0x8(%rsp), %bpl
je 0x6a62
callq 0x2b21f
testq %rax, %rax
je 0x6a15
movq %rax, %r14
movq %rax, %rdi
movl $0x1, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x29bb5
movq %r14, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x29bb5
movq 0x30(%rsp), %rsi
testq %rsi, %rsi
je 0x675f
movq %r14, %rdi
xorl %edx, %edx
callq 0x29d73
testl %eax, %eax
jne 0x6a50
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x677d
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2a671
testl %eax, %eax
jne 0x6a59
testb %bpl, %bpl
je 0x678c
movq $0x0, (%rsp)
jmp 0x67a7
movslq 0xc(%rsp), %rdi
shlq $0x3, %rdi
callq 0x46b0
movq %rax, (%rsp)
testq %rax, %rax
je 0x6a73
cmpl $0x0, 0xc(%rsp)
jle 0x6845
xorl %r12d, %r12d
leaq 0xaacca(%rip), %r13     # 0xb1486
movq (%rbx,%r12,8), %rsi
testb %bpl, %bpl
je 0x67d6
movq %r14, %rdi
callq 0x2a781
testl %eax, %eax
jne 0x6834
jmp 0x69af
movq %rsi, %rdi
leaq 0xadc3f(%rip), %rsi     # 0xb441f
callq 0x9f37
movq %rax, %rbp
movq (%rsp), %rax
movq %rbp, (%rax,%r12,8)
movq (%rbx,%r12,8), %r15
testq %rbp, %rbp
je 0x69de
movq %r15, %rdi
movq %r13, %rsi
callq 0x40d0
testq %rax, %rax
movl $0x7, %ecx
cmoveq %rax, %rcx
addq %rax, %rcx
movq %r14, %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0x2a80e
testl %eax, %eax
movb 0x8(%rsp), %bpl
je 0x69c5
incq %r12
movslq 0xc(%rsp), %rax
cmpq %rax, %r12
jl 0x67bc
movq 0x28(%r14), %rax
testq %rax, %rax
je 0x6a0c
cmpl $0x0, 0x30(%r14)
jle 0x6a0c
movl 0x10(%rsp), %edx
cmpl $0x2, %edx
jne 0x6894
movq 0x20(%rsp), %r12
testq %r12, %r12
je 0x6923
leaq 0xacdc6(%rip), %rsi     # 0xb3641
movq %r12, %rdi
callq 0x45c0
movq %rax, %r15
testq %rax, %rax
jne 0x692d
jmp 0x6a3b
cmpl $0x8, %edx
leaq 0xab307(%rip), %rcx     # 0xb1ba5
leaq 0xab36c(%rip), %r13     # 0xb1c11
cmoveq %rcx, %r13
movq 0x18(%rax), %rbp
testq %rbp, %rbp
je 0x6a81
movq 0x20(%rsp), %rax
testq %rax, %rax
leaq 0xad462(%rip), %r12     # 0xb3d27
cmovneq %rax, %r12
cmpl $0x8, %edx
leaq 0xacd6e(%rip), %rax     # 0xb3641
leaq 0xaac5e(%rip), %rsi     # 0xb1538
cmoveq %rax, %rsi
movq %r12, %rdi
callq 0x9f37
testq %rax, %rax
je 0x6a3b
movq %rax, %r15
movq %r14, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movq %r13, %rcx
callq 0x62a0
movq %r15, %rdi
callq 0xb1a1
testl %eax, %eax
movb 0x8(%rsp), %bpl
je 0x694d
leaq 0xaac20(%rip), %rdi     # 0xb153b
movq %r12, %rsi
jmp 0x6a8b
movq 0xd1686(%rip), %rax     # 0xd7fb0
movq (%rax), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x60ea
testq %r12, %r12
je 0x694d
movq %r15, %rdi
callq 0x44b0
testl %eax, %eax
jne 0x6a92
movl 0x20(%r14), %edi
testl %edi, %edi
jne 0x6a1e
movq %r14, %rdi
callq 0x2b27a
cmpb $0x0, 0x18(%rsp)
jne 0x6990
cmpl $0x0, 0xc(%rsp)
jle 0x6988
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rdi
callq 0x45d0
incq %r14
movslq 0xc(%rsp), %rax
cmpq %rax, %r14
jl 0x6972
movq %rbx, %rdi
callq 0x45d0
testb %bpl, %bpl
jne 0x699e
movq (%rsp), %rdi
callq 0x45d0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx,%r12,8), %rbx
movl 0x20(%r14), %edi
callq 0x29b38
leaq 0xaaaac(%rip), %rdi     # 0xb146f
jmp 0x69d9
movq (%rbx,%r12,8), %rbx
movl 0x20(%r14), %edi
callq 0x29b38
leaq 0xaaab5(%rip), %rdi     # 0xb148e
movq %rbx, %rsi
jmp 0x69f1
movl 0x20(%r14), %edi
callq 0x29b38
leaq 0xaaa81(%rip), %rdi     # 0xb146f
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x5f1c
movl $0x1, %edi
callq 0x5fb6
xorl %edi, %edi
callq 0x5fb6
leaq 0xaaa98(%rip), %rdi     # 0xb14ab
jmp 0x6a7a
leaq 0xaa9f4(%rip), %rdi     # 0xb1410
jmp 0x6a7a
callq 0x29b38
leaq 0xaab28(%rip), %rdi     # 0xb1552
movq %rax, %rsi
jmp 0x6a8b
leaq 0xaa934(%rip), %rdi     # 0xb136a
movq %r15, %rsi
jmp 0x6a8b
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0xaaa88(%rip), %rdi     # 0xb14d6
jmp 0x6aa5
leaq 0xaa9cf(%rip), %rdi     # 0xb1426
jmp 0x6a7a
leaq 0xaa9dd(%rip), %rdi     # 0xb143d
jmp 0x6a7a
movslq (%r12), %rax
movq (%r14,%rax,8), %rsi
leaq 0xaa98b(%rip), %rdi     # 0xb13fc
jmp 0x6a8b
leaq 0xaa9da(%rip), %rdi     # 0xb1454
xorl %eax, %eax
callq 0x5f1c
leaq 0xaaa8c(%rip), %rdi     # 0xb1514
movq %r13, %rsi
xorl %eax, %eax
callq 0x5f1c
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0xaaa55(%rip), %rdi     # 0xb14fa
movq %r12, %rsi
jmp 0x69f1
leaq 0xaa921(%rip), %rdi     # 0xb13d5
jmp 0x6a36
nop