// File: /workspace/github_c_makefile_star2_O1/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 0xa625d(%rip), %xmm0    # 0xae6f0
leaq 0x40(%rsp), %rax
movaps %xmm0, 0x20(%rax)
movaps 0xa623d(%rip), %xmm0    # 0xae6e0
movaps %xmm0, 0x10(%rax)
movaps 0xa6222(%rip), %xmm0    # 0xae6d0
movaps %xmm0, (%rax)
movl $0x9, 0x30(%rax)
movq %rax, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x13ff3
movl %eax, 0xc(%rsp)
movl $0x1, %edi
callq 0x13fec
callq 0x32e93
movq %rax, %r14
callq 0x32e93
testq %r14, %r14
je 0x8881
testq %rax, %rax
je 0x8881
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
leaq 0xa9091(%rip), %r13     # 0xb1599
leaq 0xcb1a1(%rip), %rax     # 0xd36b0
movq (%rax,%r12,8), %rdi
movq %r13, %rsi
callq 0xbee7
testq %rax, %rax
je 0x8846
movq %rax, %rbp
movq %r14, %rdi
callq 0x32ea2
movq %rbp, %rdi
callq 0x31e8f
testq %rax, %rax
je 0x885e
movq %rax, %r15
xorl %ebx, %ebx
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x3346e
testl %eax, %eax
jne 0x8819
movq 0x8(%r14), %r8
movq 0x20(%rsp,%r12,8), %rax
movslq (%rax,%rbx,4), %rcx
cmpq %rcx, %r8
jne 0x882e
incq %rbx
cmpq $0xd, %rbx
jne 0x8545
movq %r15, %rdi
callq 0x31cf6
movq %rbp, %rdi
callq 0xd151
incq %r12
cmpq $0x3, %r12
jne 0x8508
leaq 0xcc9e2(%rip), %rdi     # 0xd4f80
leaq 0xa8ff4(%rip), %rsi     # 0xb1599
callq 0xbee7
movq %rax, %r12
movabsq $0xf00000001, %rax      # imm = 0xF00000001
movq %rax, 0x10(%rsp)
movq %r14, %rdi
callq 0x32ea2
movq %r12, %rdi
callq 0x31e8f
testq %rax, %rax
je 0x8896
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x3346e
testl %eax, %eax
jne 0x8819
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x88b1
leaq 0xa5a23(%rip), %rdx     # 0xae026
movq %r13, %rdi
movq %r14, %rsi
callq 0x4348f
testl %eax, %eax
movq 0x18(%rsp), %rbx
jne 0x88c2
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x88d5
leaq 0xa5a24(%rip), %rdx     # 0xae054
movq %r13, %rdi
movq %r14, %rsi
callq 0x4348f
testl %eax, %eax
jne 0x88e6
movq 0x8(%r14), %rsi
cmpq $0x2, %rsi
jne 0x88f9
leaq 0xa5a57(%rip), %rdx     # 0xae0af
movq %r13, %rdi
movq %r14, %rsi
callq 0x4348f
testl %eax, %eax
jne 0x890a
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x891d
leaq 0x8(%rsp), %rcx
movl $0x10e3, (%rcx)         # imm = 0x10E3
leaq 0xa571c(%rip), %rdx     # 0xadda7
movq %r13, %rdi
movq %r14, %rsi
movl $0x1, %r8d
movl $0x1, %r9d
callq 0x41ced
testl %eax, %eax
jne 0x892e
movq 0x8(%r14), %rsi
cmpq $0xe, %rsi
jne 0x8941
leaq 0xa5b52(%rip), %rdx     # 0xae211
leaq 0x10(%rsp), %rcx
movq %r13, %rdi
movq %r14, %rsi
movl $0x2, %r8d
movl $0x1, %r9d
callq 0x42483
testl %eax, %eax
jne 0x8952
movq 0x8(%r14), %rsi
cmpq $0xf, %rsi
jne 0x8965
leaq 0xa56af(%rip), %rdx     # 0xadda7
leaq 0x8(%rsp), %rcx
movq %r13, %rdi
movq %r14, %rsi
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x41ced
testl %eax, %eax
jne 0x8976
movq 0x8(%r14), %rsi
cmpq $0xf, %rsi
jne 0x8989
leaq 0xa5ae3(%rip), %rdx     # 0xae211
leaq 0x8(%rsp), %rcx
movq %r13, %rdi
movq %r14, %rsi
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x42483
testl %eax, %eax
jne 0x8997
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x89aa
leaq 0xa5a73(%rip), %rdx     # 0xae1d7
movq %r13, %rdi
movq %r14, %rsi
callq 0x4348f
testl %eax, %eax
jne 0x89b8
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x89cb
movabsq $-0x500000000, %rax     # imm = 0xFFFFFFFB00000000
leaq 0x10(%rsp), %rcx
movq %rax, (%rcx)
leaq 0xa5a71(%rip), %rdx     # 0xae20f
movq %r13, %rdi
movq %r14, %rsi
movl $0x2, %r8d
movl $0x1, %r9d
callq 0x41ced
testl %eax, %eax
jne 0x89d9
movq 0x8(%r14), %rsi
cmpq $0x6, %rsi
jne 0x89ec
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3421b
cmpq $0x6, 0x8(%rbx)
jne 0x89fa
movq %r14, %rdi
callq 0x33036
movq %rbx, %rdi
callq 0x33036
movq %r13, %rdi
callq 0x31cf6
movq %r12, %rdi
callq 0xd151
movl 0xc(%rsp), %edi
callq 0x13fec
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa3869(%rip), %rdi     # 0xac089
leaq 0xa551e(%rip), %rsi     # 0xadd45
xorl %eax, %eax
callq 0x600c
incl %ebx
incl %r12d
leaq 0xa5785(%rip), %rdi     # 0xadfbf
movl %ebx, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x600c
incl %r12d
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0xa5717(%rip), %rdi     # 0xadf73
jmp 0x8874
incl %r12d
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0xa5724(%rip), %rdi     # 0xadf98
movl %r12d, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x600c
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0xa546f(%rip), %rdi     # 0xadd03
jmp 0x88a9
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0xa547d(%rip), %rdi     # 0xadd26
movq %rax, %rsi
jmp 0x8827
leaq 0xa5741(%rip), %rdi     # 0xadff9
movl $0x1, %edx
jmp 0x8a0a
leaq 0xa37c0(%rip), %rdi     # 0xac089
leaq 0xa575b(%rip), %rsi     # 0xae02b
jmp 0x8827
leaq 0xa571d(%rip), %rdi     # 0xadff9
movl $0x2, %edx
jmp 0x8a0a
leaq 0xa379c(%rip), %rdi     # 0xac089
leaq 0xa5765(%rip), %rsi     # 0xae059
jmp 0x8827
leaq 0xa5782(%rip), %rdi     # 0xae082
movl $0x3, %edx
jmp 0x8a0a
leaq 0xa3778(%rip), %rdi     # 0xac089
leaq 0xa579d(%rip), %rsi     # 0xae0b5
jmp 0x8827
leaq 0xa56d5(%rip), %rdi     # 0xadff9
movl $0x4, %edx
jmp 0x8a0a
leaq 0xa3754(%rip), %rdi     # 0xac089
leaq 0xa546f(%rip), %rsi     # 0xaddab
jmp 0x8827
leaq 0xa5797(%rip), %rdi     # 0xae0df
movl $0x5, %edx
jmp 0x8a0a
leaq 0xa3730(%rip), %rdi     # 0xac089
leaq 0xa57ad(%rip), %rsi     # 0xae10d
jmp 0x8827
leaq 0xa57d5(%rip), %rdi     # 0xae141
movl $0x6, %edx
jmp 0x8a0a
leaq 0xa370c(%rip), %rdi     # 0xac089
leaq 0xa57eb(%rip), %rsi     # 0xae16f
jmp 0x8827
leaq 0xa57b1(%rip), %rdi     # 0xae141
movl $0x7, %edx
jmp 0x8a0a
leaq 0xa36eb(%rip), %rdi     # 0xac089
leaq 0xa57fd(%rip), %rsi     # 0xae1a2
jmp 0x8827
leaq 0xa5648(%rip), %rdi     # 0xadff9
movl $0x8, %edx
jmp 0x8a0a
leaq 0xa36ca(%rip), %rdi     # 0xac089
leaq 0xa581b(%rip), %rsi     # 0xae1e1
jmp 0x8827
leaq 0xa5627(%rip), %rdi     # 0xadff9
movl $0x9, %edx
jmp 0x8a0a
leaq 0xa36a9(%rip), %rdi     # 0xac089
leaq 0xa582e(%rip), %rsi     # 0xae215
jmp 0x8827
leaq 0xa5856(%rip), %rdi     # 0xae249
movl $0xa, %edx
jmp 0x8a0a
movq 0x8(%r14), %rsi
leaq 0xa5844(%rip), %rdi     # 0xae249
movl $0xb, %edx
xorl %eax, %eax
callq 0x600c