// File: /workspace/github_c_makefile_star2_O2/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, %r12
movl %edi, %ebp
pushq $0x1
popq %rcx
movq $0x0, 0x20(%rsp)
pushq $0x2
popq %rax
leaq 0xb0623(%rip), %rbx     # 0xb6510
leaq 0x81110(%rip), %r15     # 0x87004
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movl %eax, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
movl %ecx, 0x1c(%rsp)
movl $0x0, 0x8(%rsp)
movl %ecx, %r13d
movl %ebp, %edi
movq %r12, %rsi
leaq 0x81428(%rip), %rdx     # 0x87355
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x4890
testl %eax, %eax
js 0x61d3
leal -0x68(%rax), %ecx
cmpl $0xd, %ecx
ja 0x5f65
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
movq 0xb0068(%rip), %rax     # 0xb5fc0
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
cmpl $0x4f, %eax
je 0x60f9
xorl %ecx, %ecx
cmpl $0x3e8, %eax            # imm = 0x3E8
je 0x5f1e
cmpl $0x3e9, %eax            # imm = 0x3E9
jne 0x6515
movl $0x0, 0x1c(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
movq 0xb002a(%rip), %rax     # 0xb5fc0
movq (%rax), %rax
movq %rax, 0x28(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
movq 0xb0013(%rip), %rax     # 0xb5fc0
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
movq 0xafffc(%rip), %rax     # 0xb5fc0
movq (%rax), %r14
movq %r14, %rdi
leaq 0x813b5(%rip), %rsi     # 0x87386
callq 0x46f0
testl %eax, %eax
je 0x6178
movq %r14, %rdi
leaq 0x813a3(%rip), %rsi     # 0x8738b
callq 0x46f0
testl %eax, %eax
je 0x616b
movq %r14, %rdi
leaq 0x81394(%rip), %rsi     # 0x87393
callq 0x46f0
testl %eax, %eax
je 0x616b
movq %r14, %rdi
leaq 0x81386(%rip), %rsi     # 0x8739c
callq 0x46f0
testl %eax, %eax
je 0x6192
movq %r14, %rdi
leaq 0x81376(%rip), %rsi     # 0x873a3
callq 0x46f0
testl %eax, %eax
je 0x6185
movq %r14, %rdi
leaq 0x81369(%rip), %rsi     # 0x873ad
callq 0x46f0
testl %eax, %eax
je 0x6185
movq %r14, %rdi
leaq 0x8135d(%rip), %rsi     # 0x873b8
callq 0x46f0
testl %eax, %eax
je 0x619f
movq %r14, %rdi
leaq 0x8134b(%rip), %rsi     # 0x873bd
callq 0x46f0
testl %eax, %eax
je 0x61b9
movq %r14, %rdi
leaq 0x8133d(%rip), %rsi     # 0x873c6
callq 0x46f0
testl %eax, %eax
je 0x61ac
movq %r14, %rdi
leaq 0x81e93(%rip), %rsi     # 0x87f33
callq 0x46f0
testl %eax, %eax
je 0x61ac
movq %r14, %rdi
leaq 0x81313(%rip), %rsi     # 0x873ca
callq 0x46f0
testl %eax, %eax
je 0x61c6
movq %r14, %rdi
leaq 0x81301(%rip), %rsi     # 0x873cf
callq 0x46f0
movl %r13d, %ecx
pushq $0x40
popq %rdx
movl %edx, 0x8(%rsp)
testl %eax, %eax
je 0x5f1e
jmp 0x65ab
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
movq 0xafec0(%rip), %rax     # 0xb5fc0
movq (%rax), %r14
movq %r14, %rdi
leaq 0x821c0(%rip), %rsi     # 0x882cd
callq 0x4520
pushq $0x8
popq %rcx
movl %ecx, 0x10(%rsp)
movl %r13d, %ecx
testl %eax, %eax
je 0x5f1e
movq %r14, %rdi
leaq 0x821a4(%rip), %rsi     # 0x882d2
callq 0x4520
pushq $0x9
popq %rcx
movl %ecx, 0x10(%rsp)
movl %r13d, %ecx
testl %eax, %eax
je 0x5f1e
movq %r14, %rdi
leaq 0x81213(%rip), %rsi     # 0x87362
callq 0x4520
pushq $0x2
popq %rcx
movl %ecx, 0x10(%rsp)
movl %r13d, %ecx
testl %eax, %eax
je 0x5f1e
jmp 0x652d
orl $0x11, 0x8(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
orl $0x1, 0x8(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
orl $0x22, 0x8(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
orl $0x2, 0x8(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
orl $0x3, 0x8(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
orl $0x4, 0x8(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
orl $0x33, 0x8(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
orl $0x8, 0x8(%rsp)
movl %r13d, %ecx
jmp 0x5f1e
movl 0x8(%rsp), %eax
testl %eax, %eax
pushq $0x40
popq %r14
cmovnel %eax, %r14d
movq 0xafdf8(%rip), %r15     # 0xb5fe0
movslq (%r15), %rax
cmpl %eax, %ebp
je 0x6515
leaq (%r12,%rax,8), %rbx
cmpl $0x0, 0x1c(%rsp)
je 0x6229
movq (%rbx), %rdi
pushq $0x1
popq %rsi
leaq 0x18(%rsp), %rdx
callq 0xa01b
movq %rax, %rbx
testq %rax, %rax
jne 0x622f
movslq (%r15), %rax
movq (%r12,%rax,8), %rsi
leaq 0x811d8(%rip), %rdi     # 0x873fc
jmp 0x658c
subl %eax, %ebp
movl %ebp, 0x18(%rsp)
callq 0x21f4a
testq %rax, %rax
je 0x6539
movq %rax, %r12
pushq $0x1
popq %rsi
movq %rax, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x20cfd
xorl %esi, %esi
testl %r13d, %r13d
sete %sil
addl %esi, %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x20cfd
movq 0x30(%rsp), %rsi
testq %rsi, %rsi
je 0x6281
movq %r12, %rdi
xorl %edx, %edx
callq 0x20ec4
testl %eax, %eax
jne 0x656b
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x629f
movq %r12, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x215a9
testl %eax, %eax
jne 0x6574
cmpl $0x0, 0x14(%rsp)
je 0x62ca
movslq 0x18(%rsp), %rdi
shlq $0x3, %rdi
callq 0x46b0
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x62d3
leaq 0x8118f(%rip), %rdi     # 0x87454
jmp 0x657b
movq $0x0, 0x8(%rsp)
xorl %r13d, %r13d
pushq $0x1
popq %rbp
movslq 0x18(%rsp), %rax
cmpq %rax, %r13
jge 0x6360
movq (%rbx,%r13,8), %rsi
cmpl $0x0, 0x14(%rsp)
je 0x6348
movq %rsi, %rdi
leaq 0x840ad(%rip), %rsi     # 0x8a3a5
callq 0x87fb
movq %rax, %r14
movq 0x8(%rsp), %rax
movq %r14, (%rax,%r13,8)
movq (%rbx,%r13,8), %r15
testq %r14, %r14
je 0x64c6
movq %r15, %rdi
leaq 0x81166(%rip), %rsi     # 0x87486
callq 0x40d0
testq %rax, %rax
pushq $0x7
popq %rcx
cmoveq %rax, %rcx
addq %rax, %rcx
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x21739
testl %eax, %eax
jne 0x6358
jmp 0x64dc
movq %r12, %rdi
callq 0x216af
testl %eax, %eax
je 0x64f3
incq %r13
jmp 0x62d9
movq 0x28(%r12), %rax
testq %rax, %rax
je 0x6524
cmpl $0x0, 0x30(%r12)
jle 0x6524
movl 0x10(%rsp), %edx
cmpl $0x2, %edx
jne 0x63b1
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0x643b
leaq 0x83229(%rip), %rsi     # 0x895c1
movq %r15, %rdi
callq 0x45c0
movq %rax, %r14
testq %rax, %rax
jne 0x6445
jmp 0x6553
cmpl $0x8, %edx
leaq 0x81883(%rip), %rcx     # 0x87c3e
leaq 0x818f1(%rip), %r13     # 0x87cb3
cmoveq %rcx, %r13
movq 0x18(%rax), %rbp
testq %rbp, %rbp
je 0x6582
movq 0x20(%rsp), %rax
testq %rax, %rax
leaq 0x838d3(%rip), %r15     # 0x89cb5
cmovneq %rax, %r15
cmpl $0x8, %edx
leaq 0x831d1(%rip), %rax     # 0x895c1
leaq 0x81141(%rip), %rsi     # 0x87538
cmoveq %rax, %rsi
movq %r15, %rdi
callq 0x87fb
testq %rax, %rax
je 0x6553
movq %rax, %r14
movq %r12, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movq %r13, %rcx
callq 0x5e1a
movq %r14, %rdi
callq 0x99d1
testl %eax, %eax
je 0x6465
leaq 0x81108(%rip), %rdi     # 0x8753b
movq %r15, %rsi
jmp 0x658c
movq 0xafb6e(%rip), %rax     # 0xb5fb0
movq (%rax), %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x5c92
testq %r15, %r15
je 0x6465
movq %r14, %rdi
callq 0x44b0
testl %eax, %eax
jne 0x6593
movl 0x20(%r12), %edi
testl %edi, %edi
jne 0x6542
movq %r12, %rdi
callq 0x21fa8
cmpl $0x0, 0x1c(%rsp)
je 0x64a4
xorl %r14d, %r14d
movslq 0x18(%rsp), %rax
cmpq %rax, %r14
jge 0x649c
movq (%rbx,%r14,8), %rdi
callq 0x45d0
incq %r14
jmp 0x6484
movq %rbx, %rdi
callq 0x45d0
cmpl $0x0, 0x14(%rsp)
je 0x64b5
movq 0x8(%rsp), %rdi
callq 0x45d0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x20(%r12), %edi
callq 0x20c80
leaq 0x80f98(%rip), %rdi     # 0x8746f
movq %r15, %rsi
jmp 0x650b
movq (%rbx,%r13,8), %rbx
movl 0x20(%r12), %edi
callq 0x20c80
leaq 0x80f9d(%rip), %rdi     # 0x8748e
jmp 0x6508
movq (%rbx,%r13,8), %rbx
movl 0x20(%r12), %edi
callq 0x20c80
leaq 0x80f67(%rip), %rdi     # 0x8746f
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x5adc
pushq $0x1
popq %rdi
callq 0x5b74
xorl %edi, %edi
callq 0x5b74
leaq 0x80f80(%rip), %rdi     # 0x874ab
jmp 0x657b
leaq 0x80e36(%rip), %rdi     # 0x8736a
movq %r14, %rsi
jmp 0x658c
leaq 0x80ed0(%rip), %rdi     # 0x87410
jmp 0x657b
callq 0x20c80
leaq 0x81004(%rip), %rdi     # 0x87552
movq %rax, %rsi
jmp 0x658c
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0x80f70(%rip), %rdi     # 0x874d6
jmp 0x64d7
leaq 0x80eb4(%rip), %rdi     # 0x87426
jmp 0x657b
leaq 0x80ec2(%rip), %rdi     # 0x8743d
xorl %eax, %eax
callq 0x5adc
leaq 0x80f8b(%rip), %rdi     # 0x87514
movq %r13, %rsi
xorl %eax, %eax
callq 0x5adc
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0x80f54(%rip), %rdi     # 0x874fa
jmp 0x64d7
leaq 0x80e23(%rip), %rdi     # 0x873d5
jmp 0x6534