// File: /workspace/github_c_makefile_star2_O2/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 0x7e7c9(%rip), %xmm0    # 0x866f0
leaq 0x40(%rsp), %rax
movaps %xmm0, 0x20(%rax)
movaps 0x7e7a9(%rip), %xmm0    # 0x866e0
movaps %xmm0, 0x10(%rax)
movaps 0x7e78e(%rip), %xmm0    # 0x866d0
movaps %xmm0, (%rax)
movl $0x9, 0x30(%rax)
movq %rax, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x1156f
movl %eax, 0xc(%rsp)
pushq $0x1
popq %rdi
callq 0x11568
callq 0x2849e
movq %rax, %r14
callq 0x2849e
testq %r14, %r14
je 0x8309
testq %rax, %rax
je 0x8309
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
leaq 0x81575(%rip), %r13     # 0x8950f
cmpq $0x3, %r12
je 0x8030
leaq 0xaa735(%rip), %rax     # 0xb26e0
movq (%rax,%r12,8), %rdi
movq %r13, %rsi
callq 0xa70f
testq %rax, %rax
je 0x82ce
movq %rax, %rbp
movq %r14, %rdi
callq 0x284ab
movq %rbp, %rdi
callq 0x27ba6
testq %rax, %rax
je 0x82e6
movq %rax, %r15
xorl %ebx, %ebx
cmpq $0xd, %rbx
je 0x8018
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x288ae
testl %eax, %eax
jne 0x82a1
movq 0x8(%r14), %r8
movq 0x20(%rsp,%r12,8), %rax
movslq (%rax,%rbx,4), %rcx
cmpq %rcx, %r8
jne 0x82b6
incq %rbx
jmp 0x7fe1
movq %r15, %rdi
callq 0x27a34
movq %rbp, %rdi
callq 0xb8e5
incq %r12
jmp 0x7f9a
leaq 0xabf49(%rip), %rdi     # 0xb3f80
leaq 0x814d1(%rip), %rsi     # 0x8950f
callq 0xa70f
movq %rax, %r12
movabsq $0xf00000001, %rax      # imm = 0xF00000001
movq %rax, 0x10(%rsp)
movq %r14, %rdi
callq 0x284ab
movq %r12, %rdi
callq 0x27ba6
testq %rax, %rax
je 0x831e
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x288ae
testl %eax, %eax
jne 0x82a1
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x8339
leaq 0x7df8a(%rip), %rdx     # 0x86026
movq %r13, %rdi
movq %r14, %rsi
callq 0x31fcc
testl %eax, %eax
jne 0x8347
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x835a
leaq 0x7df90(%rip), %rdx     # 0x86054
movq %r13, %rdi
movq %r14, %rsi
callq 0x31fcc
testl %eax, %eax
jne 0x8368
movq 0x8(%r14), %rsi
cmpq $0x2, %rsi
jne 0x837b
leaq 0x7dfc3(%rip), %rdx     # 0x860af
movq %r13, %rdi
movq %r14, %rsi
callq 0x31fcc
testl %eax, %eax
jne 0x8389
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x839c
leaq 0x8(%rsp), %rcx
movl $0x10e3, (%rcx)         # imm = 0x10E3
leaq 0x7dc88(%rip), %rdx     # 0x85da7
pushq $0x1
popq %r8
movq %r13, %rdi
movq %r14, %rsi
movl %r8d, %r9d
callq 0x30f42
testl %eax, %eax
jne 0x83aa
movq 0x8(%r14), %rsi
cmpq $0xe, %rsi
jne 0x83bd
leaq 0x7e0c3(%rip), %rdx     # 0x86211
leaq 0x10(%rsp), %rcx
pushq $0x2
popq %r8
pushq $0x1
popq %r9
movq %r13, %rdi
movq %r14, %rsi
callq 0x3156c
testl %eax, %eax
jne 0x83cb
movq 0x8(%r14), %rsi
cmpq $0xf, %rsi
jne 0x83de
leaq 0x7dc24(%rip), %rdx     # 0x85da7
leaq 0x8(%rsp), %rcx
pushq $0x1
popq %r9
movq %r13, %rdi
movq %r14, %rsi
xorl %r8d, %r8d
callq 0x30f42
testl %eax, %eax
jne 0x83ec
movq 0x8(%r14), %rsi
cmpq $0xf, %rsi
jne 0x83ff
leaq 0x7e05a(%rip), %rdx     # 0x86211
leaq 0x8(%rsp), %rcx
pushq $0x1
popq %r9
movq %r13, %rdi
movq %r14, %rsi
xorl %r8d, %r8d
callq 0x3156c
testl %eax, %eax
jne 0x840a
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x841d
leaq 0x7dfec(%rip), %rdx     # 0x861d7
movq %r13, %rdi
movq %r14, %rsi
callq 0x31fcc
testl %eax, %eax
jne 0x8428
movq 0x8(%r14), %rsi
cmpq $0x1, %rsi
jne 0x843b
movabsq $-0x500000000, %rax     # imm = 0xFFFFFFFB00000000
leaq 0x10(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x7dfea(%rip), %rdx     # 0x8620f
pushq $0x2
popq %r8
pushq $0x1
popq %r9
movq %r13, %rdi
movq %r14, %rsi
callq 0x30f42
testl %eax, %eax
jne 0x8446
movq 0x8(%r14), %rsi
cmpq $0x6, %rsi
jne 0x8459
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x292b4
cmpq $0x6, 0x8(%rbx)
jne 0x8464
movq %r14, %rdi
callq 0x28630
movq %rbx, %rdi
callq 0x28630
movq %r13, %rdi
callq 0x27a34
movq %r12, %rdi
callq 0xb8e5
movl 0xc(%rsp), %edi
callq 0x11568
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7bde1(%rip), %rdi     # 0x84089
leaq 0x7da96(%rip), %rsi     # 0x85d45
xorl %eax, %eax
callq 0x5abc
incl %ebx
incl %r12d
leaq 0x7dcfd(%rip), %rdi     # 0x85fbf
movl %ebx, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x5abc
incl %r12d
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0x7dc8f(%rip), %rdi     # 0x85f73
jmp 0x82fc
incl %r12d
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0x7dc9c(%rip), %rdi     # 0x85f98
movl %r12d, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x5abc
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0x7d9e7(%rip), %rdi     # 0x85d03
jmp 0x8331
callq 0x4050
movl (%rax), %edi
callq 0x4140
leaq 0x7d9f5(%rip), %rdi     # 0x85d26
movq %rax, %rsi
jmp 0x82af
leaq 0x7dcb9(%rip), %rdi     # 0x85ff9
pushq $0x1
jmp 0x8471
leaq 0x7bd3b(%rip), %rdi     # 0x84089
leaq 0x7dcd6(%rip), %rsi     # 0x8602b
jmp 0x82af
leaq 0x7dc98(%rip), %rdi     # 0x85ff9
pushq $0x2
jmp 0x8471
leaq 0x7bd1a(%rip), %rdi     # 0x84089
leaq 0x7dce3(%rip), %rsi     # 0x86059
jmp 0x82af
leaq 0x7dd00(%rip), %rdi     # 0x86082
pushq $0x3
jmp 0x8471
leaq 0x7bcf9(%rip), %rdi     # 0x84089
leaq 0x7dd1e(%rip), %rsi     # 0x860b5
jmp 0x82af
leaq 0x7dc56(%rip), %rdi     # 0x85ff9
pushq $0x4
jmp 0x8471
leaq 0x7bcd8(%rip), %rdi     # 0x84089
leaq 0x7d9f3(%rip), %rsi     # 0x85dab
jmp 0x82af
leaq 0x7dd1b(%rip), %rdi     # 0x860df
pushq $0x5
jmp 0x8471
leaq 0x7bcb7(%rip), %rdi     # 0x84089
leaq 0x7dd34(%rip), %rsi     # 0x8610d
jmp 0x82af
leaq 0x7dd5c(%rip), %rdi     # 0x86141
pushq $0x6
jmp 0x8471
leaq 0x7bc96(%rip), %rdi     # 0x84089
leaq 0x7dd75(%rip), %rsi     # 0x8616f
jmp 0x82af
leaq 0x7dd3b(%rip), %rdi     # 0x86141
pushq $0x7
jmp 0x8471
leaq 0x7bc78(%rip), %rdi     # 0x84089
leaq 0x7dd8a(%rip), %rsi     # 0x861a2
jmp 0x82af
leaq 0x7dbd5(%rip), %rdi     # 0x85ff9
pushq $0x8
jmp 0x8471
leaq 0x7bc5a(%rip), %rdi     # 0x84089
leaq 0x7ddab(%rip), %rsi     # 0x861e1
jmp 0x82af
leaq 0x7dbb7(%rip), %rdi     # 0x85ff9
pushq $0x9
jmp 0x8471
leaq 0x7bc3c(%rip), %rdi     # 0x84089
leaq 0x7ddc1(%rip), %rsi     # 0x86215
jmp 0x82af
leaq 0x7dde9(%rip), %rdi     # 0x86249
pushq $0xa
jmp 0x8471
movq 0x8(%r14), %rsi
leaq 0x7ddda(%rip), %rdi     # 0x86249
pushq $0xb
popq %rdx
xorl %eax, %eax
callq 0x5abc