// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/test/test-vcf-api.c
// Function: test_rlen_values
void test_rlen_values(void)
{
    bcf_hdr_t *hdr;
    bcf1_t *rec, *rec2;
    int i, j;
    int32_t tmpi;
    //data common for all versions, interpreted differently based on version
#define data "##reference=file://tmp\n" \
    "##FILTER=<ID=PASS,Description=\"All filters passed\">\n" \
    "##INFO=<ID=END,Number=1,Type=Integer,Description=\"end\">\n" \
    "##FORMAT=<ID=GT,Number=1,Type=String,Description=\"gt\">\n" \
    "##INFO=<ID=SVLEN,Number=A,Type=Integer,Description=\"svlen\">\n" \
    "##INFO=<ID=SVCLAIM,Number=A,Type=String,Description=\"svclaim\">\n" \
    "##FORMAT=<ID=LEN,Number=1,Type=Integer,Description=\"fmt len\">\n" \
    "##contig=<ID=1,Length=40>\n" \
    "##ALT=<ID=INS,Description=\"INS\">\n" \
    "##ALT=<ID=DEL,Description=\"DEL\">\n" \
    "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tSample1\tSample2\n" \
    "1\t4310\t.\tG\tA\t.\t.\t.\tGT\t0/0\t0|1\n" \
    "1\t4311\t.\tC\tCT\t.\t.\t.\tGT\t0/0\t1/0\n" \
    "1\t4312\t.\tTC\tT\t213.73\t.\t.\tGT\t0/1\t0|0\n" \
    "1\t4314\t.\tG\t<INS>\t213.73\t.\tSVLEN=10;SVCLAIM=J\tGT\t0/1\t0|0\n" \
    "1\t4315\t.\tG\t<DEL>\t213.73\t.\tSVLEN=-10;SVCLAIM=D\tGT\t0/1\t0|0\n" \
    "1\t4326\t.\tG\t<INS>\t213.73\t.\tEND=4326;SVLEN=10;SVCLAIM=J\tGT\t0/1\t0|0\n" \
    "1\t4327\t.\tG\t<DEL>\t213.73\t.\tEND=4337;SVLEN=-10;SVCLAIM=J\tGT\t0/1\t0|0\n" \
    "1\t4338\t.\tG\t<*>\t213.73\t.\tEND=4342;SVLEN=.;SVCLAIM=.\tGT:LEN\t0/1:7\t0|0:8\n" \
    "1\t4353\t.\tG\t<*>\t213.73\t.\tEND=4357;SVLEN=.;SVCLAIM=.\tGT:LEN\t0/1:7\t0|0:.\n" \
    "1\t4363\t.\tG\t<*>\t213.73\t.\tEND=4367;SVLEN=.;SVCLAIM=.\tGT:LEN\t0/1:7\t0|0:.\n" \
    "1\t4370\t.\tG\t<INS>,<*>\t213.73\t.\tEND=4371;SVLEN=.;SVCLAIM=.\tGT:LEN\t0/1:7\t0|0:.\n" \
    "1\t4378\t.\tG\t<DEL>,<INS>,<*>\t213.73\t.\tEND=4379;SVLEN=3,5,.;SVCLAIM=D,J,.\tGT:LEN\t0/1:7\t0|0:.\n" \
    "1\t4385\t.\tG\tT\t213.73\t.\tEND=4387;SVLEN=8;SVCLAIM=.\tGT:LEN\t0/1:.\t0|0:10\n"
    //this last one is invalid one, to showcase how SVLEN is considered even when ALT is no SV
    //this is because data are not cross checked with allele types, to make it simple

    //test vcf with different versions
    const int vcfsz = 13, testsz = 3;
    static char d43[] = "data:,"
    "##fileformat=VCFv4.3\n" data;
    static char d44[] = "data:,"
    "##fileformat=VCFv4.4\n" data;
    static char d45[] = "data:,"
    "##fileformat=VCFv4.5\n" data;
    /* ideal expected rlen for tests
    int rlen43[] = {1, 1, 2, 1, 1, 1, 11, 5, 5, 5, 2, 2, 3};
    int rlen44[] = {1, 1, 2, 1, 11, 1, 11, 5, 5, 5, 2, 4, 9};
    int rlen45[] = {1, 1, 2, 1, 11, 1, 11, 8, 7, 7, 7, 7, 9};*/
    // but we dont check version and hence resulting rlen should be
    int rlen[] = {1, 1, 2, 1, 11, 1, 11, 8, 7, 7, 7, 7, 9};

    char *darr[] = {&d43[0], &d44[0], &d45[0]};     //data array
    int *rarr[] = {&rlen[0], &rlen[0], &rlen[0]};   //result array

    enum htsLogLevel logging = hts_get_log_level();

    // Silence warning messages
    hts_set_log_level(HTS_LOG_ERROR);

    rec = bcf_init1(); rec2 = bcf_init1();
    if (!rec || !rec2) error("Failed to allocate BCF record : %s", strerror(errno));
    //calculating rlen with different vcf versions
    for (i = 0; i < testsz; ++i) {
        htsFile *fp = hts_open(darr[i], "r");
        if (!fp) error("Failed to open vcf data with %d : %s", i + 1, strerror(errno));
        bcf_clear1(rec);
        hdr = bcf_hdr_read(fp);
        if (!hdr) error("Failed to read BCF header with %d : %s", i + 1, strerror(errno));
        for (j = 0; j < vcfsz; ++j) {
            check0(bcf_read(fp, hdr, rec));
            if (rec->rlen != rarr[i][j]) {
                error("Incorrect rlen @ vcf %d on test %d - expected %d got %"PRIhts_pos"\n", j + 1, i + 1, rarr[i][j], rec->rlen);
            }
        }
        bcf_hdr_destroy(hdr);
        hts_close(fp);
    }

    //calculating rlen with update to fields
    htsFile *fp = hts_open(d45, "r");
    int id  = 1, val[]= { 1, 15 };
    bcf_clear1(rec);
    hdr = bcf_hdr_read(fp);
    if (!hdr) error("Failed to read BCF header : %s", strerror(errno));
    check0(bcf_read(fp, hdr, rec));
    //"1\t4310\t.\tG\tA\t.\t.\t.\tGT\t0/0\t0|1\n"
    if (rec->rlen != 1)
        error("Incorrect rlen set, expected 1 got %"PRIhts_pos" @ %d\n", rec->rlen, id);
    //alt change A->AT
    ++id; check0(bcf_update_alleles_str(hdr, rec, "G,AT"));
    if (rec->rlen != 1)
        error("Incorrect rlen set, expected 1 got %"PRIhts_pos" @ %d\n", rec->rlen, id);
    //ref change G->GC, alt AT -> A, "1\t4310\t.\tGC\tA\t.\t.\t.\tGT\t0/0\t0|1\n"
    id++; check0(bcf_update_alleles_str(hdr, rec, "GC,A"));
    if (rec->rlen != 2)
        error("Incorrect rlen set, expected 2 got %"PRIhts_pos" @ %d\n", rec->rlen, id);
    ++id; check0(bcf_update_alleles_str(hdr, rec, "G,<*>"));
    if (rec->rlen != 1)
        error("Incorrect rlen set, expected 1 got %"PRIhts_pos" @ %d\n", rec->rlen, id);
    tmpi = 4323;
    ++id; check0(bcf_update_info_int32(hdr, rec, "END", &tmpi, 1));  //SVLEN to infer from END and use
    if (rec->rlen != 14)
        error("Incorrect rlen set, expected 14 got %"PRIhts_pos" @ %d\n", rec->rlen, id);
    ++id; check0(bcf_update_format_int32(hdr, rec, "LEN", &val, 2));
    if (rec->rlen != 15)
        error("Incorrect rlen set, expected 15 got %"PRIhts_pos" @ %d\n", rec->rlen, id);
    ++id; check0(bcf_update_info_int32(hdr, rec, "END", &tmpi, 0));  //removes END
    if (rec->rlen != 15)
        error("Incorrect rlen set, expected 15 got %"PRIhts_pos" @ %d\n", rec->rlen, id);
    ++id; check0(bcf_update_format_int32(hdr, rec, "LEN", &tmpi, 0));  //removes LEN
    if (rec->rlen != 1)
        error("Incorrect rlen set, expected 1 got %"PRIhts_pos" @ %d\n", rec->rlen, id);
    //ALT -> DEL, "1\t4310\t.\tG\tT,<DEL>\t.\t.\t.\tGT\t0/0\t0|1\n"
    ++id; check0(bcf_update_alleles_str(hdr, rec, "G,T,<DEL>"));
    if (rec->rlen != 1)
        error("Incorrect rlen set, expected 1 got %"PRIhts_pos" @ %d\n", rec->rlen, id);
    val[0] = 0; val[1] = -5;
    ++id; check0(bcf_update_info_int32(hdr, rec, "SVLEN", &val, 2));  //Add svlen
    if (rec->rlen != 6)
        error("Incorrect rlen set, expected 6 got %"PRIhts_pos" @ %d\n", rec->rlen, id);
    ++id; bcf_copy(rec2, rec);
    if (rec2->rlen != 6)
        error("Incorrect rlen set, expected 6 got %"PRIhts_pos" @ %d\n", rec->rlen, id);

    //needs update when header version change is handled
    bcf_destroy1(rec);
    bcf_destroy1(rec2);
    bcf_hdr_destroy(hdr);

    hts_close(fp);

    hts_set_log_level(logging);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movaps 0xa422d(%rip), %xmm0    # 0xac6f0
leaq 0x40(%rsp), %rax
movaps %xmm0, 0x20(%rax)
movaps 0xa420d(%rip), %xmm0    # 0xac6e0
movaps %xmm0, 0x10(%rax)
movaps 0xa41f2(%rip), %xmm0    # 0xac6d0
movaps %xmm0, (%rax)
movl $0x9, 0x30(%rax)
movq %rax, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x13c8a
movl %eax, 0xc(%rsp)
movl $0x1, %edi
callq 0x13c83
callq 0x31d1e
movq %rax, %r14
callq 0x31d1e
testq %r14, %r14
je 0x88b1
testq %rax, %rax
je 0x88b1
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
leaq 0xa700c(%rip), %r13     # 0xaf544
leaq 0xc91a1(%rip), %rax     # 0xd16e0
movq (%rax,%r12,8), %rdi
movq %r13, %rsi
callq 0xbbc0
testq %rax, %rax
je 0x8876
movq %rax, %rbp
movq %r14, %rdi
callq 0x31d2d
movq %rbp, %rdi
callq 0x30c97
testq %rax, %rax
je 0x888e
movq %rax, %r15
xorl %ebx, %ebx
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x322f9
testl %eax, %eax
jne 0x8849
movq 0x8(%r14), %r8
movq 0x20(%rsp,%r12,8), %rax
movslq (%rax,%rbx,4), %rcx
cmpq %rcx, %r8
jne 0x885e
incq %rbx
cmpq $0xd, %rbx
jne 0x8575
movq %r15, %rdi
callq 0x30af4
movq %rbp, %rdi
callq 0xce22
incq %r12
cmpq $0x3, %r12
jne 0x8538
leaq 0xca9b2(%rip), %rdi     # 0xd2f80
leaq 0xa6f6f(%rip), %rsi     # 0xaf544
callq 0xbbc0
movq %rax, %r12
movabsq $0xf00000001, %rax      # imm = 0xF00000001
movq %rax, 0x10(%rsp)
movq %r14, %rdi
callq 0x31d2d
movq %r12, %rdi
callq 0x30c97
testq %rax, %rax
je 0x88c6
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x322f9
testl %eax, %eax
jne 0x8849
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x88e1
leaq 0xa39f3(%rip), %rdx     # 0xac026
movq %r13, %rdi
movq %r14, %rsi
callq 0x4263b
testl %eax, %eax
movq 0x18(%rsp), %rbx
jne 0x88f2
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x8905
leaq 0xa39f4(%rip), %rdx     # 0xac054
movq %r13, %rdi
movq %r14, %rsi
callq 0x4263b
testl %eax, %eax
jne 0x8916
movq 0x8(%r14), %rsi
cmpq $0x2, %rsi
jne 0x8929
leaq 0xa3a27(%rip), %rdx     # 0xac0af
movq %r13, %rdi
movq %r14, %rsi
callq 0x4263b
testl %eax, %eax
jne 0x893a
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x894d
leaq 0x8(%rsp), %rcx
movl $0x10e3, (%rcx)         # imm = 0x10E3
leaq 0xa36ec(%rip), %rdx     # 0xabda7
movq %r13, %rdi
movq %r14, %rsi
movl $0x1, %r8d
movl $0x1, %r9d
callq 0x40f50
testl %eax, %eax
jne 0x895e
movq 0x8(%r14), %rsi
cmpq $0xe, %rsi
jne 0x8971
leaq 0xa3b22(%rip), %rdx     # 0xac211
leaq 0x10(%rsp), %rcx
movq %r13, %rdi
movq %r14, %rsi
movl $0x2, %r8d
movl $0x1, %r9d
callq 0x4168f
testl %eax, %eax
jne 0x8982
movq 0x8(%r14), %rsi
cmpq $0xf, %rsi
jne 0x8995
leaq 0xa367f(%rip), %rdx     # 0xabda7
leaq 0x8(%rsp), %rcx
movq %r13, %rdi
movq %r14, %rsi
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x40f50
testl %eax, %eax
jne 0x89a6
movq 0x8(%r14), %rsi
cmpq $0xf, %rsi
jne 0x89b9
leaq 0xa3ab3(%rip), %rdx     # 0xac211
leaq 0x8(%rsp), %rcx
movq %r13, %rdi
movq %r14, %rsi
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x4168f
testl %eax, %eax
jne 0x89c7
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x89da
leaq 0xa3a43(%rip), %rdx     # 0xac1d7
movq %r13, %rdi
movq %r14, %rsi
callq 0x4263b
testl %eax, %eax
jne 0x89e8
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x89fb
movabsq $-0x500000000, %rax     # imm = 0xFFFFFFFB00000000
leaq 0x10(%rsp), %rcx
movq %rax, (%rcx)
leaq 0xa3a41(%rip), %rdx     # 0xac20f
movq %r13, %rdi
movq %r14, %rsi
movl $0x2, %r8d
movl $0x1, %r9d
callq 0x40f50
testl %eax, %eax
jne 0x8a09
movq 0x8(%r14), %rsi
cmpq $0x6, %rsi
jne 0x8a1c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3305d
cmpq $0x6, 0x8(%rbx)
jne 0x8a2a
movq %r14, %rdi
callq 0x31ec9
movq %rbx, %rdi
callq 0x31ec9
movq %r13, %rdi
callq 0x30af4
movq %r12, %rdi
callq 0xce22
movl 0xc(%rsp), %edi
callq 0x13c83
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa1839(%rip), %rdi     # 0xaa089
leaq 0xa34ee(%rip), %rsi     # 0xabd45
xorl %eax, %eax
callq 0x602c
incl %ebx
incl %r12d
leaq 0xa3755(%rip), %rdi     # 0xabfbf
movl %ebx, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x602c
incl %r12d
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0xa36e7(%rip), %rdi     # 0xabf73
jmp 0x88a4
incl %r12d
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0xa36f4(%rip), %rdi     # 0xabf98
movl %r12d, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x602c
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0xa343f(%rip), %rdi     # 0xabd03
jmp 0x88d9
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0xa344d(%rip), %rdi     # 0xabd26
movq %rax, %rsi
jmp 0x8857
leaq 0xa3711(%rip), %rdi     # 0xabff9
movl $0x1, %edx
jmp 0x8a3a
leaq 0xa1790(%rip), %rdi     # 0xaa089
leaq 0xa372b(%rip), %rsi     # 0xac02b
jmp 0x8857
leaq 0xa36ed(%rip), %rdi     # 0xabff9
movl $0x2, %edx
jmp 0x8a3a
leaq 0xa176c(%rip), %rdi     # 0xaa089
leaq 0xa3735(%rip), %rsi     # 0xac059
jmp 0x8857
leaq 0xa3752(%rip), %rdi     # 0xac082
movl $0x3, %edx
jmp 0x8a3a
leaq 0xa1748(%rip), %rdi     # 0xaa089
leaq 0xa376d(%rip), %rsi     # 0xac0b5
jmp 0x8857
leaq 0xa36a5(%rip), %rdi     # 0xabff9
movl $0x4, %edx
jmp 0x8a3a
leaq 0xa1724(%rip), %rdi     # 0xaa089
leaq 0xa343f(%rip), %rsi     # 0xabdab
jmp 0x8857
leaq 0xa3767(%rip), %rdi     # 0xac0df
movl $0x5, %edx
jmp 0x8a3a
leaq 0xa1700(%rip), %rdi     # 0xaa089
leaq 0xa377d(%rip), %rsi     # 0xac10d
jmp 0x8857
leaq 0xa37a5(%rip), %rdi     # 0xac141
movl $0x6, %edx
jmp 0x8a3a
leaq 0xa16dc(%rip), %rdi     # 0xaa089
leaq 0xa37bb(%rip), %rsi     # 0xac16f
jmp 0x8857
leaq 0xa3781(%rip), %rdi     # 0xac141
movl $0x7, %edx
jmp 0x8a3a
leaq 0xa16bb(%rip), %rdi     # 0xaa089
leaq 0xa37cd(%rip), %rsi     # 0xac1a2
jmp 0x8857
leaq 0xa3618(%rip), %rdi     # 0xabff9
movl $0x8, %edx
jmp 0x8a3a
leaq 0xa169a(%rip), %rdi     # 0xaa089
leaq 0xa37eb(%rip), %rsi     # 0xac1e1
jmp 0x8857
leaq 0xa35f7(%rip), %rdi     # 0xabff9
movl $0x9, %edx
jmp 0x8a3a
leaq 0xa1679(%rip), %rdi     # 0xaa089
leaq 0xa37fe(%rip), %rsi     # 0xac215
jmp 0x8857
leaq 0xa3826(%rip), %rdi     # 0xac249
movl $0xa, %edx
jmp 0x8a3a
movq 0x8(%r14), %rsi
leaq 0xa3814(%rip), %rdi     # 0xac249
movl $0xb, %edx
xorl %eax, %eax
callq 0x602c